Skip to content

Commit

Permalink
Refactor logic
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanZosimov committed Apr 12, 2023
1 parent 5f57067 commit 34c30d0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 37 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 1 addition & 2 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ describe('DotnetVersionResolver tests', () => {
'.2.3',
'.2.x',
'*.',
'*',
'1.2.',
'1.2.-abc',
'a.b',
Expand All @@ -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(
Expand Down
50 changes: 34 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^(?<majorTag>\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();
Expand Down
49 changes: 35 additions & 14 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,54 @@ export class DotnetVersionResolver {
}

private async resolveVersionInput(): Promise<void> {
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();
}
}

private isNumericTag(versionTag): boolean {
return /^\d+$/.test(versionTag);
}

private isLatestPatchSyntax() {
const majorTag = this.inputVersion.match(
/^(?<majorTag>\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<DotnetVersion> {
await this.resolveVersionInput();
if (!this.resolvedArgument.type) {
Expand Down

0 comments on commit 34c30d0

Please sign in to comment.