Skip to content

Commit fdb0d15

Browse files
committed
feat: allow VCINSTALLDIR to specify a portable instance
1 parent 323957b commit fdb0d15

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lib/find-visualstudio.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ class VisualStudioFinder {
5454
}
5555

5656
const checks = [
57+
() => this.findVisualStudio2019OrNewerFromSpecifiedLocation(),
5758
() => this.findVisualStudio2019OrNewerUsingSetupModule(),
5859
() => this.findVisualStudio2019OrNewer(),
60+
() => this.findVisualStudio2017FromSpecifiedLocation(),
5961
() => this.findVisualStudio2017UsingSetupModule(),
6062
() => this.findVisualStudio2017(),
6163
() => this.findVisualStudio2015(),
@@ -116,6 +118,44 @@ class VisualStudioFinder {
116118
throw new Error('Could not find any Visual Studio installation to use')
117119
}
118120

121+
async findVisualStudio2019OrNewerFromSpecifiedLocation () {
122+
return this.findVSFromSpecifiedLocation([2019, 2022])
123+
}
124+
125+
async findVisualStudio2017FromSpecifiedLocation () {
126+
if (this.nodeSemver.major >= 22) {
127+
this.addLog(
128+
'not looking for VS2017 as it is only supported up to Node.js 21')
129+
return null
130+
}
131+
return this.findVSFromSpecifiedLocation([2017])
132+
}
133+
134+
async findVSFromSpecifiedLocation (supportedYears) {
135+
if (!this.envVcInstallDir) {
136+
return null
137+
}
138+
const info = {
139+
path: path.resolve(this.envVcInstallDir),
140+
// Assume the version specified by the user is correct.
141+
version: process.env.VSCMD_VER,
142+
packages: [
143+
'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
144+
// Assume MSBuild exists. It will be checked in processing.
145+
'Microsoft.VisualStudio.VC.MSBuild.Base'
146+
]
147+
}
148+
149+
// Is there a better way to get SDK information?
150+
const envWindowsSDKVersion = process.env.WindowsSDKVersion
151+
const sdkVersionMatched = envWindowsSDKVersion?.match(/^(\d+)\.(\d+)\.(\d+)\..*/)
152+
if (sdkVersionMatched) {
153+
info.packages.push(`Microsoft.VisualStudio.Component.Windows10SDK.${sdkVersionMatched[3]}.Desktop`)
154+
}
155+
// pass for further processing
156+
return this.processData([info], supportedYears)
157+
}
158+
119159
async findVisualStudio2019OrNewerUsingSetupModule () {
120160
return this.findNewVSUsingSetupModule([2019, 2022])
121161
}
@@ -321,7 +361,7 @@ class VisualStudioFinder {
321361

322362
// Helper - process version information
323363
getVersionInfo (info) {
324-
const match = /^(\d+)\.(\d+)\..*/.exec(info.version)
364+
const match = /^(\d+)\.(\d+)(?:\..*)?/.exec(info.version)
325365
if (!match) {
326366
this.log.silly('- failed to parse version:', info.version)
327367
return {}

test/test-find-visualstudio.js

+25
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,29 @@ describe('find-visualstudio', function () {
782782
assert.ok(/find .* Visual Studio/i.test(err), 'expect error')
783783
assert.ok(!info, 'no data')
784784
})
785+
786+
it('run on a portable VS Command Prompt with sufficient environs', async function () {
787+
process.env.VCINSTALLDIR = 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC'
788+
process.env.VSCMD_VER = '16.0'
789+
process.env.WindowsSDKVersion = '10.0.17763.0'
790+
791+
const finder = new TestVisualStudioFinder(semverV1, null)
792+
793+
allVsVersions(finder)
794+
const { err, info } = await finder.findVisualStudio()
795+
assert.strictEqual(err, null)
796+
assert.deepStrictEqual(info, {
797+
msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\' +
798+
'Community\\MSBuild\\Current\\Bin\\MSBuild.exe',
799+
path:
800+
'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community',
801+
sdk: '10.0.17763.0',
802+
toolset: 'v142',
803+
// Assume version in the environ is correct.
804+
version: '16.0',
805+
versionMajor: 16,
806+
versionMinor: 0,
807+
versionYear: 2019
808+
})
809+
})
785810
})

0 commit comments

Comments
 (0)