From 0bc43909e03ff13fcbfd1901bd7fe6084e064162 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Mon, 15 May 2023 11:45:07 +0200 Subject: [PATCH] Update mechanic of outputting installed dotnet version --- src/installer.ts | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/installer.ts b/src/installer.ts index b8e628f..a12529f 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -4,7 +4,6 @@ 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 os from 'os'; import semver from 'semver'; @@ -199,7 +198,6 @@ export class DotnetCoreInstaller { } public async installDotnet(): Promise { - const listOfInstalledVersions = await this.getListOfInstalledVersions(); const windowsDefaultOptions = [ '-NoLogo', '-Sta', @@ -259,7 +257,7 @@ export class DotnetCoreInstaller { ignoreReturnCode: true, env: process.env as {string: string} }; - const {exitCode, stderr} = await exec.getExecOutput( + const {exitCode, stdout, stderr} = await exec.getExecOutput( `"${scriptPath}"`, scriptArguments, getExecOutputOptions @@ -269,25 +267,19 @@ export class DotnetCoreInstaller { `Failed to install dotnet, exit code: ${exitCode}. ${stderr}` ); } - return await this.outputDotnetVersion(listOfInstalledVersions); + + return this.parseInstalledVersion(stdout); } - private async getListOfInstalledVersions(): Promise { - const installationPath = process.env['DOTNET_INSTALL_DIR']!; - const versionsOnRunner: string[] = ( - await readdir(path.join(installationPath.replace(/'/g, ''), 'sdk')) - ).filter(el => semver.valid(el)); - return versionsOnRunner; - } + private parseInstalledVersion(stdout: string): string { + const regex = /(?\d+\.\d+\.\d+[a-z0-9._-]*)/gm; + const matchedResult = regex.exec(stdout); - private async outputDotnetVersion( - listOfInstalledVersions: string[] - ): Promise { - const updatedListOfInstalledVersions = - await this.getListOfInstalledVersions(); - const installedVersion = updatedListOfInstalledVersions.filter( - el => !listOfInstalledVersions.includes(el) - ); - return installedVersion[0]; + if (!matchedResult) { + throw new Error( + `Failed to parse installed by the script version of .NET` + ); + } + return matchedResult.groups!.version; } }