-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRun.hx
145 lines (123 loc) · 3.61 KB
/
Run.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import haxe.Exception;
import haxe.io.Path;
import sys.FileSystem;
using StringTools;
macro function getVersion() {
final version:String = try {
haxe.Json.parse(sys.io.File.getContent("haxelib.json")).version;
} catch (e) {
trace(e);
Sys.print("haxelib.json for `git-submodule-manager` not found.");
"UNKNOWN";
}
return macro $v{version};
}
function runCmd(cmd:String, ...args:String) {
final process = new sys.io.Process(cmd, args.toArray());
final exitCode = process.exitCode();
final stderr = process.stderr.readAll().toString().trim();
process.close();
if (exitCode != 0)
throw new Exception('Failed when running $cmd:\n$stderr');
}
/** Runs a git command. **/
final runGit = runCmd.bind("git");
/** Runs a haxelib command. **/
final runHaxelib = runCmd.bind("haxelib");
function init(?folder) {
Config.initConfig(folder);
final submoduleDir = Config.getSubmoduleDir();
if (FileSystem.exists(submoduleDir))
throw new Exception("This folder already contains a submodule folder.");
FileSystem.createDirectory(submoduleDir);
try {
runHaxelib("newrepo");
} catch (e) {
// already exists
}
Sys.println('Haxe module directory initialised at $submoduleDir');
Sys.println("Add `/.haxelib/` to .gitignore");
}
function delete() {
FileSystem.deleteDirectory(Config.getSubmoduleDir());
Config.clearConfig();
//runHaxelib("deleterepo");
}
function install() {
Sys.println("Updating submodules...");
runGit("submodule", "update", "--init", "--recursive");
try {
runHaxelib("newrepo");
} catch (e) {}
final projects = FileSystem.readDirectory(Config.getSubmoduleDir());
for (name in projects) {
runHaxelib("dev", name, getModulePath(name));
}
}
function getModulePath(name:String) {
return Path.join([Config.getSubmoduleDir(), name]);
}
function add(name:String, url:String) {
Sys.println('adding $name $url');
final modulePath = getModulePath(name);
try {
runGit("submodule", "add", "--force", url, modulePath);
runHaxelib("dev", name, modulePath);
} catch(e) {
try {
runGit("rm", modulePath);
} catch(_) {}
throw e;
}
}
function remove(name:String) {
final modulePath = getModulePath(name);
if (!FileSystem.exists(modulePath))
throw new Exception('No project found with name $name');
runGit("rm", "-r", getModulePath(name));
runHaxelib("dev", name);
try {
runHaxelib("remove", name);
} catch (e) {
// only works if no other versions are installed
}
}
function help() {
Sys.println('Haxe Git Submodule Manager ${getVersion()}');
Sys.println("Usage:");
final COLUMN_SIZE = 20;
function document(cmd:String, params:Array<String>, doc:String) {
final usage = cmd + " " + params.join(" ");
final padded = usage.rpad(" ", COLUMN_SIZE);
Sys.println(' $padded : $doc');
}
document("init", ["[directory]"], "Initialise scope. Optionally specify a directory to use a non-default submodule directory.");
document("add", ["<name>", "<url>"], "Add submodule `name` from `url`");
document("install", [], "Install dependencies for existing project");
document("remove", ["<name>"], "Remove submodule `name` from project");
}
function parseArgs(args:Array<String>) {
switch args {
case ["init"]: init();
case ["init", folder]: init(folder);
//case ["delete"]: delete();
case ["install"]: install();
case ["add", name, url]: add(name, url);
case ["remove", name]: remove(name);
case [] | ["help"] | ["--help"] | ["-h"]: help();
default:
Sys.println("Invalid usage");
help();
}
}
function main() {
final args = Sys.args();
Sys.setCwd(args.pop());
try {
parseArgs(args);
} catch (e) {
Sys.stderr().writeString('Error: $e\n');
Sys.stderr().flush();
Sys.exit(1);
}
}