Skip to content

Commit

Permalink
migrate old python plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed Apr 1, 2024
1 parent bb91a0b commit 7bf289b
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 145 deletions.
20 changes: 2 additions & 18 deletions hooks/available.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
local util = require("util")

--- 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
require("util")
function PLUGIN:Available(ctx)
util:DoSomeThing()
return {
{
version = "xxxx",
note = "LTS",
addition = {
{
name = "npm",
version = "8.8.8",
}
}
}
}
return parseVersion()
end
50 changes: 20 additions & 30 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
@@ -1,33 +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
--- @field ctx.path string SDK installation directory
require("util")
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 = "JAVA_HOME",
value = mainPath
},
{
key = "PATH",
value = mainPath .. "/bin"
},
{
key = "PATH",
value = mainPath .. "/bin2"
},

}

if RUNTIME.osType == "windows" then
return {
{
key = "PATH",
value = mainPath,
},
{
key = "PATH",
value = mainPath .. "\\Scripts"
}
}
else
return {
{
key = "PATH",
value = mainPath .. "/bin"
}
}
end
end
16 changes: 6 additions & 10 deletions hooks/post_install.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
--- 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!
require("util")
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
if OS_TYPE == "windows" then
return windowsCompile(ctx)
else
return linuxCompile(ctx)
end
end
54 changes: 19 additions & 35 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
--- 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
require("util")
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 version == "latest" then
version = self:Available({})[1].version
end
if not checkIsReleaseVersion(version) then
error("The current version is not released")
return
end
if OS_TYPE == "windows" then
local url, filename = checkAvailableReleaseForWindows(version)
return {
version = version,
url = url,
note = filename
}
}
else
return {
version = version,
}
end
end
27 changes: 0 additions & 27 deletions hooks/pre_use.lua

This file was deleted.

8 changes: 0 additions & 8 deletions lib/uilt.lua

This file was deleted.

147 changes: 147 additions & 0 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
local http = require("http")
local html = require("html")

local PYTHON_URL = "https://www.python.org/ftp/python/"

local DOWNLOAD_SOURCE = {
--- TODO support zip or web-based installers
WEB_BASED = "https://www.python.org/ftp/python/%s/python-%s%s-webinstall.exe",
ZIP = "https://www.python.org/ftp/python/%s/python-%s-embed-%s.zip",
MSI = "",
--- Currently only exe installers are supported
EXE = "https://www.python.org/ftp/python/%s/python-%s%s.exe",
SOURCE = "https://www.python.org/ftp/python/%s/Python-%s.tar.xz"
}

function getMirror()
local mirror = os.getenv("VFOX_PYTHON_MIRROR")
if mirror == nil then
return "https://www.python.org/ftp/python/"
end
return mirror
end

function checkIsReleaseVersion(version)
local resp, err = http.head({
url = DOWNLOAD_SOURCE.SOURCE:format(version, version)
})
if err ~= nil or resp.status_code ~= 200 then
return false
end
return true
end
function windowsCompile(ctx)
local sdkInfo = ctx.sdkInfo['python']
local path = sdkInfo.path
local filename = sdkInfo.note
--- Attention system difference
local qInstallFile = path .. "\\" .. filename
local qInstallPath = path
--local exitCode = os.execute('msiexec /quiet /a "' .. qInstallFile .. '" TargetDir="' .. qInstallPath .. '"')
print("Installing python, please wait patiently for a while, about two minutes.")
local exitCode = os.execute(qInstallFile .. ' /quiet InstallAllUsers=0 PrependPath=0 TargetDir=' .. qInstallPath)
if exitCode ~= 0 then
error("error installing python")
end
print("Cleaning up ...")
os.remove(qInstallFile)
end
function linuxCompile(ctx)
local sdkInfo = ctx.sdkInfo['python']
local path = sdkInfo.path
local version = sdkInfo.version
local pyenv_url = "https://github.com/pyenv/pyenv.git"
local dest_pyenv_path = ctx.rootPath .. "/pyenv"
local status = os.execute("git clone " .. pyenv_url .. " " .. dest_pyenv_path)
if status ~= 0 then
error("git clone failed")
end
local pyenv_build_path = dest_pyenv_path .. "/plugins/python-build/bin/python-build"
print("Building python ...")
status = os.execute(pyenv_build_path .. " " .. version .. " " .. path)
if status ~= 0 then
error("python build failed")
end
print("Build python success!")
print("Cleaning up ...")
status = os.execute("rm -rf " .. dest_pyenv_path)
if status ~= 0 then
error("remove build tool failed")
end
end
function checkAvailableReleaseForWindows(version)
local archType = RUNTIME.archType
if archType == "386" then
archType = ""
else
archType = "-" .. archType
end
--- Currently only exe installers are supported
--- TODO support zip or web-based installers
local url = DOWNLOAD_SOURCE.EXE:format(version, version, archType)
local resp, err = http.head({
url = url
})
if err ~= nil or resp.status_code ~= 200 then
error("No available installer found for current version")
end
return url, "python-" .. version .. archType .. ".exe"
end
function parseVersion()
local resp, err = http.get({
url = PYTHON_URL
})
if err ~= nil or resp.status_code ~= 200 then
error("paring release info failed." .. err)
end
local result = {}
html.parse(resp.body):find("a"):each(function(i, selection)
local href = selection:attr("href")
local sn = string.match(href, "^%d")
local es = string.match(href, "/$")
if sn and es then
local vn = string.sub(href, 1, -2)
if RUNTIME.osType == "windows" then
if compare_versions(vn, "3.5.0") >= 0 then
table.insert(result, {
version = string.sub(href, 1, -2),
note = "",
})
end
else
table.insert(result, {
version = vn,
note = "",
})
end
end
end)
table.sort(result, function(a, b)
return compare_versions(a.version, b.version) > 0
end)
return result
end

function compare_versions(v1, v2)
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 1
elseif v1_part < v2_part then
return -1
end
end

return 0
end
26 changes: 9 additions & 17 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 = "python"
--- 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-python"
--- 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 = "Python language support, https://www.python.org"


--- !!! OPTIONAL !!!
Expand All @@ -22,19 +22,11 @@ NOTE:
vfox will not load the plugin and prompt the user to upgrade vfox.
--]]
PLUGIN.minRuntimeVersion = "0.3.0"
--[[
NOTE:
If configured, vfox will check for updates to the plugin at this address,
otherwise it will check for updates at the global registry.
If you want use the global registry to distribute your plugin, you can remove this field.
If you develop a plugin based on the template, which will automatically generate a manifest file by CI,
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"
-- Some things that need user to be attention!
PLUGIN.notes = {
"",
"Mirror Setting:",
"You can use VFOX_PYTHON_MIRROR environment variable to set mirror.",
" ",
"Others:",
"For Windows, only support >=3.5.0, but no restrictions for unix-like."
}

0 comments on commit 7bf289b

Please sign in to comment.