Add feature of prefering already installed versions

This commit is contained in:
Nikolai Laevskii
2023-09-04 03:22:15 +02:00
parent faa2990a1a
commit e4db8cc43a
7 changed files with 408 additions and 228 deletions

37
src/dotnet-utils.ts Normal file
View File

@ -0,0 +1,37 @@
import * as exec from '@actions/exec';
export const listSdks = async () => {
const {stdout, exitCode} = await exec.getExecOutput(
'dotnet',
['--list-sdks'],
{
ignoreReturnCode: true
}
);
if (exitCode) {
return [];
}
return (
stdout
.trim()
.split('\n')
.map(versionInfo => versionInfo.trim())
.map(versionInfo => versionInfo.split(' ')[0])
// reverses output so newer versions are first
.reverse()
);
};
/**
* Function that matches string like that
* '3.1', '3.1.x', '3', '3.x', '6.0.4xx' to
* correct version number like '3.1.201', '3.1.201', '3.1.201', '3.1.201', '6.0.402'
*/
export const matchVersionToList = (version: string, versions: string[]) => {
const versionRegex = new RegExp(`^${version.replace(/x/g, '\\d+')}`);
const matchedVersion = versions.find(v => versionRegex.test(v));
return matchedVersion;
};

View File

@ -9,6 +9,7 @@ import os from 'os';
import semver from 'semver';
import {IS_WINDOWS, PLATFORM} from './utils';
import {QualityOptions} from './setup-dotnet';
import {listSdks, matchVersionToList} from './dotnet-utils';
export interface DotnetVersion {
type: string;
@ -22,7 +23,7 @@ export class DotnetVersionResolver {
private inputVersion: string;
private resolvedArgument: DotnetVersion;
constructor(version: string) {
constructor(version: string, private preferInstalled = false) {
this.inputVersion = version.trim();
this.resolvedArgument = {type: '', value: '', qualityFlag: false};
}
@ -33,11 +34,30 @@ export class DotnetVersionResolver {
`The 'dotnet-version' was supplied in invalid format: ${this.inputVersion}! Supported syntax: A.B.C, A.B, A.B.x, A, A.x, A.B.Cxx`
);
}
if (semver.valid(this.inputVersion)) {
this.createVersionArgument();
} else {
await this.createChannelArgument();
return;
}
if (!this.preferInstalled) {
await this.createChannelArgument();
return;
}
const requestedVersion = this.inputVersion;
const installedVersions = await listSdks();
const matchingInstalledVersion = matchVersionToList(
requestedVersion,
installedVersions
);
if (matchingInstalledVersion === undefined) {
this.createChannelArgument();
return;
}
this.createVersionArgument(matchingInstalledVersion);
}
private isNumericTag(versionTag): boolean {
@ -59,9 +79,9 @@ export class DotnetVersionResolver {
return majorTag ? true : false;
}
private createVersionArgument() {
private createVersionArgument(updatedVersion?: string) {
this.resolvedArgument.type = 'version';
this.resolvedArgument.value = this.inputVersion;
this.resolvedArgument.value = updatedVersion ?? this.inputVersion;
}
private async createChannelArgument() {
@ -253,10 +273,17 @@ export class DotnetCoreInstaller {
DotnetInstallDir.setEnvironmentVariable();
}
constructor(private version: string, private quality: QualityOptions) {}
constructor(
private version: string,
private quality: QualityOptions,
private preferInstalled = false
) {}
public async installDotnet(): Promise<string | null> {
const versionResolver = new DotnetVersionResolver(this.version);
const versionResolver = new DotnetVersionResolver(
this.version,
this.preferInstalled
);
const dotnetVersion = await versionResolver.createDotnetVersion();
/**

View File

@ -59,6 +59,7 @@ export async function run() {
if (versions.length) {
const quality = core.getInput('dotnet-quality') as QualityOptions;
const preferInstalled = core.getBooleanInput('prefer-installed');
if (quality && !qualityOptions.includes(quality)) {
throw new Error(
@ -69,7 +70,11 @@ export async function run() {
let dotnetInstaller: DotnetCoreInstaller;
const uniqueVersions = new Set<string>(versions);
for (const version of uniqueVersions) {
dotnetInstaller = new DotnetCoreInstaller(version, quality);
dotnetInstaller = new DotnetCoreInstaller(
version,
quality,
preferInstalled
);
const installedVersion = await dotnetInstaller.installDotnet();
installedDotnetVersions.push(installedVersion);
}