Skip to content

Commit

Permalink
Merge branch 'litetex-support-latest-version'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Eisinger committed May 29, 2020
2 parents db2ebd0 + e169331 commit 6efb2bd
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 64 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
- uses: actions/checkout@master
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.100' # SDK Version to use.
dotnet-version: '3.1.x' # SDK Version to use; x will use the latest version of the 3.1 channel
- run: dotnet build <my project>
```
Expand All @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-16.04
strategy:
matrix:
dotnet: [ '2.2.103', '3.0.100', '3.1.100' ]
dotnet: [ '2.2.103', '3.0', '3.1.x' ]
name: Dotnet ${{ matrix.dotnet }} sample
steps:
- uses: actions/checkout@master
Expand All @@ -49,7 +49,7 @@ steps:
# Authenticates packages to push to GPR
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.100' # SDK Version to use.
dotnet-version: '3.1.x' # SDK Version to use.
source-url: https://nuget.pkg.github.com/<owner>/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand Down
104 changes: 103 additions & 1 deletion __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import os = require('os');
import path = require('path');
import hc = require('@actions/http-client');

import each from 'jest-each';

const toolDir = path.join(__dirname, 'runner', 'tools');
const tempDir = path.join(__dirname, 'runner', 'temp');

Expand All @@ -14,6 +16,61 @@ import * as installer from '../src/installer';

const IS_WINDOWS = process.platform === 'win32';

describe('version tests', () => {
each(['3.1.999', '3.1.101-preview.3']).test(
"Exact version '%s' should be the same",
vers => {
let versInfo = new installer.DotNetVersionInfo(vers);

expect(versInfo.isExactVersion()).toBe(true);
expect(versInfo.version()).toBe(vers);
}
);

each([['3.1.x', '3.1'], ['1.1.*', '1.1'], ['2.0', '2.0']]).test(
"Generic version '%s' should be '%s'",
(vers, resVers) => {
let versInfo = new installer.DotNetVersionInfo(vers);

expect(versInfo.isExactVersion()).toBe(false);
expect(versInfo.version()).toBe(resVers);
}
);

each([
'',
'.',
'..',
' . ',
'. ',
' .',
' . . ',
' .. ',
' . ',
'-1.-1',
'-1',
'-1.-1.-1',
'..3',
'1..3',
'1..',
'.2.3',
'.2.x',
'1',
'2.x',
'*.*.1',
'*.1',
'*.',
'1.2.',
'1.2.-abc',
'a.b',
'a.b.c',
'a.b.c-preview',
' 0 . 1 . 2 '
]).test("Malformed version '%s' should throw", vers => {
expect(() => new installer.DotNetVersionInfo(vers)).toThrow();
});
});

describe('installer tests', () => {
beforeAll(async () => {
await io.rmRF(toolDir);
Expand All @@ -29,6 +86,51 @@ describe('installer tests', () => {
}
}, 100000);

it('Resolving a normal generic version works', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('3.1.x');
let versInfo = await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('3.1.x')
);

expect(versInfo.resolvedVersion.startsWith('3.1.'));
}, 100000);

it('Resolving a nonexistent generic version fails', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('999.1.x');
try {
await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('999.1.x')
);
fail();
} catch {
expect(true);
}
}, 100000);

it('Resolving a exact stable version works', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('3.1.201');
let versInfo = await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('3.1.201')
);

expect(versInfo.resolvedVersion).toBe('3.1.201');
}, 100000);

it('Resolving a exact preview version works', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller(
'5.0.0-preview.4'
);
let versInfo = await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('5.0.0-preview.4')
);

expect(versInfo.resolvedVersion).toBe('5.0.0-preview.4');
}, 100000);

it('Acquires version of dotnet if no matching version is installed', async () => {
await getDotnet('2.2.205');
const dotnetDir = path.join(toolDir, 'dncs', '2.2.205', os.arch());
Expand All @@ -39,7 +141,7 @@ describe('installer tests', () => {
} else {
expect(fs.existsSync(path.join(dotnetDir, 'dotnet'))).toBe(true);
}
}, 100000);
}, 400000); //This needs some time to download on "slower" internet connections

it('Acquires version of dotnet if no matching version is installed', async () => {
const dotnetDir = path.join(toolDir, 'dncs', '2.2.105', os.arch());
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ branding:
color: green
inputs:
dotnet-version:
description: 'SDK version to use. Example: 2.2.104'
description: 'SDK version to use. Examples: 2.2.104, 3.1, 3.1.x'
source-url:
description: 'Optional package source for which to set up authentication. Will consult any existing NuGet.config in the root of the repo and provide a temporary NuGet.config using the NUGET_AUTH_TOKEN environment variable as a ClearTextPassword'
owner:
Expand Down
145 changes: 117 additions & 28 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17392,7 +17392,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DotnetCoreInstaller = void 0;
exports.DotnetCoreInstaller = exports.DotNetVersionInfo = void 0;
// Load tempDirectory before it gets wiped by tool-cache
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
const core = __importStar(__webpack_require__(470));
Expand Down Expand Up @@ -17421,30 +17421,103 @@ if (!tempDirectory) {
}
tempDirectory = path.join(baseLocation, 'actions', 'temp');
}
class DotnetCoreInstaller {
/**
* Represents the inputted version information
*/
class DotNetVersionInfo {
constructor(version) {
if (semver.valid(semver.clean(version) || '') == null) {
throw 'Implicit version not permitted';
this.isExactVersionSet = false;
// Check for exact match
if (semver.valid(semver.clean(version) || '') != null) {
this.fullversion = semver.clean(version);
this.isExactVersionSet = true;
return;
}
//Note: No support for previews when using generic
let parts = version.split('.');
if (parts.length < 2 || parts.length > 3)
this.throwInvalidVersionFormat();
if (parts.length == 3 && parts[2] !== 'x' && parts[2] !== '*') {
this.throwInvalidVersionFormat();
}
let major = this.getVersionNumberOrThrow(parts[0]);
let minor = this.getVersionNumberOrThrow(parts[1]);
this.fullversion = major + '.' + minor;
}
getVersionNumberOrThrow(input) {
try {
if (!input || input.trim() === '')
this.throwInvalidVersionFormat();
let number = Number(input);
if (Number.isNaN(number) || number < 0)
this.throwInvalidVersionFormat();
return number;
}
catch (_a) {
this.throwInvalidVersionFormat();
return -1;
}
this.version = version;
}
throwInvalidVersionFormat() {
throw 'Invalid version format! Supported: 1.2.3, 1.2, 1.2.x, 1.2.*';
}
/**
* If true exacatly one version should be resolved
*/
isExactVersion() {
return this.isExactVersionSet;
}
version() {
return this.fullversion;
}
}
exports.DotNetVersionInfo = DotNetVersionInfo;
/**
* Represents a resolved version from the Web-Api
*/
class ResolvedVersionInfo {
constructor(downloadUrls, resolvedVersion) {
if (downloadUrls.length === 0) {
throw 'DownloadUrls can not be empty';
}
if (!resolvedVersion) {
throw 'Resolved version is invalid';
}
this.downloadUrls = downloadUrls;
this.resolvedVersion = resolvedVersion;
}
}
class DotnetCoreInstaller {
constructor(version) {
this.versionInfo = new DotNetVersionInfo(version);
this.cachedToolName = 'dncs';
this.arch = 'x64';
}
installDotnet() {
return __awaiter(this, void 0, void 0, function* () {
// Check cache
let toolPath;
let toolPath = '';
let osSuffixes = yield this.detectMachineOS();
let parts = osSuffixes[0].split('-');
if (parts.length > 1) {
this.arch = parts[1];
}
toolPath = this.getLocalTool();
// If version is not generic -> look up cache
if (this.versionInfo.isExactVersion())
toolPath = this.getLocalTool(this.versionInfo.version());
if (!toolPath) {
// download, extract, cache
console.log('Getting a download url', this.version);
let downloadUrls = yield this.getDownloadUrls(osSuffixes, this.version);
toolPath = yield this.downloadAndInstall(downloadUrls);
console.log('Getting a download url', this.versionInfo.version());
let resolvedVersionInfo = yield this.resolveInfos(osSuffixes, this.versionInfo);
//Check if cache exists for resolved version
toolPath = this.getLocalTool(resolvedVersionInfo.resolvedVersion);
if (!toolPath) {
//If not exists install it
toolPath = yield this.downloadAndInstall(resolvedVersionInfo);
}
else {
console.log('Using cached tool');
}
}
else {
console.log('Using cached tool');
Expand All @@ -17455,9 +17528,9 @@ class DotnetCoreInstaller {
core.addPath(toolPath);
});
}
getLocalTool() {
console.log('Checking tool cache');
return tc.find(this.cachedToolName, this.version, this.arch);
getLocalTool(version) {
console.log('Checking tool cache', version);
return tc.find(this.cachedToolName, version, this.arch);
}
detectMachineOS() {
return __awaiter(this, void 0, void 0, function* () {
Expand Down Expand Up @@ -17517,18 +17590,18 @@ class DotnetCoreInstaller {
return osSuffix;
});
}
downloadAndInstall(downloadUrls) {
downloadAndInstall(resolvedVersionInfo) {
return __awaiter(this, void 0, void 0, function* () {
let downloaded = false;
let downloadPath = '';
for (const url of downloadUrls) {
for (const url of resolvedVersionInfo.downloadUrls) {
try {
downloadPath = yield tc.downloadTool(url);
downloaded = true;
break;
}
catch (error) {
console.log('Could Not Download', url, JSON.stringify(error));
console.log('Could not Download', url, JSON.stringify(error));
}
}
if (!downloaded) {
Expand All @@ -17541,31 +17614,40 @@ class DotnetCoreInstaller {
: yield tc.extractTar(downloadPath);
// cache tool
console.log('Caching tool');
let cachedDir = yield tc.cacheDir(extPath, this.cachedToolName, this.version, this.arch);
console.log('Successfully installed', this.version);
let cachedDir = yield tc.cacheDir(extPath, this.cachedToolName, resolvedVersionInfo.resolvedVersion, this.arch);
console.log('Successfully installed', resolvedVersionInfo.resolvedVersion);
return cachedDir;
});
}
// OsSuffixes - The suffix which is a part of the file name ex- linux-x64, windows-x86
// Type - SDK / Runtime
// Version - Version of the SDK/Runtime
getDownloadUrls(osSuffixes, version) {
// versionInfo - versionInfo of the SDK/Runtime
resolveInfos(osSuffixes, versionInfo) {
return __awaiter(this, void 0, void 0, function* () {
let downloadUrls = [];
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
allowRetries: true,
maxRetries: 3
});
const releasesJsonUrl = yield this.getReleasesJsonUrl(httpClient, version.split('.'));
const releasesJsonUrl = yield this.getReleasesJsonUrl(httpClient, versionInfo.version().split('.'));
const releasesResponse = yield httpClient.getJson(releasesJsonUrl);
const releasesResult = releasesResponse.result || {};
let releasesInfo = releasesResult['releases'];
releasesInfo = releasesInfo.filter((releaseInfo) => {
return (releaseInfo['sdk']['version'] === version ||
releaseInfo['sdk']['version-display'] === version);
return (semver.satisfies(releaseInfo['sdk']['version'], versionInfo.version()) ||
semver.satisfies(releaseInfo['sdk']['version-display'], versionInfo.version()));
});
// Exclude versions that are newer than the latest if using not exact
if (!versionInfo.isExactVersion()) {
let latestSdk = releasesResult['latest-sdk'];
releasesInfo = releasesInfo.filter((releaseInfo) => semver.lte(releaseInfo['sdk']['version'], latestSdk));
}
// Sort for latest version
releasesInfo = releasesInfo.sort((a, b) => semver.rcompare(a['sdk']['version'], b['sdk']['version']));
let downloadedVersion = '';
let downloadUrls = [];
if (releasesInfo.length != 0) {
let release = releasesInfo[0];
downloadedVersion = release['sdk']['version'];
let files = release['sdk']['files'];
files = files.filter((file) => {
if (file['rid'] == osSuffixes[0] || file['rid'] == osSuffixes[1]) {
Expand All @@ -17582,14 +17664,21 @@ class DotnetCoreInstaller {
}
}
else {
console.log(`Could not fetch download information for version ${version}`);
downloadUrls = yield this.getFallbackDownloadUrls(version);
console.log(`Could not fetch download information for version ${versionInfo.version()}`);
if (versionInfo.isExactVersion()) {
console.log('Using fallback');
downloadUrls = yield this.getFallbackDownloadUrls(versionInfo.version());
downloadedVersion = versionInfo.version();
}
else {
console.log('Unable to use fallback, version is generic!');
}
}
if (downloadUrls.length == 0) {
throw `Could not construct download URL. Please ensure that specified version ${version} is valid.`;
throw `Could not construct download URL. Please ensure that specified version ${versionInfo.version()}/${downloadedVersion} is valid.`;
}
core.debug(`Got download urls ${downloadUrls}`);
return downloadUrls;
return new ResolvedVersionInfo(downloadUrls, downloadedVersion);
});
}
getReleasesJsonUrl(httpClient, versionParts) {
Expand Down
Loading

0 comments on commit 6efb2bd

Please sign in to comment.