Skip to content

Commit b936634

Browse files
author
John Haley
committed
Add scripts to handle submodules
1 parent 26ca8b4 commit b936634

File tree

6 files changed

+163
-11
lines changed

6 files changed

+163
-11
lines changed

generate/index.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
var generateJson = require("./scripts/generateJson");
22
var generateNativeCode = require("./scripts/generateNativeCode");
33
var generateMissingTests = require("./scripts/generateMissingTests");
4+
var submoduleStatus = require("../lifecycleScripts/submodules/getStatus");
45

56
module.exports = function generate() {
6-
return new Promise(function(resolve, reject) {
7-
try {
7+
return submoduleStatus()
8+
.then(function(statuses) {
9+
var dirtySubmodules = statuses
10+
.filter(function(status) {
11+
return status.onNewCommit
12+
|| status.needsInitialization
13+
|| status.workDirDirty;
14+
});
15+
16+
if (dirtySubmodules.length) {
17+
console.log("WARNING - Some submodules are out-of-sync");
18+
dirtySubmodules.forEach(function(submodule) {
19+
console.log("\t" + submodule.name);
20+
});
21+
}
22+
})
23+
.then(function() {
824
generateJson();
925
generateNativeCode();
1026
generateMissingTests();
11-
resolve();
12-
}
13-
catch(e) {
14-
reject(e);
15-
}
16-
});
27+
})
28+
.catch(function(e) {
29+
console.log("ERROR - Could not generate native code");
30+
console.log(e);
31+
});
1732
}
1833

1934
if (require.main === module) {

lifecycleScripts/install.js

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ function build() {
121121
return arg;
122122
});
123123

124-
console.log(args);
125124
return new Promise(function(resolve, reject) {
126125
var child = cp.spawn(cmd, args, opts);
127126
child.on("close", function(code) {

lifecycleScripts/prepareForBuild.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ var path = require("path");
33

44
var local = path.join.bind(path, __dirname);
55

6+
var submodules = require(local("submodules"));
67
var configure = require(local("configureLibssh2"));
78
var generate = require(local("../generate"));
89

910
module.exports = function prepareForBuild() {
10-
1111
return new Promise(function(resolve, reject) {
1212
cp.exec("npm install --ignore-scripts", function(err, stdout, stderr) {
1313
if (err) {
@@ -19,7 +19,11 @@ module.exports = function prepareForBuild() {
1919
console.info(stdout);
2020
}
2121
});
22-
}).then(function() {
22+
})
23+
.then(function() {
24+
return submodules();
25+
})
26+
.then(function() {
2327
return Promise.all([
2428
configure(),
2529
generate()
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var path = require("path");
2+
var rootDir = path.join(__dirname, "../..");
3+
var exec = require(path.join(rootDir, "./utils/execPromise"));
4+
5+
module.exports = function getStatus() {
6+
return exec("git submodule status", { cwd: rootDir})
7+
.then(function(stdout) {
8+
function getStatusPromiseFromLine(line) {
9+
var lineSections = line.trim().split(" ");
10+
var onNewCommit = !!~lineSections[0].indexOf("+");
11+
var needsInitialization = !!~lineSections[0].indexOf("-");
12+
var commitOid = lineSections[0].replace("+", "").replace("-", "");
13+
var name = lineSections[1];
14+
15+
return exec("git status", { cwd: path.join(rootDir, name)})
16+
.then(function(workDirStatus) {
17+
return {
18+
commitOid: commitOid,
19+
onNewCommit: onNewCommit,
20+
name: name,
21+
needsInitialization: needsInitialization,
22+
workDirDirty: !~workDirStatus
23+
.trim()
24+
.split("\n")
25+
.pop()
26+
.indexOf("nothing to commit")
27+
};
28+
});
29+
}
30+
31+
return Promise.all(stdout
32+
.trim()
33+
.split("\n")
34+
.map(getStatusPromiseFromLine)
35+
);
36+
});
37+
};

lifecycleScripts/submodules/index.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var path = require("path");
2+
var rootDir = path.join(__dirname, "../..");
3+
4+
var gitExecutableLocation = require(
5+
path.join(rootDir, "./utils/gitExecutableLocation")
6+
);
7+
var submoduleStatus = require("./getStatus");
8+
9+
var exec = require(path.join(rootDir, "./utils/execPromise"));
10+
11+
module.exports = function submodules() {
12+
return gitExecutableLocation()
13+
.catch(function() {
14+
console.log("ERROR - Compilation of NodeGit requires git CLI to be " +
15+
"installed and on the path");
16+
17+
throw new Error("git CLI is not installed or not on the path");
18+
})
19+
.then(function() {
20+
return submoduleStatus();
21+
})
22+
.then(function(statuses) {
23+
function printSubmodule(submoduleName) {
24+
console.log("\t" + submoduleName);
25+
}
26+
27+
var dirtySubmodules = statuses
28+
.filter(function(status) {
29+
return status.workDirDirty && !status.needsInitialization;
30+
})
31+
.map(function(dirtySubmodule) {
32+
return dirtySubmodule.name;
33+
});
34+
35+
if (dirtySubmodules.length) {
36+
console.log(
37+
"ERROR - The following submodules have uncommited changes:"
38+
);
39+
dirtySubmodules.forEach(printSubmodule);
40+
console.log(
41+
"\nThey must either be committed or discarded before we build"
42+
);
43+
44+
throw new Error("Dirty Submodules: " + dirtySubmodules.join(" "));
45+
}
46+
47+
var outOfSyncSubmodules = statuses
48+
.filter(function(status) {
49+
return status.onNewCommit && !status.needsInitialization;
50+
})
51+
.map(function(outOfSyncSubmodule) {
52+
return outOfSyncSubmodule.name;
53+
});
54+
55+
if (outOfSyncSubmodules.length) {
56+
console.log(
57+
"WARNING - The following submodules are pointing to an new commit:"
58+
);
59+
outOfSyncSubmodules.forEach(printSubmodule);
60+
console.log("\nThey will not be updated.");
61+
}
62+
63+
return Promise.all(statuses
64+
.filter(function(status) {
65+
return !status.onNewCommit;
66+
})
67+
.map(function(submoduleToUpdate) {
68+
return exec(
69+
"git submodule update --init --recursive " + submoduleToUpdate.name
70+
);
71+
})
72+
);
73+
});
74+
};

utils/gitExecutableLocation.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var cp = require("child_process");
2+
3+
module.exports = function gitExecutableLocation() {
4+
return new Promise(function(resolve, reject) {
5+
var cmd;
6+
7+
if (process.platform === "win32") {
8+
cmd = "where git";
9+
}
10+
else {
11+
cmd = "which git";
12+
}
13+
14+
cp.exec(cmd, function(err, stdout, stderr) {
15+
if (err) {
16+
reject(err, stderr);
17+
}
18+
else {
19+
resolve(stdout);
20+
}
21+
});
22+
});
23+
};

0 commit comments

Comments
 (0)