generated from version-fox/vfox-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpre_install.lua
47 lines (45 loc) · 1.75 KB
/
pre_install.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
local http = require("http")
local json = require("json")
local foojay = require("foojay")
local distribution_version_parser = require("distribution_version")
--- Returns some pre-installed information, such as version number, download address, local files, etc.
--- If checksum is provided, vfox will automatically check it for you.
--- @param ctx table
--- @field ctx.version string User-input version
--- @return table Version information
function PLUGIN:PreInstall(ctx)
local distribution_version = distribution_version_parser.parse_version(ctx.version)
if not distribution_version then
error("Could not extract a valid distribution: " .. ctx.version)
end
local jdks = foojay.fetchtJdkList(distribution_version.distribution.name, distribution_version.version)
if not #jdks then
return {}
end
local jdk = jdks[1]
local info = json.decode(httpGet(jdk.links.pkg_info_uri, "Failed to fetch jdk info")).result[1]
-- TODO: checksum
-- local checksum = info.checksum
-- if checksum == "" and info.checksum_uri ~= "" then
-- checksum = httpGet(info.checksum_uri, "Failed to fetch checksum")
-- end
local finalV = distribution_version.distribution.short_name == "open" and jdk.java_version or jdk.java_version .. "-" .. distribution_version.distribution.short_name
return {
-- [info.checksum_type] = checksum,
url = info.direct_download_uri,
version = finalV,
note = jdk.major_version
}
end
function httpGet(url, errMsg)
local resp, err = http.get({
url = url
})
if err ~= nil then
error(errMsg .. ": " .. err)
end
if resp.status_code ~= 200 then
error(errMsg .. ": status_code =>" .. resp.status_code)
end
return resp.body
end