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 import button.exceptions;
27 
28 int statusCommand(StatusOptions opts, GlobalOptions globalOpts)
29 {
30     import std.parallelism : TaskPool, totalCPUs;
31 
32     if (opts.threads == 0)
33         opts.threads = totalCPUs;
34 
35     auto pool = new TaskPool(opts.threads - 1);
36     scope (exit) pool.finish(true);
37 
38     immutable color = TextColor(colorOutput(opts.color));
39 
40     try
41     {
42         string path = buildDescriptionPath(opts.path);
43         auto state = new BuildState(path.stateName);
44 
45         state.begin();
46         scope (exit) state.rollback();
47 
48         if (!opts.cached)
49             path.syncState(state, pool);
50 
51         printfln("%d resources and %d tasks total",
52                 state.length!Resource,
53                 state.length!Task);
54 
55         displayPendingResources(state, color);
56         displayPendingTasks(state, color);
57     }
58     catch (BuildException e)
59     {
60         stderr.println(":: Error: ", e.msg);
61         return 1;
62     }
63 
64     return 0;
65 }
66 
67 void displayPendingResources(BuildState state, TextColor color)
68 {
69     auto resources = state.enumerate!Resource
70                           .filter!(v => v.update())
71                           .array
72                           .sort();
73 
74     if (resources.empty)
75     {
76         println("No resources have been modified.");
77     }
78     else
79     {
80         printfln("%d modified resource(s):\n", resources.length);
81 
82         foreach (v; resources)
83             println("    ", color.blue, v, color.reset);
84 
85         println();
86     }
87 }
88 
89 void displayPendingTasks(BuildState state, TextColor color)
90 {
91     auto tasks = state.pending!Task.array;
92 
93     if (tasks.empty)
94     {
95         println("No tasks are pending.");
96     }
97     else
98     {
99         printfln("%d pending task(s):\n", tasks.length);
100 
101         foreach (v; tasks)
102             println("    ", color.blue, state[v].toPrettyString, color.reset);
103 
104         println();
105     }
106 }