forked from leafo/gh-actions-lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
196 lines (150 loc) · 5.48 KB
/
main.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
const core = require("@actions/core")
const exec = require("@actions/exec")
const io = require("@actions/io")
const tc = require("@actions/tool-cache")
const ch = require("@actions/cache")
const fsp = require("fs").promises
const path = require("path").posix
const INSTALL_PREFIX = ".install"
const LUA_PREFIX = ".lua"
const VERSION_ALIASES = {
"5.1": "5.1.5",
"5.2": "5.2.4",
"5.3": "5.3.6",
"5.4": "5.4.2",
"luajit": "luajit-2.1.0-beta3",
}
const isMacOS = () => (process.platform || "").startsWith("darwin")
// Returns posix path for process.cwd()
const processCwd = () => {
const p = require("path");
return process.cwd().split(p.sep).join(p.posix.sep);
}
async function install_luajit_openresty(luaInstallPath) {
const installPath = path.join(processCwd(), INSTALL_PREFIX)
const luaCompileFlags = core.getInput('luaCompileFlags')
await io.mkdirP(installPath)
await exec.exec("git clone https://github.com/openresty/luajit2.git", undefined, {
cwd: installPath
})
const compileFlagsArray = [ "-j" ]
if (isMacOS()) {
compileFlagsArray.push("MACOSX_DEPLOYMENT_TARGET=10.15")
}
if (luaCompileFlags) {
compileFlagsArray.push(luaCompileFlags)
}
await exec.exec("make", compileFlagsArray, {
cwd: path.join(installPath, "luajit2")
})
await exec.exec(`make -j install PREFIX="${luaInstallPath}"`, undefined, {
cwd: path.join(installPath, "luajit2")
})
await exec.exec("ln -s luajit lua", undefined, {
cwd: path.join(luaInstallPath, "bin")
})
}
async function install_luajit(luaInstallPath, luajitVersion) {
const luaExtractPath = path.join(processCwd(), INSTALL_PREFIX, `LuaJIT-${luajitVersion}`)
const luaCompileFlags = core.getInput('luaCompileFlags')
const luaSourceTar = await tc.downloadTool(`https://luajit.org/download/LuaJIT-${luajitVersion}.tar.gz`)
await io.mkdirP(luaExtractPath)
await tc.extractTar(luaSourceTar, INSTALL_PREFIX)
const compileFlagsArray = [ "-j" ]
if (isMacOS()) {
compileFlagsArray.push("MACOSX_DEPLOYMENT_TARGET=10.15")
}
if (luaCompileFlags) {
compileFlagsArray.push(luaCompileFlags)
}
await exec.exec("make", compileFlagsArray, {
cwd: luaExtractPath
})
await exec.exec(`make -j install PREFIX="${luaInstallPath}"`, undefined, {
cwd: luaExtractPath
})
await exec.exec(`ln -s luajit-${luajitVersion} lua`, undefined, {
cwd: path.join(luaInstallPath, "bin")
})
}
async function install_plain_lua(luaInstallPath, luaVersion) {
const luaExtractPath = path.join(processCwd(), INSTALL_PREFIX, `lua-${luaVersion}`)
const luaCompileFlags = core.getInput('luaCompileFlags')
const luaSourceTar = await tc.downloadTool(`https://www.lua.org/ftp/lua-${luaVersion}.tar.gz`)
await io.mkdirP(luaExtractPath)
await tc.extractTar(luaSourceTar, INSTALL_PREFIX)
if (isMacOS()) {
await exec.exec("brew install readline ncurses")
} else {
await exec.exec("sudo apt-get install -q libreadline-dev libncurses-dev", undefined, {
env: {
DEBIAN_FRONTEND: "noninteractive",
TERM: "linux"
}
})
}
const makefilePlatform = isMacOS() ? "macosx" : "linux"
const compileFlagsArray = [
"-j",
makefilePlatform,
]
if (luaCompileFlags) {
compileFlagsArray.push(luaCompileFlags)
}
await exec.exec("make", compileFlagsArray, {
cwd: luaExtractPath
})
await exec.exec(`make -j INSTALL_TOP="${luaInstallPath}" install`, undefined, {
cwd: luaExtractPath
})
}
async function install(luaInstallPath, luaVersion) {
if (luaVersion == "luajit-openresty") {
return await install_luajit_openresty(luaInstallPath)
}
if (luaVersion.startsWith("luajit-")) {
const luajitVersion = luaVersion.substr("luajit-".length)
return await install_luajit(luaInstallPath, luajitVersion)
}
return await install_plain_lua(luaInstallPath, luaVersion)
}
const makeCacheKey = luaVersion=> `setup-lua-${luaVersion}-${process.platform}-${process.arch}`
const exists = (filename, mode) => fsp.access(filename, mode).then(() => true, () => false)
async function main() {
let luaVersion = core.getInput('luaVersion', { required: true })
let luaCompileFlags = core.getInput('luaCompileFlags')
if (VERSION_ALIASES[luaVersion]) {
luaVersion = VERSION_ALIASES[luaVersion]
}
const luaInstallPath = path.join(processCwd(), LUA_PREFIX)
// The tool cache is mostly useful on self-hosted runners. It doesn't persist across jobs in Github's runners
let toolCacheDir = tc.find('lua', luaVersion)
if (!toolCacheDir) {
const cacheKey = makeCacheKey(luaVersion)
if (core.getInput('buildCache') == 'true') {
await ch.restoreCache([luaInstallPath], cacheKey) // @actions/cache does persist across jobs
}
if (!(await exists(luaInstallPath))) {
await install(luaInstallPath, luaVersion)
try {
await ch.saveCache([luaInstallPath], cacheKey)
} catch (ex) {
// This could happen due to a race condition, in which case it should be safe to ignore saving the cache
if (ex instanceof ch.ReserveCacheError) {
console.log(`Cannot save cache. (${ex.message}) Skipping...`)
} else {
throw ex
}
}
}
toolCacheDir = await tc.cacheDir(luaInstallPath, 'lua', luaVersion)
}
// If .lua doesn't exist, symlink it to the tool cache dir
if (toolCacheDir && !(await exists(luaInstallPath))) {
await fsp.symlink(toolCacheDir, luaInstallPath);
}
core.addPath(path.join(luaInstallPath, "bin"))
}
main().catch(err => {
core.setFailed(`Failed to install Lua: ${err}`);
})