Skip to content

Read Ruby version from Gemfile.lock #592

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

Closed
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ jobs:
rubygems: 3.2.3
- run: gem --version | grep -F "3.3.3"

testRubyVersionFromGemfile:
name: "Test RUBY VERSION from Gemfile.lock"
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/ruby-version.gemfile
steps:
- uses: actions/checkout@v4
- uses: ./
with:
ruby-version: gemfile
- run: ruby --version | grep -F "3.3.1"

testRubyEngineVersionFromGemfile:
name: "Test RUBY VERSION from Gemfile.lock"
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/ruby-version-engine.gemfile
steps:
- uses: actions/checkout@v4
- uses: ./
with:
ruby-version: gemfile
- run: ruby --version | grep -F "truffleruby 24.0.1, like ruby 3.2.2"

testUseBundlerFromRubyGemsUpdate:
name: "Test rubygems: version uses the Bundler installed by the rubygems update"
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ and the [condition and expression syntax](https://help.github.com/en/actions/ref
* engine only like `ruby` and `truffleruby`, uses the latest stable release of that implementation
* `.ruby-version` reads from the project's `.ruby-version` file
* `.tool-versions` reads from the project's `.tool-versions` file
* If the `ruby-version` input is not specified, `.ruby-version` is tried first, followed by `.tool-versions`
* `gemfile` reads from the project's `Gemfile.lock` file
* If the `ruby-version` input is not specified, `.ruby-version` is tried first, followed by `.tool-versions`, then `Gemfile.lock`.

### Working Directory

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ branding:
icon: download
inputs:
ruby-version:
description: 'Engine and version to use, see the syntax in the README. Reads from .ruby-version or .tool-versions if unset.'
description: 'Engine and version to use, see the syntax in the README. When not set, it tries to read the version from .ruby-version, .tool-versions or Gemfile.lock.'
default: 'default'
rubygems:
description: |
Expand Down
32 changes: 31 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gemfiles/ruby-version-engine.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

ruby "3.2.2", engine: "truffleruby", engine_version: "24.0.1"
14 changes: 14 additions & 0 deletions gemfiles/ruby-version-engine.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GEM
remote: https://rubygems.org/
specs:

PLATFORMS
aarch64-darwin

DEPENDENCIES

RUBY VERSION
ruby 3.2.2p0 (truffleruby 24.0.1)

BUNDLED WITH
2.4.10
3 changes: 3 additions & 0 deletions gemfiles/ruby-version.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

ruby "3.3.1"
15 changes: 15 additions & 0 deletions gemfiles/ruby-version.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
GEM
remote: https://rubygems.org/
specs:

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES

RUBY VERSION
ruby 3.3.1p55

BUNDLED WITH
2.5.9
32 changes: 31 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,36 @@ export async function setupRuby(options = {}) {
core.setOutput('ruby-prefix', rubyPrefix)
}

function readRubyVersionFromGemfileLock(lockFile) {
if (lockFile !== null && fs.existsSync(lockFile)) {
const contents = fs.readFileSync(lockFile, 'utf8')
const lines = contents.split(/\r?\n/)
const rubyVersionLine = lines.findIndex(line => /^RUBY VERSION$/.test(line.trim()))
if (rubyVersionLine !== -1) {
const nextLine = lines[rubyVersionLine+1]
if (nextLine) {
const rubyVersion = nextLine.trim()
if (rubyVersion.includes('(')) { // Alternative engine
return rubyVersion.match(/\(([^)]+)\)/)[1].replace(' ', '-')
} else {
return rubyVersion.replace(' ', '-').replace(/p\d+$/, '') // Strip off patchlevel
}
}
}
}
return null
}

function parseRubyEngineAndVersion(rubyVersion) {
if (rubyVersion === 'default') {
if (fs.existsSync('.ruby-version')) {
rubyVersion = '.ruby-version'
} else if (fs.existsSync('.tool-versions')) {
rubyVersion = '.tool-versions'
} else if (fs.existsSync('Gemfile.lock')) {
rubyVersion = 'gemfile'
} else {
throw new Error('input ruby-version needs to be specified if no .ruby-version or .tool-versions file exists')
throw new Error('input ruby-version needs to be specified if there is no Ruby version in .ruby-version, .tool-versions or Gemfile.lock')
}
}

Expand All @@ -124,6 +146,12 @@ function parseRubyEngineAndVersion(rubyVersion) {
const rubyLine = toolVersions.split(/\r?\n/).filter(e => /^ruby\s/.test(e))[0]
rubyVersion = rubyLine.match(/^ruby\s+(.+)$/)[1]
console.log(`Using ${rubyVersion} as input from file .tool-versions`)
} else if (rubyVersion === 'gemfile') { // Read from Gemfile.lock
rubyVersion = readRubyVersionFromGemfileLock('Gemfile.lock')
if (rubyVersion === null) {
throw new Error('input ruby-version needs to be specified if there is no Ruby version in .ruby-version, .tool-versions or Gemfile.lock')
}
console.log(`Using ${rubyVersion} as input from Gemfile.lock`)
}

let engine, version
Expand All @@ -137,6 +165,8 @@ function parseRubyEngineAndVersion(rubyVersion) {
[engine, version] = common.partition(rubyVersion, '-')
}

console.log(`Using ruby engine ${engine} and version ${version}`)

return [engine, version]
}

Expand Down