Skip to content

Commit

Permalink
Pass the validated engine and version to install()
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed May 13, 2020
1 parent 381e263 commit e5857db
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 58 deletions.
4 changes: 4 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export async function measure(name, block) {
})
}

export function isHeadVersion(rubyVersion) {
return rubyVersion === 'head' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
}

export function getVirtualEnvironmentName() {
const platform = os.platform()
if (platform === 'linux') {
Expand Down
58 changes: 29 additions & 29 deletions dist/index.js

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

27 changes: 12 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function main() {
process.chdir(core.getInput('working-directory'))

const platform = common.getVirtualEnvironmentName()
const [engine, version] = parseRubyEngineAndVersion(core.getInput('ruby-version'))
const [engine, parsedVersion] = parseRubyEngineAndVersion(core.getInput('ruby-version'))

let installer
if (platform === 'windows-latest' && engine !== 'jruby') {
Expand All @@ -27,13 +27,13 @@ async function main() {
}

const engineVersions = installer.getAvailableVersions(platform, engine)
const ruby = validateRubyEngineAndVersion(platform, engineVersions, engine, version)
const version = validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion)

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') {
await common.measure('Installing Bundler', async () =>
Expand Down Expand Up @@ -78,24 +78,25 @@ function parseRubyEngineAndVersion(rubyVersion) {
return [engine, version]
}

function validateRubyEngineAndVersion(platform, engineVersions, engine, version) {
function validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion) {
if (!engineVersions) {
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 found = latestToFirstVersion.find(v => v !== 'head' && v.startsWith(version))
const found = latestToFirstVersion.find(v => v !== 'head' && v.startsWith(parsedVersion))
if (found) {
version = found
} 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(', ')}
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() {
Expand All @@ -105,7 +106,7 @@ function createGemRC() {
}
}

function setupPath(ruby, newPathEntries) {
function setupPath(newPathEntries) {
const originalPath = process.env['PATH'].split(path.delimiter)
let cleanPath = originalPath.filter(entry => !/\bruby\b/i.test(entry))

Expand Down Expand Up @@ -164,7 +165,7 @@ async function installBundler(platform, rubyPrefix, engine, rubyVersion) {
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}`)
} else if (engine === 'truffleruby' && bundlerVersion === '1') {
console.log(`Using Bundler 1 shipped with ${engine}`)
Expand All @@ -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() }
23 changes: 11 additions & 12 deletions ruby-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ export function getAvailableVersions(platform, engine) {
return rubyBuilderVersions.getVersions(platform)[engine]
}

export async function install(platform, ruby) {
const rubyPrefix = await downloadAndExtract(platform, ruby)
export async function install(platform, engine, version) {
const rubyPrefix = await downloadAndExtract(platform, engine, version)
let newPathEntries
if (ruby.startsWith('rubinius')) {
if (engine === 'rubinius') {
newPathEntries = [path.join(rubyPrefix, 'bin'), path.join(rubyPrefix, 'gems', 'bin')]
} else {
newPathEntries = [path.join(rubyPrefix, 'bin')]
}
return [rubyPrefix, newPathEntries]
}

async function downloadAndExtract(platform, ruby) {
async function downloadAndExtract(platform, engine, version) {
const rubiesDir = path.join(os.homedir(), '.rubies')
await io.mkdirP(rubiesDir)

const downloadPath = await common.measure('Downloading Ruby', async () => {
const url = getDownloadURL(platform, ruby)
const url = getDownloadURL(platform, engine, version)
console.log(url)
return await tc.downloadTool(url)
})
Expand All @@ -38,18 +38,17 @@ async function downloadAndExtract(platform, ruby) {
await common.measure('Extracting Ruby', async () =>
exec.exec(tar, [ '-xz', '-C', rubiesDir, '-f', downloadPath ]))

return path.join(rubiesDir, ruby)
return path.join(rubiesDir, `${engine}-${version}`)
}

function getDownloadURL(platform, ruby) {
if (ruby.endsWith('-head')) {
return getLatestHeadBuildURL(platform, ruby)
function getDownloadURL(platform, engine, version) {
if (common.isHeadVersion(version)) {
return getLatestHeadBuildURL(platform, engine, version)
} else {
return `${releasesURL}/download/${builderReleaseTag}/${ruby}-${platform}.tar.gz`
return `${releasesURL}/download/${builderReleaseTag}/${engine}-${version}-${platform}.tar.gz`
}
}

function getLatestHeadBuildURL(platform, ruby) {
const engine = ruby.split('-')[0]
function getLatestHeadBuildURL(platform, engine, version) {
return `https://github.com/ruby/${engine}-dev-builder/releases/latest/download/${engine}-head-${platform}.tar.gz`
}
3 changes: 1 addition & 2 deletions windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export function getAvailableVersions(platform, engine) {
}
}

export async function install(platform, ruby) {
const version = ruby.split('-', 2)[1]
export async function install(platform, engine, version) {
const url = rubyInstallerVersions[version]

if (!url.endsWith('.7z')) {
Expand Down

0 comments on commit e5857db

Please sign in to comment.