Skip to content

Commit

Permalink
xim: update runtime info
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisepeak committed Jan 11, 2025
1 parent 41fdeb3 commit 47c74b1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
11 changes: 8 additions & 3 deletions core/xim/base/runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import("platform")
pkginfo = {
version = "0.0.0",
install_file = "xim-0.0.0.exe",
projectdir = "repo",
install_dir = "name/version",
}

xim_data_dir = {
Expand All @@ -16,6 +16,7 @@ xim_data_dir = {
}

xim_data_dir = xim_data_dir[os.host()]
xim_install_basedir = path.join(xim_data_dir, "xpkgs")

if not os.isdir(xim_data_dir) then
os.mkdir(xim_data_dir)
Expand All @@ -32,8 +33,8 @@ function set_pkginfo(info)
if info.install_file then
pkginfo.install_file = info.install_file
end
if info.projectdir then
pkginfo.projectdir = info.projectdir
if info.install_dir then
pkginfo.install_dir = info.install_dir
end
end

Expand All @@ -49,6 +50,10 @@ function get_xim_data_dir()
return xim_data_dir
end

function get_xim_install_basedir()
return xim_install_basedir
end

function main()
print(xim_data_dir)
end
2 changes: 1 addition & 1 deletion core/xim/pm/XPackage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ package = {
import("xim.base.runtime")
-- pkginfo = runtime.get_pkginfo()
-- pkginfo = {install_file = "", projectdir = "", version = "x.x.x"}
-- pkginfo = {install_file = "", install_dir = "", version = "x.x.x"}
-- step 1: support check - package attribute
Expand Down
57 changes: 32 additions & 25 deletions core/xim/pm/XPkgManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ end

function XPkgManager:installed(xpkg)

_set_install_file(xpkg)

local ret = _try_execute_hook(xpkg.name, xpkg.hooks, "installed")
local ret = _try_execute_hook(xpkg.name, xpkg, "installed")
if type(ret) == "boolean" then
return ret
elseif type(ret) == "string" then
Expand Down Expand Up @@ -53,7 +51,6 @@ function XPkgManager:download(xpkg)
if string.find(url, "%.git$") then
local pdir = path.join(download_dir, path.basename(url))
os.exec("git clone --depth=1 " .. url .. " " .. pdir)
runtime.set_pkginfo({ projectdir = pdir })
else
local ok, filename = utils.try_download_and_check(url, download_dir, sha256)
if not ok then -- retry download
Expand Down Expand Up @@ -86,20 +83,21 @@ function XPkgManager:deps(xpkg)
end

function XPkgManager:build(xpkg)
return _try_execute_hook(xpkg.name, xpkg.hooks, "build")
return _try_execute_hook(xpkg.name, xpkg, "build")
end

function XPkgManager:install(xpkg)
return _try_execute_hook(xpkg.name, xpkg.hooks, "install")
return _try_execute_hook(xpkg.name, xpkg, "install")
end

function XPkgManager:config(xpkg)
return _try_execute_hook(xpkg.name, xpkg.hooks, "config")
return _try_execute_hook(xpkg.name, xpkg, "config")
end

function XPkgManager:uninstall(xpkg)
_set_install_file(xpkg)
return _try_execute_hook(xpkg.name, xpkg.hooks, "uninstall")
local ret = _try_execute_hook(xpkg.name, xpkg, "uninstall")
os.tryrm(runtime.get_pkginfo().install_dir)
return ret
end

function XPkgManager:info(xpkg)
Expand Down Expand Up @@ -139,25 +137,34 @@ function XPkgManager:info(xpkg)
cprint("")
end

function _set_install_file(xpkg)
function _try_execute_hook(name, xpkg, action)
if xpkg.hooks[action] then
_set_runtime_info(xpkg)
return xpkg.hooks[action]()
else
cprint("[xlings:xim]: ${dim}package %s no implement${clear} %s", action, name)
end
return false
end

local _set_runtime_info_called = false
function _set_runtime_info(xpkg)
if _set_runtime_info_called then return end

local url = xpkg:get_xpm_resources().url
local datadir = runtime.get_xim_data_dir()
local install_dir = path.join(runtime.get_xim_install_basedir(), xpkg.name, xpkg.version)

if url then
local datadir = runtime.get_xim_data_dir()
if string.find(url, ".git", 1, true) then
local pdir = path.join(datadir, path.basename(url))
runtime.set_pkginfo({ projectdir = pdir })
else
local filename = path.join(datadir, path.filename(url))
runtime.set_pkginfo({ install_file = filename })
end
local filename = path.join(datadir, path.filename(url))
runtime.set_pkginfo({ install_file = filename })
end
end

function _try_execute_hook(name, hooks, action)
if hooks[action] then
return hooks[action]()
else
cprint("[xlings:xim]: ${dim}package %s no implement${clear} %s", action, name)
if not os.isdir(install_dir) then
os.mkdir(install_dir)
end
return false

runtime.set_pkginfo({ install_dir = install_dir })

_set_runtime_info_called = true
end

0 comments on commit 47c74b1

Please sign in to comment.