@@ -9,23 +9,67 @@ const PROGRAM_FILES = [process.env['ProgramFiles(x86)'], process.env['ProgramFil
99
1010
1111const EDITIONS = [ 'Enterprise' , 'Professional' , 'Community' ]
12- const VERSIONS = [ '2022' , '2019' , '2017' ]
12+ const YEARS = [ '2022' , '2019' , '2017' ]
13+
14+ const VsYearVersion = {
15+ '2022' : '17.0' ,
16+ '2019' : '16.0' ,
17+ '2017' : '15.0' ,
18+ '2015' : '14.0' ,
19+ '2013' : '12.0' ,
20+ }
21+
22+ function vsversion_to_versionnumber ( vsversion ) {
23+ if ( Object . values ( VsYearVersion ) . includes ( vsversion ) ) {
24+ return vsversion
25+ } else {
26+ if ( vsversion in VsYearVersion ) {
27+ return VsYearVersion [ vsversion ]
28+ }
29+ }
30+ return vsversion
31+ }
32+ exports . vsversion_to_versionnumber = vsversion_to_versionnumber
33+
34+ function vsversion_to_year ( vsversion ) {
35+ if ( Object . keys ( VsYearVersion ) . includes ( vsversion ) ) {
36+ return vsversion
37+ } else {
38+ for ( const [ year , ver ] of Object . entries ( VsYearVersion ) ) {
39+ if ( ver === vsversion ) {
40+ return year
41+ }
42+ }
43+ }
44+ return vsversion
45+ }
46+ exports . vsversion_to_year = vsversion_to_year
1347
1448const VSWHERE_PATH = `${ PROGRAM_FILES_X86 } \\Microsoft Visual Studio\\Installer`
1549
16- function findWithVswhere ( pattern ) {
50+ function findWithVswhere ( pattern , version_pattern ) {
1751 try {
18- let installationPath = child_process . execSync ( `vswhere -products * -latest -prerelease -property installationPath` ) . toString ( ) . trim ( )
52+ let installationPath = child_process . execSync ( `vswhere -products * ${ version_pattern } -prerelease -property installationPath` ) . toString ( ) . trim ( )
1953 return installationPath + '\\' + pattern
2054 } catch ( e ) {
2155 core . warning ( `vswhere failed: ${ e } ` )
2256 }
2357 return null
2458}
59+ exports . findWithVswhere = findWithVswhere
60+
61+ function findVcvarsall ( vsversion ) {
62+ const vsversion_number = vsversion_to_versionnumber ( vsversion )
63+ let version_pattern
64+ if ( vsversion_number ) {
65+ const upper_bound = vsversion_number . split ( '.' ) [ 0 ] + '.9'
66+ version_pattern = `-version "${ vsversion_number } ,${ upper_bound } "`
67+ } else {
68+ version_pattern = "-latest"
69+ }
2570
26- function findVcvarsall ( ) {
2771 // If vswhere is available, ask it about the location of the latest Visual Studio.
28- let path = findWithVswhere ( 'VC\\Auxiliary\\Build\\vcvarsall.bat' )
72+ let path = findWithVswhere ( 'VC\\Auxiliary\\Build\\vcvarsall.bat' , version_pattern )
2973 if ( path && fs . existsSync ( path ) ) {
3074 core . info ( `Found with vswhere: ${ path } ` )
3175 return path
@@ -34,8 +78,9 @@ function findVcvarsall() {
3478
3579 // If that does not work, try the standard installation locations,
3680 // starting with the latest and moving to the oldest.
81+ const years = vsversion ? [ vsversion_to_year ( vsversion ) ] : YEARS
3782 for ( const prog_files of PROGRAM_FILES ) {
38- for ( const ver of VERSIONS ) {
83+ for ( const ver of years ) {
3984 for ( const ed of EDITIONS ) {
4085 path = `${ prog_files } \\Microsoft Visual Studio\\${ ver } \\${ ed } \\VC\\Auxiliary\\Build\\vcvarsall.bat`
4186 core . info ( `Trying standard location: ${ path } ` )
@@ -58,6 +103,7 @@ function findVcvarsall() {
58103
59104 throw new Error ( 'Microsoft Visual Studio not found' )
60105}
106+ exports . findVcvarsall = findVcvarsall
61107
62108function isPathVariable ( name ) {
63109 const pathLikeVariables = [ 'PATH' , 'INCLUDE' , 'LIB' , 'LIBPATH' ]
@@ -75,7 +121,7 @@ function filterPathValue(path) {
75121}
76122
77123/** See https://github.com/ilammy/msvc-dev-cmd#inputs */
78- function setupMSVCDevCmd ( arch , sdk , toolset , uwp , spectre ) {
124+ function setupMSVCDevCmd ( arch , sdk , toolset , uwp , spectre , vsversion ) {
79125 if ( process . platform != 'win32' ) {
80126 core . info ( 'This is not a Windows virtual environment, bye!' )
81127 return
@@ -114,7 +160,7 @@ function setupMSVCDevCmd(arch, sdk, toolset, uwp, spectre) {
114160 args . push ( '-vcvars_spectre_libs=spectre' )
115161 }
116162
117- const vcvars = `"${ findVcvarsall ( ) } " ${ args . join ( ' ' ) } `
163+ const vcvars = `"${ findVcvarsall ( vsversion ) } " ${ args . join ( ' ' ) } `
118164 core . debug ( `vcvars command-line: ${ vcvars } ` )
119165
120166 const cmd_output_string = child_process . execSync ( `set && cls && ${ vcvars } && cls && set` , { shell : "cmd" } ) . toString ( )
@@ -184,8 +230,9 @@ function main() {
184230 const toolset = core . getInput ( 'toolset' )
185231 const uwp = core . getInput ( 'uwp' )
186232 const spectre = core . getInput ( 'spectre' )
233+ const vsversion = core . getInput ( 'vsversion' )
187234
188- setupMSVCDevCmd ( arch , sdk , toolset , uwp , spectre )
235+ setupMSVCDevCmd ( arch , sdk , toolset , uwp , spectre , vsversion )
189236}
190237
191238try {
0 commit comments