Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jul 20, 2023
1 parent 42a70ec commit 0c56308
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 90 deletions.
@@ -1,6 +1,6 @@
import fs from 'fs';
import * as io from '@actions/io';
import {addExecutablesToCache, IGoVersionInfo} from '../src/installer';
import * as tc from '@actions/tool-cache';
import path from 'path';

describe('Windows performance workaround', () => {
Expand Down Expand Up @@ -43,25 +43,18 @@ describe('Windows performance workaround', () => {
jest.clearAllMocks();
process.env['RUNNER_TOOL_CACHE'] = runnerToolCache;
});
// addExecutablesToCache uses 3rd party dependency toolkit.cache under the hood
// that currently is implemented with RUNNER_TOOL_CACHE environment variable
// cacheWindowsToolkitDir depends on implementation of tc.cacheDir
// with the assumption that target dir is passed by RUNNER_TOOL_CACHE environment variable
// Make sure the implementation has not been changed
it('addExecutablesToCache should depend on env[RUNNER_TOOL_CACHE]', async () => {
const info: IGoVersionInfo = {
type: 'dist',
downloadUrl: 'http://nowhere.com',
resolvedVersion: '1.2.3',
fileName: 'ignore'
};

process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache1';
const cacheDir1 = await addExecutablesToCache('/qzx', info, 'arch');
const cacheDir1 = await tc.cacheDir('/qzx', 'go', '1.2.3', 'arch');
expect(cacheDir1).toBe(
path.join('/', 'faked-hostedtoolcache1', 'go', '1.2.3', 'arch')
);

process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache2';
const cacheDir2 = await addExecutablesToCache('/qzx', info, 'arch');
const cacheDir2 = await tc.cacheDir('/qzx', 'go', '1.2.3', 'arch');
expect(cacheDir2).toBe(
path.join('/', 'faked-hostedtoolcache2', 'go', '1.2.3', 'arch')
);
Expand Down
71 changes: 39 additions & 32 deletions dist/setup/index.js
Expand Up @@ -61338,7 +61338,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.addExecutablesToCache = exports.getGo = void 0;
exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0;
const tc = __importStar(__nccwpck_require__(7784));
const core = __importStar(__nccwpck_require__(2186));
const path = __importStar(__nccwpck_require__(1017));
Expand Down Expand Up @@ -61441,59 +61441,66 @@ function resolveVersionFromManifest(versionSpec, stable, auth, arch, manifest) {
}
});
}
function addExecutablesToCache(extPath, info, arch) {
// for github hosted windows runner handle latency of OS drive
// by avoiding write operations to C:
function cacheWindowsDir(extPath, tool, version, arch) {
return __awaiter(this, void 0, void 0, function* () {
core.info('Adding to the cache ...');
const cachedDir = yield tc.cacheDir(extPath, 'go', makeSemver(info.resolvedVersion), arch);
core.info(`Successfully cached go to ${cachedDir}`);
return cachedDir;
});
}
exports.addExecutablesToCache = addExecutablesToCache;
function installGoVersion(info, auth, arch) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
// Windows requires that we keep the extension (.zip) for extraction
const isWindows = os_1.default.platform() === 'win32';
const tempDir = process.env.RUNNER_TEMP || '.';
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;
const downloadPath = yield tc.downloadTool(info.downloadUrl, fileName, auth);
core.info('Extracting Go...');
let extPath = yield extractGoArchive(downloadPath);
core.info(`Successfully extracted go to ${extPath}`);
if (info.type === 'dist') {
extPath = path.join(extPath, 'go');
}
// for github hosted windows runner handle latency of OS drive
// by avoiding write operations to C:
if (!isWindows)
return addExecutablesToCache(extPath, info, arch);
if (os_1.default.platform() !== 'win32')
return false;
const isHosted = process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
process.env['AGENT_ISSELFHOSTED'] === '0';
if (!isHosted)
return addExecutablesToCache(extPath, info, arch);
return false;
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot)
return addExecutablesToCache(extPath, info, arch);
return false;
if (!fs_1.default.existsSync('d:\\') || !fs_1.default.existsSync('c:\\'))
return addExecutablesToCache(extPath, info, arch);
return false;
const substitutedToolCacheRoot = defaultToolCacheRoot
.replace('C:', 'D:')
.replace('c:', 'd:');
// make toolcache root to be on drive d:
process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;
const actualToolCacheDir = yield addExecutablesToCache(extPath, info, arch);
const actualToolCacheDir = yield tc.cacheDir(extPath, tool, version, arch);
// create a link from c: to d:
const defaultToolCacheDir = actualToolCacheDir.replace(substitutedToolCacheRoot, defaultToolCacheRoot);
fs_1.default.mkdirSync(path.dirname(defaultToolCacheDir), { recursive: true });
fs_1.default.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
// make outer code to continue using toolcache as if it were installed on c:
// restore toolcache root to default drive c:
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
// make outer code to continue using toolcache as if it were installed on c:
return defaultToolCacheDir;
});
}
function addExecutablesToToolCache(extPath, info, arch) {
return __awaiter(this, void 0, void 0, function* () {
const tool = 'go';
const version = makeSemver(info.resolvedVersion);
return ((yield cacheWindowsDir(extPath, tool, version, arch)) ||
(yield tc.cacheDir(extPath, tool, version, arch)));
});
}
function installGoVersion(info, auth, arch) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
// Windows requires that we keep the extension (.zip) for extraction
const isWindows = os_1.default.platform() === 'win32';
const tempDir = process.env.RUNNER_TEMP || '.';
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;
const downloadPath = yield tc.downloadTool(info.downloadUrl, fileName, auth);
core.info('Extracting Go...');
let extPath = yield extractGoArchive(downloadPath);
core.info(`Successfully extracted go to ${extPath}`);
if (info.type === 'dist') {
extPath = path.join(extPath, 'go');
}
core.info('Adding to the cache ...');
const toolCacheDir = yield addExecutablesToToolCache(extPath, info, arch);
core.info(`Successfully cached go to ${toolCacheDir}`);
return toolCacheDir;
});
}
function extractGoArchive(archivePath) {
return __awaiter(this, void 0, void 0, function* () {
const platform = os_1.default.platform();
Expand Down
99 changes: 53 additions & 46 deletions src/installer.ts
Expand Up @@ -164,66 +164,33 @@ async function resolveVersionFromManifest(
}
}

export async function addExecutablesToCache(
// for github hosted windows runner handle latency of OS drive
// by avoiding write operations to C:
async function cacheWindowsDir(
extPath: string,
info: IGoVersionInfo,
arch: string
): Promise<string> {
core.info('Adding to the cache ...');
const cachedDir = await tc.cacheDir(
extPath,
'go',
makeSemver(info.resolvedVersion),
arch
);
core.info(`Successfully cached go to ${cachedDir}`);
return cachedDir;
}

async function installGoVersion(
info: IGoVersionInfo,
auth: string | undefined,
tool: string,
version: string,
arch: string
): Promise<string> {
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);

// Windows requires that we keep the extension (.zip) for extraction
const isWindows = os.platform() === 'win32';
const tempDir = process.env.RUNNER_TEMP || '.';
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;

const downloadPath = await tc.downloadTool(info.downloadUrl, fileName, auth);

core.info('Extracting Go...');
let extPath = await extractGoArchive(downloadPath);
core.info(`Successfully extracted go to ${extPath}`);
if (info.type === 'dist') {
extPath = path.join(extPath, 'go');
}

// for github hosted windows runner handle latency of OS drive
// by avoiding write operations to C:

if (!isWindows) return addExecutablesToCache(extPath, info, arch);
): Promise<string | false> {
if (os.platform() !== 'win32') return false;

const isHosted =
process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
process.env['AGENT_ISSELFHOSTED'] === '0';
if (!isHosted) return addExecutablesToCache(extPath, info, arch);
if (!isHosted) return false;

const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot) return addExecutablesToCache(extPath, info, arch);
if (!defaultToolCacheRoot) return false;

if (!fs.existsSync('d:\\') || !fs.existsSync('c:\\'))
return addExecutablesToCache(extPath, info, arch);
if (!fs.existsSync('d:\\') || !fs.existsSync('c:\\')) return false;

const substitutedToolCacheRoot = defaultToolCacheRoot
.replace('C:', 'D:')
.replace('c:', 'd:');
// make toolcache root to be on drive d:
process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;

const actualToolCacheDir = await addExecutablesToCache(extPath, info, arch);
const actualToolCacheDir = await tc.cacheDir(extPath, tool, version, arch);

// create a link from c: to d:
const defaultToolCacheDir = actualToolCacheDir.replace(
Expand All @@ -234,13 +201,53 @@ async function installGoVersion(
fs.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);

// make outer code to continue using toolcache as if it were installed on c:
// restore toolcache root to default drive c:
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;

// make outer code to continue using toolcache as if it were installed on c:
return defaultToolCacheDir;
}

async function addExecutablesToToolCache(
extPath: string,
info: IGoVersionInfo,
arch: string
): Promise<string> {
const tool = 'go';
const version = makeSemver(info.resolvedVersion);
return (
(await cacheWindowsDir(extPath, tool, version, arch)) ||
(await tc.cacheDir(extPath, tool, version, arch))
);
}

async function installGoVersion(
info: IGoVersionInfo,
auth: string | undefined,
arch: string
): Promise<string> {
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);

// Windows requires that we keep the extension (.zip) for extraction
const isWindows = os.platform() === 'win32';
const tempDir = process.env.RUNNER_TEMP || '.';
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;

const downloadPath = await tc.downloadTool(info.downloadUrl, fileName, auth);

core.info('Extracting Go...');
let extPath = await extractGoArchive(downloadPath);
core.info(`Successfully extracted go to ${extPath}`);
if (info.type === 'dist') {
extPath = path.join(extPath, 'go');
}

core.info('Adding to the cache ...');
const toolCacheDir = await addExecutablesToToolCache(extPath, info, arch);
core.info(`Successfully cached go to ${toolCacheDir}`);

return toolCacheDir;
}

export async function extractGoArchive(archivePath: string): Promise<string> {
const platform = os.platform();
let extPath: string;
Expand Down

0 comments on commit 0c56308

Please sign in to comment.