1 /**
2  * Copyright: Copyright Jason White, 2016
3  * License:   MIT
4  * Authors:   Jason White
5  *
6  * Description:
7  * Displays status about the build.
8  */
9 module button.cli.status;
10 
11 import button.cli.options : StatusOptions, GlobalOptions;
12 
13 import std.getopt;
14 import std.range : empty;
15 import std.array : array;
16 import std.algorithm : sort, map, filter;
17 
18 import io.text,
19        io.file;
20 
21 import button.task;
22 import button.resource;
23 import button.state;
24 import button.build;
25 import button.textcolor;
26 
27 int statusCommand(StatusOptions opts, GlobalOptions globalOpts)
28 {
29     import std.parallelism : TaskPool, totalCPUs;
30 
31     if (opts.threads == 0)
32         opts.threads = totalCPUs;
33 
34     auto pool = new TaskPool(opts.threads - 1);
35     scope (exit) pool.finish(true);
36 
37     immutable color = TextColor(colorOutput(opts.color));
38 
39     try
40     {
41         string path = buildDescriptionPath(opts.path);
42         auto state = new BuildState(path.stateName);
43 
44         state.begin();
45         scope (exit) state.rollback();
46 
47         if (!opts.cached)
48             path.syncState(state, pool);
49 
50         printfln("%d resources and %d tasks total",
51                 state.length!Resource,
52                 state.length!Task);
53 
54         displayPendingResources(state, color);
55         displayPendingTasks(state, color);
56     }
57     catch (BuildException e)
58     {
59         stderr.println(":: Error: ", e.msg);
60         return 1;
61     }
62 
63     return 0;
64 }
65 
66 void displayPendingResources(BuildState state, TextColor color)
67 {
68     auto resources = state.enumerate!Resource
69                           .filter!(v => v.update())
70                           .array
71                           .sort();
72 
73     if (resources.empty)
74     {
75         println("No resources have been modified.");
76     }
77     else
78     {
79         printfln("%d modified resource(s):\n", resources.length);
80 
81         foreach (v; resources)
82             println("    ", color.blue, v, color.reset);
83 
84         println();
85     }
86 }
87 
88 void displayPendingTasks(BuildState state, TextColor color)
89 {
90     auto tasks = state.pending!Task.array;
91 
92     if (tasks.empty)
93     {
94         println("No tasks are pending.");
95     }
96     else
97     {
98         printfln("%d pending task(s):\n", tasks.length);
99 
100         foreach (v; tasks)
101             println("    ", color.blue, state[v].toPrettyString, color.reset);
102 
103         println();
104     }
105 }