Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update installers #1

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
55 changes: 38 additions & 17 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ if (IS_WINDOWS) {
}
const tempDir = path.join(__dirname, 'runner', 'temp');

const getVersionedToolDir = (majorVersion: string) =>
path.join(toolDir + '', `${majorVersion}.x`);

process.env['RUNNER_TOOL_CACHE'] = toolDir;
process.env['RUNNER_TEMP'] = tempDir;

Expand Down Expand Up @@ -59,52 +62,70 @@ describe('DotnetCoreInstaller tests', () => {
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', '3.1.120'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('2'), 'sdk', '2.2.207'))
).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'sdk', '3.1.120'))
).toBe(true);

if (IS_WINDOWS) {
expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet.exe'))
).toBe(true);
} else {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
expect(fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet'))).toBe(
true
);
}

expect(process.env.DOTNET_ROOT).toBeDefined();
expect(process.env.PATH).toBeDefined();
expect(process.env.DOTNET_ROOT).toBe(toolDir);
expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
expect(process.env.DOTNET_ROOT).toBe(getVersionedToolDir('3'));
expect(process.env.PATH?.startsWith(getVersionedToolDir('3'))).toBe(true);
}, 600000);

it('Acquires version of dotnet if no matching version is installed', async () => {
await getDotnet('3.1.201');
expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'sdk', '3.1.201'))
).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet.exe'))
).toBe(true);
} else {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
expect(fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet'))).toBe(
true
);
}

expect(process.env.DOTNET_ROOT).toBeDefined();
expect(process.env.PATH).toBeDefined();
expect(process.env.DOTNET_ROOT).toBe(toolDir);
expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
expect(process.env.DOTNET_ROOT).toBe(getVersionedToolDir('3'));
expect(process.env.PATH?.startsWith(getVersionedToolDir('3'))).toBe(true);
}, 600000); //This needs some time to download on "slower" internet connections

it('Acquires generic version of dotnet if no matching version is installed', async () => {
await getDotnet('3.1');
const directory = fs
.readdirSync(path.join(toolDir, 'sdk'))
.readdirSync(path.join(getVersionedToolDir('3'), 'sdk'))
.filter(fn => fn.startsWith('3.1.'));
expect(directory.length > 0).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet.exe'))
).toBe(true);
} else {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
expect(fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet'))).toBe(
true
);
}

expect(process.env.DOTNET_ROOT).toBeDefined();
expect(process.env.PATH).toBeDefined();
expect(process.env.DOTNET_ROOT).toBe(toolDir);
expect(process.env.PATH?.startsWith(toolDir)).toBe(true);
expect(process.env.DOTNET_ROOT).toBe(getVersionedToolDir('3'));
expect(process.env.PATH?.startsWith(getVersionedToolDir('3'))).toBe(true);
}, 600000); //This needs some time to download on "slower" internet connections

it('Returns string with installed SDK version', async () => {
Expand Down Expand Up @@ -293,6 +314,6 @@ async function getDotnet(version: string, quality = ''): Promise<string> {
quality as QualityOptions
);
const installedVersion = await dotnetInstaller.installDotnet();
installer.DotnetCoreInstaller.addToPath();
dotnetInstaller.addToPath();
return installedVersion;
}
15 changes: 12 additions & 3 deletions __tests__/setup-dotnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ if (IS_WINDOWS) {
toolDir = path.join(process.env['HOME'] + '', '.dotnet');
}

const getVersionedToolDir = (majorVersion: string) =>
path.join(toolDir + '', `${majorVersion}.x`);

function createGlobalJsonPath(dotnetVersion: string) {
const globalJsonPath = path.join(process.cwd(), 'global.json');
const jsonContents = `{${os.EOL}"sdk": {${os.EOL}"version": "${dotnetVersion}"${os.EOL}}${os.EOL}}`;
Expand Down Expand Up @@ -64,11 +67,17 @@ describe('setup-dotnet tests', () => {
createGlobalJsonPath('3.1.201');
await setup.run();

expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'sdk', '3.1.201'))
).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true);
expect(
fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet.exe'))
).toBe(true);
} else {
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
expect(fs.existsSync(path.join(getVersionedToolDir('3'), 'dotnet'))).toBe(
true
);
}
}, 400000);

Expand Down
80 changes: 37 additions & 43 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.DotnetCoreInstaller = exports.DotnetVersionResolver = void 0;
// Load tempDirectory before it gets wiped by tool-cache
Expand All @@ -240,7 +239,6 @@ const hc = __importStar(__nccwpck_require__(6255));
const fs_1 = __nccwpck_require__(7147);
const promises_1 = __nccwpck_require__(3292);
const path_1 = __importDefault(__nccwpck_require__(1017));
const os_1 = __importDefault(__nccwpck_require__(2037));
const semver_1 = __importDefault(__nccwpck_require__(5911));
const utils_1 = __nccwpck_require__(918);
class DotnetVersionResolver {
Expand Down Expand Up @@ -314,26 +312,44 @@ class DotnetVersionResolver {
}
exports.DotnetVersionResolver = DotnetVersionResolver;
DotnetVersionResolver.DotNetCoreIndexUrl = 'https://dotnetcli.azureedge.net/dotnet/release-metadata/releases-index.json';
class InstallDir {
constructor(version) {
const majorVersion = version.split('.').at(0);
if (utils_1.IS_WINDOWS) {
process.env['DOTNET_INSTALL_DIR'] = `${InstallDir.base.windows}\\${majorVersion}`;
return;
}
if (utils_1.IS_LINUX) {
process.env['DOTNET_INSTALL_DIR'] = `${InstallDir.base.linux}/${majorVersion}`;
return;
}
process.env['DOTNET_INSTALL_DIR'] = `${InstallDir.base.mac}/${majorVersion}`;
}
get path() {
if (process.env['DOTNET_INSTALL_DIR']) {
return process.env['DOTNET_INSTALL_DIR'];
}
if (utils_1.IS_WINDOWS)
return InstallDir.base.windows;
if (utils_1.IS_LINUX)
return InstallDir.base.linux;
return InstallDir.base.mac;
}
}
InstallDir.base = {
windows: path_1.default.join(process.env['PROGRAMFILES'] + '', 'dotnet'),
mac: path_1.default.join(process.env['HOME'] + '', '.dotnet'),
linux: '/usr/share/dotnet'
};
class DotnetCoreInstaller {
constructor(version, quality) {
this.version = version;
this.quality = quality;
this.installDir = new InstallDir(version);
}
static convertInstallPathToAbsolute(installDir) {
let transformedPath;
if (path_1.default.isAbsolute(installDir)) {
transformedPath = installDir;
}
else {
transformedPath = installDir.startsWith('~')
? path_1.default.join(os_1.default.homedir(), installDir.slice(1))
: (transformedPath = path_1.default.join(process.cwd(), installDir));
}
return path_1.default.normalize(transformedPath);
}
static addToPath() {
core.addPath(process.env['DOTNET_INSTALL_DIR']);
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
addToPath() {
core.addPath(this.installDir.path);
core.exportVariable('DOTNET_ROOT', this.installDir.path);
}
setQuality(dotnetVersion, scriptArguments) {
const option = utils_1.IS_WINDOWS ? '-Quality' : '--quality';
Expand Down Expand Up @@ -364,7 +380,7 @@ class DotnetCoreInstaller {
const versionResolver = new DotnetVersionResolver(this.version);
const dotnetVersion = yield versionResolver.createDotNetVersion();
if (utils_1.IS_WINDOWS) {
scriptArguments = ['&', `'${escapedScript}'`];
scriptArguments = ['&', `'${escapedScript}'`, '-SkipNonVersionedFiles'];
if (dotnetVersion.type) {
scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
}
Expand All @@ -385,7 +401,7 @@ class DotnetCoreInstaller {
else {
(0, fs_1.chmodSync)(escapedScript, '777');
scriptPath = yield io.which(escapedScript, true);
scriptArguments = [];
scriptArguments = ['--skip-non-versioned-files'];
if (dotnetVersion.type) {
scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
}
Expand All @@ -407,8 +423,7 @@ class DotnetCoreInstaller {
}
outputDotnetVersion(version) {
return __awaiter(this, void 0, void 0, function* () {
const installationPath = process.env['DOTNET_INSTALL_DIR'];
const versionsOnRunner = yield (0, promises_1.readdir)(path_1.default.join(installationPath.replace(/'/g, ''), 'sdk'));
const versionsOnRunner = yield (0, promises_1.readdir)(path_1.default.join(this.installDir.path.replace(/'/g, ''), 'sdk'));
const installedVersion = semver_1.default.maxSatisfying(versionsOnRunner, version, {
includePrerelease: true
});
Expand All @@ -417,27 +432,6 @@ class DotnetCoreInstaller {
}
}
exports.DotnetCoreInstaller = DotnetCoreInstaller;
_a = DotnetCoreInstaller;
(() => {
const installationDirectoryWindows = path_1.default.join(process.env['PROGRAMFILES'] + '', 'dotnet');
const installationDirectoryLinux = '/usr/share/dotnet';
const installationDirectoryMac = path_1.default.join(process.env['HOME'] + '', '.dotnet');
const dotnetInstallDir = process.env['DOTNET_INSTALL_DIR'];
if (dotnetInstallDir) {
process.env['DOTNET_INSTALL_DIR'] =
_a.convertInstallPathToAbsolute(dotnetInstallDir);
}
else {
if (utils_1.IS_WINDOWS) {
process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows;
}
else {
process.env['DOTNET_INSTALL_DIR'] = utils_1.IS_LINUX
? installationDirectoryLinux
: installationDirectoryMac;
}
}
})();


/***/ }),
Expand Down Expand Up @@ -539,10 +533,10 @@ function run() {
const uniqueVersions = new Set(versions);
for (const version of uniqueVersions) {
dotnetInstaller = new installer_1.DotnetCoreInstaller(version, quality);
dotnetInstaller.addToPath();
const installedVersion = yield dotnetInstaller.installDotnet();
installedDotnetVersions.push(installedVersion);
}
installer_1.DotnetCoreInstaller.addToPath();
}
const sourceUrl = core.getInput('source-url');
const configFile = core.getInput('config-file');
Expand Down
Loading