Skip to content

Commit 6eeb47f

Browse files
committed
refactoring
1 parent 42a70ec commit 6eeb47f

3 files changed

Lines changed: 96 additions & 89 deletions

File tree

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22
import * as io from '@actions/io';
3-
import {addExecutablesToCache, IGoVersionInfo} from '../src/installer';
3+
import * as tc from '@actions/tool-cache';
44
import path from 'path';
55

66
describe('Windows performance workaround', () => {
@@ -43,25 +43,18 @@ describe('Windows performance workaround', () => {
4343
jest.clearAllMocks();
4444
process.env['RUNNER_TOOL_CACHE'] = runnerToolCache;
4545
});
46-
// addExecutablesToCache uses 3rd party dependency toolkit.cache under the hood
47-
// that currently is implemented with RUNNER_TOOL_CACHE environment variable
46+
// cacheWindowsToolkitDir depends on implementation of tc.cacheDir
47+
// with the assumption that target dir is passed by RUNNER_TOOL_CACHE environment variable
4848
// Make sure the implementation has not been changed
4949
it('addExecutablesToCache should depend on env[RUNNER_TOOL_CACHE]', async () => {
50-
const info: IGoVersionInfo = {
51-
type: 'dist',
52-
downloadUrl: 'http://nowhere.com',
53-
resolvedVersion: '1.2.3',
54-
fileName: 'ignore'
55-
};
56-
5750
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache1';
58-
const cacheDir1 = await addExecutablesToCache('/qzx', info, 'arch');
51+
const cacheDir1 = await tc.cacheDir('/qzx', 'go', '1.2.3', 'arch');
5952
expect(cacheDir1).toBe(
6053
path.join('/', 'faked-hostedtoolcache1', 'go', '1.2.3', 'arch')
6154
);
6255

6356
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache2';
64-
const cacheDir2 = await addExecutablesToCache('/qzx', info, 'arch');
57+
const cacheDir2 = await tc.cacheDir('/qzx', 'go', '1.2.3', 'arch');
6558
expect(cacheDir2).toBe(
6659
path.join('/', 'faked-hostedtoolcache2', 'go', '1.2.3', 'arch')
6760
);

dist/setup/index.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61338,7 +61338,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6133861338
return (mod && mod.__esModule) ? mod : { "default": mod };
6133961339
};
6134061340
Object.defineProperty(exports, "__esModule", ({ value: true }));
61341-
exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.addExecutablesToCache = exports.getGo = void 0;
61341+
exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0;
6134261342
const tc = __importStar(__nccwpck_require__(7784));
6134361343
const core = __importStar(__nccwpck_require__(2186));
6134461344
const path = __importStar(__nccwpck_require__(1017));
@@ -61441,56 +61441,63 @@ function resolveVersionFromManifest(versionSpec, stable, auth, arch, manifest) {
6144161441
}
6144261442
});
6144361443
}
61444-
function addExecutablesToCache(extPath, info, arch) {
61445-
return __awaiter(this, void 0, void 0, function* () {
61446-
core.info('Adding to the cache ...');
61447-
const cachedDir = yield tc.cacheDir(extPath, 'go', makeSemver(info.resolvedVersion), arch);
61448-
core.info(`Successfully cached go to ${cachedDir}`);
61449-
return cachedDir;
61450-
});
61451-
}
61452-
exports.addExecutablesToCache = addExecutablesToCache;
61453-
function installGoVersion(info, auth, arch) {
61444+
// for github hosted windows runner handle latency of OS drive
61445+
// by avoiding write operations to C:
61446+
function cacheWindowsToolkitDir(extPath, tool, version, arch) {
6145461447
return __awaiter(this, void 0, void 0, function* () {
61455-
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
61456-
// Windows requires that we keep the extension (.zip) for extraction
61457-
const isWindows = os_1.default.platform() === 'win32';
61458-
const tempDir = process.env.RUNNER_TEMP || '.';
61459-
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;
61460-
const downloadPath = yield tc.downloadTool(info.downloadUrl, fileName, auth);
61461-
core.info('Extracting Go...');
61462-
let extPath = yield extractGoArchive(downloadPath);
61463-
core.info(`Successfully extracted go to ${extPath}`);
61464-
if (info.type === 'dist') {
61465-
extPath = path.join(extPath, 'go');
61466-
}
61467-
// for github hosted windows runner handle latency of OS drive
61468-
// by avoiding write operations to C:
61469-
if (!isWindows)
61470-
return addExecutablesToCache(extPath, info, arch);
61448+
if (os_1.default.platform() !== 'win32')
61449+
return false;
6147161450
const isHosted = process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
6147261451
process.env['AGENT_ISSELFHOSTED'] === '0';
6147361452
if (!isHosted)
61474-
return addExecutablesToCache(extPath, info, arch);
61453+
return false;
6147561454
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
6147661455
if (!defaultToolCacheRoot)
61477-
return addExecutablesToCache(extPath, info, arch);
61456+
return false;
6147861457
if (!fs_1.default.existsSync('d:\\') || !fs_1.default.existsSync('c:\\'))
61479-
return addExecutablesToCache(extPath, info, arch);
61458+
return false;
6148061459
const substitutedToolCacheRoot = defaultToolCacheRoot
6148161460
.replace('C:', 'D:')
6148261461
.replace('c:', 'd:');
6148361462
// make toolcache root to be on drive d:
6148461463
process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;
61485-
const actualToolCacheDir = yield addExecutablesToCache(extPath, info, arch);
61464+
const actualToolCacheDir = yield tc.cacheDir(extPath, tool, version, arch);
6148661465
// create a link from c: to d:
6148761466
const defaultToolCacheDir = actualToolCacheDir.replace(substitutedToolCacheRoot, defaultToolCacheRoot);
6148861467
fs_1.default.mkdirSync(path.dirname(defaultToolCacheDir), { recursive: true });
6148961468
fs_1.default.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
6149061469
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
61470+
// make outer code to continue using toolcache as if it were installed on c:
6149161471
// restore toolcache root to default drive c:
6149261472
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
61493-
// make outer code to continue using toolcache as if it were installed on c:
61473+
return defaultToolCacheDir;
61474+
});
61475+
}
61476+
function addExecutablesToCache(extPath, info, arch) {
61477+
return __awaiter(this, void 0, void 0, function* () {
61478+
const tool = 'go';
61479+
const version = makeSemver(info.resolvedVersion);
61480+
return ((yield cacheWindowsToolkitDir(extPath, tool, version, arch)) ||
61481+
(yield tc.cacheDir(extPath, tool, version, arch)));
61482+
});
61483+
}
61484+
function installGoVersion(info, auth, arch) {
61485+
return __awaiter(this, void 0, void 0, function* () {
61486+
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
61487+
// Windows requires that we keep the extension (.zip) for extraction
61488+
const isWindows = os_1.default.platform() === 'win32';
61489+
const tempDir = process.env.RUNNER_TEMP || '.';
61490+
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;
61491+
const downloadPath = yield tc.downloadTool(info.downloadUrl, fileName, auth);
61492+
core.info('Extracting Go...');
61493+
let extPath = yield extractGoArchive(downloadPath);
61494+
core.info(`Successfully extracted go to ${extPath}`);
61495+
if (info.type === 'dist') {
61496+
extPath = path.join(extPath, 'go');
61497+
}
61498+
core.info('Adding to the cache ...');
61499+
const defaultToolCacheDir = yield addExecutablesToCache(extPath, info, arch);
61500+
core.info(`Successfully cached go to ${defaultToolCacheDir}`);
6149461501
return defaultToolCacheDir;
6149561502
});
6149661503
}

src/installer.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -164,66 +164,33 @@ async function resolveVersionFromManifest(
164164
}
165165
}
166166

167-
export async function addExecutablesToCache(
167+
// for github hosted windows runner handle latency of OS drive
168+
// by avoiding write operations to C:
169+
async function cacheWindowsToolkitDir(
168170
extPath: string,
169-
info: IGoVersionInfo,
170-
arch: string
171-
): Promise<string> {
172-
core.info('Adding to the cache ...');
173-
const cachedDir = await tc.cacheDir(
174-
extPath,
175-
'go',
176-
makeSemver(info.resolvedVersion),
177-
arch
178-
);
179-
core.info(`Successfully cached go to ${cachedDir}`);
180-
return cachedDir;
181-
}
182-
183-
async function installGoVersion(
184-
info: IGoVersionInfo,
185-
auth: string | undefined,
171+
tool: string,
172+
version: string,
186173
arch: string
187-
): Promise<string> {
188-
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
189-
190-
// Windows requires that we keep the extension (.zip) for extraction
191-
const isWindows = os.platform() === 'win32';
192-
const tempDir = process.env.RUNNER_TEMP || '.';
193-
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;
194-
195-
const downloadPath = await tc.downloadTool(info.downloadUrl, fileName, auth);
196-
197-
core.info('Extracting Go...');
198-
let extPath = await extractGoArchive(downloadPath);
199-
core.info(`Successfully extracted go to ${extPath}`);
200-
if (info.type === 'dist') {
201-
extPath = path.join(extPath, 'go');
202-
}
203-
204-
// for github hosted windows runner handle latency of OS drive
205-
// by avoiding write operations to C:
206-
207-
if (!isWindows) return addExecutablesToCache(extPath, info, arch);
174+
): Promise<string | false> {
175+
if (os.platform() !== 'win32') return false;
208176

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

214182
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
215-
if (!defaultToolCacheRoot) return addExecutablesToCache(extPath, info, arch);
183+
if (!defaultToolCacheRoot) return false;
216184

217-
if (!fs.existsSync('d:\\') || !fs.existsSync('c:\\'))
218-
return addExecutablesToCache(extPath, info, arch);
185+
if (!fs.existsSync('d:\\') || !fs.existsSync('c:\\')) return false;
219186

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

226-
const actualToolCacheDir = await addExecutablesToCache(extPath, info, arch);
193+
const actualToolCacheDir = await tc.cacheDir(extPath, tool, version, arch);
227194

228195
// create a link from c: to d:
229196
const defaultToolCacheDir = actualToolCacheDir.replace(
@@ -234,10 +201,50 @@ async function installGoVersion(
234201
fs.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
235202
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
236203

204+
// make outer code to continue using toolcache as if it were installed on c:
237205
// restore toolcache root to default drive c:
238206
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
207+
return defaultToolCacheDir;
208+
}
209+
210+
async function addExecutablesToCache(
211+
extPath: string,
212+
info: IGoVersionInfo,
213+
arch: string
214+
): Promise<string> {
215+
const tool = 'go';
216+
const version = makeSemver(info.resolvedVersion);
217+
return (
218+
(await cacheWindowsToolkitDir(extPath, tool, version, arch)) ||
219+
(await tc.cacheDir(extPath, tool, version, arch))
220+
);
221+
}
222+
223+
async function installGoVersion(
224+
info: IGoVersionInfo,
225+
auth: string | undefined,
226+
arch: string
227+
): Promise<string> {
228+
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
229+
230+
// Windows requires that we keep the extension (.zip) for extraction
231+
const isWindows = os.platform() === 'win32';
232+
const tempDir = process.env.RUNNER_TEMP || '.';
233+
const fileName = isWindows ? path.join(tempDir, info.fileName) : undefined;
234+
235+
const downloadPath = await tc.downloadTool(info.downloadUrl, fileName, auth);
236+
237+
core.info('Extracting Go...');
238+
let extPath = await extractGoArchive(downloadPath);
239+
core.info(`Successfully extracted go to ${extPath}`);
240+
if (info.type === 'dist') {
241+
extPath = path.join(extPath, 'go');
242+
}
243+
244+
core.info('Adding to the cache ...');
245+
const defaultToolCacheDir = await addExecutablesToCache(extPath, info, arch);
246+
core.info(`Successfully cached go to ${defaultToolCacheDir}`);
239247

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

0 commit comments

Comments
 (0)