mirror of
https://github.com/ruby/setup-ruby.git
synced 2026-09-14 06:54:20 +02:00
envPreInstall - sets ENV values for runners common.setupPath - collects all Path operations into one function, runs before Ruby install Add MSYS2 paths to all Windows builds for MSYS2 Bash use
95 lines
2.9 KiB
JavaScript
95 lines
2.9 KiB
JavaScript
const os = require('os')
|
|
const path = require('path')
|
|
const fs = require('fs')
|
|
const util = require('util')
|
|
const stream = require('stream')
|
|
const crypto = require('crypto')
|
|
const core = require('@actions/core')
|
|
const { performance } = require('perf_hooks')
|
|
|
|
const isWin = (os.platform() === 'win32')
|
|
|
|
export async function measure(name, block) {
|
|
return await core.group(name, async () => {
|
|
const start = performance.now()
|
|
try {
|
|
return await block()
|
|
} finally {
|
|
const end = performance.now()
|
|
const duration = (end - start) / 1000.0
|
|
console.log(`Took ${duration.toFixed(2).padStart(6)} seconds`)
|
|
}
|
|
})
|
|
}
|
|
|
|
export function isHeadVersion(rubyVersion) {
|
|
return rubyVersion === 'head' || rubyVersion === 'debug' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
|
|
}
|
|
|
|
export async function hashFile(file) {
|
|
// See https://github.com/actions/runner/blob/master/src/Misc/expressionFunc/hashFiles/src/hashFiles.ts
|
|
const hash = crypto.createHash('sha256')
|
|
const pipeline = util.promisify(stream.pipeline)
|
|
await pipeline(fs.createReadStream(file), hash)
|
|
return hash.digest('hex')
|
|
}
|
|
|
|
export function getVirtualEnvironmentName() {
|
|
const platform = os.platform()
|
|
if (platform === 'linux') {
|
|
return `ubuntu-${findUbuntuVersion()}`
|
|
} else if (platform === 'darwin') {
|
|
return 'macos-latest'
|
|
} else if (platform === 'win32') {
|
|
return 'windows-latest'
|
|
} else {
|
|
throw new Error(`Unknown platform ${platform}`)
|
|
}
|
|
}
|
|
|
|
function findUbuntuVersion() {
|
|
const lsb_release = fs.readFileSync('/etc/lsb-release', 'utf8')
|
|
const match = lsb_release.match(/^DISTRIB_RELEASE=(\d+\.\d+)$/m)
|
|
if (match) {
|
|
return match[1]
|
|
} else {
|
|
throw new Error('Could not find Ubuntu version')
|
|
}
|
|
}
|
|
|
|
// convert windows path like C:\Users\runneradmin to /c/Users/runneradmin
|
|
export function win2nix(path) {
|
|
if (/^[A-Z]:/i.test(path)) {
|
|
// path starts with drive
|
|
path = `/${path[0].toLowerCase()}${path.split(':', 2)[1]}`
|
|
}
|
|
return path.replace(/\\/g, '/').replace(/ /g, '\\ ')
|
|
}
|
|
|
|
export function setupPath(newPathEntries) {
|
|
const envPath = isWin ? 'Path' : 'PATH'
|
|
const originalPath = process.env[envPath].split(path.delimiter)
|
|
let cleanPath = originalPath.filter(entry => !/\bruby\b/i.test(entry))
|
|
|
|
if (cleanPath.length !== originalPath.length) {
|
|
core.startGroup(`Cleaning ${envPath}`)
|
|
console.log(`Entries removed from ${envPath} to avoid conflicts with Ruby:`)
|
|
for (const entry of originalPath) {
|
|
if (!cleanPath.includes(entry)) {
|
|
console.log(` ${entry}`)
|
|
}
|
|
}
|
|
core.exportVariable(envPath, cleanPath.join(path.delimiter))
|
|
core.endGroup()
|
|
}
|
|
let newPath
|
|
if (isWin) {
|
|
// add MSYS2 path to all for bash shell
|
|
const msys2 = ['C:\\msys64\\mingw64\\bin', 'C:\\msys64\\usr\\bin']
|
|
newPath = [...newPathEntries, ...msys2]
|
|
} else {
|
|
newPath = newPathEntries
|
|
}
|
|
core.addPath(newPath.join(path.delimiter))
|
|
}
|