1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in 
6 the Software without restriction, including without limitation the rights to 
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
8 of the Software, and to permit persons to whom the Software is furnished to do 
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all 
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
20 SOFTWARE.
21 **/
22 module dxx.tool;
23 
24 private import aermicioi.aedi;
25 
26 private import std.getopt;
27 private import std.conv;
28 private import std.experimental.logger;
29 private import std.meta;
30 
31 private import dxx.util;
32 private import dxx.app;
33 private import dxx.tools;
34 private import dxx.app.platform;
35 
36 // Compile-time config
37 enum ToolConfig = DXXConfig ~ IniConfig!("tool.ini");
38 
39 mixin __Text!(ToolConfig.tools.lang);
40 
41 @component
42 class ToolsModule : PlatformRuntime!() {
43     static void registerTool(alias Cmd : string,T : Tool)(InjectionContainer injector) {
44         //debug {
45         //    sharedLog.trace("ToolsModule registerTool "~Cmd);
46         //}
47         injector.register!T("tool.cmd."~Cmd);
48     }
49     override void registerPlatformDependencies(InjectionContainer injector) {
50         debug {
51             sharedLog.info("ToolsModule registerPlatformDependencies()");
52         }
53         super.registerPlatformDependencies(injector);
54         registerTool!("init",InitTool)(injector);
55         registerTool!("install",InstallTool)(injector);
56         registerTool!("lang",LangTool)(injector);
57         registerTool!("cfg",ConfigTool)(injector);
58         registerTool!("cons",ConsoleTool)(injector);
59         registerTool!("work",WorkflowTool)(injector);
60         
61         //InitTool.registerArguments(injector);
62         //InstallTool.registerArguments(injector);
63         //LangTool.registerArguments(injector);
64         //CfgTool.registerArguments(injector);
65     }
66     mixin registerComponent!ToolsModule;
67 };
68 
69 struct Options {
70     string[] inFiles;
71 };
72 
73 int main(string[] args) {
74     MsgLog.info(ToolConfig.tools.applicationName);
75     
76     Options opt;
77     auto rslt = getopt(args,
78         std.getopt.config.passThrough,
79         "infile|i", &opt.inFiles
80     );
81     if (rslt.helpWanted) {
82         defaultGetoptPrinter(ToolConfig.tools.applicationName,
83             rslt.options);
84         return 0;
85     }
86     if(args.length < 2) {
87         defaultGetoptPrinter(ToolConfig.tools.applicationName,
88             rslt.options);
89         return -1;
90     }
91     
92     string cmd = args[1];
93     MsgLog.info("cmd = "~cmd);
94 
95     //auto loader = new PluginLoader("examples/plugin/bin/dxx_example-plugin.dll");
96     //loader.update;
97     
98     Tool tool;
99     try {
100         tool = resolveInjector!Tool("tool.cmd."~cmd);
101     } catch(Exception e) {
102         MsgLog.fatal(MsgText!(ToolConfig.toolsMessages.ERR_TOOL_NOT_FOUND)(cmd));
103         return -1;
104     }
105     WorkflowElement[] elements = [ tool ];
106     auto wf = new DefaultWorkflow(elements,args);
107     
108     WorkflowRunner runner = resolveInjector!WorkflowRunner;
109     auto job = runner.createJob(wf);
110     job.execute;
111     
112     return 0;
113 }