1 /**
2  * Copyright: Copyright Jason White, 2016
3  * License:   MIT
4  * Authors:   Jason White
5  *
6  * Description:
7  * This is the root command handler. That is, this decides which command handler
8  * to use.
9  */
10 module button.handler;
11 
12 import button.log;
13 import button.resource;
14 import button.context;
15 
16 import button.handlers;
17 
18 alias Handler = void function(
19         ref BuildContext ctx,
20         const(string)[] args,
21         string workDir,
22         ref Resources inputs,
23         ref Resources outputs,
24         TaskLogger logger
25         );
26 
27 immutable Handler[string] handlers;
28 shared static this()
29 {
30     handlers = [
31         "button": &recursive,
32         "button-lua": &base,
33         "dmd": &dmd,
34         "gcc": &gcc,
35         "g++": &gcc,
36         "c++": &gcc,
37     ];
38 }
39 
40 /**
41  * Returns a handler appropriate for the given arguments.
42  *
43  * In general, this simply looks at the base name of the first argument and
44  * determines the tool based on that.
45  */
46 Handler selectHandler(const(string)[] args)
47 {
48     import std.uni : toLower;
49     import std.path : baseName, filenameCmp;
50 
51     if (args.length)
52     {
53         auto name = baseName(args[0]);
54 
55         // Need case-insensitive comparison on Windows.
56         version (Windows)
57             name = name.toLower;
58 
59         if (auto p = name in handlers)
60             return *p;
61     }
62 
63     return &tracer;
64 }
65 
66 void execute(
67         ref BuildContext ctx,
68         const(string)[] args,
69         string workDir,
70         ref Resources inputs,
71         ref Resources outputs,
72         TaskLogger logger
73         )
74 {
75     auto handler = selectHandler(args);
76 
77     handler(ctx, args, workDir, inputs, outputs, logger);
78 }