Pass the validated engine and version to install()

This commit is contained in:
Benoit Daloze
2020-05-13 23:36:33 +02:00
parent 381e2639aa
commit e5857dbffe
5 changed files with 57 additions and 58 deletions
+4
View File
@@ -16,6 +16,10 @@ export async function measure(name, block) {
}) })
} }
export function isHeadVersion(rubyVersion) {
return rubyVersion === 'head' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
}
export function getVirtualEnvironmentName() { export function getVirtualEnvironmentName() {
const platform = os.platform() const platform = os.platform()
if (platform === 'linux') { if (platform === 'linux') {
Generated Vendored
+29 -29
View File
@@ -988,7 +988,7 @@ async function main() {
process.chdir(core.getInput('working-directory')) process.chdir(core.getInput('working-directory'))
const platform = common.getVirtualEnvironmentName() const platform = common.getVirtualEnvironmentName()
const [engine, version] = parseRubyEngineAndVersion(core.getInput('ruby-version')) const [engine, parsedVersion] = parseRubyEngineAndVersion(core.getInput('ruby-version'))
let installer let installer
if (platform === 'windows-latest' && engine !== 'jruby') { if (platform === 'windows-latest' && engine !== 'jruby') {
@@ -998,13 +998,13 @@ async function main() {
} }
const engineVersions = installer.getAvailableVersions(platform, engine) const engineVersions = installer.getAvailableVersions(platform, engine)
const ruby = validateRubyEngineAndVersion(platform, engineVersions, engine, version) const version = validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion)
createGemRC() createGemRC()
const [rubyPrefix, newPathEntries] = await installer.install(platform, ruby) const [rubyPrefix, newPathEntries] = await installer.install(platform, engine, version)
setupPath(ruby, newPathEntries) setupPath(newPathEntries)
if (core.getInput('bundler') !== 'none') { if (core.getInput('bundler') !== 'none') {
await common.measure('Installing Bundler', async () => await common.measure('Installing Bundler', async () =>
@@ -1049,24 +1049,25 @@ function parseRubyEngineAndVersion(rubyVersion) {
return [engine, version] return [engine, version]
} }
function validateRubyEngineAndVersion(platform, engineVersions, engine, version) { function validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion) {
if (!engineVersions) { if (!engineVersions) {
throw new Error(`Unknown engine ${engine} on ${platform}`) throw new Error(`Unknown engine ${engine} on ${platform}`)
} }
if (!engineVersions.includes(version)) { let version = parsedVersion
if (!engineVersions.includes(parsedVersion)) {
const latestToFirstVersion = engineVersions.slice().reverse() const latestToFirstVersion = engineVersions.slice().reverse()
const found = latestToFirstVersion.find(v => v !== 'head' && v.startsWith(version)) const found = latestToFirstVersion.find(v => v !== 'head' && v.startsWith(parsedVersion))
if (found) { if (found) {
version = found version = found
} else { } else {
throw new Error(`Unknown version ${version} for ${engine} on ${platform} throw new Error(`Unknown version ${parsedVersion} for ${engine} on ${platform}
available versions for ${engine} on ${platform}: ${engineVersions.join(', ')} available versions for ${engine} on ${platform}: ${engineVersions.join(', ')}
File an issue at https://github.com/ruby/setup-ruby/issues if would like support for a new version`) File an issue at https://github.com/ruby/setup-ruby/issues if would like support for a new version`)
} }
} }
return engine + '-' + version return version
} }
function createGemRC() { function createGemRC() {
@@ -1076,7 +1077,7 @@ function createGemRC() {
} }
} }
function setupPath(ruby, newPathEntries) { function setupPath(newPathEntries) {
const originalPath = process.env['PATH'].split(path.delimiter) const originalPath = process.env['PATH'].split(path.delimiter)
let cleanPath = originalPath.filter(entry => !/\bruby\b/i.test(entry)) let cleanPath = originalPath.filter(entry => !/\bruby\b/i.test(entry))
@@ -1135,7 +1136,7 @@ async function installBundler(platform, rubyPrefix, engine, rubyVersion) {
throw new Error(`Cannot parse bundler input: ${bundlerVersion}`) throw new Error(`Cannot parse bundler input: ${bundlerVersion}`)
} }
if (engine === 'ruby' && isHeadVersion(rubyVersion) && bundlerVersion === '2') { if (engine === 'ruby' && common.isHeadVersion(rubyVersion) && bundlerVersion === '2') {
console.log(`Using Bundler 2 shipped with ${engine}-${rubyVersion}`) console.log(`Using Bundler 2 shipped with ${engine}-${rubyVersion}`)
} else if (engine === 'truffleruby' && bundlerVersion === '1') { } else if (engine === 'truffleruby' && bundlerVersion === '1') {
console.log(`Using Bundler 1 shipped with ${engine}`) console.log(`Using Bundler 1 shipped with ${engine}`)
@@ -1147,10 +1148,6 @@ async function installBundler(platform, rubyPrefix, engine, rubyVersion) {
} }
} }
function isHeadVersion(rubyVersion) {
return rubyVersion === 'head' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
}
if (__filename.endsWith('index.js')) { run() } if (__filename.endsWith('index.js')) { run() }
@@ -1504,6 +1501,7 @@ module.exports = require("https");
"use strict"; "use strict";
__webpack_require__.r(__webpack_exports__); __webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "measure", function() { return measure; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "measure", function() { return measure; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isHeadVersion", function() { return isHeadVersion; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getVirtualEnvironmentName", function() { return getVirtualEnvironmentName; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getVirtualEnvironmentName", function() { return getVirtualEnvironmentName; });
const os = __webpack_require__(87) const os = __webpack_require__(87)
const fs = __webpack_require__(747) const fs = __webpack_require__(747)
@@ -1523,6 +1521,10 @@ async function measure(name, block) {
}) })
} }
function isHeadVersion(rubyVersion) {
return rubyVersion === 'head' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
}
function getVirtualEnvironmentName() { function getVirtualEnvironmentName() {
const platform = os.platform() const platform = os.platform()
if (platform === 'linux') { if (platform === 'linux') {
@@ -3541,10 +3543,10 @@ function getAvailableVersions(platform, engine) {
return rubyBuilderVersions.getVersions(platform)[engine] return rubyBuilderVersions.getVersions(platform)[engine]
} }
async function install(platform, ruby) { async function install(platform, engine, version) {
const rubyPrefix = await downloadAndExtract(platform, ruby) const rubyPrefix = await downloadAndExtract(platform, engine, version)
let newPathEntries let newPathEntries
if (ruby.startsWith('rubinius')) { if (engine === 'rubinius') {
newPathEntries = [path.join(rubyPrefix, 'bin'), path.join(rubyPrefix, 'gems', 'bin')] newPathEntries = [path.join(rubyPrefix, 'bin'), path.join(rubyPrefix, 'gems', 'bin')]
} else { } else {
newPathEntries = [path.join(rubyPrefix, 'bin')] newPathEntries = [path.join(rubyPrefix, 'bin')]
@@ -3552,12 +3554,12 @@ async function install(platform, ruby) {
return [rubyPrefix, newPathEntries] return [rubyPrefix, newPathEntries]
} }
async function downloadAndExtract(platform, ruby) { async function downloadAndExtract(platform, engine, version) {
const rubiesDir = path.join(os.homedir(), '.rubies') const rubiesDir = path.join(os.homedir(), '.rubies')
await io.mkdirP(rubiesDir) await io.mkdirP(rubiesDir)
const downloadPath = await common.measure('Downloading Ruby', async () => { const downloadPath = await common.measure('Downloading Ruby', async () => {
const url = getDownloadURL(platform, ruby) const url = getDownloadURL(platform, engine, version)
console.log(url) console.log(url)
return await tc.downloadTool(url) return await tc.downloadTool(url)
}) })
@@ -3566,19 +3568,18 @@ async function downloadAndExtract(platform, ruby) {
await common.measure('Extracting Ruby', async () => await common.measure('Extracting Ruby', async () =>
exec.exec(tar, [ '-xz', '-C', rubiesDir, '-f', downloadPath ])) exec.exec(tar, [ '-xz', '-C', rubiesDir, '-f', downloadPath ]))
return path.join(rubiesDir, ruby) return path.join(rubiesDir, `${engine}-${version}`)
} }
function getDownloadURL(platform, ruby) { function getDownloadURL(platform, engine, version) {
if (ruby.endsWith('-head')) { if (common.isHeadVersion(version)) {
return getLatestHeadBuildURL(platform, ruby) return getLatestHeadBuildURL(platform, engine, version)
} else { } else {
return `${releasesURL}/download/${builderReleaseTag}/${ruby}-${platform}.tar.gz` return `${releasesURL}/download/${builderReleaseTag}/${engine}-${version}-${platform}.tar.gz`
} }
} }
function getLatestHeadBuildURL(platform, ruby) { function getLatestHeadBuildURL(platform, engine, version) {
const engine = ruby.split('-')[0]
return `https://github.com/ruby/${engine}-dev-builder/releases/latest/download/${engine}-head-${platform}.tar.gz` return `https://github.com/ruby/${engine}-dev-builder/releases/latest/download/${engine}-head-${platform}.tar.gz`
} }
@@ -5001,8 +5002,7 @@ function getAvailableVersions(platform, engine) {
} }
} }
async function install(platform, ruby) { async function install(platform, engine, version) {
const version = ruby.split('-', 2)[1]
const url = rubyInstallerVersions[version] const url = rubyInstallerVersions[version]
if (!url.endsWith('.7z')) { if (!url.endsWith('.7z')) {
+12 -15
View File
@@ -17,7 +17,7 @@ async function main() {
process.chdir(core.getInput('working-directory')) process.chdir(core.getInput('working-directory'))
const platform = common.getVirtualEnvironmentName() const platform = common.getVirtualEnvironmentName()
const [engine, version] = parseRubyEngineAndVersion(core.getInput('ruby-version')) const [engine, parsedVersion] = parseRubyEngineAndVersion(core.getInput('ruby-version'))
let installer let installer
if (platform === 'windows-latest' && engine !== 'jruby') { if (platform === 'windows-latest' && engine !== 'jruby') {
@@ -27,13 +27,13 @@ async function main() {
} }
const engineVersions = installer.getAvailableVersions(platform, engine) const engineVersions = installer.getAvailableVersions(platform, engine)
const ruby = validateRubyEngineAndVersion(platform, engineVersions, engine, version) const version = validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion)
createGemRC() createGemRC()
const [rubyPrefix, newPathEntries] = await installer.install(platform, ruby) const [rubyPrefix, newPathEntries] = await installer.install(platform, engine, version)
setupPath(ruby, newPathEntries) setupPath(newPathEntries)
if (core.getInput('bundler') !== 'none') { if (core.getInput('bundler') !== 'none') {
await common.measure('Installing Bundler', async () => await common.measure('Installing Bundler', async () =>
@@ -78,24 +78,25 @@ function parseRubyEngineAndVersion(rubyVersion) {
return [engine, version] return [engine, version]
} }
function validateRubyEngineAndVersion(platform, engineVersions, engine, version) { function validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion) {
if (!engineVersions) { if (!engineVersions) {
throw new Error(`Unknown engine ${engine} on ${platform}`) throw new Error(`Unknown engine ${engine} on ${platform}`)
} }
if (!engineVersions.includes(version)) { let version = parsedVersion
if (!engineVersions.includes(parsedVersion)) {
const latestToFirstVersion = engineVersions.slice().reverse() const latestToFirstVersion = engineVersions.slice().reverse()
const found = latestToFirstVersion.find(v => v !== 'head' && v.startsWith(version)) const found = latestToFirstVersion.find(v => v !== 'head' && v.startsWith(parsedVersion))
if (found) { if (found) {
version = found version = found
} else { } else {
throw new Error(`Unknown version ${version} for ${engine} on ${platform} throw new Error(`Unknown version ${parsedVersion} for ${engine} on ${platform}
available versions for ${engine} on ${platform}: ${engineVersions.join(', ')} available versions for ${engine} on ${platform}: ${engineVersions.join(', ')}
File an issue at https://github.com/ruby/setup-ruby/issues if would like support for a new version`) File an issue at https://github.com/ruby/setup-ruby/issues if would like support for a new version`)
} }
} }
return engine + '-' + version return version
} }
function createGemRC() { function createGemRC() {
@@ -105,7 +106,7 @@ function createGemRC() {
} }
} }
function setupPath(ruby, newPathEntries) { function setupPath(newPathEntries) {
const originalPath = process.env['PATH'].split(path.delimiter) const originalPath = process.env['PATH'].split(path.delimiter)
let cleanPath = originalPath.filter(entry => !/\bruby\b/i.test(entry)) let cleanPath = originalPath.filter(entry => !/\bruby\b/i.test(entry))
@@ -164,7 +165,7 @@ async function installBundler(platform, rubyPrefix, engine, rubyVersion) {
throw new Error(`Cannot parse bundler input: ${bundlerVersion}`) throw new Error(`Cannot parse bundler input: ${bundlerVersion}`)
} }
if (engine === 'ruby' && isHeadVersion(rubyVersion) && bundlerVersion === '2') { if (engine === 'ruby' && common.isHeadVersion(rubyVersion) && bundlerVersion === '2') {
console.log(`Using Bundler 2 shipped with ${engine}-${rubyVersion}`) console.log(`Using Bundler 2 shipped with ${engine}-${rubyVersion}`)
} else if (engine === 'truffleruby' && bundlerVersion === '1') { } else if (engine === 'truffleruby' && bundlerVersion === '1') {
console.log(`Using Bundler 1 shipped with ${engine}`) console.log(`Using Bundler 1 shipped with ${engine}`)
@@ -176,8 +177,4 @@ async function installBundler(platform, rubyPrefix, engine, rubyVersion) {
} }
} }
function isHeadVersion(rubyVersion) {
return rubyVersion === 'head' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
}
if (__filename.endsWith('index.js')) { run() } if (__filename.endsWith('index.js')) { run() }
+11 -12
View File
@@ -13,10 +13,10 @@ export function getAvailableVersions(platform, engine) {
return rubyBuilderVersions.getVersions(platform)[engine] return rubyBuilderVersions.getVersions(platform)[engine]
} }
export async function install(platform, ruby) { export async function install(platform, engine, version) {
const rubyPrefix = await downloadAndExtract(platform, ruby) const rubyPrefix = await downloadAndExtract(platform, engine, version)
let newPathEntries let newPathEntries
if (ruby.startsWith('rubinius')) { if (engine === 'rubinius') {
newPathEntries = [path.join(rubyPrefix, 'bin'), path.join(rubyPrefix, 'gems', 'bin')] newPathEntries = [path.join(rubyPrefix, 'bin'), path.join(rubyPrefix, 'gems', 'bin')]
} else { } else {
newPathEntries = [path.join(rubyPrefix, 'bin')] newPathEntries = [path.join(rubyPrefix, 'bin')]
@@ -24,12 +24,12 @@ export async function install(platform, ruby) {
return [rubyPrefix, newPathEntries] return [rubyPrefix, newPathEntries]
} }
async function downloadAndExtract(platform, ruby) { async function downloadAndExtract(platform, engine, version) {
const rubiesDir = path.join(os.homedir(), '.rubies') const rubiesDir = path.join(os.homedir(), '.rubies')
await io.mkdirP(rubiesDir) await io.mkdirP(rubiesDir)
const downloadPath = await common.measure('Downloading Ruby', async () => { const downloadPath = await common.measure('Downloading Ruby', async () => {
const url = getDownloadURL(platform, ruby) const url = getDownloadURL(platform, engine, version)
console.log(url) console.log(url)
return await tc.downloadTool(url) return await tc.downloadTool(url)
}) })
@@ -38,18 +38,17 @@ async function downloadAndExtract(platform, ruby) {
await common.measure('Extracting Ruby', async () => await common.measure('Extracting Ruby', async () =>
exec.exec(tar, [ '-xz', '-C', rubiesDir, '-f', downloadPath ])) exec.exec(tar, [ '-xz', '-C', rubiesDir, '-f', downloadPath ]))
return path.join(rubiesDir, ruby) return path.join(rubiesDir, `${engine}-${version}`)
} }
function getDownloadURL(platform, ruby) { function getDownloadURL(platform, engine, version) {
if (ruby.endsWith('-head')) { if (common.isHeadVersion(version)) {
return getLatestHeadBuildURL(platform, ruby) return getLatestHeadBuildURL(platform, engine, version)
} else { } else {
return `${releasesURL}/download/${builderReleaseTag}/${ruby}-${platform}.tar.gz` return `${releasesURL}/download/${builderReleaseTag}/${engine}-${version}-${platform}.tar.gz`
} }
} }
function getLatestHeadBuildURL(platform, ruby) { function getLatestHeadBuildURL(platform, engine, version) {
const engine = ruby.split('-')[0]
return `https://github.com/ruby/${engine}-dev-builder/releases/latest/download/${engine}-head-${platform}.tar.gz` return `https://github.com/ruby/${engine}-dev-builder/releases/latest/download/${engine}-head-${platform}.tar.gz`
} }
+1 -2
View File
@@ -33,8 +33,7 @@ export function getAvailableVersions(platform, engine) {
} }
} }
export async function install(platform, ruby) { export async function install(platform, engine, version) {
const version = ruby.split('-', 2)[1]
const url = rubyInstallerVersions[version] const url = rubyInstallerVersions[version]
if (!url.endsWith('.7z')) { if (!url.endsWith('.7z')) {