Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Support shorthand version in Node.js #15

Merged
merged 3 commits into from
Feb 21, 2024
Merged
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
64 changes: 63 additions & 1 deletion nodejs/nodejs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VersionSourceUrl = "https://nodejs.org/dist/index.json"
PLUGIN = {
name = "nodejs",
author = "Aooohan",
version = "0.0.3",
version = "0.0.4",
description = "Node.js",
updateUrl = "https://raw.githubusercontent.com/version-fox/version-fox-plugins/main/nodejs/nodejs.lua",
}
Expand All @@ -28,6 +28,17 @@ function PLUGIN:PreInstall(ctx)
local lists = self:Available({})
version = lists[1].version
end

if not is_semver_simple(version) then
local lists = self:Available({})
local shorthands = calculate_shorthand(lists)
version = shorthands[version]
end

if (version == nil) then
error("version not found for provided version " .. version)
end

local arch_type = ARCH_TYPE
local ext = ".tar.gz"
local osType = OS_TYPE
Expand Down Expand Up @@ -91,7 +102,13 @@ function get_checksum(file_content, file_name)
return nil
end

available_result = nil

function PLUGIN:Available(ctx)
if available_result then
return available_result
end

local resp, err = http.get({
url = VersionSourceUrl
})
Expand All @@ -100,6 +117,7 @@ function PLUGIN:Available(ctx)
end
local body = json.decode(resp.body)
local result = {}

for _, v in ipairs(body) do
table.insert(result, {
version = string.gsub(v.version, "^v", ""),
Expand All @@ -113,6 +131,7 @@ function PLUGIN:Available(ctx)
})
end
table.sort(result, compare_versions)
available_result = result
bytemain marked this conversation as resolved.
Show resolved Hide resolved
return result
end

Expand Down Expand Up @@ -143,3 +162,46 @@ function PLUGIN:EnvKeys(ctx)
}
end
end

function is_semver_simple(str)
-- match pattern: three digits, separated by dot
local pattern = "^%d+%.%d+%.%d+$"
return str:match(pattern) ~= nil
end

function extract_semver(semver)
local pattern = "^(%d+)%.(%d+)%.[%d.]+$"
local major, minor = semver:match(pattern)
return major, minor
end

function calculate_shorthand(list)
local versions_shorthand = {}
for _, v in ipairs(list) do
local version = v.version
local major, minor = extract_semver(version)

if major then
if not versions_shorthand[major] then
versions_shorthand[major] = version
else
if compare_versions({version = version}, {version = versions_shorthand[major]}) then
versions_shorthand[major] = version
end
end

if minor then
local major_minor = major .. "." .. minor
if not versions_shorthand[major_minor] then
versions_shorthand[major_minor] = version
else
if compare_versions({version = version}, {version = versions_shorthand[major_minor]}) then
versions_shorthand[major_minor] = version
end
end
end
end
end

return versions_shorthand
end
69 changes: 68 additions & 1 deletion nodejs/npmmirror.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VersionSourceUrl = "https://cdn.npmmirror.com/binaries/node/index.json"
PLUGIN = {
name = "nodejs",
author = "yimiaoxiehou",
version = "0.0.1",
version = "0.0.2",
description = "install Node.js use https://cdn.npmmirror.com",
updateUrl = "https://raw.githubusercontent.com/version-fox/version-fox-plugins/main/nodejs/npmmirror.lua",
}
Expand All @@ -28,6 +28,17 @@ function PLUGIN:PreInstall(ctx)
local lists = self:Available({})
version = lists[1].version
end

if not is_semver_simple(version) then
local lists = self:Available({})
local shorthands = calculate_shorthand(lists)
version = shorthands[version]
end

if (version == nil) then
error("version not found for provided version " .. version)
end

local arch_type = ARCH_TYPE
local ext = ".tar.gz"
local osType = OS_TYPE
Expand Down Expand Up @@ -91,7 +102,13 @@ function get_checksum(file_content, file_name)
return nil
end

available_result = nil

function PLUGIN:Available(ctx)
if available_result then
return available_result
end

local resp, err = http.get({
url = VersionSourceUrl
})
Expand All @@ -100,6 +117,7 @@ function PLUGIN:Available(ctx)
end
local body = json.decode(resp.body)
local result = {}

for _, v in ipairs(body) do
table.insert(result, {
version = string.gsub(v.version, "^v", ""),
Expand All @@ -113,11 +131,17 @@ function PLUGIN:Available(ctx)
})
end
table.sort(result, compare_versions)
available_result = result
bytemain marked this conversation as resolved.
Show resolved Hide resolved
return result
end

--- Expansion point
function PLUGIN:PostInstall(ctx)
local rootPath = ctx.rootPath
local sdkInfo = ctx.sdkInfo['nodejs']
local path = sdkInfo.path
local version = sdkInfo.version
local name = sdkInfo.name
end

function PLUGIN:EnvKeys(ctx)
Expand All @@ -138,3 +162,46 @@ function PLUGIN:EnvKeys(ctx)
}
end
end

function is_semver_simple(str)
-- match pattern: three digits, separated by dot
local pattern = "^%d+%.%d+%.%d+$"
return str:match(pattern) ~= nil
end

function extract_semver(semver)
local pattern = "^(%d+)%.(%d+)%.[%d.]+$"
local major, minor = semver:match(pattern)
return major, minor
end

function calculate_shorthand(list)
local versions_shorthand = {}
for _, v in ipairs(list) do
local version = v.version
local major, minor = extract_semver(version)

if major then
if not versions_shorthand[major] then
versions_shorthand[major] = version
else
if compare_versions({version = version}, {version = versions_shorthand[major]}) then
versions_shorthand[major] = version
end
end

if minor then
local major_minor = major .. "." .. minor
if not versions_shorthand[major_minor] then
versions_shorthand[major_minor] = version
else
if compare_versions({version = version}, {version = versions_shorthand[major_minor]}) then
versions_shorthand[major_minor] = version
end
end
end
end
end

return versions_shorthand
end