-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
225 lines (200 loc) · 8.03 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
const exec = require('child_process').exec;
const del = require('del');
const es = require('event-stream');
const filter = require('gulp-filter');
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 params = require('./build/parameters');
const shell = require('gulp-shell');
const tag = require('gulp-tag-version');
const vss = require('./vss-extension')
const vstsBump = require('gulp-vsts-bump');
function bumpVersion() {
return gulp.src(['tasks/**/task.json'], { base: './' })
.pipe(vstsBump({ type: 'patch' }))
.pipe(gulp.dest('./'));
}
function cleanCoverage() {
return del('coverage/**', { force: true });
}
function gitCommit(done) {
var branchName = params.buildSourceBranch;
if (branchName !== 'master') {
// all branches have refs/heads/ - we don't need that
// we will also remove feature/ if it's there
branchName = branchName.replace(/refs\/heads\/(feature\/)?/i, '');
}
var gitScript = `sudo git checkout ` + branchName + ` &&
sudo git config user.email "` + params.buildRequestedForEmail + `" &&
sudo git config user.name "` + params.buildRequestedFor + `" &&
sudo git add . &&
sudo git commit --author '` + params.buildRequestedFor + ` <` + params.buildRequestedForEmail + `>' --message "[skip ci][CHORE] Update & Publish" &&
sudo git tag v` + vss.version + ` &&
sudo git push origin ` + branchName + ` &&
sudo git push origin --tags`;
console.log('Git Script: ' + gitScript);
return shell.task(gitScript)(done());
}
function tagVersion() {
return gulp.src('./vss-extension.json').pipe(tag());
}
function inlineCoverageSource() {
return gulp.src('./coverage/*.html')
.pipe(inlinesource({ attribute: false }))
.pipe(gulp.dest('./coverage/inline-html'));
}
function printVersion(done) {
let name = require('./package.json').version;
if (process.env.BUILD_REASON === 'PullRequest') {
// pull requests will be [version]_[source branch name]
const branchName = process.env.SYSTEM_PULLREQUEST_SOURCEBRANCH;
name += '_' + branchName.replace(/refs\/heads\/(feature\/)?/i, '');
} else if (process.env.BUILD_SOURCEBRANCHNAME) {
const branchName = process.env.BUILD_SOURCEBRANCH;
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 packageExtension(done) {
var child = exec('sudo tfx extension create --root . --output-path ' + params.vsixDirectory + ' --manifest-globs vss-extension.json --rev-version');
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);
done(errors);
});
child.on('close', function (code) {
console.log('closing code: ' + code);
done(null, code);
});
}
function publishExtension(done) {
var child = exec('sudo tfx extension publish --root . --share-with ' + params.orgShare +' --token ' + process.env.VSMARKETPLACETOKEN + ' --output-path ' + params.vsixDirectory + ' --manifest-globs vss-extension.json --rev-version');
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);
done(errors);
});
child.on('close', function (code) {
console.log('closing code: ' + code);
done(null, code);
});
}
function setupCoveragePool() {
return gulp.src("tasks/**/*.ts").pipe(writeFilenameToFile()).pipe(debug());
}
function sonarQube(done) {
if (!parms.isRunningOnADO) {
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-ADO",
"sonar.projectKey": "azure-bake-ado",
"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 = (process.env.BUILD_REASON === 'PullRequest') ? process.env.SYSTEM_PULLREQUEST_SOURCEBRANCH : process.env.BUILD_SOURCEBRANCH;
sourceBranch = sourceBranch.replace(/refs\/heads\//i, '');
//if running from a pull request, add the target branch option
if (process.env.BUILD_REASON === 'PullRequest') {
sonarOptions["sonar.branch.target"] = process.env.SYSTEM_PULLREQUEST_TARGETBRANCH.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 testNycMocha(done) {
return shell.task(['nyc mocha --opts test/mocha.opts'])(done());
}
function tfxInstall(done) {
var child = exec("sudo npm remove tfx-cli && sudo npm install --global tfx-cli");
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);
done(errors);
});
child.on('close', function (code) {
console.log('closing code: ' + code);
done(null, code);
});
}
function uploadExtension (done) {
if (!params.isPullRequest && params.buildSourceBranch == 'master') {
gulp.series(tfxInstall, bumpVersion, publishExtension, gitCommit)(done());
}
else {
console.log('Branch: ' + params.buildSourceBranch);
done(null, 'Failed to Upload Extension'); }
}
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);
});
}
//Tasks
exports.analysis = gulp.series(sonarQube);
exports.bump = bumpVersion;
exports.commit = gitCommit;
exports.cleancoverage = cleanCoverage;
exports.coverage = gulp.series(cleanCoverage, setupCoveragePool, testNycMocha);
exports.coveragesonarqube = gulp.series(cleanCoverage, setupCoveragePool, testNycMocha, sonarQube);
exports.inlinecoveragesource = inlineCoverageSource;
exports.package = gulp.series(tfxInstall, packageExtension);
exports.packageextension = packageExtension;
exports.pretest = gulp.series(cleanCoverage, setupCoveragePool);
exports.printversion = printVersion;
exports.setupcoveragepool = setupCoveragePool;
exports.tag = tagVersion;
exports.testnycmocha = testNycMocha;
exports.tfxinstall = tfxInstall;
exports.upload = uploadExtension;