|
| 1 | +module.exports = exports = findVisualStudio |
| 2 | +module.exports.test = { |
| 3 | + VisualStudioFinder: VisualStudioFinder, |
| 4 | + findVisualStudio: findVisualStudio |
| 5 | +} |
| 6 | + |
| 7 | +const log = require('npmlog') |
| 8 | +const execFile = require('child_process').execFile |
| 9 | +const path = require('path').win32 |
| 10 | +const logWithPrefix = require('./util').logWithPrefix |
| 11 | + |
| 12 | +function findVisualStudio (callback) { |
| 13 | + const finder = new VisualStudioFinder(callback) |
| 14 | + finder.findVisualStudio() |
| 15 | +} |
| 16 | + |
| 17 | +function VisualStudioFinder (callback) { |
| 18 | + this.callback = callback |
| 19 | + this.errorLog = [] |
| 20 | +} |
| 21 | + |
| 22 | +VisualStudioFinder.prototype = { |
| 23 | + log: logWithPrefix(log, 'find VS'), |
| 24 | + |
| 25 | + // Logs a message at verbose level, but also saves it to be displayed later |
| 26 | + // at error level if an error occurs. This should help diagnose the problem. |
| 27 | + addLog: function addLog (message) { |
| 28 | + this.log.verbose(message) |
| 29 | + this.errorLog.push(message) |
| 30 | + }, |
| 31 | + |
| 32 | + findVisualStudio: function findVisualStudio () { |
| 33 | + var ps = path.join(process.env.SystemRoot, 'System32', |
| 34 | + 'WindowsPowerShell', 'v1.0', 'powershell.exe') |
| 35 | + var csFile = path.join(__dirname, 'Find-VisualStudio.cs') |
| 36 | + var psArgs = ['-ExecutionPolicy', 'Unrestricted', '-NoProfile', |
| 37 | + '-Command', '&{Add-Type -Path \'' + csFile + '\';' + |
| 38 | + '[VisualStudioConfiguration.Main]::PrintJson()}'] |
| 39 | + |
| 40 | + this.log.silly('Running', ps, psArgs) |
| 41 | + var child = execFile(ps, psArgs, { encoding: 'utf8' }, |
| 42 | + this.parseData.bind(this)) |
| 43 | + child.stdin.end() |
| 44 | + }, |
| 45 | + |
| 46 | + parseData: function parseData (err, stdout, stderr) { |
| 47 | + this.log.silly('PS stderr = %j', stderr) |
| 48 | + |
| 49 | + if (err) { |
| 50 | + this.log.silly('PS err = %j', err && (err.stack || err)) |
| 51 | + return this.failPowershell() |
| 52 | + } |
| 53 | + |
| 54 | + var vsInfo |
| 55 | + try { |
| 56 | + vsInfo = JSON.parse(stdout) |
| 57 | + } catch (e) { |
| 58 | + this.log.silly('PS stdout = %j', stdout) |
| 59 | + this.log.silly(e) |
| 60 | + return this.failPowershell() |
| 61 | + } |
| 62 | + |
| 63 | + if (!Array.isArray(vsInfo)) { |
| 64 | + this.log.silly('PS stdout = %j', stdout) |
| 65 | + return this.failPowershell() |
| 66 | + } |
| 67 | + |
| 68 | + vsInfo = vsInfo.map((info) => { |
| 69 | + this.log.silly(`processing installation: "${info.path}"`) |
| 70 | + const versionYear = this.getVersionYear(info) |
| 71 | + return { |
| 72 | + path: info.path, |
| 73 | + version: info.version, |
| 74 | + versionYear: versionYear, |
| 75 | + hasMSBuild: this.getHasMSBuild(info), |
| 76 | + toolset: this.getToolset(info, versionYear), |
| 77 | + sdk: this.getSDK(info) |
| 78 | + } |
| 79 | + }) |
| 80 | + this.log.silly('vsInfo:', vsInfo) |
| 81 | + |
| 82 | + // Remove future versions or errors parsing version number |
| 83 | + vsInfo = vsInfo.filter((info) => { |
| 84 | + if (info.versionYear) { return true } |
| 85 | + this.addLog(`unknown version "${info.version}" found at "${info.path}"`) |
| 86 | + return false |
| 87 | + }) |
| 88 | + |
| 89 | + // Sort to place newer versions first |
| 90 | + vsInfo.sort((a, b) => b.versionYear - a.versionYear) |
| 91 | + |
| 92 | + for (var i = 0; i < vsInfo.length; ++i) { |
| 93 | + const info = vsInfo[i] |
| 94 | + this.addLog(`checking VS${info.versionYear} (${info.version}) found ` + |
| 95 | + `at\n"${info.path}"`) |
| 96 | + |
| 97 | + if (info.hasMSBuild) { |
| 98 | + this.addLog('- found "Visual Studio C++ core features"') |
| 99 | + } else { |
| 100 | + this.addLog('- "Visual Studio C++ core features" missing') |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + if (info.toolset) { |
| 105 | + this.addLog(`- found VC++ toolset: ${info.toolset}`) |
| 106 | + } else { |
| 107 | + this.addLog('- missing any VC++ toolset') |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + if (info.sdk) { |
| 112 | + this.addLog(`- found Windows SDK: ${info.sdk}`) |
| 113 | + } else { |
| 114 | + this.addLog('- missing any Windows SDK') |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + this.succeed(info) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + this.fail() |
| 123 | + }, |
| 124 | + |
| 125 | + succeed: function succeed (info) { |
| 126 | + this.log.info(`using VS${info.versionYear} (${info.version}) found ` + |
| 127 | + `at\n"${info.path}"`) |
| 128 | + process.nextTick(this.callback.bind(null, null, info)) |
| 129 | + }, |
| 130 | + |
| 131 | + failPowershell: function failPowershell () { |
| 132 | + process.nextTick(this.callback.bind(null, new Error( |
| 133 | + 'Could not use PowerShell to find Visual Studio'))) |
| 134 | + }, |
| 135 | + |
| 136 | + fail: function fail () { |
| 137 | + const errorLog = this.errorLog.join('\n') |
| 138 | + |
| 139 | + // For Windows 80 col console, use up to the column before the one marked |
| 140 | + // with X (total 79 chars including logger prefix, 62 chars usable here): |
| 141 | + // X |
| 142 | + const infoLog = [ |
| 143 | + '**************************************************************', |
| 144 | + 'You need to install the latest version of Visual Studio', |
| 145 | + 'including the "Desktop development with C++" workload.', |
| 146 | + 'For more information consult the documentation at:', |
| 147 | + 'https://github.com/nodejs/node-gyp#on-windows', |
| 148 | + '**************************************************************' |
| 149 | + ].join('\n') |
| 150 | + |
| 151 | + this.log.error(`\n${errorLog}\n\n${infoLog}\n`) |
| 152 | + process.nextTick(this.callback.bind(null, new Error( |
| 153 | + 'Could not find any Visual Studio installation to use'))) |
| 154 | + }, |
| 155 | + |
| 156 | + getVersionYear: function getVersionYear (info) { |
| 157 | + const version = parseInt(info.version, 10) |
| 158 | + if (version === 15) { |
| 159 | + return 2017 |
| 160 | + } |
| 161 | + this.log.silly('- failed to parse version:', info.version) |
| 162 | + return null |
| 163 | + }, |
| 164 | + |
| 165 | + getHasMSBuild: function getHasMSBuild (info) { |
| 166 | + const pkg = 'Microsoft.VisualStudio.VC.MSBuild.Base' |
| 167 | + return info.packages.indexOf(pkg) !== -1 |
| 168 | + }, |
| 169 | + |
| 170 | + getToolset: function getToolset (info, versionYear) { |
| 171 | + const pkg = 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' |
| 172 | + if (info.packages.indexOf(pkg) !== -1) { |
| 173 | + this.log.silly('- found VC.Tools.x86.x64') |
| 174 | + if (versionYear === 2017) { |
| 175 | + return 'v141' |
| 176 | + } |
| 177 | + } |
| 178 | + return null |
| 179 | + }, |
| 180 | + |
| 181 | + getSDK: function getSDK (info) { |
| 182 | + const win8SDK = 'Microsoft.VisualStudio.Component.Windows81SDK' |
| 183 | + const win10SDKPrefix = 'Microsoft.VisualStudio.Component.Windows10SDK.' |
| 184 | + |
| 185 | + var Win10SDKVer = 0 |
| 186 | + info.packages.forEach((pkg) => { |
| 187 | + if (!pkg.startsWith(win10SDKPrefix)) { |
| 188 | + return |
| 189 | + } |
| 190 | + const parts = pkg.split('.') |
| 191 | + if (parts.length > 5 && parts[5] !== 'Desktop') { |
| 192 | + this.log.silly('- ignoring non-Desktop Win10SDK:', pkg) |
| 193 | + return |
| 194 | + } |
| 195 | + const foundSdkVer = parseInt(parts[4], 10) |
| 196 | + if (isNaN(foundSdkVer)) { |
| 197 | + // Microsoft.VisualStudio.Component.Windows10SDK.IpOverUsb |
| 198 | + this.log.silly('- failed to parse Win10SDK number:', pkg) |
| 199 | + return |
| 200 | + } |
| 201 | + this.log.silly('- found Win10SDK:', foundSdkVer) |
| 202 | + Win10SDKVer = Math.max(Win10SDKVer, foundSdkVer) |
| 203 | + }) |
| 204 | + |
| 205 | + if (Win10SDKVer !== 0) { |
| 206 | + return `10.0.${Win10SDKVer}.0` |
| 207 | + } else if (info.packages.indexOf(win8SDK) !== -1) { |
| 208 | + this.log.silly('- found Win8SDK') |
| 209 | + return '8.1' |
| 210 | + } |
| 211 | + return null |
| 212 | + } |
| 213 | +} |
0 commit comments