forked from jhaker/nodejs-msbuild
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msbuild.js
434 lines (354 loc) · 12.9 KB
/
msbuild.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
msbuild.js
copyright (c) 2014 jonathan haker
Licensed under the MIT license.
https://github.com/jhaker/nodejs-msbuild
*/
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
var events = require('events'),
async = require('async'),
colors = require('colors'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn;
var default_os = require('os').platform();
if (default_os !== 'linux') default_os = "windows";
var args = [];
for (var arg in process.argv) {
args.push(process.argv[arg]);
}
var help = args.splice(2,1);
var x = function(){};
var _ = new x();
_.isPlainObject = function (obj) {
var ctor, key;
if (typeof obj != 'object' || !obj || toString.call(obj) != '[object Object]') {
return false;
}
ctor = typeof obj.constructor == 'function' && obj.constructor.prototype;
if (!ctor || !hasOwnProperty.call(ctor, 'isPrototypeOf')) {
return false;
}
for (key in obj) {}
return key === void 0 || hasOwnProperty.call(obj, key);
};
var validateCmdParameter = function(param){
if (param.length < 2) return false;
return true;
}
var validFrameworkDir = function(dir) {
return dir.indexOf('1') === 0;
}
var getFrameworkDirectories = function(msbuildDir){
if (fs.existsSync(msbuildDir)) {
return fs.readdirSync(msbuildDir)
.filter(validFrameworkDir);
} else {
return [];
}
}
var mapProcessor = function(processor) {
if(!isNaN(processor) && processor !== undefined){
processor = 'x'+processor;
}
if(processor !== 'x64'){
processor = 'x86';
}
return processor;
}
var defaultPath = process.cwd();
var lineBreak = '\n- - - - - - - - - - - - - - - -';
var msbuild = function(){
events.EventEmitter.call(this);
this.showHelp = help == '?';
this.processors = { 'x86': 'Framework', 'x64': 'Framework64' };
this.os = default_os; // windows, linux
this.processor = 'x64'; // 'x86', 'x64'
this.version = 'current'; // tools version; determines local path to msbuild.exe
this.sourcePath = defaultPath; // 'c:/mypath/mysolution.sln' or 'c:/mypath/myproject.csproj
this.configuration = undefined; // solution configurations; targets an environment (debug,release)
this.publishProfile = undefined; // publish profiles; targets a specific machine (app01,app02)
this.outputPath = ''; // 'c:/deploys/release'
this.verbose = false;
/***
property overrides (example: ['/clp:ErrorsOnly;', '/p:WarningLevel=2','/p:OutputDir=bin\Debug'] )
target framework overrides (example: ['/tv:4.0'] )
***/
this.overrideParams = [];
};
msbuild.prototype.toolsVersion = {
'2.0': '2.0.50727',
'3.0':'3.0',
'3.5': '3.5',
'4.0': '4.0.30319',
'4.5': '4.0.30319',
'12.0': '12.0',
'14.0': '14.0',
'15.0': '(not used)',
'16.0': '(not used)',
'17.0': '(not used)'
};
msbuild.prototype.__proto__ = events.EventEmitter.prototype;
msbuild.prototype.logger = function(msg){
console.log(msg);
}
msbuild.prototype.getMSBuildPath = function(os,processor,version){
if(os === 'linux' || os === 'darwin') return "xbuild";
var frameworkDirectories,programFilesDir,msbuildDir,exeDir;
var vsIdeType = {
Pro: 'Professional',
Enterprise: 'Enterprise',
Community: 'Community',
BuildTools: 'BuildTools'
};
programFilesDir = process.env['programfiles(x86)'] || process.env.PROGRAMFILES;
console.log('Found "programFiles" dir = ' + programFilesDir);
// For the msbuild 15+ versions, use the appropriate VS2017, VS2019 & VS2022 directories
if (version === "15.0" || version === "16.0" || version === "17.0") {
// MSBuild versions 16+ are installed in the "\current" folder under respective Visual Studio or BuildTools folders.
if (version === "17.0")
vsIdeVersion = "2022";
else
vsIdeVersion = "2019";
var msBuildSubDir = "current";
// MSBuild 15.0 is installed in the "\15.0" folder under each version of Visual Studio or BuildTools folder.
// See https://docs.microsoft.com/en-us/visualstudio/msbuild/what-s-new-in-msbuild-15-0?view=vs-2017
if (version === "15.0") {
vsIdeVersion = "2017";
msBuildSubDir = version;
}
console.log('Looking for install of VS IDE Version ' + vsIdeVersion + ' with MsBuild sub-dir = ' + msBuildSubDir);
// If VSINSTALLDIR env. var cannot be found, see what could be the directory by searching the usual suspects
// (while giving higher priority to the VS2017/2019/2022 IDE installs over the Build Tools only install)
// Note: This assumes these VS are installed on the drive stated in "programFilesDir".
if (process.env.vsInstallDir === undefined) {
var possibleVSInstallDir = programFilesDir + '\\' + 'Microsoft Visual Studio\\' + vsIdeVersion + '\\';
console.log('VSINSTALLDIR env. var cannot be found; possible VS install dir = ' + possibleVSInstallDir);
if (fs.existsSync(possibleVSInstallDir + vsIdeType.Pro))
msbuildDir = possibleVSInstallDir + vsIdeType.Pro + '\\';
else if (fs.existsSync(possibleVSInstallDir + vsIdeType.Enterprise + '\\'))
msbuildDir = possibleVSInstallDir + vsIdeType.Enterprise + '\\';
else if (fs.existsSync(possibleVSInstallDir + vsIdeType.Community + '\\'))
msbuildDir = possibleVSInstallDir + vsIdeType.Community + '\\';
else if (fs.existsSync(possibleVSInstallDir + vsIdeType.BuildTools + '\\'))
msbuildDir = possibleVSInstallDir + vsIdeType.BuildTools + '\\';
else {
// todo: try searching programFilesDir on another drive (usually D:\) or find a better way to get the VS install folder.
console.log('** Could not find VS IDE / tools install folder. Please install at least the VS Build Tools in the "programFiles" dir. **');
msbuildDir = '(not found)';
return '';
}
console.log('Assuming MS Build sub-dir = ' + msbuildDir);
}
else {
msbuildDir = process.env.vsInstallDir;
console.log('VSINSTALLDIR env. var found; using msBuild sub-dir = ' + msbuildDir);
}
exeDir = msbuildDir + 'MSBuild\\' + msBuildSubDir + '\\bin\\msbuild.exe';
console.log('Using msbuild.exe dir = ' + exeDir);
}
// If the msbuild.exe file exists, we are done.
if (exeDir != undefined && fs.existsSync(exeDir)) {
// console.log('using msbuild 15.0+ exeDir = ' + exeDir)
return exeDir;
}
// Otherwise, look for the older msbuild versions
msbuildDir = programFilesDir + '\\' + 'MSBuild';
frameworkDirectories = getFrameworkDirectories(msbuildDir);
if(this.toolsVersion[version] == undefined ){
if (frameworkDirectories.length > 0)
version = frameworkDirectories.pop();
}
exeDir = msbuildDir + '\\' + version + '\\' + 'bin';
processor = mapProcessor(processor);
if(processor === 'x64')
exeDir = exeDir + '\\' + 'amd64';
if (!fs.existsSync(exeDir))
exeDir = process.env.WINDIR + '\\' + 'microsoft.net' + '\\' + this.processors[processor] + '\\' + 'v' + this.toolsVersion[version];
return exeDir + '\\' + 'msbuild.exe';
}
msbuild.prototype.buildexe = function(){
return this.getMSBuildPath(this.os,this.processor,this.version);
}
msbuild.prototype.config = function(name, value) {
if(name.toLowerCase() === 'targetframework') {
this.logger('\n * CONFIG WARNING: \''.concat(name,'\' has been deprecated\n Please use overrideParams (example: overrideParams = [\'/tv:4.0\'] )\n'));
return;
}
var map;
if (_.isPlainObject(name)) { map = name; }
else if (value !== undefined) { this[name] = value; return this; }
else if (name === undefined) { return this.values; }
else { return this[name]; }
for (var key in map) { this.values[name] = map[key]; }
return this;
};
msbuild.prototype.setConfig = function(cg){
this.os = cg.os || this.os;
this.processor = cg.processor || this.processor;
this.version = cg.version || this.version;
this.sourcePath = cg.sourcePath || this.sourcePath;
this.configuration = cg.configuration || this.configuration;
this.publishProfile = cg.publishProfile || this.publishProfile;
this.overrideParams = cg.overrideParams || this.overrideParams;
this.outputPath = cg.outputPath || this.outputPath;
}
msbuild.prototype.abort = function (msg) {
var self = this;
self.emit('error','',msg);
return;
}
msbuild.prototype.exec = function(exe,params,cb){
var self = this;
if(self.showHelp) { self.printHelp(); return; }
function onClose(code) {
var msg = '';
if (code === 0) {
msg = msg+('\n finished - (0) errors'.white.greenBG);
self.emit('done',null,msg);
}
else {
msg = ('\n error code: ' + code).grey+('\n failed - errors'.white.redBG);
msg += '\n'+exe;
if(params !== undefined && typeof params === 'array'){
params.forEach(function(p){msg += ' ' + p; });
}
self.emit('error',code,msg);
return;
}
cb();
}
return spawn(exe, params, { stdio: 'inherit'}).on('close', onClose );
}
msbuild.prototype.getBuildParams = function(params){
if(params.indexOf('configuration') === -1 && this.configuration)
params.push('/p:configuration='+this.configuration);
if(params.indexOf('publishprofile') === -1 && this.publishProfile)
params.push('/p:publishprofile=' + this.publishProfile);
return params;
}
msbuild.prototype.getPackageParams = function(params){
if(params.indexOf('package') === -1)
params.push('/t:package');
if(params.indexOf('deployonbuild') === -1)
params.push('/p:deployonbuild=false');
if(params.indexOf('outputpath') === -1 && this.outputPath)
params.push('/p:outputpath='+this.outputPath);
return params;
}
msbuild.prototype.getPublishParams = function(params){
if(params.indexOf('deployonbuild') === -1)
params.push('/p:deployonbuild=true');
if(params.indexOf('allowUntrustedCertificate') === -1)
params.push('/p:allowUntrustedCertificate=true');
return params;
}
msbuild.prototype.getOverrideParams = function(params){
this.overrideParams.forEach(function (param) {
if (!validateCmdParameter(param)) {
this.logger('error: invalid parameter "'+param+'"');
return;
}
params.push(param);
});
return params;
}
msbuild.prototype.emitStatusStart = function(action){
var startingMsg = (' '+action+' starting').cyan;
this.emit('status',null,startingMsg);
}
msbuild.prototype.validateSourcePath = function () {
return fs.existsSync(this.sourcePath);
}
msbuild.prototype.build = function(){
this.emitStatusStart('build');
var params = [];
var self = this;
params.push(this.sourcePath);
this.getBuildParams(params);
this.getOverrideParams(params);
if(!this.validateSourcePath()){
this.abort('aborting...bad source path');
return;
}
this.exec(this.buildexe(),params,function(){
self.logger('build done');
});
}
msbuild.prototype.package = function(){
this.emitStatusStart('package');
var params = [];
var self = this;
params.push(this.sourcePath);
this.getBuildParams(params);
this.getOverrideParams(params);
this.getPackageParams(params);
if(!this.validateSourcePath()){
this.abort('aborting...bad source path');
return;
}
this.exec(this.buildexe(),params,function(){self.logger('package done');});
}
msbuild.prototype.publish = function(){
this.emitStatusStart('publish');
var params = [];
var self = this;
params.push(this.sourcePath);
this.getBuildParams(params);
this.getOverrideParams(params);
this.getPublishParams(params);
if(!this.validateSourcePath()){
this.abort('aborting...bad source path');
return;
}
this.exec(this.buildexe(),params,function(){self.logger('publish done');});
}
msbuild.prototype.printHelp = function(){
var o = this;
var helpFunctionsToIgnore = ['exec','path','buildexe','on','once','emit','addListener','removeListener','removeAllListeners','listeners','setMaxListeners','printHelp'];
this.logger("\nfunctions".cyan.bold);
this.logger('*******************'.cyan);
for(var p in o){
if(typeof(o[p]) == 'function'){
if(helpFunctionsToIgnore.indexOf(p) > -1) continue;
if(p == 'printOptions') continue;
if(p == '?') continue;
this.logger(p.redBG);
}
}
this.logger("\nevents".cyan.bold + ' [err,results]'.grey);
this.logger('*******************'.cyan);
this.logger('status'.redBG+' // msbuild.on(\'status\',myfunc)'.grey);
this.logger('error'.redBG+' // msbuild.on(\'error\',myfunc)'.grey);
this.logger('done'.redBG+' // msbuild.on(\'done\',myfunc)'.grey);
}
module.exports = function(callback){
var msb = new msbuild(callback);
msb.on('status',function(err,results){
if(this.showHelp) return;
if(err) this.logger(err.redBG);
this.logger(results);
});
msb.on('error',
function(err,results){
this.logger(' error'.red);
if(err) this.logger(err.redBG);
this.logger(results.red);
});
msb.on('done',function(err,results){
this.logger(results);
this.logger(lineBreak);
if(typeof callback == 'function') callback();
});
return msb;
};