Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support asdf formated version strings #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions hooks/available.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
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 = {}
local seen = {}
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
Expand Down
9 changes: 5 additions & 4 deletions hooks/parse_legacy_file.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
15 changes: 6 additions & 9 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions hooks/pre_use.lua
Original file line number Diff line number Diff line change
@@ -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

96 changes: 96 additions & 0 deletions lib/distribution_version.lua
Original file line number Diff line number Diff line change
@@ -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
23 changes: 0 additions & 23 deletions lib/shortname.lua

This file was deleted.