Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Use "ridk enable" like variable setup and add UCRT #197

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 28 additions & 6 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs')
const util = require('util')
const stream = require('stream')
const crypto = require('crypto')
const child_process = require('child_process')
const core = require('@actions/core')
const { performance } = require('perf_hooks')

Expand Down Expand Up @@ -149,6 +150,13 @@ export function win2nix(path) {
return path.replace(/\\/g, '/').replace(/ /g, '\\ ')
}

// JRuby is installed after setupPath is called, so folder doesn't exist
export function rubyIsUCRT(path) {
return !!(fs.existsSync(path) &&
fs.readdirSync(path, { withFileTypes: true }).find(dirent =>
dirent.isFile() && dirent.name.match(/^x64-ucrt-ruby\d{3}\.dll$/)))
}

export function setupPath(newPathEntries) {
const envPath = windows ? 'Path' : 'PATH'
const originalPath = process.env[envPath].split(path.delimiter)
Expand All @@ -168,13 +176,27 @@ export function setupPath(newPathEntries) {
}

// Then add new path entries using core.addPath()
let newPath
let newPath = newPathEntries
if (windows) {
// add MSYS2 in path for all Rubies on Windows, as it provides a better bash shell and a native toolchain
const msys2 = ['C:\\msys64\\mingw64\\bin', 'C:\\msys64\\usr\\bin']
newPath = [...newPathEntries, ...msys2]
} else {
newPath = newPathEntries
try {
// Use RubyInstaller mechanisms to set various evenironment variables including the PATH to MSYS2 tools
// Same as "ridk enable" on the command line
const envbuf = child_process.execFileSync(`${newPathEntries[0]}\\ruby`, ['-rruby_installer/runtime', '-e', 'puts RubyInstaller::Runtime.msys2_installation.enable_msys_apps_per_cmd'])
const envvars = envbuf.toString().trim().split(/\r?\n/)

envvars.forEach( (envvar) => {
console.log(`SET ${envvar}`)
const parts = envvar.split("=", 2)
core.exportVariable(parts[0], parts[1])
})
} catch (ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should at minimum log the exception here to help debug if anything goes wrong.
I think more reliable would be to have no fallback, do we need this fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, a message would be good.
I think the fallback is used on the ruby-loco mswin version. It doesn't ship the RubyInstaller::Runtime library. But both ruby-loco mingw and RubyInstaller have it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think best to have a if !mswin then and no catch

// main Ruby dll determines whether mingw or ucrt build
let build_sys = rubyIsUCRT(newPathEntries[0]) ? 'ucrt64' : 'mingw64'

// add MSYS2 in path for all Rubies on Windows, as it provides a better bash shell and a native toolchain
const msys2 = [`C:\\msys64\\${build_sys}\\bin`, 'C:\\msys64\\usr\\bin']
newPath = [...newPathEntries, ...msys2]
}
}
console.log(`Entries added to ${envPath} to use selected Ruby:`)
for (const entry of newPath) {
Expand Down
49 changes: 41 additions & 8 deletions dist/index.js

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

14 changes: 12 additions & 2 deletions windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ export async function install(platform, engine, version) {

let toolchainPaths = (version === 'mswin') ? await setupMSWin() : await setupMingw(version)

common.setupPath([`${rubyPrefix}\\bin`, ...toolchainPaths])

if (!inToolCache) {
await downloadAndExtract(engine, version, url, base, rubyPrefix);
}

common.setupPath([`${rubyPrefix}\\bin`, ...toolchainPaths])

if (common.rubyIsUCRT(`${rubyPrefix}\\bin`)) {
await installUCRT()
}

return rubyPrefix
}

Expand All @@ -79,6 +83,12 @@ async function downloadAndExtract(engine, version, url, base, rubyPrefix) {
}
}

async function installUCRT() {
await common.measure('Installing MSYS2 UCRT build tools', async () => {
cp.execSync(`pacman -S --noconfirm --noprogressbar --needed %MINGW_PACKAGE_PREFIX%-gcc`)
})
}

async function setupMingw(version) {
core.exportVariable('MAKE', 'make.exe')

Expand Down