Skip to content

Commit 9e650fb

Browse files
tisoftaooohanCopilot
authored
feat: support asdf formated version strings (#17)
* feat: support asdf formated version strings * allow short and long names for distributions * allow the distribution to prefix or suffix the version * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update hooks/available.lua Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Han Li <aooohan@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 93aad44 commit 9e650fb

File tree

6 files changed

+140
-46
lines changed

6 files changed

+140
-46
lines changed

hooks/available.lua

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
local foojay = require("foojay")
2-
local shortname = require("shortname")
2+
local distribution_version_parser = require("distribution_version")
33

44
--- Return all available versions provided by this plugin
55
--- @param ctx table Empty table used as context, for future extension
66
--- @return table Descriptions of available versions and accompanying tool descriptions
77
function PLUGIN:Available(ctx)
8-
local distribution = ctx.args[1] or "open"
8+
local query = ctx.args[1] or "open"
99
local jdks = {}
1010

11-
if distribution == "all" then
12-
for short, dist in pairs(shortname) do
13-
local tempJdks = foojay.fetchtJdkList(dist, "")
11+
if query == "all" then
12+
for _, distribution in ipairs(distribution_version_parser.distributions) do
13+
local tempJdks = foojay.fetchtJdkList(distribution.name, "")
1414
for _, jdk in ipairs(tempJdks) do
15-
jdk.short = short
15+
jdk.short = distribution.short_name
1616
table.insert(jdks, jdk)
1717
end
1818
end
1919
else
20-
jdks = foojay.fetchtJdkList(shortname[distribution] or error("Unsupported distribution: " .. distribution), "")
20+
local distribution = distribution_version_parser.parse_distribution(query)
21+
if not distribution then
22+
error("Unsupported distribution: " .. query)
23+
end
24+
jdks = foojay.fetchtJdkList(distribution.name, "")
2125
end
2226

2327
local result = {}
2428
local seen = {}
2529
for _, jdk in ipairs(jdks) do
2630
local v = jdk.java_version
2731
local short = jdk.short
28-
if distribution == "all" then
32+
if query == "all" then
2933
v = v .. "-" .. short
30-
elseif distribution == "open" then
34+
elseif query == "open" then
3135
v = v
3236
else
33-
v = v .. "-" .. distribution
37+
local distribution = distribution_version_parser.parse_distribution(query)
38+
if not distribution then
39+
error("Unsupported distribution: " .. query)
40+
end
41+
v = v .. "-" .. distribution.short_name
3442
end
3543

3644
if not seen[v] then

hooks/parse_legacy_file.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local strings = require("vfox.strings")
22

3-
local shortname = require("shortname")
3+
local distribution_version_parser = require("distribution_version")
44

55
function PLUGIN:ParseLegacyFile(ctx)
66
local filepath = ctx.filepath
@@ -20,9 +20,10 @@ function PLUGIN:ParseLegacyFile(ctx)
2020
return {}
2121
end
2222

23-
24-
local fullVersion, vendor = unpack(strings.split(javaVersion, "-"))
25-
if not vendor or not shortname[vendor] then
23+
local distributionVersion = distribution_version_parser.parse_version(javaVersion)
24+
local fullVersion = distributionVersion.version
25+
local vendor = distributionVersion.distribution.short_name
26+
if not vendor then
2627
return {}
2728
end
2829

hooks/pre_install.lua

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
local strings = require("vfox.strings")
21
local http = require("http")
32
local json = require("json")
43

54
local foojay = require("foojay")
6-
local shortname = require("shortname")
5+
local distribution_version_parser = require("distribution_version")
76
--- Returns some pre-installed information, such as version number, download address, local files, etc.
87
--- If checksum is provided, vfox will automatically check it for you.
98
--- @param ctx table
109
--- @field ctx.version string User-input version
1110
--- @return table Version information
1211
function PLUGIN:PreInstall(ctx)
13-
local version_parts = strings.split(ctx.version, "-")
14-
local version = version_parts[1]
15-
local distribution = version_parts[2] or "open"
16-
if not shortname[distribution] then
17-
error("Unsupport distribution: " .. distribution)
12+
local distribution_version = distribution_version_parser.parse_version(ctx.version)
13+
if not distribution_version then
14+
error("Could not extract a valid distribution: " .. ctx.version)
1815
end
19-
local jdks = foojay.fetchtJdkList(shortname[distribution], version)
16+
local jdks = foojay.fetchtJdkList(distribution_version.distribution.name, distribution_version.version)
2017
if not #jdks then
2118
return {}
2219
end
@@ -27,7 +24,7 @@ function PLUGIN:PreInstall(ctx)
2724
-- if checksum == "" and info.checksum_uri ~= "" then
2825
-- checksum = httpGet(info.checksum_uri, "Failed to fetch checksum")
2926
-- end
30-
local finalV = distribution == "open" and jdk.java_version or jdk.java_version .. "-" .. distribution
27+
local finalV = distribution_version.distribution.short_name == "open" and jdk.java_version or jdk.java_version .. "-" .. distribution_version.distribution.short_name
3128
return {
3229
-- [info.checksum_type] = checksum,
3330
url = info.direct_download_uri,

hooks/pre_use.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local distribution_version_parser = require("distribution_version")
2+
3+
function PLUGIN:PreUse(ctx)
4+
local distribution_version = distribution_version_parser.parse_version(ctx.version)
5+
if distribution_version and distribution_version.distribution.short_name ~= "open" then
6+
return {
7+
version = distribution_version.version .. "-" .. distribution_version.distribution.short_name
8+
}
9+
else
10+
return {
11+
version = ctx.version,
12+
}
13+
end
14+
end
15+

lib/distribution_version.lua

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
local strings = require("vfox.strings")
2+
3+
local short_name = {
4+
["open"] = "openjdk",
5+
["bsg"] = "bisheng",
6+
["amzn"] = "corretto",
7+
["albba"] = "dragonwell",
8+
["graal"] = "graalvm",
9+
["graalce"] = "graalvm_community",
10+
["oracle"] = "oracle",
11+
["kona"] = "kona",
12+
["librca"] = "liberica",
13+
["nik"] = "liberica_native",
14+
["mandrel"] = "mandrel",
15+
["ms"] = "microsoft",
16+
["sapmchn"] = "sap_machine",
17+
["sem"] = "semeru",
18+
["tem"] = "temurin",
19+
["trava"] = "trava",
20+
["zulu"] = "zulu",
21+
["jb"] = "jetbrains",
22+
}
23+
24+
local long_name={}
25+
local distribution_version={
26+
distributions={}
27+
}
28+
29+
for k,v in pairs(short_name) do
30+
long_name[v]=k
31+
table.insert(distribution_version.distributions,{
32+
name = v,
33+
short_name = k
34+
})
35+
end
36+
37+
function distribution_version.parse_distribution (name)
38+
local distribution_name=long_name[name]
39+
if distribution_name then
40+
-- already a valid long name, just return it
41+
return {
42+
name = name,
43+
short_name = distribution_name
44+
}
45+
end
46+
distribution_name=short_name[name]
47+
if distribution_name then
48+
-- already a valid short name, just return it
49+
return {
50+
name = distribution_name,
51+
short_name = name
52+
}
53+
end
54+
return nil
55+
end
56+
57+
58+
function distribution_version.parse_version (arg)
59+
local version_parts = strings.split(arg, "-")
60+
local version
61+
local distribution
62+
63+
if not version_parts[2] then
64+
-- no parts, check if we got a distribution name without version
65+
distribution = distribution_version.parse_distribution(version_parts[1])
66+
if not distribution then
67+
-- no valid distribution found, handle as version with distribution "openjdk"
68+
version=version_parts[1]
69+
distribution = distribution_version.parse_distribution("openjdk")
70+
end
71+
else
72+
-- check if the distribution is in the second part of the string (vfox/sdkman default)
73+
distribution = distribution_version.parse_distribution(version_parts[2])
74+
if distribution then
75+
-- valid distribution found, treat first part as version
76+
version=version_parts[1]
77+
else
78+
-- check if the distribution is in the first part of the string (asdf default)
79+
distribution = distribution_version.parse_distribution(version_parts[1])
80+
if distribution then
81+
-- valid distribution found, treat second part as version
82+
version=version_parts[2]
83+
else
84+
-- invalid distribution name
85+
return nil
86+
end
87+
end
88+
end
89+
90+
return {
91+
version = version,
92+
distribution = distribution,
93+
}
94+
end
95+
96+
return distribution_version

lib/shortname.lua

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)