Skip to content

Commit

Permalink
feat: impl erlang plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yeshan333 committed Apr 1, 2024
1 parent 54818ab commit e8ffd66
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 95 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
16 changes: 2 additions & 14 deletions hooks/available.lua
Original file line number Diff line number Diff line change
@@ -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
22 changes: 2 additions & 20 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 23 additions & 5 deletions hooks/post_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 15 additions & 30 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions hooks/pre_use.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +23,6 @@ function PLUGIN:PreUse(ctx)

--- return the version information
return {
version = version,
version = used_version
}
end
56 changes: 56 additions & 0 deletions lib/erlang_utils.lua
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions lib/util.lua

This file was deleted.

6 changes: 3 additions & 3 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 = "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 !!!
Expand Down

0 comments on commit e8ffd66

Please sign in to comment.