diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 0000000..f44d298 --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,79 @@ +name: generate and publish manifest + +on: + push: + tags: [ 'v*' ] + +permissions: + contents: write + +jobs: + generate-manifest-json: + runs-on: ubuntu-latest + steps: + - name: Set repo name + run: | + echo "REPO_NAME=$(echo ${{ github.repository }} | cut -d'/' -f2)" >> $GITHUB_ENV + echo "VERSION=$(echo ${{ github.ref }} | cut -d'/' -f3 | cut -c2-)" >> $GITHUB_ENV + - uses: actions/checkout@v2 + + - name: Set up Lua + uses: leafo/gh-actions-lua@v8 + with: + luaVersion: '5.3.5' + + - name: Install dependencies + run: | + sudo apt-get install luarocks + sudo luarocks install dkjson + + - name: Generate manifest + run: | + sudo lua -e ' + require("metadata"); + local dkjson = require("dkjson"); + PLUGIN.downloadUrl = "https://github.com/${{ github.repository }}/releases/download/v${{ env.VERSION }}/${{ env.REPO_NAME }}-${{ env.VERSION }}.zip"; + local str = dkjson.encode(PLUGIN); + print(str)' > manifest.json + cat manifest.json + - name: Upload JSON file + uses: actions/upload-artifact@v2 + with: + name: manifest + path: manifest.json + release-plugin-and-manifest: + needs: generate-manifest-json + runs-on: ubuntu-latest + steps: + - name: Set repo name + run: | + echo "REPO_NAME=$(echo ${{ github.repository }} | cut -d'/' -f2)" >> $GITHUB_ENV + echo "VERSION=$(echo ${{ github.ref }} | cut -d'/' -f3 | cut -c2-)" >> $GITHUB_ENV + - name: Checkout code + uses: actions/checkout@v2 + - name: Download JSON file + uses: actions/download-artifact@v2 + with: + name: manifest + - name: Compress build files + uses: thedoctor0/zip-release@0.7.6 + with: + type: "zip" + directory: "./" + filename: "${{ env.REPO_NAME }}-${{ env.VERSION }}.zip" + exclusions: "*.git* manifest.json" + - name: Publish release + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: ./${{ env.REPO_NAME }}-${{ env.VERSION }}.zip + tag: v${{ env.VERSION }} + file_glob: true + - name: Publish manifest + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: ./manifest.json + tag: "manifest" + overwrite: true + file_glob: true \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..e69de29 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f609fd2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "Lua.diagnostics.globals": [ + "fetchVersions" + ] +} \ No newline at end of file diff --git a/Injection.lua b/Injection.lua new file mode 100644 index 0000000..a18c044 --- /dev/null +++ b/Injection.lua @@ -0,0 +1,17 @@ +--[[ +Do not change any thing in the current file, +it's just there to show what objects are injected by vfox and what they do. + +It's just handy when developing plugins, IDE can use this object for code hints! + --]] +RUNTIME = { + --- Operating system type at runtime (Windows, Linux, Darwin) + osType = "", + --- Operating system architecture at runtime (amd64, arm64, etc.) + archType = "", + --- vfox runtime version + version = "", + --- Plugin directory + pluginDirPath = "", +} + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..025aeda --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020-present, Andreas Weber + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b82774 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Introduction +vfox-mani is a plugin for [vfox](https://vfox.lhan.me/). +# Install +After installing vfox, run the following command to add the plugin: +```bash +vfox add mani +``` \ No newline at end of file diff --git a/hooks/available.lua b/hooks/available.lua new file mode 100644 index 0000000..2cf46cb --- /dev/null +++ b/hooks/available.lua @@ -0,0 +1,6 @@ +--- Return all available versions provided by this plugin +--- @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) + return fetchVersions() +end diff --git a/hooks/env_keys.lua b/hooks/env_keys.lua new file mode 100644 index 0000000..c5602e2 --- /dev/null +++ b/hooks/env_keys.lua @@ -0,0 +1,23 @@ +--- Each SDK may have different environment variable configurations. +--- This allows plugins to define custom environment variables (including PATH settings) +--- 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 + return { + { + key = "PATH", + value = mainPath .. "/bin" + }, + + } +end diff --git a/hooks/parse_legacy_file.lua b/hooks/parse_legacy_file.lua new file mode 100644 index 0000000..df56b0d --- /dev/null +++ b/hooks/parse_legacy_file.lua @@ -0,0 +1,12 @@ +--- 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 new file mode 100644 index 0000000..4bf262a --- /dev/null +++ b/hooks/post_install.lua @@ -0,0 +1,12 @@ +--- 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 new file mode 100644 index 0000000..9d09f60 --- /dev/null +++ b/hooks/pre_install.lua @@ -0,0 +1,38 @@ +--- Returns some pre-installed information, such as version number, download address, local files, etc. +--- If checksum is provided, vfox will automatically check it for you. + +--- @return table Version information +function PLUGIN:PreInstall(ctx) + local version = 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", + } + } + } +end diff --git a/hooks/pre_uninstall.lua b/hooks/pre_uninstall.lua new file mode 100644 index 0000000..f78866f --- /dev/null +++ b/hooks/pre_uninstall.lua @@ -0,0 +1,9 @@ + +--- 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 new file mode 100644 index 0000000..daf6d36 --- /dev/null +++ b/hooks/pre_use.lua @@ -0,0 +1,27 @@ +--- 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 new file mode 100644 index 0000000..cd35be2 --- /dev/null +++ b/lib/client.lua @@ -0,0 +1,45 @@ +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 new file mode 100644 index 0000000..04f5ddd --- /dev/null +++ b/lib/util.lua @@ -0,0 +1,66 @@ +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) + elseif token:match("^github_pat_" .. character:rep(22) .. "_" .. character:rep(59) .. "$") then + return true + end + + return false +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 diff --git a/metadata.lua b/metadata.lua new file mode 100644 index 0000000..b73786e --- /dev/null +++ b/metadata.lua @@ -0,0 +1,44 @@ +--- !!! DO NOT EDIT OR RENAME !!! +PLUGIN = {} + +--- !!! MUST BE SET !!! +--- Plugin name +PLUGIN.name = "mani" +--- Plugin version +PLUGIN.version = "0.0.1" +--- Plugin homepage +PLUGIN.homepage = "https://github.com/anweber/vfox-mani" +--- Plugin license, please choose a correct license according to your needs. +PLUGIN.license = "MIT" +--- Plugin description +PLUGIN.description = "mani - CLI tool to help you manage repositories" + + +--- !!! OPTIONAL !!! +--[[ +NOTE: + Minimum compatible vfox version. + If the plugin is not compatible with the current vfox version, + vfox will not load the plugin and prompt the user to upgrade vfox. + --]] +PLUGIN.minRuntimeVersion = "0.3.0" +--[[ +NOTE: + If configured, vfox will check for updates to the plugin at this address, + otherwise it will check for updates at the global registry. + + If you want use the global registry to distribute your plugin, you can remove this field. + + If you develop a plugin based on the template, which will automatically generate a manifest file by CI, + you can set this address to the manifest file address, so that the plugin can be updated automatically. + + --]] +PLUGIN.manifestUrl = "https://github.com/anweber/vfox-mani/releases/download/manifest/manifest.json" +-- Some things that need user to be attention! +PLUGIN.notes = { +} + +--- List legacy configuration filenames for determining the specified version of the tool. +--- such as ".node-version", ".nvmrc", etc. +PLUGIN.legacyFilenames = { +}