Add support for gems.rb/gems.locked during bundle install (#84)

* Extract detection of gemfile and lockfile.
This commit is contained in:
Samuel Williams
2020-09-19 11:18:52 +02:00
committed by GitHub
parent 4cbbce6b48
commit 1c172ef5dc
2 changed files with 90 additions and 24 deletions
+45 -12
View File
@@ -61,18 +61,48 @@ export async function setupRuby(options = {}) {
}
if (inputs['bundler'] !== 'none') {
const [gemFile, lockFile] = detectGemfiles()
await common.measure('Installing Bundler', async () =>
installBundler(inputs['bundler'], platform, rubyPrefix, engine, version))
installBundler(inputs['bundler'], lockFile, platform, rubyPrefix, engine, version))
if (inputs['bundler-cache'] === 'true') {
await common.measure('bundle install', async () =>
bundleInstall(platform, engine, version))
bundleInstall(gemFile, lockFile, platform, engine, version))
}
}
core.setOutput('ruby-prefix', rubyPrefix)
}
function detectGemfiles() {
const gemfilePath = process.env['BUNDLE_GEMFILE'] || 'Gemfile'
if (fs.existsSync(gemfilePath)) {
const lockPath = `${gemfilePath}.lock`
if (fs.existsSync(lockPath)) {
return [gemfilePath, lockPath]
} else {
return [gemfilePath, null]
}
}
const gemsPath = "gems.rb"
if (fs.existsSync(gemsPath)) {
const lockPath = "gems.locked"
if (fs.existsSync(lockPath)) {
return [gemsPath, lockPath]
} else {
return [gemsPath, null]
}
}
return [null, null]
}
function parseRubyEngineAndVersion(rubyVersion) {
if (rubyVersion === 'default') {
if (fs.existsSync('.ruby-version')) {
@@ -168,12 +198,12 @@ function readBundledWithFromGemfileLock(path) {
return null
}
async function installBundler(bundlerVersionInput, platform, rubyPrefix, engine, rubyVersion) {
async function installBundler(bundlerVersionInput, lockFile, platform, rubyPrefix, engine, rubyVersion) {
var bundlerVersion = bundlerVersionInput
if (bundlerVersion === 'default' || bundlerVersion === 'Gemfile.lock') {
const gemfilePath = `${process.env['BUNDLE_GEMFILE'] || 'Gemfile'}.lock`
bundlerVersion = readBundledWithFromGemfileLock(gemfilePath)
bundlerVersion = readBundledWithFromGemfileLock(lockFile)
if (!bundlerVersion) {
bundlerVersion = 'latest'
}
@@ -209,15 +239,16 @@ async function installBundler(bundlerVersionInput, platform, rubyPrefix, engine,
}
}
async function bundleInstall(platform, engine, version) {
if (!fs.existsSync('Gemfile')) {
console.log('No Gemfile, skipping "bundle install" and caching')
return
async function bundleInstall(gemfilePath, lockPath, platform, engine, version) {
if (!fs.existsSync(gemfilePath)) {
console.log('Could not determine gemfile path, skipping "bundle install" and caching')
return false
}
// config
const path = 'vendor/bundle'
const hasGemfileLock = fs.existsSync('Gemfile.lock');
const hasGemfileLock = fs.existsSync(lockPath)
if (hasGemfileLock) {
await exec.exec('bundle', ['config', '--local', 'deployment', 'true'])
}
@@ -229,9 +260,9 @@ async function bundleInstall(platform, engine, version) {
let key = baseKey
let restoreKeys
if (hasGemfileLock) {
key += `-Gemfile.lock-${await common.hashFile('Gemfile.lock')}`
key += `-${lockPath}-${await common.hashFile(lockPath)}`
// If only Gemfile.lock we can reuse some of the cache (but it will keep old gem versions in the cache)
restoreKeys = [`${baseKey}-Gemfile.lock-`]
restoreKeys = [`${baseKey}-${lockPath}-`]
} else {
// Only exact key, to never mix native gems of different platforms or Ruby versions
restoreKeys = []
@@ -277,6 +308,8 @@ async function bundleInstall(platform, engine, version) {
}
}
}
return true
}
async function computeBaseKey(platform, engine, version) {