Files
setup-msbuild/src/main.ts
T

238 lines
7.2 KiB
TypeScript
Raw Normal View History

2020-01-31 11:14:06 -08:00
import * as core from '@actions/core'
import * as exec from '@actions/exec'
2020-04-04 12:59:26 -07:00
import * as fs from 'fs'
import * as os from 'os'
2020-01-31 11:14:06 -08:00
import * as path from 'path'
import * as io from '@actions/io'
2020-01-31 11:14:06 -08:00
import {ExecOptions} from '@actions/exec/lib/interfaces'
const IS_WINDOWS = process.platform === 'win32'
const VS_VERSION = core.getInput('vs-version') || 'latest'
2020-04-21 16:27:45 -07:00
const VSWHERE_PATH = core.getInput('vswhere-path')
2021-07-13 13:31:52 -08:00
const ALLOW_PRERELEASE = core.getInput('vs-prerelease') || 'false'
const MSBUILD_ARCH_INPUT = core.getInput('msbuild-architecture')
2026-11-13 12:44:32 -06:00
// The minimum Visual Studio version for the v4 automatic 64-bit default.
const MIN_VS_VERSION_FOR_X64 = 17
export function getMSBuildArchitectures(
explicitArch: string,
2026-11-13 12:44:32 -06:00
hostArch: string,
vsMajorVersion: number | undefined
): string[] {
if (explicitArch) {
return [explicitArch]
}
2026-11-13 12:44:32 -06:00
if (vsMajorVersion === undefined || vsMajorVersion < MIN_VS_VERSION_FOR_X64) {
return ['x86']
2026-11-13 12:44:32 -06:00
}
if (hostArch === 'arm64') {
return ['arm64', 'x64', 'x86']
2026-11-13 12:44:32 -06:00
}
if (hostArch === 'x64' || hostArch === 'amd64') {
return ['x64']
}
return ['x86']
}
// Parses the major version number out of a vswhere `installationVersion` string
// such as "17.9.34728.123". Returns undefined if it cannot be parsed.
export function parseMajorVersion(
installationVersion: string
): number | undefined {
const match = installationVersion.trim().match(/^(\d+)/)
if (!match) {
return undefined
}
const major = parseInt(match[1], 10)
return isNaN(major) ? undefined : major
}
2020-01-31 11:14:06 -08:00
// if a specific version of VS is requested
let VSWHERE_EXEC =
'-products * -requires Microsoft.Component.MSBuild -format json -utf8 -latest '
2021-07-13 13:31:52 -08:00
if (ALLOW_PRERELEASE === 'true') {
VSWHERE_EXEC += ' -prerelease '
}
2021-07-13 13:31:52 -08:00
2020-04-04 10:47:46 -07:00
if (VS_VERSION !== 'latest') {
2020-04-04 11:50:17 -07:00
VSWHERE_EXEC += `-version "${VS_VERSION}" `
2020-01-31 11:14:06 -08:00
}
2020-02-20 16:56:19 -08:00
core.debug(`Execution arguments: ${VSWHERE_EXEC}`)
2020-01-31 11:49:55 -08:00
2020-01-31 11:14:06 -08:00
async function run(): Promise<void> {
try {
// exit if non Windows runner
if (IS_WINDOWS === false) {
core.setFailed('setup-msbuild can only be run on Windows runners')
return
}
// check to see if we are using a specific path for vswhere
let vswhereToolExe = ''
2020-01-31 12:22:23 -08:00
if (VSWHERE_PATH) {
// specified a path for vswhere, use it
2020-04-21 16:57:28 -07:00
core.debug(`Using given vswhere-path: ${VSWHERE_PATH}`)
2020-01-31 11:14:06 -08:00
vswhereToolExe = path.join(VSWHERE_PATH, 'vswhere.exe')
} else {
// check in PATH to see if it is there
try {
const vsWhereInPath: string = await io.which('vswhere', true)
2020-04-21 16:57:28 -07:00
core.debug(`Found tool in PATH: ${vsWhereInPath}`)
vswhereToolExe = vsWhereInPath
} catch {
2020-04-21 16:40:01 -07:00
// fall back to VS-installed path
vswhereToolExe = path.join(
2020-04-21 16:27:45 -07:00
process.env['ProgramFiles(x86)'] as string,
'Microsoft Visual Studio\\Installer\\vswhere.exe'
)
2020-04-21 16:57:28 -07:00
core.debug(`Trying Visual Studio-installed path: ${vswhereToolExe}`)
2020-01-31 11:14:06 -08:00
}
}
2020-04-21 16:27:45 -07:00
if (!fs.existsSync(vswhereToolExe)) {
core.setFailed(
'setup-msbuild requires the path to where vswhere.exe exists'
)
return
}
2020-02-11 09:40:56 -08:00
core.debug(`Full tool exe: ${vswhereToolExe}`)
2020-01-31 11:14:06 -08:00
const output: Buffer[] = []
2026-11-13 12:44:32 -06:00
const options: ExecOptions = {}
options.listeners = {
stdout: (data: Buffer) => {
output.push(data)
2026-11-13 12:44:32 -06:00
}
}
await exec.exec(`"${vswhereToolExe}" ${VSWHERE_EXEC}`, [], options)
const installations: unknown = JSON.parse(
Buffer.concat(output).toString('utf8')
)
if (!Array.isArray(installations)) {
throw new Error('Invalid Visual Studio installation data from vswhere.')
}
if (installations.length === 0) {
2026-11-13 12:44:32 -06:00
core.setFailed('Unable to find MSBuild.')
return
}
const installation: unknown = installations[0]
if (
typeof installation !== 'object' ||
installation === null ||
!('installationPath' in installation)
) {
throw new Error('Invalid Visual Studio installation data from vswhere.')
}
const {installationPath, installationVersion} = installation as {
installationPath: unknown
installationVersion?: unknown
}
if (typeof installationPath !== 'string' || !installationPath) {
throw new Error('Invalid Visual Studio installation path from vswhere.')
}
core.debug(`Found installation path: ${installationPath}`)
const vsMajorVersion =
typeof installationVersion === 'string'
? parseMajorVersion(installationVersion)
: undefined
core.debug(`Found installation version: ${installationVersion}`)
// os.arch() describes Node, which can run under emulation on Windows.
const hostArch = (
process.env['PROCESSOR_ARCHITEW6432'] ||
process.env['RUNNER_ARCH'] ||
os.arch()
).toLowerCase()
const architectures = getMSBuildArchitectures(
MSBUILD_ARCH_INPUT,
hostArch,
vsMajorVersion
)
let msbuildArch = ''
2020-04-04 12:59:26 -07:00
let foundToolPath = ''
for (const architecture of architectures) {
const relativePaths =
architecture === 'x64' || architecture === 'arm64'
? [
`MSBuild\\Current\\Bin\\${
architecture === 'x64' ? 'amd64' : 'arm64'
}\\MSBuild.exe`
]
: [
'MSBuild\\Current\\Bin\\MSBuild.exe',
'MSBuild\\15.0\\Bin\\MSBuild.exe'
]
for (const relativePath of relativePaths) {
const toolPath = path.join(installationPath, relativePath)
2026-11-13 12:44:32 -06:00
core.debug(`Checking for path: ${toolPath}`)
if (fs.existsSync(toolPath)) {
foundToolPath = toolPath
msbuildArch = architecture
break
}
2026-11-13 12:44:32 -06:00
}
if (foundToolPath) {
break
2020-01-31 11:14:06 -08:00
}
}
if (!foundToolPath) {
2020-04-04 12:59:26 -07:00
core.setFailed('Unable to find MSBuild.')
2020-01-31 11:14:06 -08:00
return
}
if (!MSBUILD_ARCH_INPUT) {
if (
hostArch === 'arm64' &&
architectures[0] === 'arm64' &&
msbuildArch !== 'arm64'
) {
core.info(`ARM64 MSBuild not found; using ${msbuildArch} instead`)
}
core.info(
`Auto-detected msbuild-architecture: ${msbuildArch} (host architecture=${hostArch}, vsMajorVersion=${vsMajorVersion})`
)
}
core.info(`Found MSBuild tool path: ${foundToolPath}`)
core.info(`Using MSBuild architecture: ${msbuildArch}`)
// try to get the MSBuild version for diagnostic purposes; a failure here shouldn't fail the action
try {
let msbuildVersion = ''
const versionOptions: ExecOptions = {}
versionOptions.listeners = {
stdout: (data: Buffer) => {
msbuildVersion += data.toString()
}
}
await exec.exec(`"${foundToolPath}" -version -nologo`, [], versionOptions)
core.info(`Found MSBuild version: ${msbuildVersion.trim()}`)
} catch (error) {
core.debug(`Unable to determine MSBuild version: ${error.message}`)
}
2020-01-31 11:14:06 -08:00
// extract the folder location for the tool
const toolFolderPath = path.dirname(foundToolPath)
// set the outputs for the action to the folder path of msbuild
core.setOutput('msbuildPath', toolFolderPath)
// add tool path to PATH
core.addPath(toolFolderPath)
core.info(`Tool path added to PATH: ${toolFolderPath}`)
2020-01-31 11:14:06 -08:00
} catch (error) {
core.setFailed(error.message)
}
}
run()