generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
index.js
116 lines (100 loc) · 3.62 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
const os = require('os')
const fs = require('fs')
const core = require('@actions/core')
const io = require('@actions/io')
const tc = require('@actions/tool-cache')
const axios = require('axios')
const windows = require('./windows')
const releasesURL = 'https://github.com/eregon/ruby-install-builder/releases'
const metadataURL = 'https://raw.githubusercontent.com/eregon/ruby-install-builder/metadata'
async function run() {
try {
const platform = getVirtualEnvironmentName()
const ruby = await getRubyEngineAndVersion(core.getInput('ruby-version'))
let rubyPrefix
if (platform === 'windows-latest') {
rubyPrefix = await windows.downloadExtractAndSetPATH(ruby)
} else {
if (ruby.startsWith('jruby')) {
// Workaround for https://github.com/actions/virtual-environments/issues/242
core.exportVariable('CLASSPATH', '')
}
rubyPrefix = await downloadAndExtract(platform, ruby)
core.addPath(`${rubyPrefix}/bin`)
}
core.setOutput('ruby-prefix', rubyPrefix)
} catch (error) {
core.setFailed(error.message)
}
}
async function downloadAndExtract(platform, ruby) {
const rubiesDir = `${process.env.HOME}/.rubies`
await io.mkdirP(rubiesDir)
const tag = await getLatestReleaseTag()
const url = `${releasesURL}/download/${tag}/${ruby}-${platform}.tar.gz`
console.log(url)
const downloadPath = await tc.downloadTool(url)
await tc.extractTar(downloadPath, rubiesDir)
return `${rubiesDir}/${ruby}`
}
async function getLatestReleaseTag() {
const response = await axios.get(`${metadataURL}/latest_release.tag`)
return response.data.trim()
}
async function getRubyEngineAndVersion(rubyVersion) {
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`)
}
let engine, version
if (rubyVersion.match(/^\d+/)) { // X.Y.Z => ruby-X.Y.Z
engine = 'ruby'
version = rubyVersion
} else if (!rubyVersion.includes('-')) { // myruby -> myruby-stableVersion
engine = rubyVersion
version = '' // Let the logic below find the version
} else { // engine-X.Y.Z
[engine, version] = rubyVersion.split('-', 2)
}
const response = await axios.get(`${metadataURL}/versions.json`)
const stableVersions = response.data
const engineVersions = stableVersions[engine]
if (!engineVersions) {
throw new Error(`Unknown engine ${engine} (input: ${rubyVersion})`)
}
if (!engineVersions.includes(version)) {
const latestToFirstVersion = engineVersions.slice().reverse()
const found = latestToFirstVersion.find(v => v.startsWith(version))
if (found) {
version = found
} else {
throw new Error(`Unknown version ${version} for ${engine}
input: ${rubyVersion}
available versions for ${engine}: ${engineVersions.join(', ')}
File an issue at https://github.com/eregon/ruby-install-builder/issues if would like support for a new version`)
}
}
return engine + '-' + version
}
function getVirtualEnvironmentName() {
const platform = os.platform()
if (platform == 'linux') {
return `ubuntu-${findUbuntuVersion()}`
} else if (platform == 'darwin') {
return 'macos-latest'
} else if (platform == 'win32') {
return 'windows-latest'
} else {
throw new Error(`Unknown platform ${platform}`)
}
}
function findUbuntuVersion() {
const lsb_release = fs.readFileSync('/etc/lsb-release', 'utf8')
const match = lsb_release.match(/^DISTRIB_RELEASE=(\d+\.\d+)$/m)
if (match) {
return match[1]
} else {
throw new Error('Could not find Ubuntu version')
}
}
run()