This repository has been archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
msbuild-finder.js
171 lines (151 loc) · 4.87 KB
/
msbuild-finder.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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var constants = require('./constants');
var fs = require('fs');
var PluginError = gutil.PluginError;
var child = require ('child_process');
var msBuildFromWhere = function(pathRoot) {
var vsWherePath = path.join(pathRoot, 'Microsoft Visual Studio','Installer', 'vswhere.exe');
var whereProcess = child.spawnSync(vsWherePath,
['-latest', '-products', '*', '-requires', 'Microsoft.Component.MSBuild'],
{
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
encoding: 'utf-8'
}
);
var cmdOutput = '';
if (whereProcess.output === null) {
return '';
}
if (whereProcess.output.length > 0){
for (var index = 0; index < whereProcess.output.length; index++) {
cmdOutput = whereProcess.output[index] || '';
if (cmdOutput.length > 0) {
break;
}
}
}
var installKeyword = 'installationPath';
if (cmdOutput.length > 0) {
var results = cmdOutput.split(/\r?\n/);
for (var cmdLineIndex = 0; cmdLineIndex < results.length; cmdLineIndex++) {
var cmdLine = results[cmdLineIndex];
if (cmdLine.startsWith(installKeyword)) {
var match = cmdLine.replace(installKeyword + ': ', '');
return match;
}
}
}
return '';
}
var detectMsBuild15Dir = function (pathRoot) {
var wherePath = msBuildFromWhere(pathRoot) || '';
if (wherePath.length > 0) {
return wherePath;
}
var vs2017Path = path.join(pathRoot, 'Microsoft Visual Studio', '2017');
var possibleFolders = ['BuildTools', 'Enterprise', 'Professional', 'Community'];
for (var index = 0; index < possibleFolders.length; index++) {
try {
var folderPath = path.join(vs2017Path, possibleFolders[index]);
fs.statSync(folderPath);
return folderPath;
} catch (e) {}
}
}
// Use MSBuild over XBuild where possible
var detectMsBuildOverXBuild = function () {
try {
var output = child.spawnSync('which', ['msbuild'], {encoding: 'utf8'});
if (output.stderr && output.stderr !== 0) {
return 'xbuild';
}
return 'msbuild';
} catch (e) {}
}
var autoDetectVersion = function (pathRoot) {
// Try to detect MSBuild 15.0.
var msbuild15Dir = detectMsBuild15Dir(pathRoot);
if (msbuild15Dir) {
return [msbuild15Dir, 15.0];
}
// Detect MSBuild lower than 15.0.
// ported from https://github.com/stevewillcock/grunt-msbuild/blob/master/tasks/msbuild.js#L167-L181
var msbuildDir = path.join(pathRoot, 'MSBuild');
var msbuildDirExists = true;
try {
fs.statSync(msbuildDir);
} catch (e) {
msbuildDirExists = false;
}
if (msbuildDirExists) {
var msbuildVersions = fs.readdirSync(msbuildDir)
.filter(function (entryName) {
var binDirExists = true;
var binDirPath = path.join(msbuildDir, entryName, 'Bin');
try {
fs.statSync(binDirPath);
} catch (e) {
binDirExists = false;
}
return entryName.indexOf('1') === 0 && binDirExists;
});
if (msbuildVersions.length > 0) {
// Return latest installed msbuild version
return [pathRoot, parseFloat(msbuildVersions.pop())];
}
}
return [pathRoot, 4.0];
};
module.exports.find = function (options) {
if (options.platform.match(/linux|darwin/)) {
var msbuildPath = detectMsBuildOverXBuild();
if (msbuildPath) {
return msbuildPath;
}
return 'xbuild';
} else if (!options.platform.match(/^win/)) {
return 'xbuild';
}
var msbuildRoot;
var is64Bit = options.architecture === 'x64';
// On 64-bit systems msbuild is always under the x86 directory. If this
// doesn't exist we are on a 32-bit system. See also:
// https://blogs.msdn.microsoft.com/visualstudio/2013/07/24/msbuild-is-now-part-of-visual-studio/
var pathRoot;
if (is64Bit) {
pathRoot = process.env['ProgramFiles(x86)'] || 'C:/Program Files (x86)';
} else {
pathRoot = process.env['ProgramFiles'] || 'C:/Program Files';
}
if (options.toolsVersion === 'auto') {
var result = autoDetectVersion(pathRoot);
msbuildRoot = result[0]
options.toolsVersion = result[1];
} else {
if (options.toolsVersion === 15.0) {
var msbuildDir = detectMsBuild15Dir(pathRoot);
if (msbuildDir) {
msbuildRoot = msbuildDir;
} else {
msbuildRoot = pathRoot;
}
} else {
msbuildRoot = pathRoot;
}
}
var version = constants.MSBUILD_VERSIONS[options.toolsVersion];
if (!version) {
throw new PluginError(constants.PLUGIN_NAME, 'No MSBuild Version was supplied!');
}
if (version === '12.0' || version === '14.0' || version === '15.0') {
var x64_dir = is64Bit ? 'amd64' : '';
return path.join(msbuildRoot, 'MSBuild', version, 'Bin', x64_dir, 'MSBuild.exe');
} else {
var framework = is64Bit ? 'Framework64' : 'Framework';
return path.join(options.windir, 'Microsoft.Net', framework, version, 'MSBuild.exe');
}
};