Skip to content

Commit

Permalink
feat: update mani
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed May 11, 2024
1 parent 608c756 commit 8ab3f7e
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 199 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.idea
.vscode
47 changes: 47 additions & 0 deletions hooks/available.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,50 @@
function PLUGIN:Available(ctx)
return fetchVersions()
end

local http = require("http")
local json = require("json")
local util = require("./util")

function fetchVersions()
local result = {}
local headers = {
["Accept"] = "application/vnd.github+json",
}
if isGithubToken(util.githubToken) then
headers = {
["Accept"] = "application/vnd.github+json",
["Authorization"] = "Bearer " .. util.githubToken,
}
end
local resp, err = http.get({
-- Authenticate to get higher rate limit
headers = headers,
url = "https://api.github.com/repos/alajmo/mani/releases?per_page=60",
})
if err ~= nil then
error("Failed to get information: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get information: " .. err .. "\nstatus_code => " .. resp.status_code)
end
local releases = json.decode(resp.body)

for i, release in ipairs(releases) do
local version = release.tag_name:sub(2)
if i == 1 then
table.insert(result, {
version = version,
note = "latest",
})
else
table.insert(result, {
version = version,
})
end
::continue::
end


return result
end
14 changes: 2 additions & 12 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@
--- Note: Be sure to distinguish between environment variable settings for different platforms!
--- @param ctx table Context information
function PLUGIN:EnvKeys(ctx)
--- this variable is same as ctx.sdkInfo['plugin-name'].path
local mainPath = ctx.path
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
print(ctx.path)
return {
{
key = "PATH",
value = mainPath .. "/bin"
value = ctx.path
},

}
end
12 changes: 0 additions & 12 deletions hooks/parse_legacy_file.lua

This file was deleted.

12 changes: 0 additions & 12 deletions hooks/post_install.lua

This file was deleted.

93 changes: 63 additions & 30 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,69 @@

--- @return table Version information
function PLUGIN:PreInstall(ctx)
local version = ctx.version
local file, headers, version = getDownloadInfo(ctx.version)

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",
}
}
url = file,
headers = headers,
version = version
}
end

-- pre_install.lua
function getDownloadInfo(version)
local file
local headers
if version == "latest" then
version = getLatestVersion()
end
file = generateURL(version, RUNTIME.osType, RUNTIME.archType)

return file, headers, version
end

local http = require("http")
local json = require("json")
local util = require("./util")

function getLatestVersion()
local headers = {
["Accept"] = "application/vnd.github+json",
}
if isGithubToken(util.githubToken) then
headers = {
["Accept"] = "application/vnd.github+json",
["Authorization"] = "Bearer " .. util.githubToken,
}
end
local resp, err = http.get({
headers = headers,
url = "https://api.github.com/repos/alajmo/mani/releases/latest",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get latest version: " .. err .. "\nstatus_code => " .. resp.status_code)
end
local latest = json.decode(resp.body)
local version = latest.tag_name:sub(2)

return version
end

-- releases/download/v${version}/mani_${version}_${platform}_${arch}.tar.gz"

function generateURL(version, osType, archType)
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local baseURL = githubURL:gsub("/$", "") .. "/alajmo/mani/releases/download/v%s/mani_%s_%s_%s%s"

local ending = ".tar.gz"

if osType == "windows" then
ending = ".zip"
end
local file = baseURL:format(version, version, osType, archType, ending)

return file
end
9 changes: 0 additions & 9 deletions hooks/pre_uninstall.lua

This file was deleted.

27 changes: 0 additions & 27 deletions hooks/pre_use.lua

This file was deleted.

45 changes: 0 additions & 45 deletions lib/client.lua

This file was deleted.

53 changes: 2 additions & 51 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
local strings = require("vfox.strings")

function getDate()
local current_date = os.date("*t")
local formatted_date = string.format("%04d%02d%02d", current_date.year, current_date.month, current_date.day)
return formatted_date
end

function compareVersion(currentVersion, targetVersion)
local currentVersionArray = strings.split(currentVersion, ".")
local compareVersionArray = strings.split(targetVersion, ".")

for i, v in ipairs(currentVersionArray) do
if tonumber(v) > tonumber(compareVersionArray[i]) then
return 1
elseif tonumber(v) < tonumber(compareVersionArray[i]) then
return -1
end
end
return 0
end

function generateURL(version, osType, archType)
local file
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local baseURL = githubURL:gsub("/$", "") .. "/crystal-lang/crystal/releases/download/%s/crystal-%s-"

if osType == "darwin" then
-- new filename since 1.2.0
if compareVersion(version, "1.2.0") >= 0 then
file = baseURL .. "1-darwin-universal.tar.gz"
elseif archType == "amd64" then
file = baseURL .. "1-darwin-x86_64.tar.gz"
else
error("Crystal does not provide darwin-" .. archType .. " v" .. version .. " release")
end
elseif osType == "linux" and archType == "amd64" then
file = baseURL .. "1-linux-x86_64.tar.gz"
elseif osType == "windows" and archType == "amd64" then
file = baseURL .. "windows-x86_64-msvc-unsupported.zip"
else
error("Crystal does not provide " .. osType .. "-" .. archType .. " release")
end
file = file:format(version, version)

return file
end

function isGithubToken(token)
local character = "[a-zA-Z0-9]"
-- Personal Access Token (Classic)
if token:match("^ghp_" .. character:rep(36) .. "$") then
return true
-- Personal Access Token (Fine-Grained)
-- Personal Access Token (Fine-Grained)
elseif token:match("^github_pat_" .. character:rep(22) .. "_" .. character:rep(59) .. "$") then
return true
end
Expand All @@ -62,5 +14,4 @@ end
return {
-- Authenticate to get higher rate limit ↓ Add your GitHub Token here
githubToken = os.getenv("GITHUB_TOKEN") or "",
dataVersion = getDate(),
}
}

0 comments on commit 8ab3f7e

Please sign in to comment.