diff --git a/hooks/available.lua b/hooks/available.lua index a2cd1c3..69219b5 100644 --- a/hooks/available.lua +++ b/hooks/available.lua @@ -1,23 +1,23 @@ local foojay = require("foojay") -local shortname = require("shortname") +local distribution_version_parser = require("distribution_version") --- Return all available versions provided by this plugin --- @param ctx table Empty table used as context, for future extension --- @return table Descriptions of available versions and accompanying tool descriptions function PLUGIN:Available(ctx) - local distribution = ctx.args[1] or "open" + local query = ctx.args[1] or "open" local jdks = {} - if distribution == "all" then - for short, dist in pairs(shortname) do - local tempJdks = foojay.fetchtJdkList(dist, "") + if query == "all" then + for _, distribution in ipairs(distribution_version_parser.distributions) do + local tempJdks = foojay.fetchtJdkList(distribution.name, "") for _, jdk in ipairs(tempJdks) do - jdk.short = short + jdk.short = distribution.short_name table.insert(jdks, jdk) end end else - jdks = foojay.fetchtJdkList(shortname[distribution] or error("Unsupported distribution: " .. distribution), "") + jdks = foojay.fetchtJdkList(distribution_version_parser.parse_distribution(query).name or error("Unsupported distribution: " .. query), "") end local result = {} @@ -25,12 +25,12 @@ function PLUGIN:Available(ctx) for _, jdk in ipairs(jdks) do local v = jdk.java_version local short = jdk.short - if distribution == "all" then + if query == "all" then v = v .. "-" .. short - elseif distribution == "open" then + elseif query == "open" then v = v else - v = v .. "-" .. distribution + v = v .. "-" .. distribution_version_parser.parse_distribution(query).short_name end if not seen[v] then diff --git a/hooks/parse_legacy_file.lua b/hooks/parse_legacy_file.lua index c341324..bacfd6d 100644 --- a/hooks/parse_legacy_file.lua +++ b/hooks/parse_legacy_file.lua @@ -1,6 +1,6 @@ local strings = require("vfox.strings") -local shortname = require("shortname") +local distribution_version_parser = require("distribution_version") function PLUGIN:ParseLegacyFile(ctx) local filepath = ctx.filepath @@ -20,9 +20,10 @@ function PLUGIN:ParseLegacyFile(ctx) return {} end - - local fullVersion, vendor = unpack(strings.split(javaVersion, "-")) - if not vendor or not shortname[vendor] then + local distributionVersion = distribution_version_parser.parse_version(javaVersion) + local fullVersion = distributionVersion.version + local vendor = distributionVersion.distribution.short_name + if not vendor then return {} end diff --git a/hooks/pre_install.lua b/hooks/pre_install.lua index 36473c2..467f246 100644 --- a/hooks/pre_install.lua +++ b/hooks/pre_install.lua @@ -1,22 +1,19 @@ -local strings = require("vfox.strings") local http = require("http") local json = require("json") local foojay = require("foojay") -local shortname = require("shortname") +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 version_parts = strings.split(ctx.version, "-") - local version = version_parts[1] - local distribution = version_parts[2] or "open" - if not shortname[distribution] then - error("Unsupport distribution: " .. distribution) + 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(shortname[distribution], version) + local jdks = foojay.fetchtJdkList(distribution_version.distribution.name, distribution_version.version) if not #jdks then return {} end @@ -27,7 +24,7 @@ function PLUGIN:PreInstall(ctx) -- if checksum == "" and info.checksum_uri ~= "" then -- checksum = httpGet(info.checksum_uri, "Failed to fetch checksum") -- end - local finalV = distribution == "open" and jdk.java_version or jdk.java_version .. "-" .. distribution + 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, diff --git a/hooks/pre_use.lua b/hooks/pre_use.lua new file mode 100644 index 0000000..bdeea1a --- /dev/null +++ b/hooks/pre_use.lua @@ -0,0 +1,15 @@ +local distribution_version_parser = require("distribution_version") + +function PLUGIN:PreUse(ctx) + local distribution_version = distribution_version_parser.parse_version(ctx.version) + if distribution_version and distribution_version.distribution.short_name ~= "open" then + return { + version = distribution_version.version .. "-" .. distribution_version.distribution.short_name + } + else + return { + version = version, + } + end +end + diff --git a/lib/distribution_version.lua b/lib/distribution_version.lua new file mode 100644 index 0000000..5ad2120 --- /dev/null +++ b/lib/distribution_version.lua @@ -0,0 +1,96 @@ +local strings = require("vfox.strings") + +local short_name = { + ["open"] = "openjdk", + ["bsg"] = "bisheng", + ["amzn"] = "corretto", + ["albba"] = "dragonwell", + ["graal"] = "graalvm", + ["graalce"] = "graalvm_community", + ["oracle"] = "oracle", + ["kona"] = "kona", + ["librca"] = "liberica", + ["nik"] = "liberica_native", + ["mandrel"] = "mandrel", + ["ms"] = "microsoft", + ["sapmchn"] = "sap_machine", + ["sem"] = "semeru", + ["tem"] = "temurin", + ["trava"] = "trava", + ["zulu"] = "zulu", + ["jb"] = "jetbrains", +} + +local long_name={} +local distribution_version={ + distributions={} +} + +for k,v in pairs(short_name) do + long_name[v]=k + table.insert(distribution_version.distributions,{ + name = v, + short_name = k + }) +end + +function distribution_version.parse_distribution (name) + local distribution_name=long_name[name] + if distribution_name then + -- already a valid long name, just return it + return { + name = name, + short_name = distribution_name + } + end + distribution_name=short_name[name] + if distribution_name then + -- already a valid short name, just return it + return { + name = distribution_name, + short_name = name + } + end + return nil +end + + +function distribution_version.parse_version (arg) + local version_parts = strings.split(arg, "-") + local version + local distribution + + if not version_parts[2] then + -- no parts, check if we got a distribution name without version + distribution = distribution_version.parse_distribution(version_parts[1]) + if not distribution then + -- no valid distribution found, handle as version with distribution "openjdk" + version=version_parts[1] + distribution = distribution_version.parse_distribution("openjdk") + end + else + -- check if the distribution is in the second part of the string (vfox/sdkman default) + distribution = distribution_version.parse_distribution(version_parts[2]) + if distribution then + -- valid distribution found, treat first part as version + version=version_parts[1] + else + -- check if the distribution is in the first part of the string (asdf default) + distribution = distribution_version.parse_distribution(version_parts[1]) + if distribution then + -- valid distribution found, treat second part as version + version=version_parts[2] + else + -- invalid distribution name + return nil + end + end + end + + return { + version = version, + distribution = distribution, + } +end + +return distribution_version diff --git a/lib/shortname.lua b/lib/shortname.lua deleted file mode 100644 index f814615..0000000 --- a/lib/shortname.lua +++ /dev/null @@ -1,23 +0,0 @@ -local short_name = { - ["open"] = "openjdk", - ["bsg"] = "bisheng", - ["amzn"] = "corretto", - ["albba"] = "dragonwell", - ["graal"] = "graalvm", - ["graalce"] = "graalvm_community", - ["oracle"] = "oracle", - ["kona"] = "kona", - ["librca"] = "liberica", - ["nik"] = "liberica_native", - ["mandrel"] = "mandrel", - ["ms"] = "microsoft", - ["sapmchn"] = "sap_machine", - ["sem"] = "semeru", - ["tem"] = "temurin", - ["trava"] = "trava", - ["zulu"] = "zulu", - ["jb"] = "jetbrains", -} - - -return short_name \ No newline at end of file