Skip to content

Commit

Permalink
Update mechanic of outputting installed dotnet version
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanZosimov committed May 15, 2023
1 parent c5a57b2 commit 0bc4390
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -199,7 +198,6 @@ export class DotnetCoreInstaller {
}

public async installDotnet(): Promise<string> {
const listOfInstalledVersions = await this.getListOfInstalledVersions();
const windowsDefaultOptions = [
'-NoLogo',
'-Sta',
Expand Down Expand Up @@ -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
Expand All @@ -269,25 +267,19 @@ export class DotnetCoreInstaller {
`Failed to install dotnet, exit code: ${exitCode}. ${stderr}`
);
}
return await this.outputDotnetVersion(listOfInstalledVersions);
}

private async getListOfInstalledVersions(): Promise<string[]> {
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;
return this.parseInstalledVersion(stdout);
}

private async outputDotnetVersion(
listOfInstalledVersions: string[]
): Promise<string> {
const updatedListOfInstalledVersions =
await this.getListOfInstalledVersions();
const installedVersion = updatedListOfInstalledVersions.filter(
el => !listOfInstalledVersions.includes(el)
);
return installedVersion[0];
private parseInstalledVersion(stdout: string): string {
const regex = /(?<version>\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`
);
}
return matchedResult.groups!.version;
}
}

0 comments on commit 0bc4390

Please sign in to comment.