1 /**
2 * Copyright © DiamondMVC 2018
3 * License: MIT (https://github.com/DiamondMVC/emeralD/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module cmd;
7 
8 import std.conv : to;
9 import std.array : replace, array, split;
10 import std.algorithm : map, endsWith, startsWith;
11 import std.stdio : File;
12 import std.file : write, append;
13 import std..string : indexOf;
14 
15 import meta : thisExeDir;
16 
17 /**
18 * Executes an array of command-line args.
19 * Params:
20 *   args = The command-line args.
21 */
22 void executeCommands(string[] args)
23 {
24   if (!args || !args.length)
25   {
26     return;
27   }
28 
29   if (args[0] == "--remote")
30   {
31     if (args.length == 4)
32     {
33       import templates;
34 
35       addRemoteTemplate(args[1], args[2], args[3]);
36       loadRemoteTemplates();
37     }
38 
39     return;
40   }
41 
42   string path;
43   bool appending;
44   string root;
45   string templateName;
46   string name;
47   string[] arguments;
48   string remoteUrl;
49   string fileName;
50 
51   foreach (arg; args)
52   {
53     if (!arg || !arg.length)
54     {
55       continue;
56     }
57 
58     if (arg.startsWith("--path="))
59     {
60       auto pathEndIndex = arg.indexOf('=');
61 
62       if (pathEndIndex < (arg.length - 1))
63       {
64         path = arg[pathEndIndex + 1 .. $];
65       }
66     }
67     else if (arg == "--append")
68     {
69       appending = true;
70     }
71     else if (arg.startsWith("--file="))
72     {
73       auto pathEndIndex = arg.indexOf('=');
74 
75       if (pathEndIndex < (arg.length - 1))
76       {
77         fileName = arg[pathEndIndex + 1 .. $];
78       }
79     }
80     else
81     {
82       if (!root)
83       {
84         root = arg;
85       }
86       else if (!templateName)
87       {
88         templateName = arg;
89       }
90       else if (!name)
91       {
92         name ~= arg;
93       }
94       else
95       {
96         arguments ~= arg;
97       }
98     }
99   }
100 
101   if (!root || !root.length || !templateName || !templateName.length)
102   {
103     return;
104   }
105 
106   if (fileName && fileName.length)
107   {
108     fileName = fileName.replace("$1", name);
109 
110     foreach (i; 0 .. arguments.length)
111     {
112       fileName = fileName.replace("$" ~ to!string(i + 2), arguments[i]);
113     }
114   }
115 
116   if (templateName.endsWith(".emd"))
117   {
118     auto cmdArgs = File(thisExeDir ~ "/templates/" ~ root ~ "/" ~ templateName);
119 
120     foreach (line; cmdArgs.byLine.map!(a => a.replace("\r", "")))
121     {
122       if (!line || !line.length)
123       {
124         continue;
125       }
126 
127       executeCommands(line.split(" ").map!((lineArg)
128       {
129         if (name && name.length)
130         {
131           lineArg = lineArg.replace("$1", name);
132         }
133 
134         foreach (i; 0 .. arguments.length)
135         {
136           lineArg = lineArg.replace("$" ~ to!string(i + 2), arguments[i]);
137         }
138 
139         return cast(string)lineArg;
140       }).array);
141     }
142 
143     return;
144   }
145 
146   if (!name || !name.length)
147   {
148     return;
149   }
150 
151   import templates;
152 
153   auto templateContent = readTemplate(root, templateName);
154 
155   if (!templateContent || !templateContent.length)
156   {
157     return;
158   }
159 
160   templateContent = templateContent.replace("$1", name);
161 
162   foreach (i; 0 .. arguments.length)
163   {
164     templateContent = templateContent.replace("$" ~ to!string(i + 2), arguments[i]);
165   }
166 
167   if (appending)
168   {
169     append(path, templateContent);
170   }
171   else
172   {
173     fileName = fileName ? fileName : name ~ templateName[templateName.indexOf('.') .. $];
174 
175     if (path)
176     {
177       path ~= "/" ~ fileName;
178     }
179     else
180     {
181       path = fileName;
182     }
183 
184     write(path, templateContent);
185   }
186 }