generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
index.js
225 lines (193 loc) · 8.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const os = require('os')
const fs = require('fs')
const path = require('path')
const core = require('@actions/core')
const exec = require('@actions/exec')
const common = require('./common')
const rubygems = require('./rubygems')
const bundler = require('./bundler')
const windows = common.windows
const inputDefaults = {
'ruby-version': 'default',
'rubygems': 'default',
'bundler': 'Gemfile.lock',
'bundler-cache': 'false',
'working-directory': '.',
'cache-version': bundler.DEFAULT_CACHE_VERSION,
'self-hosted': 'false',
'windows-toolchain': 'default',
}
// entry point when this action is run on its own
export async function run() {
try {
await setupRuby()
} catch (error) {
if (/\bprocess\b.+\bfailed\b/.test(error.message)) {
core.setFailed(error.message)
} else {
core.setFailed(error.stack)
}
}
// Explicit process.exit() to not wait hanging promises,
// see https://github.com/ruby/setup-ruby/issues/543
process.exit()
}
// entry point when this action is run from other actions
export async function setupRuby(options = {}) {
const inputs = { ...options }
for (const key in inputDefaults) {
if (!Object.prototype.hasOwnProperty.call(inputs, key)) {
inputs[key] = core.getInput(key) || inputDefaults[key]
}
}
common.inputs.selfHosted = inputs['self-hosted']
process.chdir(inputs['working-directory'])
const platform = common.getOSNameVersion()
const [engine, parsedVersion] = parseRubyEngineAndVersion(inputs['ruby-version'])
let installer
if (platform.startsWith('windows-') && engine === 'ruby' && !common.isSelfHostedRunner()) {
installer = require('./windows')
} else {
installer = require('./ruby-builder')
}
let version
if (common.isSelfHostedRunner()) {
// The list of available Rubies in the hostedtoolcache is unrelated to getAvailableVersions()
version = parsedVersion
} else {
const engineVersions = installer.getAvailableVersions(platform, engine)
version = validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion)
}
createGemRC(engine, version)
envPreInstall()
// JRuby can use compiled extension code, so make sure gcc exists.
// As of Jan-2022, JRuby compiles against msvcrt.
if (platform.startsWith('windows') && engine === 'jruby' &&
!fs.existsSync('C:\\msys64\\mingw64\\bin\\gcc.exe')) {
await require('./windows').installJRubyTools()
}
const rubyPrefix = await installer.install(platform, engine, version)
await common.measure('Print Ruby version', async () =>
await exec.exec('ruby', ['--version']))
const rubygemsInputSet = inputs['rubygems'] !== 'default'
if (rubygemsInputSet) {
await common.measure('Updating RubyGems', async () =>
rubygems.rubygemsUpdate(inputs['rubygems'], rubyPrefix, platform, engine, version))
}
// When setup-ruby is used by other actions, this allows code in them to run
// before 'bundle install'. Installed dependencies may require additional
// libraries & headers, build tools, etc.
if (inputs['afterSetupPathHook'] instanceof Function) {
await inputs['afterSetupPathHook']({ platform, rubyPrefix, engine, version })
}
const [gemfile, lockFile] = bundler.detectGemfiles()
let bundlerVersion = 'unknown'
if (inputs['bundler'] !== 'none') {
bundlerVersion = await common.measure('Installing Bundler', async () =>
bundler.installBundler(inputs['bundler'], rubygemsInputSet, lockFile, platform, rubyPrefix, engine, version))
}
if (inputs['bundler-cache'] === 'true') {
await common.time('bundle install', async () =>
bundler.bundleInstall(gemfile, lockFile, platform, engine, version, bundlerVersion, inputs['cache-version']))
}
core.setOutput('ruby-prefix', rubyPrefix)
}
function parseRubyEngineAndVersion(rubyVersion) {
if (rubyVersion === 'default') {
if (fs.existsSync('.ruby-version')) {
rubyVersion = '.ruby-version'
} else if (fs.existsSync('.tool-versions')) {
rubyVersion = '.tool-versions'
} else {
throw new Error('input ruby-version needs to be specified if no .ruby-version or .tool-versions file exists')
}
}
if (rubyVersion === '.ruby-version') { // Read from .ruby-version
rubyVersion = fs.readFileSync('.ruby-version', 'utf8').trim()
console.log(`Using ${rubyVersion} as input from file .ruby-version`)
} else if (rubyVersion === '.tool-versions') { // Read from .tool-versions
const toolVersions = fs.readFileSync('.tool-versions', 'utf8').trim()
const rubyLine = toolVersions.split(/\r?\n/).filter(e => /^ruby\s/.test(e))[0]
rubyVersion = rubyLine.match(/^ruby\s+(.+)$/)[1]
console.log(`Using ${rubyVersion} as input from file .tool-versions`)
}
let engine, version
if (/^(\d+)/.test(rubyVersion) || common.isHeadVersion(rubyVersion)) { // X.Y.Z => ruby-X.Y.Z
engine = 'ruby'
version = rubyVersion
} else if (!rubyVersion.includes('-')) { // myruby -> myruby-stableVersion
engine = rubyVersion
version = '' // Let the logic in validateRubyEngineAndVersion() find the version
} else { // engine-X.Y.Z
[engine, version] = common.partition(rubyVersion, '-')
}
return [engine, version]
}
function validateRubyEngineAndVersion(platform, engineVersions, engine, parsedVersion) {
if (!engineVersions) {
throw new Error(`Unknown engine ${engine} on ${platform}`)
}
let version = parsedVersion
if (!engineVersions.includes(parsedVersion)) {
const latestToFirstVersion = engineVersions.slice().reverse()
// Try to match stable versions first, so an empty version (engine-only) matches the latest stable version
let found = latestToFirstVersion.find(v => common.isStableVersion(engine, v) && v.startsWith(parsedVersion))
if (!found) {
// Exclude head versions, they must be exact matches
found = latestToFirstVersion.find(v => !common.isHeadVersion(v) && v.startsWith(parsedVersion))
}
if (found) {
version = found
} else {
throw new Error(`Unknown version ${parsedVersion} for ${engine} on ${platform}
Available versions for ${engine} on ${platform}: ${engineVersions.join(', ')}
Make sure you use the latest version of the action with - uses: ruby/setup-ruby@v1`)
}
}
// Well known version-platform combinations which do not work:
if (engine === 'ruby' && platform.startsWith('macos') && os.arch() === 'arm64' && common.floatVersion(version) < 2.6) {
throw new Error(`CRuby < 2.6 does not support macos-arm64.
Either use a newer Ruby version or use a macOS image running on amd64, e.g., macos-13 or macos-12.
Note that GitHub changed the meaning of macos-latest from macos-12 (amd64) to macos-14 (arm64):
https://github.blog/changelog/2024-04-01-macos-14-sonoma-is-generally-available-and-the-latest-macos-runner-image/
If you are using a matrix of Ruby versions, a good solution is to run only < 2.6 on amd64, like so:
matrix:
ruby: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3']
os: [ ubuntu-latest, macos-latest ]
# CRuby < 2.6 does not support macos-arm64, so test those on amd64 instead
exclude:
- { os: macos-latest, ruby: '2.4' }
- { os: macos-latest, ruby: '2.5' }
include:
- { os: macos-13, ruby: '2.4' }
- { os: macos-13, ruby: '2.5' }
But of course you should consider dropping support for these long-EOL Rubies, which cannot even be built on recent macOS machines.`)
} else if (engine === 'truffleruby' && platform.startsWith('windows')) {
throw new Error('TruffleRuby does not currently support Windows.')
}
return version
}
function createGemRC(engine, version) {
const gemrc = path.join(os.homedir(), '.gemrc')
if (!fs.existsSync(gemrc)) {
if (engine === 'ruby' && common.floatVersion(version) < 2.0) {
fs.writeFileSync(gemrc, `install: --no-rdoc --no-ri${os.EOL}update: --no-rdoc --no-ri${os.EOL}`)
} else {
fs.writeFileSync(gemrc, `gem: --no-document${os.EOL}`)
}
}
}
// sets up ENV variables
// currently only used on Windows runners
function envPreInstall() {
const ENV = process.env
if (windows) {
// puts normal Ruby temp folder on SSD
core.exportVariable('TMPDIR', ENV['RUNNER_TEMP'])
// bash - sets home to match native windows, normally C:\Users\<user name>
core.exportVariable('HOME', ENV['HOMEDRIVE'] + ENV['HOMEPATH'])
// bash - needed to maintain Path from Windows
core.exportVariable('MSYS2_PATH_TYPE', 'inherit')
}
}
if (__filename.endsWith('index.js')) { run() }