From 3d6280ad197b30d731628cec26cac42b7f361b5d Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 16 Feb 2020 14:32:39 +0100 Subject: [PATCH] Add support for .tool-versions --- .github/workflows/test.yml | 2 +- .tool-versions | 2 ++ README.md | 9 +++++++-- action.yml | 2 +- dist/index.js | 15 +++++++++++++++ index.js | 15 +++++++++++++++ 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .tool-versions diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c8df580..fc3ee26 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: matrix: os: [ ubuntu-16.04, ubuntu-18.04, macos-latest, windows-latest ] # Use various version syntaxes here for testing - ruby: [ .ruby-version, 2.2, 2.3, 2.7.0, ruby-head, jruby, truffleruby, truffleruby-head, rubinius ] + ruby: [ .ruby-version, .tool-versions, 2.2, 2.3, ruby-head, jruby, truffleruby, truffleruby-head, rubinius ] exclude: - os: windows-latest ruby: truffleruby diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..063be9b --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +nodejs 12.0.0 +ruby 2.7.0 diff --git a/README.md b/README.md index b0881f1..fe39e78 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,11 @@ jobs: * short version like `2.6`, automatically using the latest release matching that version (`2.6.5`) * version only like `2.6.5`, assumes MRI for the engine * engine only like `truffleruby`, uses the latest stable release of that implementation -* `.ruby-version` (also the default value) reads from the project's `.ruby-version` file + + +* `.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` ### Bundler @@ -117,7 +121,8 @@ You can cache the installed gems with these two steps: ``` When using a single job with a Ruby version, replace `${{ matrix.ruby }}` with the Ruby version. -When using `.ruby-version`, replace `${{ matrix.ruby }}` with `${{ hashFiles('.ruby-version') }}`. +When using `.ruby-version`, replace `${{ matrix.ruby }}` with `${{ hashFiles('.ruby-version') }}`. +When using `.tool-versions`, replace `${{ matrix.ruby }}` with `${{ hashFiles('.tool-versions') }}`. This uses the [cache action](https://github.com/actions/cache). The code above is a more complete version of the [Ruby - Bundler example](https://github.com/actions/cache/blob/master/examples.md#ruby---bundler). diff --git a/action.yml b/action.yml index 404e899..1ae6ed2 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,7 @@ inputs: ruby-version: description: 'Engine and version to use, see the syntax in the README. Reads from .ruby-version if unset.' required: false - default: '.ruby-version' + default: 'default' outputs: ruby-prefix: description: 'The prefix of the installed ruby' diff --git a/dist/index.js b/dist/index.js index 098cd76..245257b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1416,9 +1416,24 @@ async function run() { } function parseRubyEngineAndVersion(rubyVersion) { + if (rubyVersion === 'default') { + if (fs.existsSync('.ruby-version')) { + rubyVersion = '.ruby-version' + } else if (fs.existsSync('.tool-versions')) { + rubyVersion = '.tool-versions' + } else { + throw new Error('input ruby-version needs to be specified if no .ruby-version or .tool-versions file exists') + } + } + if (rubyVersion === '.ruby-version') { // Read from .ruby-version rubyVersion = fs.readFileSync('.ruby-version', 'utf8').trim() console.log(`Using ${rubyVersion} as input from file .ruby-version`) + } else if (rubyVersion === '.tool-versions') { // Read from .tool-versions + const toolVersions = fs.readFileSync('.tool-versions', 'utf8').trim() + const rubyLine = toolVersions.split(/\r?\n/).filter(e => e.match(/^ruby\s/))[0] + rubyVersion = rubyLine.split(/\s+/, 2)[1] + console.log(`Using ${rubyVersion} as input from file .tool-versions`) } let engine, version diff --git a/index.js b/index.js index 77ca96f..2c44712 100644 --- a/index.js +++ b/index.js @@ -25,9 +25,24 @@ async function run() { } function parseRubyEngineAndVersion(rubyVersion) { + if (rubyVersion === 'default') { + if (fs.existsSync('.ruby-version')) { + rubyVersion = '.ruby-version' + } else if (fs.existsSync('.tool-versions')) { + rubyVersion = '.tool-versions' + } else { + throw new Error('input ruby-version needs to be specified if no .ruby-version or .tool-versions file exists') + } + } + if (rubyVersion === '.ruby-version') { // Read from .ruby-version rubyVersion = fs.readFileSync('.ruby-version', 'utf8').trim() console.log(`Using ${rubyVersion} as input from file .ruby-version`) + } else if (rubyVersion === '.tool-versions') { // Read from .tool-versions + const toolVersions = fs.readFileSync('.tool-versions', 'utf8').trim() + const rubyLine = toolVersions.split(/\r?\n/).filter(e => e.match(/^ruby\s/))[0] + rubyVersion = rubyLine.split(/\s+/, 2)[1] + console.log(`Using ${rubyVersion} as input from file .tool-versions`) } let engine, version