forked from HomecareHomebase/azure-bake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
288 lines (254 loc) · 10.8 KB
/
gulpfile.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const argv = require('yargs');
const bump = require('gulp-bump');
const exec = require('child_process').exec;
const del = require('del');
const es = require('event-stream');
const fs = require('file-system');
const git = require('gulp-git');
const gulp = require('gulp');
const debug = require('gulp-debug');
const inlinesource = require('gulp-inline-source');
const moment = require('moment');
const rev = require('git-rev');
const shell = require('gulp-shell');
//Parameters and Variables
const params = require('./build/parameters');
let lerna = require('./lerna.json');
function adoPrep(done) {
var branchName = params.build.buildSourceBranch;
if (branchName !== 'master') {
branchName = branchName.replace(/refs\/heads\/(feature\/)?/i, '');
}
var gitScript = `sudo git checkout ${branchName}`;
console.log('ADO Prep Script: ' + gitScript);
return runCmd(gitScript, done);
}
function build(done) {
if (!!params.agent.agentId) {
if ((!!params.build.buildReason.match(/IndividualCI/ig) || !!params.build.buildReason.match(/BatchedCI/ig)) &&
!!params.build.buildSourceBranch.replace(/refs\/heads\/(feature\/)?/i, '').match(/master/ig)) {
console.log('Running Azure DevOps Release Build');
gulp.series(printVersion, adoPrep, toolInstall, lernaBuild, gitCommit, lernaPublish, resetNpmAuth, systemPublish, tagAndPush)(done);
}
else if (!!params.build.buildReason.match(/PullRequest/ig)) {
console.log('Running Azure DevOps Pull Request Build');
gulp.series(printVersion, toolInstall, lernaBuild)(done);
}
else if (!!params.build.buildReason.match(/Manual/ig)) {
console.log('Running Azure DevOps Manual Build');
gulp.series(printVersion, toolInstall, lernaBuild)(done)
}
else {
console.log('Running Inner Default Build');
gulp.series(lernaBuild)(done);
}
}
else {
console.log('Running Outer Default Build');
gulp.series(lernaBuild)(done);
}
}
function cleanCoverage() {
return del('coverage/**', { force: true });
}
function conditions(done) {
console.log(`Build Conditions: `);
console.log(`Is Agent? ${!!params.agent.agentId}`);
var release = (!!params.build.buildReason.match(/IndividualCI/ig) || !!params.build.buildReason.match(/BatchedCI/ig)) &&
!!params.build.buildSourceBranch.replace(/refs\/heads\/(feature\/)?/i, '').match(/master/ig);
console.log(`Is Release Build? ${release}`);
console.log(`Is Pullrequest? ${!!params.build.buildReason.match(/PullRequest/ig)}`);
console.log(`Is Manual Build? ${!!params.build.buildReason.match(/manual/ig)}`);
done();
}
function gitCommit(done) {
var branchName = params.build.buildSourceBranch.replace(/refs\/heads\/(feature\/)?/i, '');
var gitScript = `sudo git checkout ${branchName} &&
sudo git config user.email "${params.build.buildRequestedForEmail}" &&
sudo git config user.name "${params.build.buildRequestedFor}" &&
sudo git update-index --assume-unchanged .npmrc &&
sudo git add . &&
sudo git commit --author '${params.build.buildRequestedFor} <${params.build.buildRequestedForEmail}>' --message "chore[skip ci]: Update & Commit Locks" &&
sudo git tag v${lerna.version} &&
sudo git push origin ${branchName} &&
sudo git push origin --tags`;
console.log('Git Script: ' + gitScript);
return shell.task(gitScript)(done());
}
function inlineCoverageSource() {
return gulp.src('./coverage/*.html')
.pipe(inlinesource({ attribute: false }))
.pipe(gulp.dest('./coverage/inline-html'));
}
function lernaBuild(done) {
var gitScript = `sudo npm run clean:build`;
console.log('Build Script: ' + gitScript);
return runCmd(gitScript, done);
}
function lernaPublish(done) {
var gitScript = `sudo npm run publish`;
console.log('Build Script: ' + gitScript);
return runCmd(gitScript, done);
}
function listEnvironment(done) {
let envList = [];
console.log(`Local Build Environment: ${params.conditions.isLocalBuild}`);
console.log(`AzureDevOps Build Environment: ${params.conditions.isRunningOnADO}`);
rev.branch(function (str) {
let envKeys = Object.keys(params)
envKeys.forEach(function (a) {
let subKeys = Object.getOwnPropertyNames(Object.getPrototypeOf(params[a]));
for (let b = 1; b < subKeys.length; b++) {
let c = params[a]
if (!!(typeof (c[subKeys[b]])).match(/object/ig)) {
let lastKeys = Object.getOwnPropertyNames(c[subKeys[b]]);
lastKeys.forEach(function (d) {
envList.push({ Object: `${a}.${subKeys[b]}`, Key: d, Value: c[subKeys[b]][d] });
})
} else if (c[subKeys[b]] != 'constructor') {
envList.push({ Object: a, Key: subKeys[b], Value: c[subKeys[b]] });
}
}
})
console.table(envList);
});
console.log(`\x1b[37m\x1b[40m`)
done()
}
function printVersion(done) {
var name = lerna.version;
if (params.build.buildReason === 'PullRequest') {
// pull requests will be [version]_[source branch name]
const branchName = params.system.pullRequestSourceBranch;
name += '_' + branchName.replace(/refs\/heads\/(feature\/)?/i, '');
} else if (params.build.buildSourceBranch) {
const branchName = params.build.buildSourceBranch;
if (branchName !== 'master') {
// all branches have refs/heads/ - we don't need that
// we will also remove feature/ if it's there
name += '_' + branchName.replace(/refs\/heads\/(feature\/)?/i, '');
}
}
// make sure no illegal characters are there
name = name.replace(/\"|\/|:|<|>|\\|\|\?|\@|\*/g, '_');
// add YYYYMMDD_HHmm to mark the date and time of this build
name += `_${moment().format('YYYYMMDD.HHmm')}`;
console.log('##vso[build.updatebuildnumber]' + name);
done();
}
function runCmd(command, done) {
var child = exec(command);
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('error', function (errors) {
console.log('Comand Errors: ' + errors);
throw(errors);
});
child.on('close', function (code) {
console.log('closing code: ' + code);
if (code != 0) { throw('Build failed with errors!'); }
else { done(null, code); }
});
}
function setupCoveragePool() {
return gulp.src(["ingredinent/**/src/*.ts", "system/**/src/*.ts", "core/src/*.ts"]).pipe(writeFilenameToFile()).pipe(debug());
}
function sonarQube(done) {
if (!params.agent.agentId) {
console.log('Skipping SonarQube analysis for local build...');
done();
}
else {
let version = require('./package.json').version;
//standard SonarQube configuration options
let sonarOptions = {
"sonar.projectName": "Azure-Bake",
"sonar.projectKey": "azure-bake",
"sonar.typescript.lcov.reportPaths": "coverage/lcov.info",
"sonar.projectVersion": version,
//"sonar.cpd.exclusions": "src/index.html, dist/index.html",
"sonar.coverage.exclusions": "**/*.spec.ts, gulpfile.js, karma.conf.js, protractor.conf.js, **/node_modules/*"
};
//get source branch name
let sourceBranch = (parameters.build.buildReason === 'PullRequest') ? parameters.build.pullRequestSourceBranch : parameters.build.sourceBranch;
sourceBranch = sourceBranch.replace(/refs\/heads\//i, '');
//if running from a pull request, add the target branch option
if (parameters.build.buildReason === 'PullRequest') {
sonarOptions["sonar.branch.target"] = parameters.build.pullRequestTargetBranch.replace(/refs\/heads\//i, '');
}
//if not running on the master branch, add the source branch option
if (sourceBranch != 'master') {
sonarOptions["sonar.branch.name"] = sourceBranch
}
sonarqubeScanner({
serverUrl: "https://sonarqube.hchb.com",
token: argv.sonarToken,
options: sonarOptions
}, done);
}
}
function systemPublish(done) {
var gitScript = `sudo npm run release-build --prefix ./system`;
console.log('Build Script: ' + gitScript);
return runCmd(gitScript, done);
}
function tagAndPush(done) {
var imageVersion = JSON.parse(fs.readFileSync('lerna.json')).version;
var imageTags = [`${params.docker.baseRepository}/bake:${imageVersion}`, `${params.docker.baseRepository}/bake:latest`];
var result = imageTags.forEach( function (tag) {
console.log(`Tagging docker image: bake:release with ${tag}`);
let dockerScript = `docker image tag bake:release ${tag} && docker push ${tag}`;
runCmd(dockerScript, done);
});
done(null, result);
}
function testNycMocha(done) {
return shell.task(['nyc mocha --opts test/mocha.opts'])(done());
}
function toolInstall(done) {
var gitScript = `sudo npm install [email protected] [email protected] --global`;
console.log('Tool Script: ' + gitScript);
return runCmd(gitScript, done);
}
function writeFilenameToFile() {
let output = fs.createWriteStream(__dirname + '/test/app.spec.ts');
output.write('// I am an automatically generated file. I help ensure that unit tests have accurate code coverage numbers. You can ignore me.\n\n')
//Return event-stream map to the pipeline
return es.map((file, cb) => {
let name = file.history[0];
if (name) {
name = name.replace(__dirname + '.').replace(/\\/g, '/');
output.write('require(\'' + name + '\');\n');
}
//Callback signals the operation is done and returns the object to the pipeline
cb(null, file);
});
}
function resetNpmAuth() {
let filename = './.npmrc'
let npmString = '//registry.npmjs.org/:_authToken=$(Npm_Auth_Token)'
return new Promise (function(cb){
fs.writeFile(filename, npmString, cb);
});
}
//Tasks
exports.build = build;
exports.conditions = conditions;
exports.commit = gitCommit;
exports.prep = adoPrep;
exports.analysis = gulp.series(sonarQube);
exports.cleancoverage = cleanCoverage;
exports.coverage = gulp.series(cleanCoverage, setupCoveragePool, testNycMocha);
exports.coveragesonarqube = gulp.series(cleanCoverage, setupCoveragePool, testNycMocha, sonarQube);
exports.inlinecoveragesource = inlineCoverageSource;
exports.listenvironment = listEnvironment;
exports.pretest = gulp.series(cleanCoverage, setupCoveragePool);
exports.printversion = printVersion;
exports.setupcoveragepool = setupCoveragePool;
exports.tagandpush = tagAndPush;
exports.testnycmocha = testNycMocha;
exports.resetNpmAuth = resetNpmAuth;