From a77dbf07aa217b2264fc05b4a780f2dbbffd09b3 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Tue, 27 Sep 2022 15:13:09 +0200 Subject: [PATCH] Implement dotnet-version output --- action.yml | 3 +++ dist/index.js | 25 ++++++++++++++++++++++++- src/installer.ts | 22 ++++++++++++++++++++++ src/setup-dotnet.ts | 12 +++++++++++- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index f259c3b..dafec86 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.' +outputs: + dotnet-version: + description: 'Contains the installed by action .NET SDK version for reuse.' runs: using: 'node16' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index 9e6f7b4..26214d0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -356,8 +356,25 @@ class DotnetCoreInstaller { if (exitCode) { throw new Error(`Failed to install dotnet ${exitCode}. ${stdout}`); } + return this.outputDotnetVersion(stdout); }); } + outputDotnetVersion(logs) { + let resolvedVersion = ''; + const installedByScriptPattern = /Installed version is (?\d+\.\d+\.\d.*)$/m; + const preinstalledOnRunnerPattern = /.NET Core SDK with version '(?\d+\.\d+\.\d.*)'/m; + let regExpressions = [ + installedByScriptPattern, + preinstalledOnRunnerPattern + ]; + for (let regExp of regExpressions) { + if (regExp.test(logs)) { + resolvedVersion = logs.match(regExp).groups.version; + break; + } + } + return resolvedVersion; + } } exports.DotnetCoreInstaller = DotnetCoreInstaller; DotnetCoreInstaller.installationDirectoryWindows = path_1.default.join(process.env['PROGRAMFILES'] + '', 'dotnet'); @@ -408,6 +425,7 @@ const core = __importStar(__nccwpck_require__(2186)); const installer_1 = __nccwpck_require__(1480); const fs = __importStar(__nccwpck_require__(7147)); const path_1 = __importDefault(__nccwpck_require__(1017)); +const semver_1 = __importDefault(__nccwpck_require__(5911)); const auth = __importStar(__nccwpck_require__(8527)); const qualityOptions = [ 'daily', @@ -429,6 +447,7 @@ function run() { // Proxy, auth, (etc) are still set up, even if no version is identified // const versions = core.getMultilineInput('dotnet-version'); + let installedDotnetVersions = []; const globalJsonFileInput = core.getInput('global-json-file'); if (globalJsonFileInput) { const globalJsonPath = path_1.default.join(process.cwd(), globalJsonFileInput); @@ -454,7 +473,8 @@ function run() { const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { dotnetInstaller = new installer_1.DotnetCoreInstaller(version, quality); - yield dotnetInstaller.installDotnet(); + let installedVersion = yield dotnetInstaller.installDotnet(); + installedDotnetVersions.push(installedVersion); } installer_1.DotnetCoreInstaller.addToPath(); } @@ -463,6 +483,9 @@ function run() { if (sourceUrl) { auth.configAuthentication(sourceUrl, configFile); } + core.setOutput('dotnet-version', semver_1.default.maxSatisfying(installedDotnetVersions, '*', { + includePrerelease: true + })); const matchersPath = path_1.default.join(__dirname, '..', '.github'); core.info(`##[add-matcher]${path_1.default.join(matchersPath, 'csc.json')}`); } diff --git a/src/installer.ts b/src/installer.ts index 24bab1b..abf54ac 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -238,5 +238,27 @@ export class DotnetCoreInstaller { if (exitCode) { throw new Error(`Failed to install dotnet ${exitCode}. ${stdout}`); } + + return this.outputDotnetVersion(stdout); + } + + private outputDotnetVersion(logs: string): string { + let resolvedVersion: string = ''; + const installedByScriptPattern = /Installed version is (?\d+\.\d+\.\d.*)$/m; + const preinstalledOnRunnerPattern = /.NET Core SDK with version '(?\d+\.\d+\.\d.*)'/m; + + let regExpressions: RegExp[] = [ + installedByScriptPattern, + preinstalledOnRunnerPattern + ]; + + for (let regExp of regExpressions) { + if (regExp.test(logs)) { + resolvedVersion = logs.match(regExp)!.groups!.version; + break; + } + } + + return resolvedVersion; } } diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 2306362..eccdc26 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core'; import {DotnetCoreInstaller} from './installer'; import * as fs from 'fs'; import path from 'path'; +import semver from 'semver'; import * as auth from './authutil'; const qualityOptions = [ @@ -26,6 +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'); + let installedDotnetVersions: string[] = []; const globalJsonFileInput = core.getInput('global-json-file'); if (globalJsonFileInput) { @@ -60,7 +62,8 @@ export async function run() { const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { dotnetInstaller = new DotnetCoreInstaller(version, quality); - await dotnetInstaller.installDotnet(); + let installedVersion = await dotnetInstaller.installDotnet(); + installedDotnetVersions.push(installedVersion); } DotnetCoreInstaller.addToPath(); } @@ -71,6 +74,13 @@ export async function run() { auth.configAuthentication(sourceUrl, configFile); } + core.setOutput( + 'dotnet-version', + semver.maxSatisfying(installedDotnetVersions, '*', { + includePrerelease: true + }) + ); + const matchersPath = path.join(__dirname, '..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'csc.json')}`); } catch (error) {