🚚 (split): 从 moon-source 拆出独立仓库,更新只管本插件
书源插件自成仓库、独立发版;update.lua 改为只检查并更新本插件(仓库 GarfieldMoon/moonsource-netease), 打包、测试脚本与发布流程改为单插件,跨书源的测试按插件拆开。公共文件见 moonsource-template。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
1 parent
ec1cc71e48
commit
a8daa509c9
8 files changed
+996
-110
No files matched your search
@@ -1,19 +1,19 @@
|
||||
--[[--
|
||||
书源插件在线更新(Codeberg Release)。
|
||||
|
||||
本文件是 src/_common/update.lua 的副本,由 tools/sync-common.sh 同步到每个插件的 <包名>/update.lua,
|
||||
请改 src/_common/update.lua,不要手改副本。副本按自己的包名(require 时的模块名)工作,
|
||||
各插件互不共享模块,不会因为版本不同互相覆盖。
|
||||
本文件是 moonsource-template 仓库 common/update.lua 的副本,由 tools/sync.sh 同步到每个插件仓库的
|
||||
<包名>/update.lua,请改模板仓库,不要手改副本。
|
||||
|
||||
所有书源插件同一个版本号、同一个 Release:任何一个插件发现新版本,同目录下已安装的 moonsource-*.koplugin
|
||||
就都需要更新,所以一次更新会把已安装的全部书源插件一起换成最新包。
|
||||
包体校验值取自同一 Release 的 SHA256SUMS。
|
||||
每个插件在自己的仓库(GarfieldMoon/moonsource-<名字>)独立发版,只检查并更新它自己:
|
||||
仓库名和目录名都由包名(require 时的模块名)推出,包体校验值取自同一 Release 的 SHA256SUMS。
|
||||
|
||||
@module moonsource.update
|
||||
--]]
|
||||
|
||||
local PKG = (...):match("^(.-)%.update$")
|
||||
local REPO = "GarfieldTony/moon-source"
|
||||
local NAME = PKG:gsub("_", "-")
|
||||
local DIR = NAME .. ".koplugin"
|
||||
local REPO = "GarfieldMoon/" .. NAME
|
||||
local SITE = "https://codeberg.org"
|
||||
local API = SITE .. "/api/v1/repos/" .. REPO .. "/releases/latest"
|
||||
local CHECK_INTERVAL = 24 * 60 * 60
|
||||
@@ -25,14 +25,14 @@ local T = require("ffi/util").template
|
||||
local CATALOG = {
|
||||
en = {
|
||||
["下载并安装"] = "Download and install",
|
||||
["书源插件已是最新(%1)。重新下载并替换已安装的 %2 个书源插件?"] = "Source plugins are up to date (%1). Download again and replace the %2 installed source plugin(s)?",
|
||||
["「%1」已是最新(%2)。重新下载并替换已安装的版本?"] = "%1 is up to date (%2). Download again and replace the installed version?",
|
||||
["书源插件更新"] = "Source plugin update",
|
||||
["书源插件更新失败:%1"] = "Source plugin update failed: %1",
|
||||
["关"] = "Off",
|
||||
["发现书源插件新版本 %1(当前 %2)。会一并更新已安装的 %3 个书源插件,更新后重启 KOReader。"] = "Source plugins %1 available (current %2). All %3 installed source plugin(s) will be updated together, then KOReader restarts.",
|
||||
["发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"] = "%1 %2 is available (current %3). KOReader restarts after the update.",
|
||||
["已是最新版本(%1)"] = "Already up to date (%1)",
|
||||
["开"] = "On",
|
||||
["所有书源插件同版本,一起更新"] = "All source plugins share one version and update together",
|
||||
["只更新本书源,其它书源各自独立更新"] = "Updates this source only; other sources update on their own",
|
||||
["新版本缺少插件包,请稍后再试"] = "The new release is missing plugin packages. Try again later.",
|
||||
["更新书源插件"] = "Updating source plugins",
|
||||
["更新信息无效,请稍后再试"] = "Invalid update information. Try again later.",
|
||||
@@ -42,26 +42,25 @@ local CATALOG = {
|
||||
["检查书源插件更新"] = "Check source plugin updates",
|
||||
["检查更新"] = "Check for updates",
|
||||
["检查更新失败:%1"] = "Update check failed: %1",
|
||||
["正在下载 %1(%2/%3)"] = "Downloading %1 (%2/%3)",
|
||||
["正在下载…"] = "Downloading…",
|
||||
["正在检查书源插件更新…"] = "Checking for source plugin updates…",
|
||||
["正在检查或安装更新,请稍候"] = "An update check or install is in progress. Please wait.",
|
||||
["稍后"] = "Later",
|
||||
["自动检查书源插件更新"] = "Check source plugin updates automatically",
|
||||
["更新"] = "Updates",
|
||||
["%1 更新"] = "%1 updates",
|
||||
["书源插件检查更新与自动更新"] = "Source plugin update checks and auto-update",
|
||||
["书源插件"] = "Source plugins",
|
||||
},
|
||||
zh_TW = {
|
||||
["下载并安装"] = "下載並安裝",
|
||||
["书源插件已是最新(%1)。重新下载并替换已安装的 %2 个书源插件?"] = "書源外掛已是最新(%1)。重新下載並替換已安裝的 %2 個書源外掛?",
|
||||
["「%1」已是最新(%2)。重新下载并替换已安装的版本?"] = "「%1」已是最新(%2)。重新下載並替換已安裝的版本?",
|
||||
["书源插件更新"] = "書源外掛更新",
|
||||
["书源插件更新失败:%1"] = "書源外掛更新失敗:%1",
|
||||
["关"] = "關",
|
||||
["发现书源插件新版本 %1(当前 %2)。会一并更新已安装的 %3 个书源插件,更新后重启 KOReader。"] = "發現書源外掛新版本 %1(目前 %2)。會一併更新已安裝的 %3 個書源外掛,更新後重新啟動 KOReader。",
|
||||
["发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"] = "發現「%1」新版本 %2(目前 %3),更新後重新啟動 KOReader。",
|
||||
["已是最新版本(%1)"] = "已是最新版本(%1)",
|
||||
["开"] = "開",
|
||||
["所有书源插件同版本,一起更新"] = "所有書源外掛同版本,一起更新",
|
||||
["只更新本书源,其它书源各自独立更新"] = "只更新本書源,其它書源各自獨立更新",
|
||||
["新版本缺少插件包,请稍后再试"] = "新版本缺少外掛包,請稍後再試",
|
||||
["更新书源插件"] = "更新書源外掛",
|
||||
["更新信息无效,请稍后再试"] = "更新資訊無效,請稍後再試",
|
||||
@@ -71,13 +70,12 @@ local CATALOG = {
|
||||
["检查书源插件更新"] = "檢查書源外掛更新",
|
||||
["检查更新"] = "檢查更新",
|
||||
["检查更新失败:%1"] = "檢查更新失敗:%1",
|
||||
["正在下载 %1(%2/%3)"] = "正在下載 %1(%2/%3)",
|
||||
["正在下载…"] = "正在下載…",
|
||||
["正在检查书源插件更新…"] = "正在檢查書源外掛更新…",
|
||||
["正在检查或安装更新,请稍候"] = "正在檢查或安裝更新,請稍候",
|
||||
["稍后"] = "稍後",
|
||||
["自动检查书源插件更新"] = "自動檢查書源外掛更新",
|
||||
["更新"] = "更新",
|
||||
["%1 更新"] = "%1 更新",
|
||||
["书源插件检查更新与自动更新"] = "書源外掛檢查更新與自動更新",
|
||||
["书源插件"] = "書源外掛",
|
||||
},
|
||||
@@ -96,14 +94,15 @@ end
|
||||
|
||||
local Update = {}
|
||||
|
||||
-- 同一进程里各插件共用:只自动检查一次、同一版本只弹一次
|
||||
-- 按包名分状态:各书源互不影响,各自只自动检查一次、同一版本只弹一次
|
||||
local function shared()
|
||||
local state = rawget(_G, "__moonsource_update")
|
||||
if not state then
|
||||
state = {}
|
||||
rawset(_G, "__moonsource_update", state)
|
||||
local all = rawget(_G, "__moonsource_update")
|
||||
if not all then
|
||||
all = {}
|
||||
rawset(_G, "__moonsource_update", all)
|
||||
end
|
||||
return state
|
||||
all[PKG] = all[PKG] or {}
|
||||
return all[PKG]
|
||||
end
|
||||
|
||||
local function pluginsDir()
|
||||
@@ -139,22 +138,6 @@ function Update.newer(candidate, installed)
|
||||
return installed:find("%-") ~= nil and candidate:find("%-") == nil
|
||||
end
|
||||
|
||||
--- 同目录下已装的书源插件目录名(含本插件)。
|
||||
---@return string[]
|
||||
function Update.installed()
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local parent, out = pluginsDir(), {}
|
||||
if not parent then return out end
|
||||
for name in lfs.dir(parent) do
|
||||
if name:match("^moonsource%-[%w_]+%.koplugin$")
|
||||
and lfs.attributes(parent .. "/" .. name .. "/main.lua", "mode") == "file" then
|
||||
out[#out + 1] = name
|
||||
end
|
||||
end
|
||||
table.sort(out)
|
||||
return out
|
||||
end
|
||||
|
||||
local function parseSums(body)
|
||||
local sums = {}
|
||||
for hash, name in tostring(body):gmatch("(%x+)%s+%*?(%S+)") do
|
||||
@@ -205,7 +188,7 @@ local function launch(start)
|
||||
if not settled then job = handle end
|
||||
end
|
||||
|
||||
--- 查询最新稳定版,并为每个已装插件配好 { dir, file, url, size, sha256 }。
|
||||
--- 查询最新稳定版,并配好本插件的安装包 release.target = { dir, file, url, size, sha256 }。
|
||||
---@param cb fun(release: table|nil, err: any)
|
||||
function Update.check(cb)
|
||||
if job then return cb(nil, "busy") end
|
||||
@@ -213,15 +196,10 @@ function Update.check(cb)
|
||||
local timeout = require("http.timeout").net(1)
|
||||
local function finish(release, sums_body)
|
||||
local sums = parseSums(sums_body)
|
||||
release.targets = {}
|
||||
for _idx, dir in ipairs(Update.installed()) do
|
||||
local file = dir .. "-" .. release.version .. ".zip"
|
||||
local asset = release.assets[file]
|
||||
if asset and sums[file] then
|
||||
release.targets[#release.targets + 1] = { dir = dir, file = file, url = asset.url, size = asset.size, sha256 = sums[file] }
|
||||
end
|
||||
end
|
||||
if #release.targets == 0 then return cb(nil, "release has no plugin archive") end
|
||||
local file = DIR .. "-" .. release.version .. ".zip"
|
||||
local asset = release.assets[file]
|
||||
if not (asset and sums[file]) then return cb(nil, "release has no plugin archive") end
|
||||
release.target = { dir = DIR, file = file, url = asset.url, size = asset.size, sha256 = sums[file] }
|
||||
release.installed = Update.version()
|
||||
release.available = Update.newer(release.version, release.installed)
|
||||
local settings = require("utils.settings")
|
||||
@@ -245,59 +223,53 @@ function Update.check(cb)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 依次下载、校验并换掉每个已装的书源插件目录。
|
||||
--- 下载、校验并换掉已装的本插件目录。
|
||||
---@param release table Update.check 的结果
|
||||
---@param cb fun(ok: boolean, err: any)
|
||||
---@param on_step fun(index: integer, total: integer, bytes: integer|nil, target: table)|nil
|
||||
function Update.install(release, cb, on_step)
|
||||
---@param on_progress fun(bytes: integer|nil, target: table)|nil
|
||||
function Update.install(release, cb, on_progress)
|
||||
if job then return cb(false, "busy") end
|
||||
local Request, Job = require("http.request"), require("workers.job")
|
||||
local Paths, Install = require("utils.paths"), require("utils.plugin_install")
|
||||
local parent, targets = pluginsDir(), release.targets
|
||||
local target = release.target
|
||||
Paths.ensureDir(Paths.root())
|
||||
local function step(i)
|
||||
local target = targets[i]
|
||||
if not target then return cb(true) end
|
||||
if on_step then on_step(i, #targets, 0, target) end
|
||||
local archive = Paths.root() .. "/" .. target.dir .. ".update.zip"
|
||||
os.remove(archive)
|
||||
launch(function(wrap)
|
||||
return Request.download({
|
||||
url = target.url,
|
||||
timeout = require("http.timeout").net(10),
|
||||
allow_redirects = true,
|
||||
max_bytes = MAX_ARCHIVE_BYTES,
|
||||
on_progress = function(bytes)
|
||||
if on_step then on_step(i, #targets, bytes, target) end
|
||||
end,
|
||||
}, archive, wrap(function(ok, err)
|
||||
if not ok then
|
||||
os.remove(archive)
|
||||
return cb(false, err)
|
||||
local archive = Paths.root() .. "/" .. target.dir .. ".update.zip"
|
||||
os.remove(archive)
|
||||
launch(function(wrap)
|
||||
return Request.download({
|
||||
url = target.url,
|
||||
timeout = require("http.timeout").net(10),
|
||||
allow_redirects = true,
|
||||
max_bytes = MAX_ARCHIVE_BYTES,
|
||||
on_progress = function(bytes)
|
||||
if on_progress then on_progress(bytes, target) end
|
||||
end,
|
||||
}, archive, wrap(function(ok, err)
|
||||
if not ok then
|
||||
os.remove(archive)
|
||||
return cb(false, err)
|
||||
end
|
||||
launch(function(wrap_job)
|
||||
local function settle(failure)
|
||||
return wrap_job(function()
|
||||
os.remove(archive)
|
||||
if failure then return cb(false, failure) end
|
||||
cb(true)
|
||||
end)
|
||||
end
|
||||
launch(function(wrap_job)
|
||||
local function settle(failure)
|
||||
return wrap_job(function()
|
||||
os.remove(archive)
|
||||
if failure then return cb(false, failure) end
|
||||
step(i + 1)
|
||||
end)
|
||||
end
|
||||
return Job.run(function()
|
||||
local installed, detail = Install.run(archive, parent, target.sha256, { name = target.dir })
|
||||
if not installed then error(detail, 0) end
|
||||
end, {
|
||||
name = "moonsource.update",
|
||||
kind = "heavy",
|
||||
timeout = 120,
|
||||
on_done = settle(),
|
||||
on_failed = function(install_err) settle(install_err)() end,
|
||||
})
|
||||
end)
|
||||
end))
|
||||
end)
|
||||
end
|
||||
step(1)
|
||||
return Job.run(function()
|
||||
local installed, detail = Install.run(archive, pluginsDir(), target.sha256, { name = target.dir })
|
||||
if not installed then error(detail, 0) end
|
||||
end, {
|
||||
name = "moonsource.update",
|
||||
kind = "heavy",
|
||||
timeout = 120,
|
||||
on_done = settle(),
|
||||
on_failed = function(install_err) settle(install_err)() end,
|
||||
})
|
||||
end)
|
||||
end))
|
||||
end)
|
||||
end
|
||||
|
||||
local function errorText(err)
|
||||
@@ -330,10 +302,9 @@ local function startInstall(release)
|
||||
end
|
||||
dialog:close()
|
||||
require("ui.themes.widgets.toast"):new{ text = T(_("书源插件更新失败:%1"), errorText(err)), timeout = 5 }:show()
|
||||
end, function(index, total, bytes, target)
|
||||
local name = target.dir:gsub("%.koplugin$", "")
|
||||
end, function(bytes, target)
|
||||
local fraction = target.size and target.size > 0 and bytes and math.min(1, bytes / target.size) or 0
|
||||
dialog:set(T(_("正在下载 %1(%2/%3)"), name, index, total), math.floor(((index - 1) + fraction) / total * 100))
|
||||
dialog:set(_("正在下载…"), math.floor(fraction * 100))
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -344,10 +315,10 @@ function Update.prompt(release, force)
|
||||
local state = shared()
|
||||
if not force and state.offered == release.version then return end
|
||||
state.offered = release.version
|
||||
local header = T(_("发现书源插件新版本 %1(当前 %2)。会一并更新已安装的 %3 个书源插件,更新后重启 KOReader。"),
|
||||
release.version, release.installed, #release.targets)
|
||||
local name = require(PKG .. "._meta").name
|
||||
local header = T(_("发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"), name, release.version, release.installed)
|
||||
if force and not release.available then
|
||||
header = T(_("书源插件已是最新(%1)。重新下载并替换已安装的 %2 个书源插件?"), release.version, #release.targets)
|
||||
header = T(_("「%1」已是最新(%2)。重新下载并替换已安装的版本?"), name, release.version)
|
||||
end
|
||||
local dialog
|
||||
dialog = require("ui.themes.widgets.text_viewer"):new{
|
||||
@@ -393,7 +364,7 @@ local function autoEnabled()
|
||||
return require("utils.settings").getSource(sourceId()).update_auto_check ~= false
|
||||
end
|
||||
|
||||
--- 按 24 小时间隔静默检查;只提示,不自动安装。同一进程只由第一个满足条件的插件发起。
|
||||
--- 按 24 小时间隔静默检查;只提示,不自动安装。
|
||||
function Update.autoCheck()
|
||||
local state = shared()
|
||||
if state.checked or not autoEnabled() then return end
|
||||
@@ -411,22 +382,20 @@ function Update.register()
|
||||
require("utils.extension").add("desktop.activated", function() Update.autoCheck() end)
|
||||
end
|
||||
|
||||
--- 设置页「书库 → 更新」。所有书源插件同版本、一起更新,只由第一个被问到的插件登记这一项,其余返回空。
|
||||
--- 设置页「书库 → 更新」:每个书源各登记一项,只管自己。
|
||||
---@param desktop table
|
||||
---@return table[]
|
||||
function Update.entries(desktop)
|
||||
local state = shared()
|
||||
state.owner = state.owner or PKG
|
||||
if state.owner ~= PKG then return {} end
|
||||
local name = require(PKG .. "._meta").name
|
||||
local Row = require("ui.themes.widgets.row")
|
||||
return {
|
||||
{
|
||||
group = "library", order = 40, id = "moonsource_update", icon = "system_update",
|
||||
title = _("更新"),
|
||||
group = "library", order = 40, id = "moonsource_update_" .. sourceId(), icon = "system_update",
|
||||
title = T(_("%1 更新"), name),
|
||||
subtitle = _("书源插件检查更新与自动更新"),
|
||||
status = Update.version(),
|
||||
page = {
|
||||
title = _("更新"),
|
||||
title = T(_("%1 更新"), name),
|
||||
sections = function()
|
||||
return {{
|
||||
title = _("书源插件"),
|
||||
@@ -435,7 +404,7 @@ function Update.entries(desktop)
|
||||
return Row.widget({
|
||||
width = iw, chevron = true, kind = "nav", icon = "system_update",
|
||||
title = _("检查书源插件更新"),
|
||||
subtitle = _("所有书源插件同版本,一起更新"),
|
||||
subtitle = _("只更新本书源,其它书源各自独立更新"),
|
||||
status = Update.version(),
|
||||
callback = function() Update.manualCheck() end,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user