forked from StefanKert/azuredevops-codesigning-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
183 lines (154 loc) · 5.73 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
const { task, series } = require('gulp');
var gulp = require('gulp');
var debug = require('gulp-debug');
var ts = require("gulp-typescript");
var path = require('path');
var shell = require('shelljs');
var semver = require('semver');
var fs = require('fs');
var del = require('del');
var merge = require('merge-stream');
var _buildRoot = path.join(__dirname, '_build');
var _packagesRoot = path.join(__dirname, '_packages');
function errorHandler(err) {
console.error(err);
process.exit(1);
}
function build() {
var extension = gulp.src(['README.md', 'LICENSE', 'images/**/*.png', 'vss-extension.json'], { base: '.' })
.pipe(debug({ title: 'extension:' }))
.pipe(gulp.dest(_buildRoot));
var task = gulp.src(['task/**/*', '!task/**/*.ts'], { base: '.' })
.pipe(debug({ title: 'task:' }))
.pipe(gulp.dest(_buildRoot));
getExternalModules();
return merge(extension, task);
}
function clean() {
return del([_buildRoot]);
}
function compile() {
var taskPath = path.join(__dirname, 'task', '*.ts');
var tsConfigPath = path.join(__dirname, 'tsconfig.json');
return gulp.src([taskPath], { base: './task' })
.pipe(ts.createProject(tsConfigPath)())
.on('error', errorHandler)
.pipe(gulp.dest(path.join(_buildRoot, 'task')));
}
function package(done) {
var version = getVersion();
updateExtensionManifest(version);
updateTaskManifest(version);
shell.cd("node_modules/.bin");
shell.exec('tfx extension create --root "' + _buildRoot + '" --output-path "' + _packagesRoot + '"');
done();
}
function upload() {
var version = getVersion();
updateExtensionManifest(version, true);
updateTaskManifest(version);
shell.cd("node_modules/.bin");
shell.exec('tfx build tasks upload --task-path "' + path.join(_buildRoot, 'task'))
}
getVersion = function () {
var packages = require("./package.json");
var semverVersion = semver.coerce(packages.version);
var version = {
major: semverVersion.major,
minor: semverVersion.minor,
patch: semverVersion.patch
};
if (/^(refs\/tags\/.*)/g.test(process.env.BUILD_SOURCEBRANCH)) {
console.log("Version: ", version);
return version;
}
else if (/^(refs\/heads\/master)/g.test(process.env.BUILD_SOURCEBRANCH)) {
version.prerelease = "rc";
}
else if (/^(refs\/heads\/dev)/g.test(process.env.BUILD_SOURCEBRANCH)) {
version.prerelease = "beta";
}
else {
version.prerelease = "alpha";
}
var buildnumber = process.env.BUILD_BUILDNUMBER;
if (!buildnumber) {
var date = new Date();
buildnumber = date.getFullYear().toString().slice(-2) + "" + date.getMonth().toString().padStart(2, "0") + "" + date.getDay().toString().padStart(2, "0");
}
console.log("Buildnumber: ", buildnumber);
version.buildnumber = buildnumber;
console.log("Version: ", version);
return version;
}
getVersionAsText = function (version) {
if (version.prerelease) {
return version.major + '.' + version.minor + '.' + version.patch + '-' + version.prerelease + "." + version.buildnumber;
}
else {
return version.major + '.' + version.minor + '.' + version.patch
}
}
getExternalModules = function () {
try {
// copy package.json without dev dependencies
var libPath = path.join(_buildRoot, 'task');
var pkg = require('./package.json');
delete pkg.devDependencies;
fs.writeFileSync(path.join(libPath, 'package.json'), JSON.stringify(pkg, null, 4));
shell.cd(libPath);
if (shell.exec('npm install --loglevel=error').code !== 0) {
shell.echo('Error: npm install failed');
shell.exit(1);
}
else {
console.log("Successfully installed npm packages");
}
shell.cd(__dirname);
fs.unlinkSync(path.join(libPath, 'package.json'));
} catch (e) {
console.error(e);
throw e;
}
}
updateExtensionManifest = function (version) {
var manifestPath = path.join(_buildRoot, 'vss-extension.json')
var manifest = JSON.parse(fs.readFileSync(manifestPath));
manifest.version = version.major + "." + version.minor + "." + version.patch;
if (version.prerelease) {
var versionAsText = getVersionAsText(version);
manifest.version = version.major + "." + version.minor + "." + version.patch + "." + version.buildnumber;
manifest.id = manifest.id + '-' + (versionAsText.includes("alpha") ? "alpha" : "beta");
manifest.name = manifest.name + ' (' + versionAsText + ')';
manifest.public = false;
manifest.galleryFlags.push("Preview");
}
else {
manifest.public = true;
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
}
updateTaskManifest = function (version) {
var manifestPath = path.join(_buildRoot, 'task', 'task.json')
var manifest = JSON.parse(fs.readFileSync(manifestPath));
var versionAsText = getVersionAsText(version);
manifest.version.Major = version.major;
manifest.version.Minor = version.minor;
manifest.version.Patch = version.patch;
manifest.helpMarkDown = 'v' + versionAsText + ' - ' + manifest.helpMarkDown;
if (version.prerelease) {
manifest.version.Prerelease = version.Patch;
manifest.friendlyName = manifest.friendlyName + ' (' + versionAsText + ')';
manifest.id = '4df4abb0-38d5-11e8-9466-7fef5455a13d';
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
}
exports.build = (done) => {
series(clean, compile, build)(done);
};
exports.upload = (done) => {
series(clean, compile, build, upload)(done);
};
exports.package = (done) => {
series(clean, compile, build, package)(done);
};