Update mechanic of outputting installed dotnet version

This commit is contained in:
IvanZosimov 2023-05-15 11:45:07 +02:00
parent c5a57b219c
commit 0bc43909e0
1 changed files with 12 additions and 20 deletions

View File

@ -4,7 +4,6 @@ import * as exec from '@actions/exec';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as hc from '@actions/http-client'; import * as hc from '@actions/http-client';
import {chmodSync} from 'fs'; import {chmodSync} from 'fs';
import {readdir} from 'fs/promises';
import path from 'path'; import path from 'path';
import os from 'os'; import os from 'os';
import semver from 'semver'; import semver from 'semver';
@ -199,7 +198,6 @@ export class DotnetCoreInstaller {
} }
public async installDotnet(): Promise<string> { public async installDotnet(): Promise<string> {
const listOfInstalledVersions = await this.getListOfInstalledVersions();
const windowsDefaultOptions = [ const windowsDefaultOptions = [
'-NoLogo', '-NoLogo',
'-Sta', '-Sta',
@ -259,7 +257,7 @@ export class DotnetCoreInstaller {
ignoreReturnCode: true, ignoreReturnCode: true,
env: process.env as {string: string} env: process.env as {string: string}
}; };
const {exitCode, stderr} = await exec.getExecOutput( const {exitCode, stdout, stderr} = await exec.getExecOutput(
`"${scriptPath}"`, `"${scriptPath}"`,
scriptArguments, scriptArguments,
getExecOutputOptions getExecOutputOptions
@ -269,25 +267,19 @@ export class DotnetCoreInstaller {
`Failed to install dotnet, exit code: ${exitCode}. ${stderr}` `Failed to install dotnet, exit code: ${exitCode}. ${stderr}`
); );
} }
return await this.outputDotnetVersion(listOfInstalledVersions);
return this.parseInstalledVersion(stdout);
} }
private async getListOfInstalledVersions(): Promise<string[]> { private parseInstalledVersion(stdout: string): string {
const installationPath = process.env['DOTNET_INSTALL_DIR']!; const regex = /(?<version>\d+\.\d+\.\d+[a-z0-9._-]*)/gm;
const versionsOnRunner: string[] = ( const matchedResult = regex.exec(stdout);
await readdir(path.join(installationPath.replace(/'/g, ''), 'sdk'))
).filter(el => semver.valid(el));
return versionsOnRunner;
}
private async outputDotnetVersion( if (!matchedResult) {
listOfInstalledVersions: string[] throw new Error(
): Promise<string> { `Failed to parse installed by the script version of .NET`
const updatedListOfInstalledVersions = );
await this.getListOfInstalledVersions(); }
const installedVersion = updatedListOfInstalledVersions.filter( return matchedResult.groups!.version;
el => !listOfInstalledVersions.includes(el)
);
return installedVersion[0];
} }
} }