Skip to content

Commit 082bc54

Browse files
committed
Merge branch 'use-pwshv2-if-necessary-only'
2 parents 3f50a96 + 0595377 commit 082bc54

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

lib/find-visualstudio.js

+35-20
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,46 @@ VisualStudioFinder.prototype = {
120120

121121
this.log.error(`\n${errorLog}\n\n${infoLog}\n`)
122122
process.nextTick(this.callback.bind(null, new Error(
123-
'Could not find any Visual Studio installation to use')))
123+
'Could not find any Visual Studio installation to use')))
124124
},
125125

126126
// Invoke the PowerShell script to get information about Visual Studio 2017
127127
// or newer installations
128-
findVisualStudio2017OrNewer: function findVisualStudio2017OrNewer (cb) {
129-
var ps = path.join(process.env.SystemRoot, 'System32',
130-
'WindowsPowerShell', 'v1.0', 'powershell.exe')
131-
var csFile = path.join(__dirname, 'Find-VisualStudio.cs')
132-
var psArgs = [
133-
'-Version',
134-
'2',
135-
'-ExecutionPolicy',
136-
'Unrestricted',
137-
'-NoProfile',
138-
'-Command',
139-
'&{Add-Type -Path \'' + csFile + '\';' + '[VisualStudioConfiguration.Main]::PrintJson()}'
140-
]
141-
128+
findVisualStudio2017OrNewer: function findVisualStudio2017OrNewer(cb) {
129+
const ps = path.join(process.env.SystemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe')
130+
const csFile = path.join(__dirname, 'Find-VisualStudio.cs')
131+
const basePsArgs = ['-ExecutionPolicy', 'Unrestricted', '-NoProfile', '-Command',]
132+
const findLanguageModePsCmd =
133+
`&{ try { $ExecutionContext.SessionState.LanguageMode } catch { try { (Get-PSSessionConfiguration -Name Test).LanguageMode } catch { 'error' } } }`
134+
135+
const psArgs = [...basePsArgs, findLanguageModePsCmd]
142136
this.log.silly('Running', ps, psArgs)
143-
var child = execFile(ps, psArgs, { encoding: 'utf8' },
144-
(err, stdout, stderr) => {
145-
this.parseData(err, stdout, stderr, cb)
146-
})
147-
child.stdin.end()
137+
const childFindPsLanguageMode = execFile(
138+
ps,
139+
psArgs,
140+
{encoding: 'utf8'},
141+
(err, stdout, stderr) => {
142+
this.log.silly('PS stderr = %j', stderr)
143+
if (err) {
144+
this.log.silly('PS err = %j', err && (err.stack || err))
145+
this.addLog(
146+
'could not determine PowerShell LanguageMode, try re-running with \'--loglevel silly\' for more details')
147+
return cb(null)
148+
}
149+
const findVscDataPsCmd = `&{Add-Type -Path '${csFile}';[VisualStudioConfiguration.Main]::PrintJson()}`
150+
const psArgs = [...basePsArgs, findVscDataPsCmd]
151+
// the Add-Type command requires powershell to run with languageMode == FullLanguage
152+
// Because languageMode feature is not introduced yet in powershell v2, we can try to use it.
153+
// If powershell v2 is not installed (a problem only if languageMode is not FullLanguage), then it is not
154+
// possible to use feature Add-Type.
155+
if (stdout.toString('utf8').trim().toLowerCase() !== 'fulllanguage') {
156+
psArgs.unshift('-Version', '2',)
157+
}
158+
this.log.silly('Running', ps, psArgs)
159+
const child = execFile(ps, psArgs, {encoding: 'utf8'}, (err, stdout, stderr) => this.parseData(err, stdout, stderr, cb))
160+
child.stdin.end()
161+
})
162+
childFindPsLanguageMode.stdin.end()
148163
},
149164

150165
// Parse the output of the PowerShell script and look for an installation

0 commit comments

Comments
 (0)