From e8ffd66fd4cbf2b6e383d43b909d1725a1dcf9cc Mon Sep 17 00:00:00 2001 From: yeshan333 Date: Tue, 2 Apr 2024 01:01:46 +0800 Subject: [PATCH] feat: impl erlang plugin --- README.md | 16 ++++-------- hooks/available.lua | 16 ++---------- hooks/env_keys.lua | 22 ++--------------- hooks/post_install.lua | 28 +++++++++++++++++---- hooks/pre_install.lua | 45 +++++++++++---------------------- hooks/pre_use.lua | 9 ++++--- lib/erlang_utils.lua | 56 ++++++++++++++++++++++++++++++++++++++++++ lib/util.lua | 8 ------ metadata.lua | 6 ++--- 9 files changed, 111 insertions(+), 95 deletions(-) create mode 100644 lib/erlang_utils.lua delete mode 100644 lib/util.lua diff --git a/README.md b/README.md index 5f50b89..26ee49d 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,9 @@ -# vfox-plugin-template +# vfox-erlang plugin -This is a [vfox plugin](https://vfox.lhan.me/plugins/create/howto.html) template with CI that package and publish the plugin. +Erlang/OTP [vfox](https://github.com/version-fox) plugin. Use the vfox to manage multiple Erlang/OTP versions in Linux/Darwin. ## 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). +```shell +vfox add erlang +``` diff --git a/hooks/available.lua b/hooks/available.lua index a5e6e3b..27a48b2 100644 --- a/hooks/available.lua +++ b/hooks/available.lua @@ -1,20 +1,8 @@ -local util = require("util") +local erlangUtils = require("erlang_utils") --- 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) - util:DoSomeThing() - return { - { - version = "xxxx", - note = "LTS", - addition = { - { - name = "npm", - version = "8.8.8", - } - } - } - } + return erlangUtils.get_erlang_release_verions() end \ No newline at end of file diff --git a/hooks/env_keys.lua b/hooks/env_keys.lua index aa80ad2..bacb549 100644 --- a/hooks/env_keys.lua +++ b/hooks/env_keys.lua @@ -5,29 +5,11 @@ --- @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 + local mainPath = ctx.path .. "/release/bin" return { - { - key = "JAVA_HOME", - value = mainPath - }, { key = "PATH", - value = mainPath .. "/bin" - }, - { - key = "PATH", - value = mainPath .. "/bin2" + value = mainPath }, - } - end \ No newline at end of file diff --git a/hooks/post_install.lua b/hooks/post_install.lua index 4bf262a..b1cbb34 100644 --- a/hooks/post_install.lua +++ b/hooks/post_install.lua @@ -3,10 +3,28 @@ --- Currently can be left unimplemented! function PLUGIN:PostInstall(ctx) --- ctx.rootPath SDK installation directory - local rootPath = ctx.rootPath - local sdkInfo = ctx.sdkInfo['sdk-name'] + -- use ENV OTP_COMPILE_ARGS to control compile behavior + local compile_args = os.getenv("OTP_COMPILE_ARGS") or "" + print("Erlang/OTP compile with: %s", compile_args) + + -- use ENV OTP_BUILD_DOCS to control bytecode with docs chunks + local with_docs_chunks = os.getenv("OTP_BUILD_DOCS") or "" + + print("If you enable some Erlang/OTP features, maybe you can reference this guide: https://github.com/asdf-vm/asdf-erlang?tab=readme-ov-file#before-asdf-install") + os.execute("sleep " .. tonumber(3)) + + local sdkInfo = ctx.sdkInfo['erlang'] local path = sdkInfo.path - local version = sdkInfo.version - local name = sdkInfo.name - local note = sdkInfo.note + + local install_erlang_cmd = "cd " .. path .. " && ./configure --prefix=" .. path .. "/release " .. compile_args .. "&& make && make install" + -- install for IDE docs hits & type hits + local install_erlang_docs_cmd = "cd " .. path .. " && make docs DOC_TARGETS=chunks && make release_docs DOC_TARGETS=chunks" + + local status = os.execute(install_erlang_cmd .. " && " ..install_erlang_docs_cmd) + + -- local status = os.execute("cd " .. path .. " && ./configure --prefix=" .. path .. "/release " .. compile_args .. "&& make && make install") + -- status = os.execute("cd " .. path .. " && make docs DOC_TARGETS=chunks && make release_docs DOC_TARGETS=chunks") + if status ~= 0 then + error("Erlang/OTP install failed, please check the stdout for details. Make sure you have the required utilties: https://www.erlang.org/doc/installation_guide/install#required-utilities") + end end \ No newline at end of file diff --git a/hooks/pre_install.lua b/hooks/pre_install.lua index 1a9c34c..4c55e93 100644 --- a/hooks/pre_install.lua +++ b/hooks/pre_install.lua @@ -1,39 +1,24 @@ +local erlangUtils = require("erlang_utils") + --- 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 + + erlangUtils.check_platform() + + local erlang_version = ctx.version + if erlang_version == nil then + print("You will install the Erlang/OTP version is" .. erlang_version) + error("You must provide a version number for Erlang/OTP, eg: vfox install erlang@24.1") + end + erlangUtils.check_version_existence("https://github.com/erlang/otp/releases/tag/OTP-" .. erlang_version) + + local download_url = "https://github.com/erlang/otp/archive/refs/tags/OTP-" .. erlang_version .. ".tar.gz" 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", - } - } + version = erlang_version, + url = download_url } end \ No newline at end of file diff --git a/hooks/pre_use.lua b/hooks/pre_use.lua index daf6d36..0390b29 100644 --- a/hooks/pre_use.lua +++ b/hooks/pre_use.lua @@ -1,17 +1,18 @@ + --- 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 + local input_version = ctx.version --- user current used version local previousVersion = ctx.previousVersion --- installed sdks - local sdkInfo = ctx.installedSdks['version'] + local sdkInfo = ctx.installedSdks[input_version] local path = sdkInfo.path local name = sdkInfo.name - local version = sdkInfo.version + local used_version = sdkInfo.version --- working directory local cwd = ctx.cwd @@ -22,6 +23,6 @@ function PLUGIN:PreUse(ctx) --- return the version information return { - version = version, + version = used_version } end \ No newline at end of file diff --git a/lib/erlang_utils.lua b/lib/erlang_utils.lua new file mode 100644 index 0000000..b34846d --- /dev/null +++ b/lib/erlang_utils.lua @@ -0,0 +1,56 @@ +local http = require("http") + +local erlang_utils = {} + +local function peek_lua_table(o, indent) + indent = indent or 0 + + local function handle_table(t, currentIndent) + local result = {} + for k, v in pairs(t) do + local key = type(k) ~= 'number' and ('[' .. peek_lua_table(k, currentIndent + 1) .. ']') or '' + local value = type(v) == 'table' and handle_table(v, currentIndent + 1) or peek_lua_table(v, currentIndent + 1) + table.insert(result, key .. (key ~= '' and ' = ' or '') .. value) + end + return '{\n' .. table.concat(result, ',\n') .. '\n' .. string.rep(' ', currentIndent) .. '}' + end + + if type(o) == 'table' then + return handle_table(o, indent) + else + return tostring(o) + end +end + +function erlang_utils.check_platform() + if RUNTIME.OS_TYPE == "windows" then + error("Windows is not supported. Please direct use the offcial installer to setup Erlang/OTP. visit: https://www.erlang.org/downloads") + end +end + +function erlang_utils.check_version_existence(url) + local resp, err = http.get({ + url = url + }) + if err ~= nil or resp.status_code ~= 200 then + error("Please confirm whether the corresponding Erlang/OTP release version exists! visit: https://github.com/erlang/otp/releases") + end +end + +function erlang_utils.get_erlang_release_verions() + local resp, err = http.get({ + url = "https://github.com/raw/yeshan333/vfox-erlang/main/.github/workflows/versions.txt" + }) + local result = {} + -- print("resp.body: " .. resp.body) + print(type(resp.body)) + for version in string.gmatch(resp.body, '([^\n]+)') do + table.insert(result, { + version = version + }) + end + -- print(peek_lua_table(result)) + return result +end + +return erlang_utils \ No newline at end of file diff --git a/lib/util.lua b/lib/util.lua deleted file mode 100644 index 7a7f8d9..0000000 --- a/lib/util.lua +++ /dev/null @@ -1,8 +0,0 @@ - -local util = {} - -function util:DoSomeThing() - print("Do some thing") -end - -return util \ No newline at end of file diff --git a/metadata.lua b/metadata.lua index d221628..ac99624 100644 --- a/metadata.lua +++ b/metadata.lua @@ -3,15 +3,15 @@ PLUGIN = {} --- !!! MUST BE SET !!! --- Plugin name -PLUGIN.name = "your plugin name" +PLUGIN.name = "erlang" --- Plugin version PLUGIN.version = "0.0.1" --- Plugin homepage -PLUGIN.homepage = "https://github.com/version-fox/vfox-plugin-template" +PLUGIN.homepage = "https://github.com/version-fox/vfox-erlang" --- 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 = "Erlang/OTP vfox plugin, support for managing multiple Erlang/OTP versions." --- !!! OPTIONAL !!!