Skip to content

Commit

Permalink
initial plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ahai-code committed Apr 14, 2024
1 parent d596632 commit 075fa29
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 116 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/update_versions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: update_versions

on:
schedule:
- cron: "0 0 * * 1"
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4

- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Update version file
run: |
cd assets
python -m pip install requests
python -m pip install bs4
python get_versions.py
- name: Push updated version file
run: |
pwd
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"github-actions[bot]
if [[ -n $(git status -s) ]]; then
git add .
git commit -m "chore(versions.txt): update Cmake version file"
git push https://github-actions[bot]:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
else
echo "never updated."
fi
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-cmake is a plugin for [vfox](https://vfox.lhan.me/).
# Install
After installing vfox, run the following command to add the plugin:
```bash
vfox add cmake
```
78 changes: 78 additions & 0 deletions assets/get_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import requests
from bs4 import BeautifulSoup
import re
import json

def get_info():
base_url = "https://cmake.org/files/"

releases = {'darwin': [],'windows':[],"linux":[]}
resp = requests.get(base_url)
resp.raise_for_status()

big_version_urls = []
soup = BeautifulSoup(resp.content, 'html.parser')

for a in soup.find_all('a', href=True):
href = a['href']
sn = re.match(r'^v\d', href)
es = href.endswith('/')
version = href[:-1]
if sn and es :
big_version_urls.append(base_url + version)

pattern = r"cmake-(\d+\.\d+\.?\d*[-\w\d]*)-([\w\d]+)-([\w\d_-]+)"

for url in big_version_urls:
resp = requests.get(url)
resp.raise_for_status()
soup = BeautifulSoup(resp.content, 'html.parser')

for a in soup.find_all('a', href=True):
href = a['href']
sn = re.match(r'^cmake', href)
es = href.endswith('.txt')
if sn and es:
resp = requests.get(url + '/' + href)
resp.raise_for_status()
sha256_files = resp.text.split('\n')
macos_universal_versions = set()
for line in sha256_files:
if is_matched(line):
sha256, filename = line.split()
if re.match(pattern, filename):
version, os, arch = re.match(pattern, filename).groups()
else:
continue
os = os.lower()
arch = arch.lower()
download_url = url + '/' + filename

if os in ["macos","darwin"]:
if arch =="x86_64":
if version not in macos_universal_versions:
releases["darwin"].append({'arch':'amd64','version': version, 'download_url': download_url, 'sha256': sha256})
elif arch == "universal":
releases["darwin"].append({'arch':'','version': version, 'download_url': download_url, 'sha256': sha256})
macos_universal_versions.add(version)
elif os in["windows","win32","win64"]:
if arch in ["x64", "x86_64"]:
releases["windows"].append({'arch':'amd64','version': version, 'download_url': download_url, 'sha256': sha256})
elif arch in ["i386", "x86"]:
releases["windows"].append({'arch':'386','version': version, 'download_url': download_url, 'sha256': sha256})
elif arch == "arm64":
releases["windows"].append({'arch':'arm64','version': version, 'download_url': download_url, 'sha256': sha256})
elif os == "linux":
if arch in ["i386", "aarch64"]:
releases["linux"].append({'arch':'386','version': version, 'download_url': download_url, 'sha256': sha256})
elif arch == "x86_64":
releases["linux"].append({'arch':'amd64','version': version, 'download_url': download_url, 'sha256': sha256})
with open("version.json", 'w', encoding="utf-8") as file:
json.dump(releases, file, indent=4)

def is_matched(line):
line = line.lower()
return any(sub in line for sub in ["macos", "darwin","windows", "win32", "win64","linux"]) and (line.endswith(".tar.gz") or line.endswith(".zip"))

if __name__ == "__main__":
get_info()
14 changes: 1 addition & 13 deletions hooks/available.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,5 @@ 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()
return {
{
version = "xxxx",
note = "LTS",
addition = {
{
name = "npm",
version = "8.8.8",
}
}
}
}
return util:getInfo()
end
22 changes: 4 additions & 18 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,15 @@
--- @param ctx table Context information
--- @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 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
if RUNTIME.osType=="darwin" then
mainPath = mainPath.."/CMake.app/Contents"
end
return {
{
key = "JAVA_HOME",
value = mainPath
},
{
key = "PATH",
value = mainPath .. "/bin"
},
{
key = "PATH",
value = mainPath .. "/bin2"
},
}

}

Expand Down
8 changes: 1 addition & 7 deletions hooks/post_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@
--- 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
-- do nothing
end
43 changes: 11 additions & 32 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
local util = require("util")
--- 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.
--- @param ctx table
--- @field ctx.version string User-input version
--- @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",
}
}
}
if #util.RELEASES == 0 then
self:Available(ctx)
end
local releases = util.RELEASES
for _, release in ipairs(releases) do
if release.version == ctx.version then
return release
end
end
return {}
end
23 changes: 1 addition & 22 deletions hooks/pre_use.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,5 @@
--- 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,
}
--do nothing
end
68 changes: 65 additions & 3 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
local http = require("http")
local json = require("json")

local util = {}

function util:DoSomeThing()
print("Do some thing")
util.__index = util
local utilSingleton = setmetatable({}, util)

utilSingleton.BASE_URL = "https://cmake.org/files/"
utilSingleton.RELEASES ={}

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:getInfo()
local result = {}
local resp, err = http.get({
url = "https://fastly.jsdelivr.net/gh/ahai-code/vfox-cmake@main/assets/version.json"
})

if err ~= nil then
error("Failed to get information: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get information: status_code =>" .. resp.status_code)
end

local respInfo = json.decode(resp.body)[RUNTIME.osType]

for _, info in pairs(respInfo) do
for _, obj in ipairs(info) do
if obj.arch=="" then
table.insert(result, {version = obj.version,note=""})
table.insert(utilSingleton.RELEASES,{version = obj.version,url=obj.download_url,sha256=obj.sha256})
elseif obj.arch == RUNTIME.archType then
table.insert(result, {version = obj.version,note=""})
table.insert(utilSingleton.RELEASES,{version = obj.version,url=obj.download_url,sha256=obj.sha256})
end
end
end
table.sort(result, function(a, b)
return util:compare_versions(a,b)
end)
return result
end

return util
return utilSingleton
12 changes: 6 additions & 6 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 = "cmake"
--- 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-cmake"
--- 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 = "Cmake plugin, https://cmake.org/"


--- !!! OPTIONAL !!!
Expand All @@ -33,8 +33,8 @@ 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-cmake/releases/download/manifest/manifest.json"
-- Some things that need user to be attention!
PLUGIN.notes = {
"",
"Supports version 3.0 and above.",
}

0 comments on commit 075fa29

Please sign in to comment.