From c55599bc7e067ad0ec4b7c85c97ebf3b1ce6d80f Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 1 Apr 2024 00:35:16 -0700 Subject: [PATCH 1/3] feat: add types for the public functions --- lib.d.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib.js | 31 ++++++++++++++++++++++++++++++- package.json | 2 ++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 lib.d.ts diff --git a/lib.d.ts b/lib.d.ts new file mode 100644 index 0000000..f3aaafb --- /dev/null +++ b/lib.d.ts @@ -0,0 +1,46 @@ +/** + * Convert the vs version (e.g. 2022) or year (e.g. 17.0) to the version number (e.g. 17.0) + * @param {string} vsversion the year (e.g. 2022) or version number (e.g. 17.0) + * @returns {string} the version number (e.g. 17.0) + */ +export function vsversion_to_versionnumber(version: string): string + +/** + * Convert the vs version (e.g. 17.0) or year (e.g. 2022) to the year (e.g. 2022) + * @param {string} vsversion the version number (e.g. 17.0) or year (e.g. 2022) + * @returns {string} the year (e.g. 2022) + */ +export function vsversion_to_year(version: string): string + +/** + * Find MSVC tools with vswhere + * @param {string} pattern the pattern to search for + * @param {string} version_pattern the version pattern to search for + * @returns {string | null} the path to the found MSVC tools + */ +export function findWithVswhere(version: string): string | null + +/** + * Find the vcvarsall.bat file for the given Visual Studio version + * @param {string} vsversion the version of Visual Studio to find (year or version number) + * @returns {string} the path to the vcvarsall.bat file + */ +export function findVcvarsall(version: string): string + +/** + * Setup MSVC Developer Command Prompt + * @param {string} arch - Target architecture + * @param {string} sdk - Windows SDK number to build for + * @param {string} toolset - VC++ compiler toolset version + * @param {'true' | 'false'} uwp - Build for Universal Windows Platform + * @param {'true' | 'false'} spectre - Enable Spectre mitigations + * @param {string} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). + */ +export function setupMSVCDevCmd( + arch: string, + sdk?: string, + toolset?: string, + uwp?: 'true' | 'false', + spectre?: 'true' | 'false', + vsversion?: string, +) diff --git a/lib.js b/lib.js index e1774d8..0c44a1a 100644 --- a/lib.js +++ b/lib.js @@ -19,6 +19,11 @@ const VsYearVersion = { '2013': '12.0', } +/** + * Convert the vs version (e.g. 2022) or year (e.g. 17.0) to the version number (e.g. 17.0) + * @param {string} vsversion the year (e.g. 2022) or version number (e.g. 17.0) + * @returns {string} the version number (e.g. 17.0) + */ function vsversion_to_versionnumber(vsversion) { if (Object.values(VsYearVersion).includes(vsversion)) { return vsversion @@ -31,6 +36,11 @@ function vsversion_to_versionnumber(vsversion) { } exports.vsversion_to_versionnumber = vsversion_to_versionnumber +/** + * Convert the vs version (e.g. 17.0) or year (e.g. 2022) to the year (e.g. 2022) + * @param {string} vsversion the version number (e.g. 17.0) or year (e.g. 2022) + * @returns {string} the year (e.g. 2022) + */ function vsversion_to_year(vsversion) { if (Object.keys(VsYearVersion).includes(vsversion)) { return vsversion @@ -47,6 +57,12 @@ exports.vsversion_to_year = vsversion_to_year const VSWHERE_PATH = `${PROGRAM_FILES_X86}\\Microsoft Visual Studio\\Installer` +/** + * Find MSVC tools with vswhere + * @param {string} pattern the pattern to search for + * @param {string} version_pattern the version pattern to search for + * @returns {string | null} the path to the found MSVC tools + */ function findWithVswhere(pattern, version_pattern) { try { let installationPath = child_process.execSync(`vswhere -products * ${version_pattern} -prerelease -property installationPath`).toString().trim() @@ -58,6 +74,11 @@ function findWithVswhere(pattern, version_pattern) { } exports.findWithVswhere = findWithVswhere +/** + * Find the vcvarsall.bat file for the given Visual Studio version + * @param {string} vsversion the version of Visual Studio to find (year or version number) + * @returns {string} the path to the vcvarsall.bat file + */ function findVcvarsall(vsversion) { const vsversion_number = vsversion_to_versionnumber(vsversion) let version_pattern @@ -120,7 +141,15 @@ function filterPathValue(path) { return paths.filter(unique).join(';') } -/** See https://github.com/ilammy/msvc-dev-cmd#inputs */ +/** + * Setup MSVC Developer Command Prompt + * @param {string} arch - Target architecture + * @param {string} sdk - Windows SDK number to build for + * @param {string} toolset - VC++ compiler toolset version + * @param {'true' | 'false'} uwp - Build for Universal Windows Platform + * @param {'true' | 'false'} spectre - Enable Spectre mitigations + * @param {string} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). + */ function setupMSVCDevCmd(arch, sdk, toolset, uwp, spectre, vsversion) { if (process.platform != 'win32') { core.info('This is not a Windows virtual environment, bye!') diff --git a/package.json b/package.json index 1b35f6e..d7028f7 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "version": "1.13.0-dev", "description": "GitHub Action to setup Developer Command Prompt for Microsoft Visual C++", "main": "index.js", + "types": "./lib.d.ts", + "type": "commonjs", "scripts": { "lint": "eslint index.js" }, From 57abb83850ea85109ce838a54b7968c041c6087d Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 1 Apr 2024 00:38:01 -0700 Subject: [PATCH 2/3] fix: support boolean in uwp and spectre --- action.yml | 2 ++ lib.d.ts | 8 ++++---- lib.js | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/action.yml b/action.yml index b20fad3..ef1f492 100644 --- a/action.yml +++ b/action.yml @@ -8,10 +8,12 @@ inputs: description: Windows SDK number to build for spectre: description: Enable Specre mitigations + default: "false" toolset: description: VC++ compiler toolset version uwp: description: Build for Universal Windows Platform + default: "false" vsversion: description: The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). runs: diff --git a/lib.d.ts b/lib.d.ts index f3aaafb..9187791 100644 --- a/lib.d.ts +++ b/lib.d.ts @@ -32,15 +32,15 @@ export function findVcvarsall(version: string): string * @param {string} arch - Target architecture * @param {string} sdk - Windows SDK number to build for * @param {string} toolset - VC++ compiler toolset version - * @param {'true' | 'false'} uwp - Build for Universal Windows Platform - * @param {'true' | 'false'} spectre - Enable Spectre mitigations + * @param {boolean | 'true' | 'false'} uwp - Build for Universal Windows Platform + * @param {boolean | 'true' | 'false'} spectre - Enable Spectre mitigations * @param {string} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). */ export function setupMSVCDevCmd( arch: string, sdk?: string, toolset?: string, - uwp?: 'true' | 'false', - spectre?: 'true' | 'false', + uwp?: boolean | 'true' | 'false', + spectre?: boolean | 'true' | 'false', vsversion?: string, ) diff --git a/lib.js b/lib.js index 0c44a1a..7ea08ba 100644 --- a/lib.js +++ b/lib.js @@ -146,8 +146,8 @@ function filterPathValue(path) { * @param {string} arch - Target architecture * @param {string} sdk - Windows SDK number to build for * @param {string} toolset - VC++ compiler toolset version - * @param {'true' | 'false'} uwp - Build for Universal Windows Platform - * @param {'true' | 'false'} spectre - Enable Spectre mitigations + * @param {boolean | 'true' | 'false'} uwp - Build for Universal Windows Platform + * @param {boolean | 'true' | 'false'} spectre - Enable Spectre mitigations * @param {string} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). */ function setupMSVCDevCmd(arch, sdk, toolset, uwp, spectre, vsversion) { @@ -176,7 +176,8 @@ function setupMSVCDevCmd(arch, sdk, toolset, uwp, spectre, vsversion) { // Call the configuration batch file and then output *all* the environment variables. var args = [arch] - if (uwp == 'true') { + + if (uwp && JSON.parse(uwp) === true) { args.push('uwp') } if (sdk) { @@ -185,7 +186,7 @@ function setupMSVCDevCmd(arch, sdk, toolset, uwp, spectre, vsversion) { if (toolset) { args.push(`-vcvars_ver=${toolset}`) } - if (spectre == 'true') { + if (spectre && JSON.parse(spectre) === true) { args.push('-vcvars_spectre_libs=spectre') } From c01f519bd995460228ed3dec4df51df92dc290fd Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 1 Apr 2024 00:49:09 -0700 Subject: [PATCH 3/3] fix: consider optional params in the types --- lib.d.ts | 20 ++++++++++---------- lib.js | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib.d.ts b/lib.d.ts index 9187791..88b4e83 100644 --- a/lib.d.ts +++ b/lib.d.ts @@ -1,9 +1,9 @@ /** * Convert the vs version (e.g. 2022) or year (e.g. 17.0) to the version number (e.g. 17.0) - * @param {string} vsversion the year (e.g. 2022) or version number (e.g. 17.0) - * @returns {string} the version number (e.g. 17.0) + * @param {string | undefined} vsversion the year (e.g. 2022) or version number (e.g. 17.0) + * @returns {string | undefined} the version number (e.g. 17.0) */ -export function vsversion_to_versionnumber(version: string): string +export function vsversion_to_versionnumber(version: string | undefined): string | undefined /** * Convert the vs version (e.g. 17.0) or year (e.g. 2022) to the year (e.g. 2022) @@ -18,11 +18,11 @@ export function vsversion_to_year(version: string): string * @param {string} version_pattern the version pattern to search for * @returns {string | null} the path to the found MSVC tools */ -export function findWithVswhere(version: string): string | null +export function findWithVswhere(version: string, version_pattern: string): string | null /** * Find the vcvarsall.bat file for the given Visual Studio version - * @param {string} vsversion the version of Visual Studio to find (year or version number) + * @param {string | undefined} vsversion the version of Visual Studio to find (year or version number) * @returns {string} the path to the vcvarsall.bat file */ export function findVcvarsall(version: string): string @@ -30,11 +30,11 @@ export function findVcvarsall(version: string): string /** * Setup MSVC Developer Command Prompt * @param {string} arch - Target architecture - * @param {string} sdk - Windows SDK number to build for - * @param {string} toolset - VC++ compiler toolset version - * @param {boolean | 'true' | 'false'} uwp - Build for Universal Windows Platform - * @param {boolean | 'true' | 'false'} spectre - Enable Spectre mitigations - * @param {string} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). + * @param {string | undefined} sdk - Windows SDK number to build for + * @param {string | undefined} toolset - VC++ compiler toolset version + * @param {boolean | 'true' | 'false' | undefined} uwp - Build for Universal Windows Platform + * @param {boolean | 'true' | 'false' | undefined} spectre - Enable Spectre mitigations + * @param {string | undefined} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). */ export function setupMSVCDevCmd( arch: string, diff --git a/lib.js b/lib.js index 7ea08ba..03762e1 100644 --- a/lib.js +++ b/lib.js @@ -21,8 +21,8 @@ const VsYearVersion = { /** * Convert the vs version (e.g. 2022) or year (e.g. 17.0) to the version number (e.g. 17.0) - * @param {string} vsversion the year (e.g. 2022) or version number (e.g. 17.0) - * @returns {string} the version number (e.g. 17.0) + * @param {string | undefined} vsversion the year (e.g. 2022) or version number (e.g. 17.0) + * @returns {string | undefined} the version number (e.g. 17.0) */ function vsversion_to_versionnumber(vsversion) { if (Object.values(VsYearVersion).includes(vsversion)) { @@ -76,7 +76,7 @@ exports.findWithVswhere = findWithVswhere /** * Find the vcvarsall.bat file for the given Visual Studio version - * @param {string} vsversion the version of Visual Studio to find (year or version number) + * @param {string | undefined} vsversion the version of Visual Studio to find (year or version number) * @returns {string} the path to the vcvarsall.bat file */ function findVcvarsall(vsversion) { @@ -144,11 +144,11 @@ function filterPathValue(path) { /** * Setup MSVC Developer Command Prompt * @param {string} arch - Target architecture - * @param {string} sdk - Windows SDK number to build for - * @param {string} toolset - VC++ compiler toolset version - * @param {boolean | 'true' | 'false'} uwp - Build for Universal Windows Platform - * @param {boolean | 'true' | 'false'} spectre - Enable Spectre mitigations - * @param {string} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). + * @param {string | undefined} sdk - Windows SDK number to build for + * @param {string | undefined} toolset - VC++ compiler toolset version + * @param {boolean | 'true' | 'false' | undefined} uwp - Build for Universal Windows Platform + * @param {boolean | 'true' | 'false' | undefined} spectre - Enable Spectre mitigations + * @param {string | undefined} vsversion - The Visual Studio version to use. This can be the version number (e.g. 16.0 for 2019) or the year (e.g. "2019"). */ function setupMSVCDevCmd(arch, sdk, toolset, uwp, spectre, vsversion) { if (process.platform != 'win32') {