From 168eb53ca5da36975ebdf2534526483200e8cac5 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Thu, 29 Sep 2022 13:15:50 +0200 Subject: [PATCH] Change logic for getting the installed version --- dist/index.js | 43 ++++++++++++++++++++++++------------------- src/installer.ts | 46 ++++++++++++++++++++++++++-------------------- src/utils.ts | 1 + 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/dist/index.js b/dist/index.js index 5a43f0c..5eced1b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -189,6 +189,7 @@ const exec = __importStar(__nccwpck_require__(1514)); const io = __importStar(__nccwpck_require__(7436)); const hc = __importStar(__nccwpck_require__(6255)); const fs_1 = __nccwpck_require__(7147); +const promises_1 = __nccwpck_require__(3292); const path_1 = __importDefault(__nccwpck_require__(1017)); const semver_1 = __importDefault(__nccwpck_require__(5911)); const utils_1 = __nccwpck_require__(918); @@ -284,8 +285,8 @@ class DotnetCoreInstaller { } else { // This is the default set in install-dotnet.sh - core.addPath(path_1.default.join(process.env['HOME'] + '', '.dotnet')); - core.exportVariable('DOTNET_ROOT', path_1.default.join(process.env['HOME'] + '', '.dotnet')); + core.addPath(DotnetCoreInstaller.installationDirectoryMac); + core.exportVariable('DOTNET_ROOT', DotnetCoreInstaller.installationDirectoryMac); } } } @@ -351,34 +352,29 @@ class DotnetCoreInstaller { if (utils_1.IS_LINUX) { scriptArguments.push('--install-dir', DotnetCoreInstaller.installationDirectoryLinux); } + if (utils_1.IS_MAC) { + scriptArguments.push('--install-dir', DotnetCoreInstaller.installationDirectoryMac); + } } const { exitCode, stdout } = yield exec.getExecOutput(`"${scriptPath}"`, scriptArguments, { ignoreReturnCode: true }); if (exitCode) { throw new Error(`Failed to install dotnet ${exitCode}. ${stdout}`); } - return this.outputDotnetVersion(stdout); + return this.outputDotnetVersion(dotnetVersion.value, scriptArguments[scriptArguments.length - 1]); }); } - outputDotnetVersion(logs) { - let resolvedVersion = ''; - const installedByScriptPattern = /Installed version is (?\d+\.\d+\.\d.*)$/m; - const preinstalledOnRunnerPattern = /.NET Core SDK with version '(?\d+\.\d+\.\d.*)'/m; - const regExpressions = [ - installedByScriptPattern, - preinstalledOnRunnerPattern - ]; - for (let regExp of regExpressions) { - if (regExp.test(logs)) { - resolvedVersion = logs.match(regExp).groups.version; - break; - } - } - return resolvedVersion; + outputDotnetVersion(version, installationPath) { + return __awaiter(this, void 0, void 0, function* () { + let versionsOnRunner = yield promises_1.readdir(installationPath); + let installedVersion = semver_1.default.maxSatisfying(versionsOnRunner, version); + return installedVersion; + }); } } exports.DotnetCoreInstaller = DotnetCoreInstaller; DotnetCoreInstaller.installationDirectoryWindows = path_1.default.join(process.env['PROGRAMFILES'] + '', 'dotnet'); DotnetCoreInstaller.installationDirectoryLinux = '/usr/share/dotnet'; +DotnetCoreInstaller.installationDirectoryMac = path_1.default.join(process.env['HOME'] + '', '.dotnet'); /***/ }), @@ -525,9 +521,10 @@ run(); "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.IS_LINUX = exports.IS_WINDOWS = void 0; +exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0; exports.IS_WINDOWS = process.platform === 'win32'; exports.IS_LINUX = process.platform === 'linux'; +exports.IS_MAC = process.platform === 'darwin'; /***/ }), @@ -25715,6 +25712,14 @@ module.exports = require("fs"); /***/ }), +/***/ 3292: +/***/ ((module) => { + +"use strict"; +module.exports = require("fs/promises"); + +/***/ }), + /***/ 3685: /***/ ((module) => { diff --git a/src/installer.ts b/src/installer.ts index 3d94a5d..f0698f6 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -4,9 +4,10 @@ import * as exec from '@actions/exec'; import * as io from '@actions/io'; import * as hc from '@actions/http-client'; import {chmodSync} from 'fs'; +import {readdir} from 'fs/promises'; import path from 'path'; import semver from 'semver'; -import {IS_LINUX, IS_WINDOWS} from './utils'; +import {IS_LINUX, IS_WINDOWS, IS_MAC} from './utils'; import {QualityOptions} from './setup-dotnet'; export interface DotnetVersion { @@ -116,6 +117,10 @@ export class DotnetCoreInstaller { 'dotnet' ); private static readonly installationDirectoryLinux = '/usr/share/dotnet'; + private static readonly installationDirectoryMac = path.join( + process.env['HOME'] + '', + '.dotnet' + ); static addToPath() { if (process.env['DOTNET_INSTALL_DIR']) { @@ -136,10 +141,10 @@ export class DotnetCoreInstaller { ); } else { // This is the default set in install-dotnet.sh - core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); + core.addPath(DotnetCoreInstaller.installationDirectoryMac); core.exportVariable( 'DOTNET_ROOT', - path.join(process.env['HOME'] + '', '.dotnet') + DotnetCoreInstaller.installationDirectoryMac ); } } @@ -229,6 +234,13 @@ export class DotnetCoreInstaller { DotnetCoreInstaller.installationDirectoryLinux ); } + + if (IS_MAC) { + scriptArguments.push( + '--install-dir', + DotnetCoreInstaller.installationDirectoryMac + ); + } } const {exitCode, stdout} = await exec.getExecOutput( `"${scriptPath}"`, @@ -239,26 +251,20 @@ export class DotnetCoreInstaller { throw new Error(`Failed to install dotnet ${exitCode}. ${stdout}`); } - return this.outputDotnetVersion(stdout); + return this.outputDotnetVersion( + dotnetVersion.value, + scriptArguments[scriptArguments.length - 1] + ); } - 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; + private async outputDotnetVersion( + version, + installationPath + ): Promise { + let versionsOnRunner: string[] = await readdir(installationPath); - const regExpressions: RegExp[] = [ - installedByScriptPattern, - preinstalledOnRunnerPattern - ]; + let installedVersion = semver.maxSatisfying(versionsOnRunner, version)!; - for (let regExp of regExpressions) { - if (regExp.test(logs)) { - resolvedVersion = logs.match(regExp)!.groups!.version; - break; - } - } - - return resolvedVersion; + return installedVersion; } } diff --git a/src/utils.ts b/src/utils.ts index 77886ce..ff20ee3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,2 +1,3 @@ export const IS_WINDOWS = process.platform === 'win32'; export const IS_LINUX = process.platform === 'linux'; +export const IS_MAC = process.platform === 'darwin';