Skip to content

Commit 59c6f1e

Browse files
committed
feat: add Injection.lua for code hints and update hooks and util functions
1 parent c377a4b commit 59c6f1e

5 files changed

Lines changed: 41 additions & 52 deletions

File tree

.available.cache

3.06 KB
Binary file not shown.

Injection.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--[[
2+
Do not change any thing in the current file,
3+
it's just there to show what objects are injected by vfox and what they do.
4+
5+
It's just handy when developing plugins, IDE can use this object for code hints!
6+
--]]
7+
RUNTIME = {
8+
--- Operating system type at runtime (Windows, Linux, Darwin)
9+
osType = "",
10+
--- Operating system architecture at runtime (amd64, arm64, etc.)
11+
archType = "",
12+
--- vfox runtime version
13+
version = "",
14+
}
15+

hooks/env_keys.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
--- @field ctx.rootPath string The root path of the tool currently in use
55
--- @return table Environment variables to be set
66
function PLUGIN:EnvKeys(ctx)
7+
print("ctx.rootPath", ctx.rootPath)
78
return {
89
{
910
key = "CHAOSBLADE_HOME",
1011
value = ctx.rootPath
1112
},
1213
{
1314
key = "PATH",
14-
value = ctx.rootPath .. "/bin"
15+
value = ctx.rootPath
1516
}
1617
}
1718
end

hooks/pre_install.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ local util = require("util")
77
--- @return table Version information
88
function PLUGIN:PreInstall(ctx)
99
local version = ctx.version
10-
local info, err = util:GetDownloadInfo(version)
11-
if err then
12-
error(err)
13-
end
10+
local download_url = util:GetDownloadInfo(version)
11+
-- https://github.com/chaosblade-io/chaosblade/releases/download/v1.7.2/chaosblade-1.7.2-linux-amd64.tar.gz
12+
print("Download URL: " .. download_url)
1413
return {
1514
version = version,
16-
url = info.url,
17-
sha256 = info.sha256
15+
url = download_url
1816
}
1917
end

lib/util.lua

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,45 @@
11
local http = require("http")
22
local json = require("json")
3-
local os = require("os")
43
local util = {}
54

65
local GITHUB_API_URL = "https://api.github.com/repos/chaosblade-io/chaosblade/releases"
76

87
function util:Available()
9-
local code, body = http.get(GITHUB_API_URL)
10-
if code ~= 200 then
8+
local resp, err = http.get({
9+
url = GITHUB_API_URL
10+
})
11+
if err ~= nil or resp.status_code ~= 200 then
1112
return nil, "Failed to fetch releases from GitHub API."
1213
end
13-
local releases = json.decode(body)
14+
local releases = json.decode(resp.body)
1415
local versions = {}
1516
for _, release in ipairs(releases) do
17+
-- Remove 'v' prefix from tag_name to avoid double 'v' prefix
18+
local clean_version = release.tag_name:gsub("^v", "")
1619
table.insert(versions, {
17-
version = release.tag_name,
20+
version = clean_version,
1821
note = release.prerelease and "prerelease" or ""
1922
})
2023
end
2124
return versions
2225
end
2326

2427
function util:GetDownloadInfo(version)
25-
local arch = os.arch()
28+
local os_type = RUNTIME.osType
29+
os_type = "linux"
30+
-- if os_type ~= "Linux" then
31+
-- return nil, "Unsupported OS type: " .. os_type
32+
-- end
33+
34+
local arch = RUNTIME.archType
2635
if arch == "x86_64" then
2736
arch = "amd64"
2837
end
29-
local os_type = os.type()
30-
if os_type == "Darwin" then
31-
os_type = "darwin"
32-
elseif os_type == "Linux" then
33-
os_type = "linux"
34-
else
35-
return nil, "Unsupported OS type: " .. os_type
36-
end
37-
38-
-- Remove 'v' prefix from version for filename
39-
local clean_version = version:gsub("^v", "")
40-
local file_name = string.format("chaosblade-%s-%s-%s.tar.gz", clean_version, os_type, arch)
41-
local url = string.format("https://github.com/chaosblade-io/chaosblade/releases/download/%s/%s", version, file_name)
38+
arch = "amd64"
39+
local file_name = string.format("chaosblade-%s-%s-%s.tar.gz", version, os_type, arch)
40+
local url = string.format("https://github.com/chaosblade-io/chaosblade/releases/download/v%s/%s", version, file_name)
4241

43-
local checksum_url = string.format("https://github.com/chaosblade-io/chaosblade/releases/download/%s/checksum.txt", version)
44-
local code, body = http.get(checksum_url)
45-
local checksum = ""
46-
47-
-- Try to get checksum if available, but don't fail if not found
48-
if code == 200 then
49-
for line in string.gmatch(body, "[^\r\n]+") do
50-
local sha, name = line:match("([a-f0-9]+)%s+(.+)")
51-
if name == file_name then
52-
checksum = sha
53-
break
54-
end
55-
end
56-
end
57-
58-
-- Return the download info, with or without checksum
59-
local result = {
60-
url = url
61-
}
62-
63-
if checksum ~= "" then
64-
result.sha256 = checksum
65-
end
66-
67-
return result
42+
return url
6843
end
6944

70-
return util
45+
return util

0 commit comments

Comments
 (0)