-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
447 lines (389 loc) · 13.7 KB
/
util.lua
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
local http = require("http")
local json = require("json")
local strings = require("vfox.strings")
function fetchVersions()
local rubyVersions, jrubyVersions, homebrewRubyVersions
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local resp, err = http.get({
url = githubURL:gsub("/$", "") .. "/yanecc/vfox-ruby/releases/manifest",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get versions: " .. err .. "\nstatus_code => " .. resp.status_code)
end
local versionList = resp.body:match("<code>(.-)</code>")
local versionJson = json.decode(versionList)
rubyVersions = versionJson.ruby
jrubyVersions = versionJson.jruby
homebrewRubyVersions = versionJson.homebrew
return rubyVersions, jrubyVersions, homebrewRubyVersions
end
local RubyVersions, JRubyVersions, HomebrewRubyVersions = fetchVersions()
-- available.lua
function fetchAvailable(noCache)
local result
if noCache then
clearCache()
end
if RUNTIME.osType == "windows" then
result = fetchForWindows()
else
result = fetchForUnix()
end
for _, v in ipairs(JRubyVersions) do
table.insert(result, {
version = v,
note = "jruby",
})
end
return result
end
function clearCache()
os.remove(RUNTIME.pluginDirPath .. "/available.cache")
end
function fetchForWindows()
local result = {}
local versions = {}
local resp, err = http.get({
url = "https://rubyinstaller.org/downloads/archives/",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get Ruby versions: " .. err .. "\nstatus_code => " .. resp.status_code)
end
for version in resp.body:gmatch('7z">Ruby (%d.%d.%d+)%-1 %(x64%)') do
table.insert(versions, version)
end
sortVersions(versions)
for i, v in ipairs(versions) do
if i == 1 then
table.insert(result, {
version = v,
note = "latest",
})
else
table.insert(result, {
version = v,
})
end
end
return result
end
function sortVersions(versions)
table.sort(versions, function(a, b)
return compareVersion(a, b) == 1
end)
end
function compareVersion(currentVersion, targetVersion)
local currentVersionArray = strings.split(currentVersion, ".")
local compareVersionArray = strings.split(targetVersion, ".")
for i, v in ipairs(compareVersionArray) do
if tonumber(currentVersionArray[i]) > tonumber(v) then
return 1
elseif tonumber(currentVersionArray[i]) < tonumber(v) then
return -1
end
end
return 0
end
function fetchForUnix()
local result = {}
for i, v in ipairs(RubyVersions) do
if i == 1 then
table.insert(result, {
version = v,
note = "latest",
})
elseif compareVersion(v, "20.0.0") >= 0 then
table.insert(result, {
version = v,
note = "truffleruby",
})
else
table.insert(result, {
version = v,
})
end
end
return result
end
-- pre_install.lua
function getDownloadInfo(version)
local file, sha256
if version == "latest" then
version = getLatestVersion()
end
file, sha256 = generateURL(version, RUNTIME.osType, RUNTIME.archType)
return file, version, sha256
end
function getLatestVersion()
local version
if RUNTIME.osType == "windows" then
local resp, err = http.get({
url = "https://rubyinstaller.org/downloads/",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get latest version: " .. err .. "\nstatus_code => " .. resp.status_code)
end
version = resp.body:match("Ruby (%d.%d.%d+)%-1 %(x64%)")
else
version = RubyVersions[1]
end
return version
end
function generateURL(version, osType, archType)
local file, sha256
if hasValue(JRubyVersions, version) then
file, sha256 = generateJRuby(version)
elseif osType == "windows" then
file, sha256 = generateWindowsRuby(version, archType)
elseif version:match("^[1-3]%.%d%.%d%-?%w*%.m?rb$") then
file = generateRubyBuild()
elseif osType ~= "darwin" and osType ~= "linux" then
print("Unsupported OS: " .. osType)
os.exit(1)
elseif not hasValue(RubyVersions, version) then
print("Unsupported version: " .. version)
os.exit(1)
elseif hasValue(HomebrewRubyVersions, version) then
file = generateHomebrewRuby(version, osType, archType)
elseif compareVersion(version, "20.0.0") >= 0 then
file = generateTruffleRuby(version, osType, archType)
end
return file, sha256
end
function generateJRuby(version)
local file, sha256
if os.getenv("GITHUB_URL") then
file = os.getenv("GITHUB_URL"):gsub("/$", "") .. "/jruby/jruby/releases/download/%s/jruby-bin-%s.tar.gz"
else
file = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/%s/jruby-dist-%s-bin.tar.gz"
end
file = file:format(version, version)
sha256 = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/%s/jruby-dist-%s-bin.tar.gz.sha256"
local resp, err = http.get({
url = sha256:format(version, version),
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get sha256: " .. err .. "\nstatus_code => " .. resp.status_code)
end
sha256 = resp.body
return file, sha256
end
function generateWindowsRuby(version, archType)
local file, sha256
local bit = archType == "amd64" and "64" or "86"
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
file = githubURL:gsub("/$", "")
.. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/rubyinstaller-%s-1-x%s.7z"
file = file:format(version, version, bit)
local resp, err = http.get({
url = "https://rubyinstaller.org/downloads/archives/",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get sha256: " .. err .. "\nstatus_code => " .. resp.status_code)
end
sha256 = resp.body:match(version .. "%-1%-x" .. bit .. '.7z[%s%S]-value="([0-9a-z]+)')
return file, sha256
end
function generateRubyBuild()
local file
local latestRubyBuild = "https://api.github.com/repos/rbenv/ruby-build/releases/latest"
local resp, err = http.get({
url = latestRubyBuild,
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get latest ruby-build: " .. err .. "\nstatus_code => " .. resp.status_code)
end
latestRubyBuild = json.decode(resp.body)
file = latestRubyBuild.tarball_url .. "#/ruby-build.tar.gz"
return file
end
function hasValue(table, value)
for _, v in ipairs(table) do
if v == value then
return true
end
end
return false
end
function generateHomebrewRuby(version, osType, archType)
local file
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local releaseURL = githubURL:gsub("/$", "") .. "/Homebrew/homebrew-portable-ruby/releases/"
if osType == "linux" and archType == "amd64" then
file = releaseURL .. "download/%s/portable-ruby-%s.x86_64_linux.bottle.tar.gz"
elseif osType == "darwin" and archType == "amd64" then
file = releaseURL .. "download/%s/portable-ruby-%s.el_capitan.bottle.tar.gz"
elseif osType == "darwin" and archType == "arm64" then
file = releaseURL .. "download/%s/portable-ruby-%s.arm64_big_sur.bottle.tar.gz"
else
print("Unsupported environment: " .. osType .. "-" .. archType)
os.exit(1)
end
file = file:format(version, version)
return file
end
function generateTruffleRuby(version, osType, archType)
local tag, file
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local releaseURL = githubURL:gsub("/$", "") .. "/oracle/truffleruby/releases/"
if compareVersion(version, "22.2.0") < 0 and archType == "arm64" then
print("Unsupported version " .. version .. " for " .. archType)
os.exit(1)
end
if archType == "arm64" then
archType = "aarch64"
end
if osType == "darwin" then
osType = "macos"
end
if version:sub(-4) == ".jvm" and compareVersion(version, "23.1.0") >= 0 then
version = version:gsub("%.jvm$", "")
file = releaseURL .. "download/%s/truffleruby-jvm-%s-%s-%s.tar.gz"
else
file = releaseURL .. "download/%s/truffleruby-%s-%s-%s.tar.gz"
end
tag = compareVersion(version, "23.0.0") >= 0 and "graal-" .. version or "vm-" .. version
file = file:format(tag, version, osType, archType)
return file
end
-- post_install.lua
function unixInstall(rootPath, path, version)
if hasValue(HomebrewRubyVersions, version) then
patchHomebrewRuby(path, version)
elseif hasValue(JRubyVersions, version) then
patchJRuby(path)
elseif version:match("%.m?rb$") then
patchRubyBuild(path, version)
elseif compareVersion(version, "20.0.0") >= 0 then
patchTruffleRuby(path)
else
mambaInstall(rootPath, path, version)
end
end
function patchHomebrewRuby(path, version)
local command1 = "mv " .. path .. "/" .. version .. "/* " .. path
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
local command3 = "rm -rf " .. path .. "/.brew " .. path .. "/" .. version
for _, command in ipairs({ command1, command2, command3 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end
end
function patchRubyBuild(path, version)
version = version:gsub("%.mrb$", "")
version = version:sub(-3) == ".rb" and version:sub(1, -4) or "mruby-" .. version
local builder = path .. "/../ruby-build"
local command1 = "mkdir -p " .. builder
local command2 = "mv " .. path .. "/* " .. path .. "/.git* " .. builder
local command3 = builder .. "/bin/ruby-build " .. version .. " " .. path .. " > /dev/null"
local command4 = "mkdir -p " .. path .. "/share/gems/bin"
local command5 = "rm -rf " .. builder
for _, command in ipairs({ command1, command2, command3, command4, command5 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end
end
function patchTruffleRuby(path)
local command1 = path .. "/lib/truffle/post_install_hook.sh > /dev/null"
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
local command3 = "rm -rf " .. path .. "/src"
for _, command in ipairs({ command1, command2, command3 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end
end
function patchJRuby(path)
local command1 = "mkdir -p " .. path .. "/share/gems/bin"
local command2 = "rm -f " .. path .. "/bin/*.{exe,bat,dll}"
for _, command in ipairs({ command1, command2 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end
end
function mambaInstall(rootPath, path, version)
local conda = rootPath .. "/conda"
local mamba = rootPath .. "/conda/micromamba"
local condaForge = os.getenv("Conda_Forge") or "conda-forge"
local command1 = "mv " .. path .. " " .. conda
local command2 = "chmod +x " .. mamba
local command3 = mamba .. " create -yqp " .. path .. " -r " .. conda .. " ruby=" .. version .. " -c " .. condaForge
local command4 = "mkdir -p " .. path .. "/share/gems/bin"
local command5 = "rm -rf " .. path .. "/etc " .. path .. "/conda-meta " .. conda
downloadMicroMamba(path .. "/micromamba")
for _, command in ipairs({ command1, command2, command3, command4, command5 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end
end
function downloadMicroMamba(path)
local file = generateMicroMamba(RUNTIME.osType, RUNTIME.archType)
local err = http.download_file({
url = file,
}, path)
if err ~= nil then
error("Failed to download micromamba: " .. err)
end
end
function generateMicroMamba(osType, archType)
local file
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
if osType == "darwin" then
osType = "osx"
end
if archType == "amd64" then
archType = "64"
elseif archType == "arm64" then
if osType == "linux" then
archType = "aarch64"
end
elseif archType == "ppc64le" and osType == "linux" then
else
print("Unsupported environment: " .. osType .. "-" .. archType)
os.exit(1)
end
file = githubURL:gsub("/$", "") .. "/mamba-org/micromamba-releases/releases/latest/download/micromamba-%s-%s"
file = file:format(osType, archType)
return file
end
function makeGemsPath(path)
local gemsPath = path .. "\\share\\gems\\bin"
local command = "mkdir " .. gemsPath
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end