Skip to content

Commit

Permalink
initialization plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ahai-code committed Mar 28, 2024
1 parent 0ccb522 commit d7dd4bc
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 123 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
# vfox-plugin-template

This is a [vfox plugin](https://vfox.lhan.me/plugins/create/howto.html) template with CI that package and publish the plugin.

## Usage

1. [Generate](https://github.com/version-fox/vfox-plugin-template/generate) a new repository based on this template.
2. Configure [metadata](https://github.com/version-fox/vfox-plugin-template/blob/main/metadata.lua) information
3. To develop your plugin further, please read [the plugins create section of the docs](https://vfox.lhan.me/plugins/create/howto.html).


## How to publish?

1. Push a new tag to the repository which name is `vX.Y.Z` (X.Y.Z is the version number).
2. The CI will automatically package, then publish [release](https://github.com/version-fox/vfox-plugin-template/releases/tag/v0.0.1) and publish [manifest](https://github.com/version-fox/vfox-plugin-template/releases/tag/manifest).
# Introduction
vfox-zig is a plugin for [vfox](https://vfox.lhan.me/).
# Install
After installing vfox, run the following command to add the plugin:
```bash
vfox add zig
```
47 changes: 33 additions & 14 deletions hooks/available.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@ local util = require("util")
--- @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)
util:DoSomeThing()
local runtimeVersion = ctx.runtimeVersion
return {
{
version = "xxxx",
note = "LTS",
addition = {
{
name = "npm",
version = "8.8.8",
}
}
}
}
local archs = util:getArchArr()
local os = util:getOsType()
local base = util:getResults(util.BaseUrl, archs, os, "tarball")
local mach = util:getResults(util.MachUrl, archs, os, "zigTarball")

--merge the two together
for k, v in pairs(mach) do
if v.note == "nightly" then
goto continue
end
if base[k] == nil then
base[k] = v
elseif base[k].note ~= "" then
base[k].note = base[k].note .. "|" .. v.note
end
::continue::
end

-- Need an list to sort it
local result = {}
for _, v in pairs(base) do
table.insert(result, v)
end
table.sort(result, function(a,b) return util:compare_versions(a,b) end)

-- Get the first non-noted version to dictate latest
for _, v in ipairs(result) do
if v.note == "" then
v.note = "latest"
break
end
end
return result
end
22 changes: 2 additions & 20 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@
--- @field ctx.path string SDK installation directory
function PLUGIN:EnvKeys(ctx)
--- this variable is same as ctx.sdkInfo['plugin-name'].path
local mainPath = ctx.path
local runtimeVersion = ctx.runtimeVersion
local mainSdkInfo = ctx.main
local mpath = mainSdkInfo.path
local mversion = mainSdkInfo.version
local mname = mainSdkInfo.name
local sdkInfo = ctx.sdkInfo['sdk-name']
local path = sdkInfo.path
local version = sdkInfo.version
local name = sdkInfo.name
local version_path = ctx.path
return {
{
key = "JAVA_HOME",
value = mainPath
},
{
key = "PATH",
value = mainPath .. "/bin"
},
{
key = "PATH",
value = mainPath .. "/bin2"
value = version_path,
},

}

end
7 changes: 1 addition & 6 deletions hooks/post_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
--- Currently can be left unimplemented!
function PLUGIN:PostInstall(ctx)
--- ctx.rootPath SDK installation directory
local rootPath = ctx.rootPath
local runtimeVersion = ctx.runtimeVersion
local sdkInfo = ctx.sdkInfo['sdk-name']
local path = sdkInfo.path
local version = sdkInfo.version
local name = sdkInfo.name
--do nothing
end
45 changes: 13 additions & 32 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,17 @@
--- @return table Version information
function PLUGIN:PreInstall(ctx)
local version = ctx.version
local runtimeVersion = ctx.runtimeVersion
return {
--- Version number
version = "xxx",
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum [optional]
sha256 = "xxx",
--- md5 checksum [optional]
md5 = "xxx",
--- sha1 checksum [optional]
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
--- additional need files [optional]
addition = {
{
--- additional file name !
name = "xxx",
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum [optional]
sha256 = "xxx",
--- md5 checksum [optional]
md5 = "xxx",
--- sha1 checksum [optional]
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
}
}
}
local releases = self:Available({})
for _, release in ipairs(releases) do
if release.version == version then
return release
else
for note in string.gmatch(release.note, "[^|]+") do
if note == version then
return release
end
end
end
end
return {}
end
24 changes: 1 addition & 23 deletions hooks/pre_use.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,5 @@
--- valid version information.
--- @param ctx table Context information
function PLUGIN:PreUse(ctx)
local runtimeVersion = ctx.runtimeVersion
--- user input version
local version = ctx.version
--- user current used version
local previousVersion = ctx.previousVersion

--- installed sdks
local sdkInfo = ctx.installedSdks['version']
local path = sdkInfo.path
local name = sdkInfo.name
local version = sdkInfo.version

--- working directory
local cwd = ctx.cwd

--- user input scope
--- could be one of global/project/session
local scope = ctx.scope

--- return the version information
return {
version = version,
}
--do nothing
end
8 changes: 0 additions & 8 deletions lib/uil.lua

This file was deleted.

101 changes: 101 additions & 0 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
local http = require("http")
local json = require("json")
local util = {}
util.BaseUrl = "https://ziglang.org/download/index.json"
util.MachUrl = "https://machengine.org/zig/index.json"

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:getResults(url, archs, os, tar)
local resp, err = http.get({
url = url,
})
if err ~= nil or resp.status_code ~= 200 then
error("get version failed" .. err)
end
local body = json.decode(resp.body)
local result = {}
for k, v in pairs(body) do
local version = k
local note = ""
if v.version ~= nil then
version = v.version
if k == "master" then
note = "nightly"
else
note = k
end
end
for _, arch in ipairs(archs) do
local key = arch .. "-" .. os
if v[key] ~= nil then
if result[version] ~= nil then
result[version].note = result[version].note .. "|" .. note
else
result[version] = {
version = version,
url = v[key][tar],
sha256 = v[key].shasum,
note = note,
}
end
end
end
end
return result
end

function util:getOsType()
if RUNTIME.osType == "darwin" then
return "macos"
end
return RUNTIME.osType
end

function util:getArchArr()
if RUNTIME.archType == "amd64" then
return {
"x86_64",
}
elseif RUNTIME.archType == "arm64" then
return {
"aarch64",
}
elseif RUNTIME.archType == "386" then
return {
"x86",
"i386",
}
else
return {
RUNTIME.archType,
}
end
end

return util
10 changes: 5 additions & 5 deletions metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ PLUGIN = {}

--- !!! MUST BE SET !!!
--- Plugin name
PLUGIN.name = "your plugin name"
PLUGIN.name = "zig"
--- Plugin version
PLUGIN.version = "0.0.1"
PLUGIN.version = "0.1.0"
--- Plugin homepage
PLUGIN.homepage = "https://github.com/version-fox/vfox-plugin-template"
PLUGIN.homepage = "https://github.com/version-fox/vfox-zig"
--- Plugin license, please choose a correct license according to your needs.
PLUGIN.license = "Apache 2.0"
--- Plugin description
PLUGIN.description = "your plugin description"
PLUGIN.description = "Zig runtime environment."


--- !!! OPTIONAL !!!
Expand All @@ -33,7 +33,7 @@ NOTE:
you can set this address to the manifest file address, so that the plugin can be updated automatically.
--]]
PLUGIN.manifestUrl = "https://github.com/version-fox/vfox-plugin-template/releases/download/manifest/manifest.json"
PLUGIN.manifestUrl = "https://github.com/version-fox/vfox-zig/releases/download/manifest/manifest.json"
-- Some things that need user to be attention!
PLUGIN.notes = {
"",
Expand Down

0 comments on commit d7dd4bc

Please sign in to comment.