-
Notifications
You must be signed in to change notification settings - Fork 9
/
make.js
155 lines (122 loc) · 4.65 KB
/
make.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
// parse command line options
var minimist = require('minimist');
var mopts = {
string: [
'version',
'stage',
'taskId'
],
boolean: [
'public'
]
};
var options = minimist(process.argv, mopts);
// remove well-known parameters from argv before loading make
process.argv = options._;
// modules
var shell = require('shelljs');
var make = require('shelljs/make');
var path = require('path');
var os = require('os');
var cp = require('child_process');
var fs = require('fs');
var semver = require('semver');
// global paths
var sourcePath = path.join(__dirname, 'task');
var binariesPath = path.join(__dirname, '_artifacts', 'binaries');
var packagesPath = path.join(__dirname, '_artifacts', 'packages');
// add node modules .bin to path
var binPath = path.join(__dirname, 'node_modules', '.bin');
var separator = os.platform() === 'win32' ? ';' : ':';
var existing = process.env['PATH'];
if (existing)
process.env['PATH'] = binPath + separator + existing;
else
process.env['PATH'] = binPath;
// make targets
target.clean = function() {
console.log('clean: cleaning binaries');
shell.rm('-Rf', binariesPath);
shell.mkdir('-p', binariesPath);
}
target.build = function() {
target.clean();
// build task
console.log('build: building task');
var taskOutputPath = path.join(binariesPath, 'task');
shell.cp('-Rf', sourcePath, taskOutputPath);
console.log(' task -> ' + taskOutputPath);
// copy resources
console.log('build: copying resources');
['README.md', 'LICENSE.txt', 'vss-extension.json'].forEach(function(file) {
shell.cp('-Rf', path.join(__dirname, file), binariesPath);
console.log(' ' + file + ' -> ' + path.join(binariesPath, file));
});
var imagesOutputPath = path.join(binariesPath, 'images')
shell.mkdir('-p', imagesOutputPath);
shell.cp('-Rf', path.join(__dirname, 'images', '*.png'), imagesOutputPath);
console.log(' images -> ' + imagesOutputPath);
// versioning
console.log('build: versioning');
if (options.version) {
if (options.version === 'auto') {
var ref = new Date(2000, 1, 1);
var now = new Date();
var major = 3
var minor = Math.floor((now - ref) / 86400000);
var patch = Math.floor(Math.floor(now.getSeconds() + (60 * (now.getMinutes() + (60 * now.getHours())))) * 0.5)
options.version = major + '.' + minor + '.' + patch
}
if (!semver.valid(options.version)) {
console.error('build', 'Invalid semver version: ' + options.version);
process.exit(1);
}
}
switch (options.stage) {
case 'dev':
options.taskId = 'BC5D1625-874F-4ABF-AC07-4A55EC6DCC76';
options.public = false;
break;
}
updateExtensionManifest(options);
updateTaskManifest(options);
}
target.package = function() {
console.log('package: packaging task');
shell.exec('tfx extension create --root "' + binariesPath + '" --output-path "' + packagesPath +'"')
}
updateExtensionManifest = function(options) {
var manifestPath = path.join(binariesPath, 'vss-extension.json')
var manifest = JSON.parse(fs.readFileSync(manifestPath));
if (options.version) {
manifest.version = options.version;
}
if (options.stage) {
manifest.id = manifest.id + '-' + options.stage
manifest.name = manifest.name + ' (' + options.stage + ')'
}
manifest.public = options.public;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
}
updateTaskManifest = function(options) {
var manifestPath = path.join(binariesPath, 'task', 'task.json')
var manifest = JSON.parse(fs.readFileSync(manifestPath));
if (options.version) {
manifest.version.Major = semver.major(options.version);
manifest.version.Minor = semver.minor(options.version);
manifest.version.Patch = semver.patch(options.version);
}
manifest.helpMarkDown = 'v' + manifest.version.Major + '.' + manifest.version.Minor + '.' + manifest.version.Patch + ' - ' + manifest.helpMarkDown;
if (options.stage) {
manifest.friendlyName = manifest.friendlyName + ' (' + options.stage
if (options.version) {
manifest.friendlyName = manifest.friendlyName + ' ' + options.version
}
manifest.friendlyName = manifest.friendlyName + ')'
}
if (options.taskId) {
manifest.id = options.taskId
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
return manifest.version.Major + '.' + manifest.version.Minor + '.' + manifest.version.Patch
}