|
| 1 | +module.exports=exports=findVisualStudio |
| 2 | +module.exports.test={ |
| 3 | +VisualStudioFinder: VisualStudioFinder, |
| 4 | +findVisualStudio: findVisualStudio |
| 5 | +} |
| 6 | + |
| 7 | +constlog=require('npmlog') |
| 8 | +constexecFile=require('child_process').execFile |
| 9 | +constpath=require('path').win32 |
| 10 | +constlogWithPrefix=require('./util').logWithPrefix |
| 11 | + |
| 12 | +functionfindVisualStudio(callback){ |
| 13 | +constfinder=newVisualStudioFinder(callback) |
| 14 | +finder.findVisualStudio() |
| 15 | +} |
| 16 | + |
| 17 | +functionVisualStudioFinder(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: functionaddLog(message){ |
| 28 | +this.log.verbose(message) |
| 29 | +this.errorLog.push(message) |
| 30 | +}, |
| 31 | + |
| 32 | +findVisualStudio: functionfindVisualStudio(){ |
| 33 | +varps=path.join(process.env.SystemRoot,'System32', |
| 34 | +'WindowsPowerShell','v1.0','powershell.exe') |
| 35 | +varcsFile=path.join(__dirname,'Find-VisualStudio.cs') |
| 36 | +varpsArgs=['-ExecutionPolicy','Unrestricted','-NoProfile', |
| 37 | +'-Command','&{Add-Type -Path \''+csFile+'\';'+ |
| 38 | +'[VisualStudioConfiguration.Main]::PrintJson()}'] |
| 39 | + |
| 40 | +this.log.silly('Running',ps,psArgs) |
| 41 | +varchild=execFile(ps,psArgs,{encoding: 'utf8'}, |
| 42 | +this.parseData.bind(this)) |
| 43 | +child.stdin.end() |
| 44 | +}, |
| 45 | + |
| 46 | +parseData: functionparseData(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 | +returnthis.failPowershell() |
| 52 | +} |
| 53 | + |
| 54 | +varvsInfo |
| 55 | +try{ |
| 56 | +vsInfo=JSON.parse(stdout) |
| 57 | +}catch(e){ |
| 58 | +this.log.silly('PS stdout = %j',stdout) |
| 59 | +this.log.silly(e) |
| 60 | +returnthis.failPowershell() |
| 61 | +} |
| 62 | + |
| 63 | +if(!Array.isArray(vsInfo)){ |
| 64 | +this.log.silly('PS stdout = %j',stdout) |
| 65 | +returnthis.failPowershell() |
| 66 | +} |
| 67 | + |
| 68 | +vsInfo=vsInfo.map((info)=>{ |
| 69 | +this.log.silly(`processing installation: "${info.path}"`) |
| 70 | +constversionYear=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){returntrue} |
| 85 | +this.addLog(`unknown version "${info.version}" found at "${info.path}"`) |
| 86 | +returnfalse |
| 87 | +}) |
| 88 | + |
| 89 | +// Sort to place newer versions first |
| 90 | +vsInfo.sort((a,b)=>b.versionYear-a.versionYear) |
| 91 | + |
| 92 | +for(vari=0;i<vsInfo.length;++i){ |
| 93 | +constinfo=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: functionsucceed(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: functionfailPowershell(){ |
| 132 | +process.nextTick(this.callback.bind(null,newError( |
| 133 | +'Could not use PowerShell to find Visual Studio'))) |
| 134 | +}, |
| 135 | + |
| 136 | +fail: functionfail(){ |
| 137 | +consterrorLog=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 | +constinfoLog=[ |
| 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,newError( |
| 153 | +'Could not find any Visual Studio installation to use'))) |
| 154 | +}, |
| 155 | + |
| 156 | +getVersionYear: functiongetVersionYear(info){ |
| 157 | +constversion=parseInt(info.version,10) |
| 158 | +if(version===15){ |
| 159 | +return2017 |
| 160 | +} |
| 161 | +this.log.silly('- failed to parse version:',info.version) |
| 162 | +returnnull |
| 163 | +}, |
| 164 | + |
| 165 | +getHasMSBuild: functiongetHasMSBuild(info){ |
| 166 | +constpkg='Microsoft.VisualStudio.VC.MSBuild.Base' |
| 167 | +returninfo.packages.indexOf(pkg)!==-1 |
| 168 | +}, |
| 169 | + |
| 170 | +getToolset: functiongetToolset(info,versionYear){ |
| 171 | +constpkg='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 | +returnnull |
| 179 | +}, |
| 180 | + |
| 181 | +getSDK: functiongetSDK(info){ |
| 182 | +constwin8SDK='Microsoft.VisualStudio.Component.Windows81SDK' |
| 183 | +constwin10SDKPrefix='Microsoft.VisualStudio.Component.Windows10SDK.' |
| 184 | + |
| 185 | +varWin10SDKVer=0 |
| 186 | +info.packages.forEach((pkg)=>{ |
| 187 | +if(!pkg.startsWith(win10SDKPrefix)){ |
| 188 | +return |
| 189 | +} |
| 190 | +constparts=pkg.split('.') |
| 191 | +if(parts.length>5&&parts[5]!=='Desktop'){ |
| 192 | +this.log.silly('- ignoring non-Desktop Win10SDK:',pkg) |
| 193 | +return |
| 194 | +} |
| 195 | +constfoundSdkVer=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 | +}elseif(info.packages.indexOf(win8SDK)!==-1){ |
| 208 | +this.log.silly('- found Win8SDK') |
| 209 | +return'8.1' |
| 210 | +} |
| 211 | +returnnull |
| 212 | +} |
| 213 | +} |
0 commit comments