From 2f8de776e1e4c4f6e358e818ee527c5c8d1ca4e9 Mon Sep 17 00:00:00 2001 From: Kevin Gosse Date: Fri, 21 Oct 2022 16:22:30 +0200 Subject: [PATCH 1/2] Add an optional architecture parameter to set the target architecture (x86, x64, arm64, ...). --- README.md | 11 +++++++++++ __tests__/installer.test.ts | 22 +++++++++++++++++++++- action.yml | 3 +++ src/installer.ts | 8 +++++++- src/setup-dotnet.ts | 7 ++++++- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cec68e955..8ecede894 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,17 @@ steps: - run: dotnet build ``` +**Specific architecture:** +```yml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-dotnet@v2 + with: + dotnet-version: '6.0.x' + architecture: 'x86' +- run: dotnet build +``` + **Multiple version installation**: ```yml steps: diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 79a90c34b..bd569986a 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -107,6 +107,24 @@ describe('DotnetCoreInstaller tests', () => { expect(process.env.PATH?.startsWith(toolDir)).toBe(true); }, 600000); //This needs some time to download on "slower" internet connections + it('Acquires architecture-specific version of dotnet if no matching version is installed', async () => { + await getDotnet('3.1', '', 'x64'); + var directory = fs + .readdirSync(path.join(toolDir, 'sdk')) + .filter(fn => fn.startsWith('3.1.')); + expect(directory.length > 0).toBe(true); + if (IS_WINDOWS) { + expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true); + } else { + expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true); + } + + expect(process.env.DOTNET_ROOT).toBeDefined; + expect(process.env.PATH).toBeDefined; + expect(process.env.DOTNET_ROOT).toBe(toolDir); + expect(process.env.PATH?.startsWith(toolDir)).toBe(true); + }, 600000); //This needs some time to download on "slower" internet connections + it('Returns string with installed SDK version', async () => { const version = '3.1.120'; let installedVersion: string; @@ -279,10 +297,12 @@ function normalizeFileContents(contents: string): string { async function getDotnet( version: string, quality: string = '' + architecture: string = '' ): Promise { const dotnetInstaller = new installer.DotnetCoreInstaller( version, - quality as QualityOptions + quality as QualityOptions, + architecture ); const installedVersion = await dotnetInstaller.installDotnet(); installer.DotnetCoreInstaller.addToPath(); diff --git a/action.yml b/action.yml index dafec8689..a5f51f556 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,9 @@ inputs: description: 'Optional OWNER for using packages from GitHub Package Registry organizations/users other than the current repository''s owner. Only used if a GPR URL is also provided in source-url' config-file: description: 'Optional NuGet.config location, if your NuGet.config isn''t located in the root of the repo.' + architecture: + description: 'Optional architecture to use. If not provided, will default to the OS architecture.' + required: False outputs: dotnet-version: description: 'Contains the installed by action .NET SDK version for reuse.' diff --git a/src/installer.ts b/src/installer.ts index be0444f98..0a3a5f18e 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -20,9 +20,11 @@ export interface DotnetVersion { export class DotnetVersionResolver { private inputVersion: string; private resolvedArgument: DotnetVersion; + private architecture: string; - constructor(version: string) { + constructor(version: string, architecture: string = '') { this.inputVersion = version.trim(); + this.architecture = architecture; this.resolvedArgument = {type: '', value: '', qualityFlag: false}; } @@ -230,6 +232,10 @@ export class DotnetCoreInstaller { if (this.quality) { this.setQuality(dotnetVersion, scriptArguments); } + + if (this.architecture != '') { + scriptArguments.push('--architecture', this.architecture); + } } // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used const getExecOutputOptions = { diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index ba2e419e2..0bc43d9e1 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -28,6 +28,11 @@ export async function run() { // const versions = core.getMultilineInput('dotnet-version'); const installedDotnetVersions: string[] = []; + let architecture = core.getInput('architecture'); + + if (!architecture) { + architecture = ''; + } const globalJsonFileInput = core.getInput('global-json-file'); if (globalJsonFileInput) { @@ -61,7 +66,7 @@ export async function run() { let dotnetInstaller: DotnetCoreInstaller; const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { - dotnetInstaller = new DotnetCoreInstaller(version, quality); + dotnetInstaller = new DotnetCoreInstaller(version, quality, architecture); const installedVersion = await dotnetInstaller.installDotnet(); installedDotnetVersions.push(installedVersion); } From 70a19cc0ab9192f41061fb1200e564691fe0b1d9 Mon Sep 17 00:00:00 2001 From: Kevin Gosse Date: Fri, 21 Oct 2022 16:29:07 +0200 Subject: [PATCH 2/2] Fix errors + Update generated files --- __tests__/installer.test.ts | 2 +- dist/index.js | 12 ++++++++++-- src/installer.ts | 14 +++++++++----- src/setup-dotnet.ts | 8 ++++++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index bd569986a..9126b2f00 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -296,7 +296,7 @@ function normalizeFileContents(contents: string): string { async function getDotnet( version: string, - quality: string = '' + quality: string = '', architecture: string = '' ): Promise { const dotnetInstaller = new installer.DotnetCoreInstaller( diff --git a/dist/index.js b/dist/index.js index 82fb7257a..8992da647 100644 --- a/dist/index.js +++ b/dist/index.js @@ -275,9 +275,10 @@ class DotnetVersionResolver { exports.DotnetVersionResolver = DotnetVersionResolver; DotnetVersionResolver.DotNetCoreIndexUrl = 'https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/releases-index.json'; class DotnetCoreInstaller { - constructor(version, quality) { + constructor(version, quality, architecture = '') { this.version = version; this.quality = quality; + this.architecture = architecture; } static convertInstallPathToAbsolute(installDir) { let transformedPath; @@ -352,6 +353,9 @@ class DotnetCoreInstaller { if (this.quality) { this.setQuality(dotnetVersion, scriptArguments); } + if (this.architecture != '') { + scriptArguments.push('--architecture', this.architecture); + } } // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used const getExecOutputOptions = { @@ -471,6 +475,10 @@ function run() { // const versions = core.getMultilineInput('dotnet-version'); const installedDotnetVersions = []; + let architecture = core.getInput('architecture'); + if (!architecture) { + architecture = ''; + } const globalJsonFileInput = core.getInput('global-json-file'); if (globalJsonFileInput) { const globalJsonPath = path_1.default.join(process.cwd(), globalJsonFileInput); @@ -495,7 +503,7 @@ function run() { let dotnetInstaller; const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { - dotnetInstaller = new installer_1.DotnetCoreInstaller(version, quality); + dotnetInstaller = new installer_1.DotnetCoreInstaller(version, quality, architecture); const installedVersion = yield dotnetInstaller.installDotnet(); installedDotnetVersions.push(installedVersion); } diff --git a/src/installer.ts b/src/installer.ts index 0a3a5f18e..cf0ad11ed 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -20,11 +20,9 @@ export interface DotnetVersion { export class DotnetVersionResolver { private inputVersion: string; private resolvedArgument: DotnetVersion; - private architecture: string; - constructor(version: string, architecture: string = '') { + constructor(version: string) { this.inputVersion = version.trim(); - this.architecture = architecture; this.resolvedArgument = {type: '', value: '', qualityFlag: false}; } @@ -115,6 +113,7 @@ export class DotnetVersionResolver { export class DotnetCoreInstaller { private version: string; private quality: QualityOptions; + private architecture: string; static { const installationDirectoryWindows = path.join( @@ -142,9 +141,14 @@ export class DotnetCoreInstaller { } } - constructor(version: string, quality: QualityOptions) { + constructor( + version: string, + quality: QualityOptions, + architecture: string = '' + ) { this.version = version; this.quality = quality; + this.architecture = architecture; } private static convertInstallPathToAbsolute(installDir: string): string { @@ -232,7 +236,7 @@ export class DotnetCoreInstaller { if (this.quality) { this.setQuality(dotnetVersion, scriptArguments); } - + if (this.architecture != '') { scriptArguments.push('--architecture', this.architecture); } diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 0bc43d9e1..ec997ff26 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -29,7 +29,7 @@ export async function run() { const versions = core.getMultilineInput('dotnet-version'); const installedDotnetVersions: string[] = []; let architecture = core.getInput('architecture'); - + if (!architecture) { architecture = ''; } @@ -66,7 +66,11 @@ export async function run() { let dotnetInstaller: DotnetCoreInstaller; const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { - dotnetInstaller = new DotnetCoreInstaller(version, quality, architecture); + dotnetInstaller = new DotnetCoreInstaller( + version, + quality, + architecture + ); const installedVersion = await dotnetInstaller.installDotnet(); installedDotnetVersions.push(installedVersion); }