From fefaa59d2eb646c641920b07bfc9897559f8d6d6 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Tue, 16 May 2023 14:58:18 +0200 Subject: [PATCH] update logic of outputting dotnet-version --- dist/index.js | 40 ++++++++++++++++++++++++------- src/installer.ts | 9 ++++--- src/setup-dotnet.ts | 58 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/dist/index.js b/dist/index.js index 51fc3a2..dc3e1f6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -439,7 +439,8 @@ class DotnetCoreInstaller { const regex = /(?\d+\.\d+\.\d+[a-z0-9._-]*)/gm; const matchedResult = regex.exec(stdout); if (!matchedResult) { - throw new Error(`Failed to parse installed by the script version of .NET`); + core.warning(`Failed to parse installed by the script version of .NET`); + return null; } return matchedResult.groups.version; } @@ -577,13 +578,18 @@ function run() { if (sourceUrl) { auth.configAuthentication(sourceUrl, configFile); } - const comparisonRange = globalJsonFileInput - ? versions[versions.length - 1] - : '*'; - const versionToOutput = semver_1.default.maxSatisfying(installedDotnetVersions, comparisonRange, { - includePrerelease: true - }); - core.setOutput('dotnet-version', versionToOutput); + // const comparisonRange: string = globalJsonFileInput + // ? versions[versions.length - 1]! + // : '*'; + // const versionToOutput = semver.maxSatisfying( + // installedDotnetVersions, + // comparisonRange, + // { + // includePrerelease: true + // } + // ); + // core.setOutput('dotnet-version', versionToOutput); + outputInstalledVersion(installedDotnetVersions, globalJsonFileInput); const matchersPath = path_1.default.join(__dirname, '..', '.github'); core.info(`##[add-matcher]${path_1.default.join(matchersPath, 'csc.json')}`); } @@ -608,6 +614,24 @@ function getVersionFromGlobalJson(globalJsonPath) { } return version; } +function outputInstalledVersion(installedVersions, globalJsonFileInput) { + if (!installedVersions.length) { + core.info(`No .NET version was installed. The 'dotnet-version' output will not be set.`); + } + if (installedVersions.includes(null)) { + core.warning(`Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.`); + return; + } + if (globalJsonFileInput) { + const versionToOutput = installedVersions.at(-1); + core.setOutput('dotnet-version', versionToOutput); + return; + } + const versionToOutput = semver_1.default.maxSatisfying(installedVersions, '*', { + includePrerelease: true + }); + core.setOutput('dotnet-version', versionToOutput); +} run(); diff --git a/src/installer.ts b/src/installer.ts index a12529f..d31ec00 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -197,7 +197,7 @@ export class DotnetCoreInstaller { } } - public async installDotnet(): Promise { + public async installDotnet(): Promise { const windowsDefaultOptions = [ '-NoLogo', '-Sta', @@ -271,14 +271,13 @@ export class DotnetCoreInstaller { return this.parseInstalledVersion(stdout); } - private parseInstalledVersion(stdout: string): string { + private parseInstalledVersion(stdout: string): string | null { const regex = /(?\d+\.\d+\.\d+[a-z0-9._-]*)/gm; const matchedResult = regex.exec(stdout); if (!matchedResult) { - throw new Error( - `Failed to parse installed by the script version of .NET` - ); + core.warning(`Failed to parse installed by the script version of .NET`); + return null; } return matchedResult.groups!.version; } diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 31c80a2..1a4755a 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -27,7 +27,7 @@ export async function run() { // Proxy, auth, (etc) are still set up, even if no version is identified // const versions = core.getMultilineInput('dotnet-version'); - const installedDotnetVersions: string[] = []; + const installedDotnetVersions: (string | null)[] = []; const globalJsonFileInput = core.getInput('global-json-file'); if (globalJsonFileInput) { @@ -78,19 +78,20 @@ export async function run() { auth.configAuthentication(sourceUrl, configFile); } - const comparisonRange: string = globalJsonFileInput - ? versions[versions.length - 1]! - : '*'; + // const comparisonRange: string = globalJsonFileInput + // ? versions[versions.length - 1]! + // : '*'; - const versionToOutput = semver.maxSatisfying( - installedDotnetVersions, - comparisonRange, - { - includePrerelease: true - } - ); + // const versionToOutput = semver.maxSatisfying( + // installedDotnetVersions, + // comparisonRange, + // { + // includePrerelease: true + // } + // ); - core.setOutput('dotnet-version', versionToOutput); + // core.setOutput('dotnet-version', versionToOutput); + outputInstalledVersion(installedDotnetVersions, globalJsonFileInput); const matchersPath = path.join(__dirname, '..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'csc.json')}`); @@ -116,4 +117,37 @@ function getVersionFromGlobalJson(globalJsonPath: string): string { return version; } +function outputInstalledVersion( + installedVersions: (string | null)[], + globalJsonFileInput: string +): void { + if (!installedVersions.length) { + core.info( + `No .NET version was installed. The 'dotnet-version' output will not be set.` + ); + } + if (installedVersions.includes(null)) { + core.warning( + `Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.` + ); + return; + } + + if (globalJsonFileInput) { + const versionToOutput = installedVersions.at(-1); + core.setOutput('dotnet-version', versionToOutput); + return; + } + + const versionToOutput = semver.maxSatisfying( + installedVersions as string[], + '*', + { + includePrerelease: true + } + ); + + core.setOutput('dotnet-version', versionToOutput); +} + run();