-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.lua
More file actions
95 lines (81 loc) · 2.66 KB
/
util.lua
File metadata and controls
95 lines (81 loc) · 2.66 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local UTIL = {}
UTIL.NodeBaseUrl = "/v%s/"
UTIL.FileName = "node-v%s-%s-%s%s"
UTIL.VersionSourceUrl = "/index.json"
function UTIL.getBaseUrl()
local mirror = os.getenv("VFOX_NODEJS_MIRROR")
if mirror == "" or mirror == nil then
return "https://nodejs.org/dist"
end
-- Strip trailing slash to avoid double slashes in URL paths
return mirror:gsub("/$", "")
end
function UTIL.compare_versions(v1o, v2o)
local v1 = v1o.version
local v2 = v2o.version
local v1_parts = {}
for part in string.gmatch(v1, "[^.]+") do
table.insert(v1_parts, tonumber(part))
end
local v2_parts = {}
for part in string.gmatch(v2, "[^.]+") do
table.insert(v2_parts, tonumber(part))
end
for i = 1, math.max(#v1_parts, #v2_parts) do
local v1_part = v1_parts[i] or 0
local v2_part = v2_parts[i] or 0
if v1_part > v2_part then
return true
elseif v1_part < v2_part then
return false
end
end
return false
end
function UTIL.get_checksum(file_content, file_name)
for line in string.gmatch(file_content, '([^\n]*)\n?') do
local checksum, name = string.match(line, '(%w+)%s+(%S+)')
if name == file_name then
return checksum
end
end
return nil
end
function UTIL.is_semver_simple(str)
-- match pattern: three digits, separated by dot
local pattern = "^%d+%.%d+%.%d+$"
return str:match(pattern) ~= nil
end
function UTIL.extract_semver(semver)
local pattern = "^(%d+)%.(%d+)%.[%d.]+$"
local major, minor = semver:match(pattern)
return major, minor
end
function UTIL.calculate_shorthand(list)
local versions_shorthand = {}
for _, v in pairs(list) do
local version = v.version
local major, minor = UTIL.extract_semver(version)
if major then
if not versions_shorthand[major] then
versions_shorthand[major] = version
else
if UTIL.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 UTIL.compare_versions({ version = version }, { version = versions_shorthand[major_minor] }) then
versions_shorthand[major_minor] = version
end
end
end
end
end
return versions_shorthand
end
return UTIL