From a2a4fe766b7c6b58f0580ca97225a4adc977d94a Mon Sep 17 00:00:00 2001 From: sunrisepeak Date: Thu, 4 Dec 2025 23:38:08 +0800 Subject: [PATCH] core/xim: add update command, and sync schedule (default 7day Signed-off-by: sunrisepeak --- core/xim/CmdProcessor.lua | 20 ++++++++++++++++++-- core/xim/index/IndexManager.lua | 4 ++-- core/xim/index/RepoManager.lua | 26 ++++++++++++++++++++++++-- core/xlings.lua | 2 ++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/core/xim/CmdProcessor.lua b/core/xim/CmdProcessor.lua index 8b629e4..a81d053 100644 --- a/core/xim/CmdProcessor.lua +++ b/core/xim/CmdProcessor.lua @@ -81,6 +81,7 @@ function CmdProcessor:run_target_cmds() local pkg = index_manager:load_package(target_pkgname) self._pm_executor = pm_service:create_pm_executor(pkg) self.target = target_pkgname + self._input_target = input_target -- backup if self.cmds.info_json then local pkginfo = self._pm_executor._pkg:info() @@ -196,6 +197,8 @@ function CmdProcessor:run_nontarget_cmds() -- target is nil if self.cmds.list then self:list() + elseif self.cmds.update then + self:update() elseif self.cmds.sysdetect then self:sys_detect() elseif self.cmds.sysupdate then @@ -336,7 +339,20 @@ function CmdProcessor:remove() end function CmdProcessor:update() - cprint("[xlings:xim]: update not implement") + cprint("[xlings:xim]: ${dim bright}sync repo and rebuild index...${clear}") + + index_manager:sync_repo() + index_manager:rebuild() + self:sys_detect() + + -- TODO: support install latest version for target + if self._input_target and self._input_target ~= "" then + cprint("[xlings:xim]: ${bright}try to update [ %s ] to latest version...${clear}", self._input_target) + new(self._input_target .. "@latest", { + yes = true, + disable_info = true + }):run() + end end function CmdProcessor:sys_detect() @@ -360,7 +376,7 @@ end function CmdProcessor:sys_update() local datadir = runtime.get_xim_data_dir() if self.cmds.sysupdate == "index" then - index_manager:sync_repo() + index_manager:sync_repo(true) index_manager:rebuild() self:sys_detect() elseif self.cmds.sysupdate == "self" then diff --git a/core/xim/index/IndexManager.lua b/core/xim/index/IndexManager.lua index 08f3aea..ffbdf54 100644 --- a/core/xim/index/IndexManager.lua +++ b/core/xim/index/IndexManager.lua @@ -32,8 +32,8 @@ function IndexManager:update() IndexManager.status_changed_pkg = {} end -function IndexManager:sync_repo() - repo_manager:sync() +function IndexManager:sync_repo(force) + repo_manager:sync(force) end function IndexManager:rebuild() diff --git a/core/xim/index/RepoManager.lua b/core/xim/index/RepoManager.lua index b1a9417..c7a8cda 100644 --- a/core/xim/index/RepoManager.lua +++ b/core/xim/index/RepoManager.lua @@ -20,11 +20,16 @@ RepoManager.__index = RepoManager function new() local config = xconfig.load() RepoManager.repo = config.xim["index-repo"] - RepoManager.updateSchedule = 30 -- days + RepoManager.updateSchedule = 7 -- days return RepoManager end -function RepoManager:sync() +function RepoManager:sync(force) + + if not self:need_to_update(force) then + cprint("[xlings: xim]: ${dim}skip sync indexrepos, not reach update schedule(use `xim --update index` to force)...") + return + end cprint("[xlings: xim]: sync indexrepos...") @@ -135,6 +140,23 @@ function RepoManager:add_subrepo(namespace, repo) return _to_repodir(repo, index_reposdir) end +function RepoManager:need_to_update(force) + local last_update_file = path.join(data_dir, "xim_last_update_time") + if force or (not os.isfile(last_update_file)) then + io.writefile(last_update_file, tostring(os.time())) + return true + else + local last_update_time = tonumber(io.readfile(last_update_file)) + local current_time = os.time() + local delta_days = (current_time - last_update_time) / (24 * 3600) + if delta_days >= RepoManager.updateSchedule then + io.writefile(last_update_file, tostring(current_time)) + return true + end + end + return false +end + function _sync_repo(repo, rootdir) cprint("[xlings: xim]: sync package index repo: ${dim green}%s", repo) diff --git a/core/xlings.lua b/core/xlings.lua index 0f34820..2129278 100644 --- a/core/xlings.lua +++ b/core/xlings.lua @@ -254,6 +254,8 @@ function main() _command_dispatch(d2x, "checker", cmd_target, cmd_args) elseif command == "remove" then _command_dispatch(xinstall, "-r", cmd_target, cmd_args) + elseif command == "update" then + _command_dispatch(xinstall, "-u", cmd_target, cmd_args) elseif command == "config" then config_llm() else -- submodule