Reorganize path manipulation, mswin tools ENV

1. Use only one 'core' call to set PATH
2. Clean PATH
3. Windows mswin - add vcvars*.bat changes to ENV
This commit is contained in:
MSP-Greg
2020-03-07 13:41:58 +01:00
committed by Benoit Daloze
parent c125fbc596
commit 8d89ffaa2b
4 changed files with 886 additions and 1621 deletions
Generated Vendored
+786 -1560
View File
File diff suppressed because it is too large Load Diff
+40 -2
View File
@@ -1,5 +1,6 @@
const os = require('os') const os = require('os')
const fs = require('fs') const fs = require('fs')
const path = require('path')
const core = require('@actions/core') const core = require('@actions/core')
async function run() { async function run() {
@@ -17,8 +18,12 @@ async function run() {
const engineVersions = installer.getAvailableVersions(platform, engine) const engineVersions = installer.getAvailableVersions(platform, engine)
const ruby = validateRubyEngineAndVersion(platform, engineVersions, engine, version) const ruby = validateRubyEngineAndVersion(platform, engineVersions, engine, version)
const rubyPrefix = await installer.install(platform, ruby) // toolsPath is Windows build tools path additions
core.setOutput('ruby-prefix', rubyPrefix) const [rubyPrefix, toolsPath] = await installer.install(platform, ruby)
await setupPath(rubyPrefix, ruby, toolsPath)
core.setOutput('ruby-prefix', rubyPrefix, toolsPath)
} catch (error) { } catch (error) {
core.setFailed(error.message) core.setFailed(error.message)
} }
@@ -102,4 +107,37 @@ function findUbuntuVersion() {
} }
} }
function setupPath(rubyPrefix, ruby, toolsPath) {
// not used, saved for future
// const rubyPath = path.join(rubyPrefix, 'bin', 'ruby')
// const gemUserDir = cp.execSync(`${rubyPath} -e "puts Gem.user_dir"`).toString().trim()
const originalPath = process.env['PATH'].split(path.delimiter)
let cleanPath = originalPath.filter(e => !/\bruby\b/i.test(e))
if (cleanPath.length !== originalPath.length) {
console.log("Entries removed from PATH to avoid conflicts with Ruby:")
for (const entry of originalPath) {
if (!cleanPath.includes(entry)) {
console.log(` ${entry}`)
}
}
}
let newPath = [path.join(rubyPrefix, 'bin')]
if (ruby.startsWith('rubinius')) {
newPath.push(path.join(rubyPrefix, 'gems', 'bin'))
}
if (toolsPath) { newPath.push(toolsPath) }
if (ruby.startsWith('rubinius')) {
newPath.push(path.join(rubyPrefix, 'gems', 'bin'))
}
core.exportVariable('PATH', [...newPath, ...cleanPath].join(path.delimiter))
}
run() run()
+1 -12
View File
@@ -1,6 +1,5 @@
const os = require('os') const os = require('os')
const path = require('path') const path = require('path')
const core = require('@actions/core')
const exec = require('@actions/exec') const exec = require('@actions/exec')
const io = require('@actions/io') const io = require('@actions/io')
const tc = require('@actions/tool-cache') const tc = require('@actions/tool-cache')
@@ -15,17 +14,7 @@ export function getAvailableVersions(platform, engine) {
export async function install(platform, ruby) { export async function install(platform, ruby) {
const rubyPrefix = await downloadAndExtract(platform, ruby) const rubyPrefix = await downloadAndExtract(platform, ruby)
return [rubyPrefix, null]
if (platform === 'windows-latest') {
require('./windows').setupPath(undefined, rubyPrefix)
} else {
core.addPath(path.join(rubyPrefix, 'bin'))
if (ruby.startsWith('rubinius')) {
core.addPath(path.join(rubyPrefix, 'gems', 'bin'))
}
}
return rubyPrefix
} }
async function downloadAndExtract(platform, ruby) { async function downloadAndExtract(platform, ruby) {
+59 -47
View File
@@ -2,6 +2,7 @@
// https://github.com/MSP-Greg/actions-ruby/blob/master/lib/main.js // https://github.com/MSP-Greg/actions-ruby/blob/master/lib/main.js
const fs = require('fs') const fs = require('fs')
const cp = require('child_process')
const core = require('@actions/core') const core = require('@actions/core')
const exec = require('@actions/exec') const exec = require('@actions/exec')
const tc = require('@actions/tool-cache') const tc = require('@actions/tool-cache')
@@ -32,23 +33,44 @@ export async function install(platform, ruby) {
await exec.exec('7z', ['x', downloadPath, `-xr!${base}\\share\\doc`, `-o${drive}:\\`], { silent: true }) await exec.exec('7z', ['x', downloadPath, `-xr!${base}\\share\\doc`, `-o${drive}:\\`], { silent: true })
const rubyPrefix = `${drive}:\\${base}` const rubyPrefix = `${drive}:\\${base}`
const [hostedRuby, msys2] = await linkMSYS2() // we use certs and embedded MSYS2 from hostedRuby
setupPath((ruby === 'ruby-mswin' ? null : msys2), rubyPrefix) const hostedRuby = latestHostedRuby()
if (version.startsWith('2.2') || version.startsWith('2.3')) { let toolsPath = (version === 'mswin') ?
core.exportVariable('SSL_CERT_FILE', `${hostedRuby}\\ssl\\cert.pem`) await setupMSWin(hostedRuby) : await setupMingw(hostedRuby, version)
} else if (version === 'mswin') {
setupMSWin(hostedRuby)
}
if (!fs.existsSync(`${rubyPrefix}\\bin\\bundle.cmd`)) { if (!fs.existsSync(`${rubyPrefix}\\bin\\bundle.cmd`)) {
await exec.exec(`${rubyPrefix}\\bin\\gem install bundler -v "~> 1" --no-document`) await exec.exec(`${rubyPrefix}\\bin\\gem install bundler -v "~> 1" --no-document`)
} }
return rubyPrefix return [rubyPrefix, toolsPath]
} }
function setupMSWin(hostedRuby) { function latestHostedRuby() {
const toolCacheVersions = tc.findAllVersions('Ruby')
toolCacheVersions.sort()
if (toolCacheVersions.length === 0) {
throw new Error('Could not find MSYS2 in the toolcache')
}
const latestVersion = toolCacheVersions.slice(-1)[0]
return tc.find('Ruby', latestVersion)
}
async function setupMingw(hostedRuby, version) {
if (version.startsWith('2.2') || version.startsWith('2.3')) {
core.exportVariable('SSL_CERT_FILE', `${hostedRuby}\\ssl\\cert.pem`)
}
// Link to embedded MSYS2 in hostedRuby
const msys2 = 'C:\\msys64'
if (!fs.existsSync(msys2)) {
const hostedMSYS2 = `${hostedRuby}\\msys64`
await exec.exec(`cmd /c mklink /D ${msys2} ${hostedMSYS2}`)
}
return `${msys2}\\mingw64\\bin;${msys2}\\usr\\bin`
}
async function setupMSWin(hostedRuby) {
// All standard MSVC OpenSSL builds use C:\Program Files\Common Files\SSL // All standard MSVC OpenSSL builds use C:\Program Files\Common Files\SSL
const certsDir = 'C:\\Program Files\\Common Files\\SSL\\certs' const certsDir = 'C:\\Program Files\\Common Files\\SSL\\certs'
if (!fs.existsSync(certsDir)) { if (!fs.existsSync(certsDir)) {
@@ -61,51 +83,41 @@ function setupMSWin(hostedRuby) {
const hostedCert = `${hostedRuby}\\ssl\\cert.pem` const hostedCert = `${hostedRuby}\\ssl\\cert.pem`
fs.copyFileSync(hostedCert, cert) fs.copyFileSync(hostedCert, cert)
} }
return addVCVARSEnv()
// Add a convenience VCVARS environment variable to ease setting up MSVC
// This assumes a single Visual Studio version being available in the windows-latest image
core.exportVariable('VCVARS', '"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat"')
} }
async function linkMSYS2() { /* Sets msvc environment for use in Actions
const toolCacheVersions = tc.findAllVersions('Ruby') * allows steps to run without running vcvars*.bat, also allows using PS scripts
toolCacheVersions.sort() * adds a convenience VCVARS environment variable
if (toolCacheVersions.length === 0) { * this assumes a single Visual Studio version being available in the windows-latest image
throw new Error('Could not find MSYS2 in the toolcache') */
} export function addVCVARSEnv() {
const latestVersion = toolCacheVersions.slice(-1)[0] const vcVars = '"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat"'
const latestHostedRuby = tc.find('Ruby', latestVersion) core.exportVariable('VCVARS', vcVars)
const hostedMSYS2 = `${latestHostedRuby}\\msys64` let newEnv = new Map()
const msys2 = 'C:\\msys64'
if (!fs.existsSync(msys2)) {
await exec.exec(`cmd /c mklink /D ${msys2} ${hostedMSYS2}`)
}
return [latestHostedRuby, msys2]
}
export function setupPath(msys2, rubyPrefix) { let cmd = `cmd.exe /c "${vcVars} && set"`
const originalPath = process.env['PATH'].split(';')
let path = originalPath.slice()
// Remove default Ruby in PATH let newSet = cp.execSync(cmd).toString().trim().split(/\r?\n/)
path = path.filter(e => !e.match(/\bRuby\b/))
if (msys2) { newSet = newSet.filter(line => line.match(/\S=\S/))
// Add MSYS2 in PATH
path.unshift(`${msys2}\\mingw64\\bin`, `${msys2}\\usr\\bin`)
}
// Add the downloaded Ruby in PATH newSet.forEach(s => {
path.unshift(`${rubyPrefix}\\bin`) let [k,v] = s.split('=', 2)
newEnv.set(k,v)
})
console.log("Entries removed from PATH to avoid conflicts with Ruby:") let pathAdd
for (const entry of originalPath) {
if (!path.includes(entry)) { newEnv.forEach( (v, k, ) => {
console.log(entry) if (process.env[k] !== v) {
if (k === 'Path') {
pathAdd = v.replace(process.env['Path'], '')
} else {
core.exportVariable(k, v)
}
} }
} })
return pathAdd
const newPath = path.join(';')
core.exportVariable('PATH', newPath)
} }