refactored version logic

This commit is contained in:
La'Kaleigh Harris 2021-11-09 15:14:49 +00:00 committed by GitHub
parent aca1816f14
commit 866d7435eb
6 changed files with 161 additions and 175 deletions

View File

@ -34,7 +34,10 @@ describe('installer tests', () => {
it('Aquires multiple versions of dotnet', async () => { it('Aquires multiple versions of dotnet', async () => {
const versions = ['2.2.207', '3.1.120']; const versions = ['2.2.207', '3.1.120'];
await getDotnet(versions);
for (const version of versions) {
await getDotnet(version);
}
expect(fs.existsSync(path.join(toolDir, 'sdk', '2.2.207'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'sdk', '2.2.207'))).toBe(true);
expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.120'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.120'))).toBe(true);
@ -51,7 +54,7 @@ describe('installer tests', () => {
}, 600000); }, 600000);
it('Acquires version of dotnet if no matching version is installed', async () => { it('Acquires version of dotnet if no matching version is installed', async () => {
await getDotnet(['3.1.201']); await getDotnet('3.1.201');
expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true);
if (IS_WINDOWS) { if (IS_WINDOWS) {
expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true);
@ -66,7 +69,7 @@ describe('installer tests', () => {
}, 600000); //This needs some time to download on "slower" internet connections }, 600000); //This needs some time to download on "slower" internet connections
it('Acquires generic version of dotnet if no matching version is installed', async () => { it('Acquires generic version of dotnet if no matching version is installed', async () => {
await getDotnet(['3.1']); await getDotnet('3.1');
var directory = fs var directory = fs
.readdirSync(path.join(toolDir, 'sdk')) .readdirSync(path.join(toolDir, 'sdk'))
.filter(fn => fn.startsWith('3.1.')); .filter(fn => fn.startsWith('3.1.'));
@ -86,7 +89,7 @@ describe('installer tests', () => {
it('Throws if no location contains correct dotnet version', async () => { it('Throws if no location contains correct dotnet version', async () => {
let thrown = false; let thrown = false;
try { try {
await getDotnet(['1000.0.0']); await getDotnet('1000.0.0');
} catch { } catch {
thrown = true; thrown = true;
} }
@ -141,7 +144,7 @@ function normalizeFileContents(contents: string): string {
.replace(new RegExp('\r', 'g'), '\n'); .replace(new RegExp('\r', 'g'), '\n');
} }
async function getDotnet(versions: string[]): Promise<void> { async function getDotnet(version: string): Promise<void> {
const dotnetInstaller = new installer.DotnetCoreInstaller(versions); const dotnetInstaller = new installer.DotnetCoreInstaller(version);
await dotnetInstaller.installDotnet(); await dotnetInstaller.installDotnet();
} }

View File

@ -53,10 +53,10 @@ describe('setup-dotnet tests', () => {
fs.writeFileSync(globalJsonPath, jsonContents); fs.writeFileSync(globalJsonPath, jsonContents);
} }
const version = ['3.1']; const version = '3.1';
const installer = new dotnetInstaller.DotnetCoreInstaller(version); const installer = new dotnetInstaller.DotnetCoreInstaller(version);
const patchVersion = await installer.resolveVersion( const patchVersion = await installer.resolveVersion(
new dotnetInstaller.DotNetVersionInfo(version[0]) new dotnetInstaller.DotNetVersionInfo(version)
); );
await setup.run(); await setup.run();

View File

@ -78,7 +78,7 @@ describe('version tests', () => {
); );
it('Resolving a nonexistent generic version fails', async () => { it('Resolving a nonexistent generic version fails', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller(['999.1.x']); const dotnetInstaller = new installer.DotnetCoreInstaller('999.1.x');
try { try {
await dotnetInstaller.resolveVersion( await dotnetInstaller.resolveVersion(
new installer.DotNetVersionInfo('999.1.x') new installer.DotNetVersionInfo('999.1.x')

151
dist/index.js vendored
View File

@ -8686,8 +8686,11 @@ function run() {
if (versions) { if (versions) {
const includePrerelease = (core.getInput('include-prerelease') || 'false').toLowerCase() === const includePrerelease = (core.getInput('include-prerelease') || 'false').toLowerCase() ===
'true'; 'true';
const dotnetInstaller = new installer.DotnetCoreInstaller(versions, includePrerelease); let dotnetInstaller;
yield dotnetInstaller.installDotnet(); for (const version of versions) {
dotnetInstaller = new installer.DotnetCoreInstaller(version, includePrerelease);
yield dotnetInstaller.installDotnet();
}
} }
const sourceUrl = core.getInput('source-url'); const sourceUrl = core.getInput('source-url');
const configFile = core.getInput('config-file'); const configFile = core.getInput('config-file');
@ -17963,13 +17966,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next()); step((generator = generator.apply(thisArg, _arguments || [])).next());
}); });
}; };
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.DotnetCoreInstaller = exports.DotNetVersionInfo = void 0; exports.DotnetCoreInstaller = exports.DotNetVersionInfo = void 0;
// Load tempDirectory before it gets wiped by tool-cache // Load tempDirectory before it gets wiped by tool-cache
@ -18035,90 +18031,77 @@ class DotNetVersionInfo {
} }
exports.DotNetVersionInfo = DotNetVersionInfo; exports.DotNetVersionInfo = DotNetVersionInfo;
class DotnetCoreInstaller { class DotnetCoreInstaller {
constructor(versions, includePrerelease = false) { constructor(version, includePrerelease = false) {
this.versions = versions; this.version = version;
this.includePrerelease = includePrerelease; this.includePrerelease = includePrerelease;
} }
installDotnet() { installDotnet() {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let output = ''; let output = '';
let resultCode = 0; let resultCode = 0;
try { let calculatedVersion = yield this.resolveVersion(new DotNetVersionInfo(this.version));
for (var _b = __asyncValues(this.versions), _c; _c = yield _b.next(), !_c.done;) { var envVariables = {};
const version = _c.value; for (let key in process.env) {
let calculatedVersion = yield this.resolveVersion(new DotNetVersionInfo(version)); if (process.env[key]) {
var envVariables = {}; let value = process.env[key];
for (let key in process.env) { envVariables[key] = value;
if (process.env[key]) {
let value = process.env[key];
envVariables[key] = value;
}
}
if (IS_WINDOWS) {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.ps1')
.replace(/'/g, "''");
let command = `& '${escapedScript}'`;
if (calculatedVersion) {
command += ` -Version ${calculatedVersion}`;
}
if (process.env['https_proxy'] != null) {
command += ` -ProxyAddress ${process.env['https_proxy']}`;
}
// This is not currently an option
if (process.env['no_proxy'] != null) {
command += ` -ProxyBypassList ${process.env['no_proxy']}`;
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const powershellPath = yield io.which('powershell', true);
var options = {
listeners: {
stdout: (data) => {
output += data.toString();
}
},
env: envVariables
};
resultCode = yield exec.exec(`"${powershellPath}"`, [
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
], options);
}
else {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.sh')
.replace(/'/g, "''");
fs_1.chmodSync(escapedScript, '777');
const scriptPath = yield io.which(escapedScript, true);
let scriptArguments = [];
if (calculatedVersion) {
scriptArguments.push('--version', calculatedVersion);
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
resultCode = yield exec.exec(`"${scriptPath}"`, scriptArguments, {
listeners: {
stdout: (data) => {
output += data.toString();
}
},
env: envVariables
});
}
} }
} }
catch (e_1_1) { e_1 = { error: e_1_1 }; } if (IS_WINDOWS) {
finally { let escapedScript = path
try { .join(__dirname, '..', 'externals', 'install-dotnet.ps1')
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); .replace(/'/g, "''");
let command = `& '${escapedScript}'`;
if (calculatedVersion) {
command += ` -Version ${calculatedVersion}`;
} }
finally { if (e_1) throw e_1.error; } if (process.env['https_proxy'] != null) {
command += ` -ProxyAddress ${process.env['https_proxy']}`;
}
// This is not currently an option
if (process.env['no_proxy'] != null) {
command += ` -ProxyBypassList ${process.env['no_proxy']}`;
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const powershellPath = yield io.which('powershell', true);
var options = {
listeners: {
stdout: (data) => {
output += data.toString();
}
},
env: envVariables
};
resultCode = yield exec.exec(`"${powershellPath}"`, [
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
], options);
}
else {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.sh')
.replace(/'/g, "''");
fs_1.chmodSync(escapedScript, '777');
const scriptPath = yield io.which(escapedScript, true);
let scriptArguments = [];
if (calculatedVersion) {
scriptArguments.push('--version', calculatedVersion);
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
resultCode = yield exec.exec(`"${scriptPath}"`, scriptArguments, {
listeners: {
stdout: (data) => {
output += data.toString();
}
},
env: envVariables
});
} }
if (process.env['DOTNET_INSTALL_DIR']) { if (process.env['DOTNET_INSTALL_DIR']) {
core.addPath(process.env['DOTNET_INSTALL_DIR']); core.addPath(process.env['DOTNET_INSTALL_DIR']);

View File

@ -79,8 +79,8 @@ export class DotNetVersionInfo {
} }
export class DotnetCoreInstaller { export class DotnetCoreInstaller {
constructor(versions: string[], includePrerelease: boolean = false) { constructor(version: string, includePrerelease: boolean = false) {
this.versions = versions; this.version = version;
this.includePrerelease = includePrerelease; this.includePrerelease = includePrerelease;
} }
@ -88,83 +88,81 @@ export class DotnetCoreInstaller {
let output = ''; let output = '';
let resultCode = 0; let resultCode = 0;
for await (const version of this.versions) { let calculatedVersion = await this.resolveVersion(
let calculatedVersion = await this.resolveVersion( new DotNetVersionInfo(this.version)
new DotNetVersionInfo(version) );
var envVariables: {[key: string]: string} = {};
for (let key in process.env) {
if (process.env[key]) {
let value: any = process.env[key];
envVariables[key] = value;
}
}
if (IS_WINDOWS) {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.ps1')
.replace(/'/g, "''");
let command = `& '${escapedScript}'`;
if (calculatedVersion) {
command += ` -Version ${calculatedVersion}`;
}
if (process.env['https_proxy'] != null) {
command += ` -ProxyAddress ${process.env['https_proxy']}`;
}
// This is not currently an option
if (process.env['no_proxy'] != null) {
command += ` -ProxyBypassList ${process.env['no_proxy']}`;
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const powershellPath = await io.which('powershell', true);
var options: ExecOptions = {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
},
env: envVariables
};
resultCode = await exec.exec(
`"${powershellPath}"`,
[
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
],
options
); );
} else {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.sh')
.replace(/'/g, "''");
chmodSync(escapedScript, '777');
var envVariables: {[key: string]: string} = {}; const scriptPath = await io.which(escapedScript, true);
for (let key in process.env) {
if (process.env[key]) { let scriptArguments: string[] = [];
let value: any = process.env[key]; if (calculatedVersion) {
envVariables[key] = value; scriptArguments.push('--version', calculatedVersion);
}
} }
if (IS_WINDOWS) {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.ps1')
.replace(/'/g, "''");
let command = `& '${escapedScript}'`;
if (calculatedVersion) {
command += ` -Version ${calculatedVersion}`;
}
if (process.env['https_proxy'] != null) {
command += ` -ProxyAddress ${process.env['https_proxy']}`;
}
// This is not currently an option
if (process.env['no_proxy'] != null) {
command += ` -ProxyBypassList ${process.env['no_proxy']}`;
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
const powershellPath = await io.which('powershell', true); resultCode = await exec.exec(`"${scriptPath}"`, scriptArguments, {
listeners: {
var options: ExecOptions = { stdout: (data: Buffer) => {
listeners: { output += data.toString();
stdout: (data: Buffer) => { }
output += data.toString(); },
} env: envVariables
}, });
env: envVariables
};
resultCode = await exec.exec(
`"${powershellPath}"`,
[
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
],
options
);
} else {
let escapedScript = path
.join(__dirname, '..', 'externals', 'install-dotnet.sh')
.replace(/'/g, "''");
chmodSync(escapedScript, '777');
const scriptPath = await io.which(escapedScript, true);
let scriptArguments: string[] = [];
if (calculatedVersion) {
scriptArguments.push('--version', calculatedVersion);
}
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
resultCode = await exec.exec(`"${scriptPath}"`, scriptArguments, {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
},
env: envVariables
});
}
} }
if (process.env['DOTNET_INSTALL_DIR']) { if (process.env['DOTNET_INSTALL_DIR']) {
core.addPath(process.env['DOTNET_INSTALL_DIR']); core.addPath(process.env['DOTNET_INSTALL_DIR']);
@ -287,7 +285,7 @@ export class DotnetCoreInstaller {
return releasesInfo[0]['releases.json']; return releasesInfo[0]['releases.json'];
} }
private versions: string[]; private version: string;
private includePrerelease: boolean; private includePrerelease: boolean;
} }

View File

@ -27,12 +27,14 @@ export async function run() {
const includePrerelease: boolean = const includePrerelease: boolean =
(core.getInput('include-prerelease') || 'false').toLowerCase() === (core.getInput('include-prerelease') || 'false').toLowerCase() ===
'true'; 'true';
let dotnetInstaller: installer.DotnetCoreInstaller;
const dotnetInstaller = new installer.DotnetCoreInstaller( for (const version of versions) {
versions, dotnetInstaller = new installer.DotnetCoreInstaller(
includePrerelease version,
); includePrerelease
await dotnetInstaller.installDotnet(); );
await dotnetInstaller.installDotnet();
}
} }
const sourceUrl: string = core.getInput('source-url'); const sourceUrl: string = core.getInput('source-url');