From 34c30d0e8188386834a5bb496c316cdeba7658c0 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Wed, 12 Apr 2023 15:44:03 +0200 Subject: [PATCH] Refactor logic --- .github/workflows/e2e-tests.yml | 10 +++---- __tests__/installer.test.ts | 3 +- dist/index.js | 50 ++++++++++++++++++++++----------- src/installer.ts | 49 +++++++++++++++++++++++--------- 4 files changed, 75 insertions(+), 37 deletions(-) diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 36fd1ef..f3588e8 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -177,17 +177,17 @@ jobs: - name: Clear toolcache shell: pwsh run: __tests__/clear-toolcache.ps1 ${{ runner.os }} - - name: Setup dotnet '3.1.1xx' + - name: Setup dotnet '5.0.1xx' uses: ./ with: - dotnet-version: '3.1.1xx' - - name: Setup dotnet '6.0.3xx' + dotnet-version: '5.0.1xx' + - name: Setup dotnet '7.0.1xx' uses: ./ with: - dotnet-version: '6.0.3xx' + dotnet-version: '7.0.1xx' - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 3.1.1 6.0.3 + run: __tests__/verify-dotnet.ps1 5.0.1 7.0.1 test-setup-global-json-specified-and-version: runs-on: ${{ matrix.operating-system }} diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 6c20afe..363310c 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -201,7 +201,6 @@ describe('DotnetVersionResolver tests', () => { '.2.3', '.2.x', '*.', - '*', '1.2.', '1.2.-abc', 'a.b', @@ -222,7 +221,7 @@ describe('DotnetVersionResolver tests', () => { } ); - each(['3.1', '3.1.x', '3.1.*', '3.1.X', '3.1.1xx']).test( + each(['3.1', '3.1.x', '3.1.*', '3.1.X', '5.0.1xx']).test( "if version: '%s' that can be resolved to 'channel' option is supplied, it should set type to 'channel' in version object", async version => { const dotnetVersionResolver = new installer.DotnetVersionResolver( diff --git a/dist/index.js b/dist/index.js index bc00897..ed522cc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -250,33 +250,51 @@ class DotnetVersionResolver { } resolveVersionInput() { return __awaiter(this, void 0, void 0, function* () { - const isLatestPatchSyntax = /^\d+\.\d+\.\d{1}x{2}$/.test(this.inputVersion); - if (!semver_1.default.validRange(this.inputVersion) && !isLatestPatchSyntax) { + if (!semver_1.default.validRange(this.inputVersion) && !this.isLatestPatchSyntax()) { throw new Error(`'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_1.default.valid(this.inputVersion)) { - this.resolvedArgument.type = 'version'; - this.resolvedArgument.value = this.inputVersion; + this.createVersionArgument(); } else { - this.resolvedArgument.type = 'channel'; - const [major, minor] = this.inputVersion.split('.'); - if (isLatestPatchSyntax) { - this.resolvedArgument.value = this.inputVersion; - } - else if (this.isNumericTag(major) && this.isNumericTag(minor)) { - this.resolvedArgument.value = `${major}.${minor}`; - } - else { - this.resolvedArgument.value = yield this.getLatestByMajorTag(major); - } - this.resolvedArgument.qualityFlag = +major >= 6 ? true : false; + yield this.createChannelArgument(); } }); } isNumericTag(versionTag) { return /^\d+$/.test(versionTag); } + isLatestPatchSyntax() { + var _b, _c; + const majorTag = (_c = (_b = this.inputVersion.match(/^(?\d+)\.\d+\.\d{1}x{2}$/)) === null || _b === void 0 ? void 0 : _b.groups) === null || _c === void 0 ? void 0 : _c.majorTag; + if (majorTag && parseInt(majorTag) < 5) { + throw new Error(`'dotnet-version' was supplied in invalid format: ${this.inputVersion}! The A.B.Cxx syntax is available since the .NET 5.0 release.`); + } + return majorTag ? true : false; + } + createVersionArgument() { + this.resolvedArgument.type = 'version'; + this.resolvedArgument.value = this.inputVersion; + } + createChannelArgument() { + return __awaiter(this, void 0, void 0, function* () { + this.resolvedArgument.type = 'channel'; + const [major, minor] = this.inputVersion.split('.'); + if (this.isLatestPatchSyntax()) { + this.resolvedArgument.value = this.inputVersion; + } + else if (this.isNumericTag(major) && this.isNumericTag(minor)) { + this.resolvedArgument.value = `${major}.${minor}`; + } + else if (this.isNumericTag(major)) { + this.resolvedArgument.value = yield this.getLatestByMajorTag(major); + } + else { + this.resolvedArgument.value = 'LTS'; + } + this.resolvedArgument.qualityFlag = +major >= 6 ? true : false; + }); + } createDotNetVersion() { return __awaiter(this, void 0, void 0, function* () { yield this.resolveVersionInput(); diff --git a/src/installer.ts b/src/installer.ts index 22fa100..9b85f7b 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -27,26 +27,15 @@ export class DotnetVersionResolver { } private async resolveVersionInput(): Promise { - const isLatestPatchSyntax = /^\d+\.\d+\.\d{1}x{2}$/.test(this.inputVersion); - if (!semver.validRange(this.inputVersion) && !isLatestPatchSyntax) { + if (!semver.validRange(this.inputVersion) && !this.isLatestPatchSyntax()) { throw new Error( `'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.resolvedArgument.type = 'version'; - this.resolvedArgument.value = this.inputVersion; + this.createVersionArgument(); } else { - this.resolvedArgument.type = 'channel'; - const [major, minor] = this.inputVersion.split('.'); - if (isLatestPatchSyntax) { - this.resolvedArgument.value = this.inputVersion; - } else if (this.isNumericTag(major) && this.isNumericTag(minor)) { - this.resolvedArgument.value = `${major}.${minor}`; - } else { - this.resolvedArgument.value = await this.getLatestByMajorTag(major); - } - this.resolvedArgument.qualityFlag = +major >= 6 ? true : false; + await this.createChannelArgument(); } } @@ -54,6 +43,38 @@ export class DotnetVersionResolver { return /^\d+$/.test(versionTag); } + private isLatestPatchSyntax() { + const majorTag = this.inputVersion.match( + /^(?\d+)\.\d+\.\d{1}x{2}$/ + )?.groups?.majorTag; + if (majorTag && parseInt(majorTag) < 5) { + throw new Error( + `'dotnet-version' was supplied in invalid format: ${this.inputVersion}! The A.B.Cxx syntax is available since the .NET 5.0 release.` + ); + } + return majorTag ? true : false; + } + + private createVersionArgument() { + this.resolvedArgument.type = 'version'; + this.resolvedArgument.value = this.inputVersion; + } + + private async createChannelArgument() { + this.resolvedArgument.type = 'channel'; + const [major, minor] = this.inputVersion.split('.'); + if (this.isLatestPatchSyntax()) { + this.resolvedArgument.value = this.inputVersion; + } else if (this.isNumericTag(major) && this.isNumericTag(minor)) { + this.resolvedArgument.value = `${major}.${minor}`; + } else if (this.isNumericTag(major)) { + this.resolvedArgument.value = await this.getLatestByMajorTag(major); + } else { + this.resolvedArgument.value = 'LTS'; + } + this.resolvedArgument.qualityFlag = +major >= 6 ? true : false; + } + public async createDotNetVersion(): Promise { await this.resolveVersionInput(); if (!this.resolvedArgument.type) {