|
| 1 | +import * as os from 'os' |
| 2 | +import * as path from 'path' |
| 3 | +import {getExecOutput} from '@actions/exec' |
| 4 | + |
| 5 | +export class VisualStudio { |
| 6 | + private constructor( |
| 7 | + readonly installationPath: string, |
| 8 | + readonly installationVersion: string, |
| 9 | + readonly catalog: VisualStudioCatalog, |
| 10 | + readonly properties: VisualStudioProperties |
| 11 | + ) {} |
| 12 | + |
| 13 | + // eslint-disable-next-line no-explicit-any |
| 14 | + static createFromJSON(json: any) { |
| 15 | + return new VisualStudio( |
| 16 | + json.installationPath, |
| 17 | + json.installationVersion, |
| 18 | + json.catalog, |
| 19 | + json.properties |
| 20 | + ) |
| 21 | + } |
| 22 | + |
| 23 | + async env() { |
| 24 | + /// https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170 |
| 25 | + const nativeToolsScriptx86 = path.join( |
| 26 | + this.installationPath, |
| 27 | + 'Common7', |
| 28 | + 'Tools', |
| 29 | + 'VsDevCmd.bat' |
| 30 | + ) |
| 31 | + const {stdout} = await getExecOutput( |
| 32 | + 'cmd', |
| 33 | + [ |
| 34 | + '/k', |
| 35 | + nativeToolsScriptx86, |
| 36 | + `-arch=${os.arch()}`, |
| 37 | + '&&', |
| 38 | + 'set', |
| 39 | + '&&', |
| 40 | + 'exit' |
| 41 | + ], |
| 42 | + {failOnStdErr: true} |
| 43 | + ) |
| 44 | + return Object.fromEntries( |
| 45 | + stdout |
| 46 | + .split(os.EOL) |
| 47 | + .filter(s => s.indexOf('=')) |
| 48 | + .map(s => s.trim()) |
| 49 | + .map(s => s.split('=', 2)) |
| 50 | + .filter(s => s.length === 2) |
| 51 | + .map(s => [s[0].trim(), s[1].trim()] as const) |
| 52 | + ) as VisualStudioEnv |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export interface VisualStudioCatalog { |
| 57 | + productDisplayVersion: string |
| 58 | +} |
| 59 | + |
| 60 | +export interface VisualStudioProperties { |
| 61 | + setupEngineFilePath: string |
| 62 | +} |
| 63 | + |
| 64 | +export interface VisualStudioRequirement { |
| 65 | + version: string |
| 66 | + components: string[] |
| 67 | +} |
| 68 | + |
| 69 | +export interface VisualStudioEnv { |
| 70 | + readonly UniversalCRTSdkDir?: string |
| 71 | + readonly UCRTVersion?: string |
| 72 | + readonly VCToolsInstallDir?: string |
| 73 | + readonly [name: string]: string | undefined |
| 74 | +} |
0 commit comments