Files
msvc-dev-cmd/index.js
T

129 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-10-01 22:13:57 +03:00
const core = require('@actions/core')
2019-10-01 22:51:02 +03:00
const execFile = require('util').promisify(require('child_process').execFile)
const fs = require('fs').promises
const os = require('os')
const path = require('path')
const process = require('process')
2020-04-26 21:54:04 +01:00
const EDITIONS = ['Enterprise', 'Professional', 'Community']
const VERSIONS = ['2019', '2017']
2019-10-01 22:51:02 +03:00
const InterestingVariables = [
'INCLUDE',
'LIB',
'LIBPATH',
'Path',
'Platform',
'VisualStudioVersion',
2019-10-01 22:51:02 +03:00
/^VCTools/,
/^VSCMD_/,
/^WindowsSDK/i,
]
2019-10-01 22:13:57 +03:00
async function main() {
2019-10-01 22:51:02 +03:00
if (process.platform != 'win32') {
core.info('This is not a Windows virtual environment, bye!')
return
}
2020-04-26 21:54:04 +01:00
// this should generate an array like
// [
// ['P2017', 'path\to\2017\Professional...'],
// ['C2017', 'path\to\2017\Entreprise...'],
// etc...
// [
// Given the order of each list it should check
// for the more recent versions first and the
// highest grade edition first.
var search_map = []
VERSIONS.forEach(ver => {
EDITIONS.forEach(ed => {
let label = ed.charAt(0) + ver
let path = `%ProgramFiles(x86)%\\Microsoft Visual Studio\\${ver}\\${ed}\\VC\\Auxiliary\\Build\\vcvarsall.bat`
search_map.push([label, path])
})
})
2019-10-01 22:51:02 +03:00
const arch = core.getInput('arch')
const sdk = core.getInput('sdk')
const toolset = core.getInput('toolset')
const uwp = core.getInput('uwp')
const spectre = core.getInput('spectre')
// Due to the way Microsoft Visual C++ is configured, we have to resort to the following hack:
// write a helper batch file which calls the configuration batch file and then outputs the
// configured environment variables, which we then pass to GitHub Actions.
const helper = path.join(os.homedir(), 'msvc-dev-cmd.bat')
var args = [arch]
if (uwp == 'true') {
args.push('uwp')
}
if (sdk) {
args.push(sdk)
}
if (toolset) {
args.push(`-vcvars_ver=${toolset}`)
}
if (spectre == 'true') {
args.push('-vcvars_spectre_libs=spectre')
}
core.debug(`Arguments: ${args.join(' ')}`)
2020-04-26 21:54:04 +01:00
var script = '';
search_map.forEach(pair => {
script += `@IF EXIST "${pair[1]}" GOTO :${pair[0]}\n`
})
script += `@ECHO "Microsoft Visual Studio not found"\n
@EXIT 1\n`
search_map.forEach(pair => {
script += `:${pair[0]}\n
@CALL "${pair[1]}" ${args.join(' ')}\n
@GOTO ENV\n`
})
script += `:ENV\n
@IF ERRORLEVEL 1 EXIT\n
@SET`
core.debug(script)
2019-10-01 22:51:02 +03:00
core.debug(`Writing helper file: ${helper}`)
2020-04-26 21:54:04 +01:00
await fs.writeFile(helper, script)
2019-10-01 22:51:02 +03:00
var environment
try {
core.debug('Executing helper')
const { stdout } = await execFile('cmd.exe', ['/q', '/c', helper])
environment = stdout.split('\r\n')
}
catch (error) {
core.debug(`Helper failed: ${error.message}`)
throw error
}
finally {
core.debug('Removing helper')
await fs.unlink(helper)
}
2019-10-02 01:09:14 +03:00
for (let string of environment) {
2019-10-01 22:51:02 +03:00
const [name, value] = string.split('=')
2019-10-02 01:09:14 +03:00
for (let pattern of InterestingVariables) {
2019-10-01 22:51:02 +03:00
if (name.match(pattern)) {
core.exportVariable(name, value)
break
}
}
}
core.info(`Configured Developer Command Prompt`)
2019-10-01 22:13:57 +03:00
}
2020-04-26 21:54:04 +01:00
main().catch((e) => core.setFailed('Could not setup Developer Command Prompt: ' + e.message))