Skip to content

Commit

Permalink
feat: add mani plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed May 11, 2024
0 parents commit 608c756
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
Empty file added .tool-versions
Empty file.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Lua.diagnostics.globals": [
"fetchVersions"
]
}
17 changes: 17 additions & 0 deletions Injection.lua
Original file line number Diff line number Diff line change
@@ -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 = "",
}

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
6 changes: 6 additions & 0 deletions hooks/available.lua
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions hooks/parse_legacy_file.lua
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions hooks/post_install.lua
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions hooks/pre_uninstall.lua
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions hooks/pre_use.lua
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions lib/client.lua
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -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(),
}
Loading

0 comments on commit 608c756

Please sign in to comment.