Skip to content

Commit 42fc2d3

Browse files
committed
Reuse builds in the toolcache when they exist
1 parent 30f5e2d commit 42fc2d3

File tree

4 files changed

+81
-45
lines changed

4 files changed

+81
-45
lines changed

common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function getVirtualEnvironmentName() {
7777
throw new Error(`Unknown ImageOS ${imageOS}`)
7878
}
7979

80-
export function shouldExtractInToolCache(engine, version) {
80+
export function shouldUseToolCache(engine, version) {
8181
return engine === 'ruby' && !isHeadVersion(version)
8282
}
8383

dist/index.js

Lines changed: 41 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby-builder.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const os = require('os')
22
const path = require('path')
3-
const core = require('@actions/core')
43
const exec = require('@actions/exec')
54
const io = require('@actions/io')
65
const tc = require('@actions/tool-cache')
@@ -17,24 +16,33 @@ export function getAvailableVersions(platform, engine) {
1716
}
1817

1918
export async function install(platform, engine, version) {
20-
return await downloadAndExtract(platform, engine, version)
21-
}
22-
23-
async function downloadAndExtract(platform, engine, version) {
24-
let rubyPrefix
25-
if (common.shouldExtractInToolCache(engine, version)) {
26-
rubyPrefix = common.getToolCacheRubyPrefix(platform, version)
19+
let rubyPrefix, inToolCache
20+
if (common.shouldUseToolCache(engine, version)) {
21+
inToolCache = tc.find('Ruby', version)
22+
if (inToolCache) {
23+
rubyPrefix = inToolCache
24+
} else {
25+
rubyPrefix = common.getToolCacheRubyPrefix(platform, version)
26+
}
2727
} else if (windows) {
2828
rubyPrefix = path.join(`${common.drive}:`, `${engine}-${version}`)
2929
} else {
3030
rubyPrefix = path.join(os.homedir(), '.rubies', `${engine}-${version}`)
3131
}
3232

33-
const parentDir = path.dirname(rubyPrefix)
34-
3533
// Set the PATH now, so the MSYS2 'tar' is in Path on Windows
3634
common.setupPath([path.join(rubyPrefix, 'bin')])
3735

36+
if (!inToolCache) {
37+
await downloadAndExtract(platform, engine, version, rubyPrefix);
38+
}
39+
40+
return rubyPrefix
41+
}
42+
43+
async function downloadAndExtract(platform, engine, version, rubyPrefix) {
44+
const parentDir = path.dirname(rubyPrefix)
45+
3846
await io.rmRF(rubyPrefix)
3947
await io.mkdirP(parentDir)
4048

@@ -47,13 +55,11 @@ async function downloadAndExtract(platform, engine, version) {
4755
await common.measure('Extracting Ruby', async () => {
4856
if (windows) {
4957
// Windows 2016 doesn't have system tar, use MSYS2's, it needs unix style paths
50-
await exec.exec('tar', [ '-xz', '-C', common.win2nix(parentDir), '-f', common.win2nix(downloadPath) ])
58+
await exec.exec('tar', ['-xz', '-C', common.win2nix(parentDir), '-f', common.win2nix(downloadPath)])
5159
} else {
52-
await exec.exec('tar', [ '-xz', '-C', parentDir, '-f', downloadPath ])
60+
await exec.exec('tar', ['-xz', '-C', parentDir, '-f', downloadPath])
5361
}
5462
})
55-
56-
return rubyPrefix
5763
}
5864

5965
function getDownloadURL(platform, engine, version) {

windows.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,43 @@ export async function install(platform, engine, version) {
3636
}
3737
const base = url.slice(url.lastIndexOf('/') + 1, url.length - '.7z'.length)
3838

39-
let rubyPrefix
40-
if (common.shouldExtractInToolCache(engine, version)) {
41-
rubyPrefix = common.getToolCacheRubyPrefix(platform, version)
39+
let rubyPrefix, inToolCache
40+
if (common.shouldUseToolCache(engine, version)) {
41+
inToolCache = tc.find('Ruby', version)
42+
if (inToolCache) {
43+
rubyPrefix = inToolCache
44+
} else {
45+
rubyPrefix = common.getToolCacheRubyPrefix(platform, version)
46+
}
4247
} else {
4348
rubyPrefix = `${drive}:\\${base}`
4449
}
45-
const parentDir = path.dirname(rubyPrefix)
4650

4751
let toolchainPaths = (version === 'mswin') ? await setupMSWin() : await setupMingw(version)
4852

4953
common.setupPath([`${rubyPrefix}\\bin`, ...toolchainPaths])
5054

55+
if (!inToolCache) {
56+
await downloadAndExtract(url, base, rubyPrefix);
57+
}
58+
59+
return rubyPrefix
60+
}
61+
62+
async function downloadAndExtract(url, base, rubyPrefix) {
63+
const parentDir = path.dirname(rubyPrefix)
64+
5165
const downloadPath = await common.measure('Downloading Ruby', async () => {
5266
console.log(url)
5367
return await tc.downloadTool(url)
5468
})
5569

5670
await common.measure('Extracting Ruby', async () =>
57-
exec.exec('7z', ['x', downloadPath, `-xr!${base}\\share\\doc`, `-o${parentDir}`], { silent: true }))
71+
exec.exec('7z', ['x', downloadPath, `-xr!${base}\\share\\doc`, `-o${parentDir}`], {silent: true}))
5872

5973
if (base !== path.basename(rubyPrefix)) {
6074
await io.mv(path.join(parentDir, base), rubyPrefix)
6175
}
62-
63-
return rubyPrefix
6476
}
6577

6678
async function setupMingw(version) {

0 commit comments

Comments
 (0)