|
| 1 | +// Most of this logic is from |
| 2 | +// https://github.com/MSP-Greg/actions-ruby/blob/master/lib/main.js |
| 3 | + |
| 4 | +const core = require('@actions/core') |
| 5 | +const exec = require('@actions/exec') |
| 6 | +const tc = require('@actions/tool-cache') |
| 7 | + |
| 8 | +const releasesURL = 'https://github.com/oneclick/rubyinstaller2/releases' |
| 9 | + |
| 10 | +export async function downloadExtractAndSetPATH(ruby) { |
| 11 | + const version = ruby.split('-', 2)[1] |
| 12 | + if (!ruby.startsWith('ruby-') || version.startsWith('2.3')) { |
| 13 | + throw new Error(`Only ruby >= 2.4 is supported on Windows currently (input: ${ruby})`) |
| 14 | + } |
| 15 | + const tag = `RubyInstaller-${version}-1` |
| 16 | + const base = `${tag.toLowerCase()}-x64` |
| 17 | + |
| 18 | + const url = `${releasesURL}/download/${tag}/${base}.7z` |
| 19 | + console.log(url) |
| 20 | + |
| 21 | + const downloadPath = await tc.downloadTool(url) |
| 22 | + await exec.exec(`7z x ${downloadPath} -xr!${base}\\share\\doc -oC:\\`) |
| 23 | + const rubyPrefix = `C:\\${base}` |
| 24 | + |
| 25 | + const msys2 = await linkMSYS2() |
| 26 | + const newPath = setupPath(msys2, rubyPrefix) |
| 27 | + core.exportVariable('PATH', newPath) |
| 28 | + |
| 29 | + return rubyPrefix |
| 30 | +} |
| 31 | + |
| 32 | +async function linkMSYS2() { |
| 33 | + const toolCacheVersions = tc.findAllVersions('Ruby') |
| 34 | + toolCacheVersions.sort() |
| 35 | + const latestVersion = toolCacheVersions.slice(-1)[0] |
| 36 | + const latestHostedRuby = tc.find('Ruby', latestVersion) |
| 37 | + |
| 38 | + const hostedMSYS2 = `${latestHostedRuby}\\msys64` |
| 39 | + const msys2 = 'C:\\msys64' |
| 40 | + await exec.exec(`cmd /c mklink /D ${msys2} ${hostedMSYS2}`) |
| 41 | + return msys2 |
| 42 | +} |
| 43 | + |
| 44 | +function setupPath(msys2, rubyPrefix) { |
| 45 | + let path = process.env['PATH'].split(';') |
| 46 | + |
| 47 | + // Remove conflicting dev tools from PATH |
| 48 | + path = path.filter(e => !e.match(/\b(Chocolatey|CMake|mingw64|OpenSSL|Strawberry)\b/)) |
| 49 | + |
| 50 | + // Remove default Ruby in PATH |
| 51 | + path = path.filter(e => !e.match(/\bRuby\b/)) |
| 52 | + |
| 53 | + // Add MSYS2 in PATH |
| 54 | + path.unshift(`${msys2}\\mingw64`, `${msys2}\\usr`) |
| 55 | + |
| 56 | + // Add the downloaded Ruby in PATH |
| 57 | + path.unshift(`${rubyPrefix}\\bin`) |
| 58 | + |
| 59 | + return path.join(';') |
| 60 | +} |
0 commit comments