diff --git a/.gitignore b/.gitignore index 723ef36..600d2d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.idea \ No newline at end of file +.vscode \ No newline at end of file diff --git a/hooks/available.lua b/hooks/available.lua index 2cf46cb..3e533a6 100644 --- a/hooks/available.lua +++ b/hooks/available.lua @@ -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 diff --git a/hooks/env_keys.lua b/hooks/env_keys.lua index c5602e2..24ca488 100644 --- a/hooks/env_keys.lua +++ b/hooks/env_keys.lua @@ -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 diff --git a/hooks/parse_legacy_file.lua b/hooks/parse_legacy_file.lua deleted file mode 100644 index df56b0d..0000000 --- a/hooks/parse_legacy_file.lua +++ /dev/null @@ -1,12 +0,0 @@ ---- Parse the legacy file found by vfox to determine the version of the tool. ---- Useful to extract version numbers from files like JavaScript's package.json or Golangs go.mod. -function PLUGIN:ParseLegacyFile(ctx) - local filename = ctx.filename - local filepath = ctx.filepath - --- You can get a list of installed versions of the current plugin by this function. - local versions = ctx:getInstalledVersions() - - return { - version = "xxx" - } -end \ No newline at end of file diff --git a/hooks/post_install.lua b/hooks/post_install.lua deleted file mode 100644 index 4bf262a..0000000 --- a/hooks/post_install.lua +++ /dev/null @@ -1,12 +0,0 @@ ---- Extension point, called after PreInstall, can perform additional operations, ---- such as file operations for the SDK installation directory or compile source code ---- Currently can be left unimplemented! -function PLUGIN:PostInstall(ctx) - --- ctx.rootPath SDK installation directory - local rootPath = ctx.rootPath - local sdkInfo = ctx.sdkInfo['sdk-name'] - local path = sdkInfo.path - local version = sdkInfo.version - local name = sdkInfo.name - local note = sdkInfo.note -end \ No newline at end of file diff --git a/hooks/pre_install.lua b/hooks/pre_install.lua index 9d09f60..4953bab 100644 --- a/hooks/pre_install.lua +++ b/hooks/pre_install.lua @@ -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 diff --git a/hooks/pre_uninstall.lua b/hooks/pre_uninstall.lua deleted file mode 100644 index f78866f..0000000 --- a/hooks/pre_uninstall.lua +++ /dev/null @@ -1,9 +0,0 @@ - ---- This is called before the SDK is uninstalled. ---- @param ctx table Context information -function PLUGIN:PreUninstall(ctx) - local mainSdkInfo = ctx.main - local mpath = mainSdkInfo.path - local mversion = mainSdkInfo.version - local mname = mainSdkInfo.name -end \ No newline at end of file diff --git a/hooks/pre_use.lua b/hooks/pre_use.lua deleted file mode 100644 index daf6d36..0000000 --- a/hooks/pre_use.lua +++ /dev/null @@ -1,27 +0,0 @@ ---- When user invoke `use` command, this function will be called to get the ---- valid version information. ---- @param ctx table Context information -function PLUGIN:PreUse(ctx) - --- 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, - } -end \ No newline at end of file diff --git a/lib/client.lua b/lib/client.lua deleted file mode 100644 index cd35be2..0000000 --- a/lib/client.lua +++ /dev/null @@ -1,45 +0,0 @@ -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 - if i == 1 then - table.insert(result, { - version = release.tag_name, - note = "latest", - }) - else - table.insert(result, { - version = release.tag_name, - }) - end - ::continue:: - end - - - return result -end diff --git a/lib/util.lua b/lib/util.lua index 04f5ddd..870d4dd 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -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 @@ -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(), -} \ No newline at end of file +}