|
| 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 -- <command></>"); |
| 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 | +} |
0 commit comments