forked from ruby/setup-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
191 lines (162 loc) · 6.68 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
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',
}
// 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)
}
}
}
// 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.getVirtualEnvironmentName()
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')
}
const engineVersions = installer.getAvailableVersions(platform, engine)
const 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))
}
// 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(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`)
}
}
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() }