From 896aa4d79b5d879070295e9a3f9c5821dfd1ff1a Mon Sep 17 00:00:00 2001 From: Haibo Z <61728435+ahai-code@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:15:31 +0800 Subject: [PATCH] first merge (#1) * init gradle plugin * Update README.md * Add mirroring option --- .github/workflows/publish.yaml | 2 +- README.md | 20 ++++------ hooks/available.lua | 33 +++++++++------- hooks/env_keys.lua | 24 +++--------- hooks/post_install.lua | 8 +--- hooks/pre_install.lua | 70 ++++++++++++++++++---------------- hooks/pre_use.lua | 24 +----------- lib/util.lua | 35 +++++++++++++++++ metadata.lua | 12 +++--- 9 files changed, 113 insertions(+), 115 deletions(-) create mode 100644 lib/util.lua diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 83c8d9c..7cc64ec 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -63,7 +63,7 @@ jobs: uses: svenstaro/upload-release-action@v2 with: repo_token: ${{ secrets.GITHUB_TOKEN }} - file: ./${{ env.REPO_NAME }}_${{ env.VERSION }}.zip + file: ./${{ env.REPO_NAME }}-${{ env.VERSION }}.zip tag: v${{ env.VERSION }} file_glob: true - name: Publish manifest diff --git a/README.md b/README.md index 5f50b89..8141f67 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,9 @@ -# vfox-plugin-template +# vfox-gradle -This is a [vfox plugin](https://vfox.lhan.me/plugins/create/howto.html) template with CI that package and publish the plugin. +Gradle plugin for vfox [vfox](https://vfox.lhan.me/) . -## 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). +## Install +After install vfox [vfox](https://vfox.lhan.me/),install the plugin by running: +```bash +vfox install gradle +``` diff --git a/hooks/available.lua b/hooks/available.lua index 339bc66..e68ed54 100644 --- a/hooks/available.lua +++ b/hooks/available.lua @@ -4,18 +4,23 @@ 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() - local runtimeVersion = ctx.runtimeVersion - return { - { - version = "xxxx", - note = "LTS", - addition = { - { - name = "npm", - version = "8.8.8", - } - } - } - } + local resp, err = http.get({ + url = util.AvailableVersionsUrl + }) + if err ~= nil or resp.status_code ~= 200 then + error('get release info failed.') + end + local htmlBody = resp.body + local htmlContent= [[]] .. htmlBody .. [[]] + + local result = {} + + for version in htmlContent:gmatch('') do + table.insert(result, {version=version,note=""}) + end + table.sort(result, function(a, b) + return util:compare_versions(a, b) + end) + + return result end \ No newline at end of file diff --git a/hooks/env_keys.lua b/hooks/env_keys.lua index fbe82d3..3541260 100644 --- a/hooks/env_keys.lua +++ b/hooks/env_keys.lua @@ -5,30 +5,16 @@ --- @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 runtimeVersion = ctx.runtimeVersion - 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 + local path = ctx.path return { { - key = "JAVA_HOME", - value = mainPath + key = "GRADLE_HOME", + value = path }, { key = "PATH", - value = mainPath .. "/bin" - }, - { - key = "PATH", - value = mainPath .. "/bin2" - }, - + value = path .. "/bin" + } } end \ No newline at end of file diff --git a/hooks/post_install.lua b/hooks/post_install.lua index e489f61..1eb41a1 100644 --- a/hooks/post_install.lua +++ b/hooks/post_install.lua @@ -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 runtimeVersion = ctx.runtimeVersion - local sdkInfo = ctx.sdkInfo['sdk-name'] - local path = sdkInfo.path - local version = sdkInfo.version - local name = sdkInfo.name + --- DO-NOTHING end \ No newline at end of file diff --git a/hooks/pre_install.lua b/hooks/pre_install.lua index 8f3c82c..a07a3c0 100644 --- a/hooks/pre_install.lua +++ b/hooks/pre_install.lua @@ -1,40 +1,46 @@ +local util = require("util") +local os = require("os") --- 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 result = {} local version = ctx.version - local runtimeVersion = ctx.runtimeVersion - 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", - } - } - } + result.version = version + + local downloadUrl = "" + local imageUrl = os.getenv("GRADLE_DOWNLOAD_IMAGE") + + if imageUrl ~=nil then + downloadUrl = imageUrl.."/gradle-"..version.."-bin.zip" + else + downloadUrl = util.DownloadInfoUrl:format(version) + end + + result.url = downloadUrl + + local resp, err = http.get({ + url = downloadUrl..".sha256" + }) + + if err then + error("HTTP request error: " .. err) + end + + if resp.status_code == 200 then + if resp.body then + result.sha256 = resp.body + else + error("Empty body in HTTP response") + end + elseif resp.status_code == 404 then + print("This version does not have checksums") + else + return nil + end + + return result end \ No newline at end of file diff --git a/hooks/pre_use.lua b/hooks/pre_use.lua index e723328..c290ede 100644 --- a/hooks/pre_use.lua +++ b/hooks/pre_use.lua @@ -2,27 +2,5 @@ --- valid version information. --- @param ctx table Context information function PLUGIN:PreUse(ctx) - local runtimeVersion = ctx.runtimeVersion - --- 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 \ No newline at end of file diff --git a/lib/util.lua b/lib/util.lua new file mode 100644 index 0000000..9816a00 --- /dev/null +++ b/lib/util.lua @@ -0,0 +1,35 @@ +local Util = {} + +Util.__index = Util + +local UtilSingleton = setmetatable({}, Util) +UtilSingleton.AvailableVersionsUrl = "https://gradle.org/releases/" +UtilSingleton.DownloadInfoUrl = "https://services.gradle.org/distributions/gradle-%s-bin.zip" + +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 + +return UtilSingleton \ No newline at end of file diff --git a/metadata.lua b/metadata.lua index c15ae41..ac34e8d 100644 --- a/metadata.lua +++ b/metadata.lua @@ -3,17 +3,17 @@ PLUGIN = {} --- !!! MUST BE SET !!! --- Plugin name -PLUGIN.name = "your plugin name" +PLUGIN.name = "gradle" --- Plugin author -PLUGIN.author = "your name" +PLUGIN.author = "ahai" --- 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-gradle" --- 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 = "gradle" --- !!! OPTIONAL !!! @@ -35,7 +35,7 @@ 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-gradle/releases/download/manifest/manifest.json" -- Some things that need user to be attention! PLUGIN.notes = { "",