Skip to content

Commit 42ba4dc

Browse files
committed
feat: get versions from pyenv
1 parent c969823 commit 42ba4dc

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

hooks/available.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
require("util")
22
function PLUGIN:Available(ctx)
3-
return parseVersion()
3+
if OS_TYPE == "windows" then
4+
return parseVersion()
5+
else
6+
return parseVersionFromPyenv()
7+
end
48
end

hooks/pre_install.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
require("util")
22
function PLUGIN:PreInstall(ctx)
33
local version = ctx.version
4+
45
if version == "latest" then
56
version = self:Available({})[1].version
67
end
7-
if not checkIsReleaseVersion(version) then
8+
9+
if OS_TYPE == "windows" and not checkIsReleaseVersion(version) then
810
error("The current version is not released")
911
return
1012
end

lib/util.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local http = require("http")
22
local html = require("html")
3+
local json = require("json")
34

45
-- get mirror
56
local PYTHON_URL = "https://www.python.org/ftp/python/"
@@ -10,6 +11,8 @@ if VFOX_PYTHON_MIRROR then
1011
os.setenv("PYTHON_BUILD_MIRROR_URL", PYTHON_URL)
1112
end
1213

14+
local version_vault_url = "https://version-vault.cdn.dog/pyenv-versions"
15+
1316
-- request headers
1417
local REQUEST_HEADERS = {
1518
["User-Agent"] = "vfox"
@@ -231,6 +234,7 @@ function linuxCompile(ctx)
231234
error("remove build tool failed")
232235
end
233236
end
237+
234238
function getReleaseForWindows(version)
235239
local archType = RUNTIME.archType
236240
if archType == "386" then
@@ -259,7 +263,14 @@ function getReleaseForWindows(version)
259263
print("url:\t" .. url)
260264
error("No available installer found for current version")
261265
end
266+
267+
function fixHeaders()
268+
REQUEST_HEADERS["User-Agent"] = "vfox v" .. RUNTIME.version;
269+
end
270+
262271
function parseVersion()
272+
fixHeaders()
273+
263274
local resp, err = http.get({
264275
url = PYTHON_URL,
265276
headers = REQUEST_HEADERS
@@ -318,3 +329,54 @@ function compare_versions(v1, v2)
318329

319330
return 0
320331
end
332+
333+
function parseVersionFromPyenv()
334+
fixHeaders()
335+
local resp, err = http.get({
336+
url = version_vault_url,
337+
headers = REQUEST_HEADERS
338+
})
339+
if err ~= nil or resp.status_code ~= 200 then
340+
error("paring release info failed." .. err)
341+
end
342+
local result = {}
343+
local jsonObj = json.decode(resp.body)
344+
345+
local tagName = jsonObj.tagName;
346+
local versions = jsonObj.versions;
347+
348+
local numericVersions = {}
349+
local namedVersions = {}
350+
351+
for _, version in ipairs(versions) do
352+
if string.match(version, "^%d") then
353+
table.insert(numericVersions, version)
354+
else
355+
table.insert(namedVersions, version)
356+
end
357+
end
358+
359+
table.sort(numericVersions, function(a, b)
360+
return compare_versions(a, b) > 0
361+
end)
362+
363+
table.sort(namedVersions, function(a, b)
364+
return compare_versions(a, b) > 0
365+
end)
366+
367+
for _, version in ipairs(numericVersions) do
368+
table.insert(result, {
369+
version = version,
370+
note = ""
371+
})
372+
end
373+
374+
for _, version in ipairs(namedVersions) do
375+
table.insert(result, {
376+
version = version,
377+
note = ""
378+
})
379+
end
380+
381+
return result
382+
end

0 commit comments

Comments
 (0)