Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 88dd296

Browse files
committed
Merge pull request #6076 from adobe/jasonsanjose/build-prop-keys
Changes to build.prop file to support Jenkins jobs
2 parents 7b4e768 + cca208c commit 88dd296

3 files changed

Lines changed: 108 additions & 26 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ src/brackets.min.css
99
/node_modules
1010
/npm-debug.log
1111

12+
# ignore compiled files
13+
/dist
1214

1315
# ignore everything in the dev extension directory EXCEPT the README
1416
# (so that the directory is non-empty and can be in git)

tasks/build.js

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,101 @@
2525
module.exports = function (grunt) {
2626
"use strict";
2727

28-
var child_process = require("child_process"),
28+
var build = {},
29+
child_process = require("child_process"),
30+
path = require("path"),
2931
q = require("q"),
3032
qexec = q.denodeify(child_process.exec);
33+
34+
function getGitInfo(cwd) {
35+
var opts = { cwd: cwd, maxBuffer: 1024 * 1024 },
36+
json = {};
37+
38+
// count the number of commits for our version number
39+
// <major>.<sprint>.<patch>-<number of commits>
40+
return qexec("git log --format=%h", opts).then(function (stdout) {
41+
json.commits = stdout.toString().match(/[0-9a-f]\n/g).length;
42+
43+
// get the hash for the current commit (HEAD)
44+
return qexec("git rev-parse HEAD", opts);
45+
}).then(function (stdout) {
46+
json.sha = /([a-f0-9]+)/.exec(stdout.toString())[1];
47+
48+
// compare HEAD to the HEADs on the remote
49+
return qexec("git ls-remote --heads origin", opts);
50+
}).then(function (stdout) {
51+
var log = stdout.toString(),
52+
re = new RegExp(json.sha + "\\srefs/heads/(\\S+)\\s"),
53+
match = re.exec(log),
54+
reflog;
55+
56+
// if HEAD matches to a remote branch HEAD, grab the branch name
57+
if (match) {
58+
json.branch = match[1];
59+
return json;
60+
}
61+
62+
// else, try match HEAD using reflog
63+
reflog = qexec("git reflog show --no-abbrev-commit --all", opts);
64+
65+
return reflog.then(function (stdout) {
66+
var log = stdout.toString(),
67+
re = new RegExp(json.sha + "\\srefs/(remotes/origin|heads)/(\\S+)@"),
68+
match = re.exec(log);
69+
70+
json.branch = (match && match[2]) || "(no branch)";
71+
72+
return json;
73+
});
74+
});
75+
}
76+
77+
function toProperties(prefix, json) {
78+
var out = "";
79+
80+
Object.keys(json).forEach(function (key) {
81+
out += prefix + key + "=" + json[key] + "\n";
82+
});
83+
84+
return out;
85+
}
3186

3287
// task: build-num
3388
grunt.registerTask("build-prop", "Write build.prop properties file for Jenkins", function () {
34-
var done = this.async(),
35-
out = "",
36-
num,
37-
branch,
38-
sha,
39-
opts = { cwd: process.cwd(), maxBuffer: 1024*1024 },
40-
version = grunt.config("pkg").version;
41-
42-
qexec("git log --format=%h", opts).then(function (stdout, stderr) {
43-
num = stdout.toString().match(/[0-9a-f]\n/g).length;
44-
return qexec("git status", opts);
45-
}).then(function (stdout, stderr) {
46-
branch = /On branch (.*)/.exec(stdout.toString().trim())[1];
47-
return qexec("git log -1", opts);
48-
}).then(function (stdout, stderr) {
49-
sha = /commit (.*)/.exec(stdout.toString().trim())[1];
89+
var done = this.async(),
90+
json = {},
91+
out = "",
92+
opts = { cwd: process.cwd(), maxBuffer: 1024 * 1024 },
93+
version = grunt.config("pkg").version,
94+
www_repo = process.cwd(),
95+
shell_repo = path.resolve(www_repo, grunt.config("shell.repo")),
96+
www_git,
97+
shell_git;
5098

51-
out += "build.version=" + version.substr(0, version.lastIndexOf("-") + 1) + num + "\n";
52-
out += "build.number=" + num + "\n";
53-
out += "build.branch=" + branch + "\n";
54-
out += "build.sha=" + sha + "\n";
99+
getGitInfo(www_repo).then(function (json) {
100+
www_git = json;
101+
return getGitInfo(shell_repo);
102+
}).then(function (json) {
103+
shell_git = json;
104+
}, function (err) {
105+
// shell git info is optional
106+
grunt.log.writeln(err);
107+
}).finally(function () {
108+
out += "brackets_build_version=" + version.substr(0, version.lastIndexOf("-") + 1) + www_git.commits + "\n";
109+
out += toProperties("brackets_www_", www_git);
110+
111+
if (shell_git) {
112+
out += toProperties("brackets_shell_", shell_git);
113+
}
55114

56115
grunt.log.write(out);
57116
grunt.file.write("build.prop", out);
58117

59118
done();
60-
}, function (err) {
61-
grunt.log.writeln(err);
62-
done(false);
63119
});
64120
});
121+
122+
build.getGitInfo = getGitInfo;
123+
124+
return build;
65125
};

tasks/write-config.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
* DEALINGS IN THE SOFTWARE.
2121
*
2222
*/
23-
/*global module, require*/
23+
/*global module, require, process*/
2424
module.exports = function (grunt) {
2525
"use strict";
2626

27-
var common = require("./lib/common")(grunt);
27+
var common = require("./lib/common")(grunt),
28+
build = require("./build")(grunt);
2829

2930
// task: write-config
3031
grunt.registerTask("write-config", "Merge package.json and src/brackets.config.json into src/config.json", function () {
@@ -39,4 +40,23 @@ module.exports = function (grunt) {
3940

4041
common.writeJSON(grunt, "src/config.json", appConfigJSON);
4142
});
43+
44+
// task: build-config
45+
grunt.registerTask("build-config", "Update config.json with the branch and SHA being built", function () {
46+
var done = this.async(),
47+
distConfig = grunt.file.readJSON("src/config.json");
48+
49+
build.getGitInfo(process.cwd()).then(function (gitInfo) {
50+
distConfig.version = distConfig.version.substr(0, distConfig.version.lastIndexOf("-") + 1) + gitInfo.commits;
51+
distConfig.repository.SHA = gitInfo.sha;
52+
distConfig.repository.branch = gitInfo.branch;
53+
54+
common.writeJSON(grunt, "dist/config.json", distConfig);
55+
56+
done();
57+
}, function (err) {
58+
grunt.log.writeln(err);
59+
done(false);
60+
});
61+
});
4262
};

0 commit comments

Comments
 (0)