Skip to content

Commit b667284

Browse files
author
Guillaume Piolat
committed
Add all-plugins tools.
1 parent 226daf1 commit b667284

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

tools/all-plugins/dub.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "all-plugins",
3+
"description": "Run the same command in all plugins subdirectories",
4+
"stringImportPaths": ["."],
5+
6+
"dependencies":
7+
{
8+
"console-colors": ">=1.3.1 <2.0.0"
9+
}
10+
}

tools/all-plugins/source/main.d

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
module main;
2+
3+
import std;
4+
import utils;
5+
import consolecolors;
6+
7+
void usage()
8+
{
9+
void flag(string arg, string desc, string possibleValues, string defaultDesc)
10+
{
11+
string argStr = format(" %s", arg);
12+
cwrite(argStr.lcyan);
13+
for(size_t i = argStr.length; i < 19; ++i)
14+
write(" ");
15+
cwritefln("%s", desc);
16+
if (possibleValues)
17+
cwritefln(" Accepts: ".grey ~ "%s".yellow, possibleValues);
18+
if (defaultDesc)
19+
cwritefln(" Default: ".grey ~ "%s", defaultDesc.orange);
20+
// cwriteln;
21+
}
22+
23+
cwriteln();
24+
cwriteln( "This is the <strong><lcyan>all-plugins</lcyan></strong> tool: run a command inside several sub-directories.🔧");
25+
cwriteln("The subdirectory is read from plugin-list.json");
26+
cwriteln();
27+
cwriteln( "Usage: <lcyan>all-plugins -- &lt;command&gt;</>");
28+
cwriteln();
29+
cwriteln("────────── <on_blue> FLAGS </> ────────── 😸🚩".white);
30+
cwriteln();
31+
flag("--", "Command start after that flag", null, null);
32+
flag("-h --help", "Show this help", null, null);
33+
flag("--pause", "Press enter between each action", null, null);
34+
cwriteln();
35+
}
36+
37+
int main(string[] args)
38+
{
39+
try
40+
{
41+
enableConsoleUTF8();
42+
string[] command = null;
43+
bool help;
44+
45+
// Expand macro arguments
46+
for (int i = 1; i < args.length; )
47+
{
48+
string arg = args[i];
49+
if (arg == "--help" || arg == "-h")
50+
{
51+
help = true;
52+
}
53+
else if (arg == "--")
54+
{
55+
command = args[i+1 .. $];
56+
break;
57+
}
58+
else
59+
throw new Exception(format("unknown argumen %s, did you forgot --", arg));
60+
++i;
61+
}
62+
63+
if (help)
64+
{
65+
usage();
66+
return 0;
67+
}
68+
69+
if (command.length == 0)
70+
{
71+
throw new Exception("No command given, see --help");
72+
}
73+
74+
// read list of plugins
75+
JSONValue configFile = parseJSON(cast(string)(std.file.read("all-plugins.json")));
76+
77+
JSONValue[] jsonProjects = configFile["projects"].array;
78+
string[] projects;
79+
foreach(p; jsonProjects)
80+
projects ~= p.str;
81+
82+
auto cwd = getcwd();
83+
scope(exit) cwd.chdir;
84+
85+
foreach(project; projects)
86+
{
87+
auto path = cwd.buildPath(project);
88+
89+
cwritefln("# Moving to sub-directory %s/".lmagenta, project);
90+
path.chdir;
91+
safeCommand( command.join(" ") );
92+
cwritefln("# End sub-directory %s/".lmagenta, project);
93+
cwd.chdir;
94+
}
95+
return 0;
96+
}
97+
catch(CCLException e)
98+
{
99+
cwritefln("error: %s", e.msg);
100+
return 1;
101+
}
102+
catch(Exception e)
103+
{
104+
cwritefln("error: %s", escapeCCL(e.msg));
105+
return 1;
106+
}
107+
}

tools/all-plugins/source/utils.d

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module utils;
2+
3+
import std;
4+
import consolecolors;
5+
6+
7+
class ExternalProgramErrored : Exception
8+
{
9+
public
10+
{
11+
@safe pure nothrow this(int errorCode,
12+
string message,
13+
string file =__FILE__,
14+
size_t line = __LINE__,
15+
Throwable next = null)
16+
{
17+
super(message, file, line, next);
18+
this.errorCode = errorCode;
19+
}
20+
21+
int errorCode;
22+
}
23+
}
24+
25+
void safeCommand(string cmd)
26+
{
27+
cwritefln("$ %s".lcyan, escapeCCL(cmd));
28+
auto pid = spawnShell(cmd);
29+
auto errorCode = wait(pid);
30+
//cwritefln(" =&gt; returned error code %s", errorCode);
31+
if (errorCode != 0)
32+
throw new ExternalProgramErrored(errorCode, format("Command '%s' returned %s", cmd, errorCode));
33+
}
34+
35+
int unsafeCommand(string cmd)
36+
{
37+
cwritefln("$ %s".lcyan, escapeCCL(cmd));
38+
auto pid = spawnShell(cmd);
39+
auto errorCode = wait(pid);
40+
return errorCode;
41+
}
42+
43+
// Currently this only escapes spaces...
44+
string escapeShellArgument(string arg)
45+
{
46+
version(Windows)
47+
{
48+
return `"` ~ arg ~ `"`;
49+
}
50+
else
51+
return arg.replace(" ", "\\ ");
52+
}

0 commit comments

Comments
 (0)