Compare commits
3
Commits
d2cccc51e5
...
cdc1deb848
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdc1deb848 | ||
|
|
66fd8061ff | ||
|
|
577ad28380 |
No files matched your search
@@ -1,5 +1,10 @@
|
||||
# 更新日志
|
||||
|
||||
## 0.3.0
|
||||
|
||||
- :lipstick: (setting): 账号页使用书源自己的图标
|
||||
- 🔥 去掉书源自己的在线更新
|
||||
|
||||
## 0.0.1
|
||||
|
||||
- 「得到」书源拆成独立仓库,独立发版、独立更新;原来所有书源同版本一起更新的做法取消。
|
||||
|
||||
@@ -6,13 +6,10 @@ local function lazy(module)
|
||||
return setmetatable({}, { __index = function(_, key) return require(module)[key] end })
|
||||
end
|
||||
|
||||
require("moonsource_dedao.update").register()
|
||||
|
||||
return {
|
||||
api = 1,
|
||||
provides = {
|
||||
source = true,
|
||||
features = { setting = { entries = function(desktop) return require("moonsource_dedao.update").entries(desktop) end } },
|
||||
},
|
||||
meta = function() return require("moonsource_dedao.source").meta() end,
|
||||
new = function() return require("moonsource_dedao.source").new() end,
|
||||
|
||||
@@ -9,6 +9,11 @@ local CookieLogin = require("source.cookie_login")
|
||||
|
||||
local SOURCE_ID = "dedao"
|
||||
local Setting = {}
|
||||
|
||||
function Setting.rowIcon()
|
||||
return "lightbulb"
|
||||
end
|
||||
|
||||
local LOGIN_URL = "https://www.dedao.cn/"
|
||||
|
||||
---@return boolean
|
||||
|
||||
@@ -1,435 +0,0 @@
|
||||
--[[--
|
||||
书源插件在线更新(Forgejo Release)。
|
||||
|
||||
本文件是 moonsource-template 仓库 common/update.lua 的副本,由 tools/sync.sh 同步到每个插件仓库的
|
||||
<包名>/update.lua,请改模板仓库,不要手改副本。
|
||||
|
||||
每个插件在自己的仓库(moon-source/moonsource-<名字>)独立发版,只检查并更新它自己:
|
||||
仓库名和目录名都由包名(require 时的模块名)推出,包体校验值取自同一 Release 的 SHA256SUMS。
|
||||
|
||||
@module moonsource.update
|
||||
--]]
|
||||
|
||||
local PKG = (...):match("^(.-)%.update$")
|
||||
local NAME = PKG:gsub("_", "-")
|
||||
local DIR = NAME .. ".koplugin"
|
||||
local REPO = "moon-source/" .. NAME
|
||||
local SITE = "https://git.garfieldho.me"
|
||||
local API = SITE .. "/api/v1/repos/" .. REPO .. "/releases/latest"
|
||||
local CHECK_INTERVAL = 24 * 60 * 60
|
||||
local MAX_ARCHIVE_BYTES = 50 * 1024 * 1024
|
||||
local GetText = require("gettext")
|
||||
local T = require("ffi/util").template
|
||||
|
||||
-- 源文是简体中文;这点文案不值得单开 l10n 目录,翻译内联在这里。
|
||||
local CATALOG = {
|
||||
en = {
|
||||
["下载并安装"] = "Download and install",
|
||||
["「%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。"] = "%1 %2 is available (current %3). KOReader restarts after the update.",
|
||||
["已是最新版本(%1)"] = "Already up to date (%1)",
|
||||
["开"] = "On",
|
||||
["只更新本书源,其它书源各自独立更新"] = "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.",
|
||||
["更新包校验失败,请稍后再试"] = "Update package checksum failed. Try again later.",
|
||||
["更新包过大,已中止下载"] = "The update package is too large. Download aborted.",
|
||||
["更新完成,正在重启 KOReader…"] = "Update complete. Restarting KOReader…",
|
||||
["检查书源插件更新"] = "Check source plugin updates",
|
||||
["检查更新"] = "Check for updates",
|
||||
["检查更新失败:%1"] = "Update check failed: %1",
|
||||
["正在下载…"] = "Downloading…",
|
||||
["正在检查书源插件更新…"] = "Checking for source plugin updates…",
|
||||
["正在检查或安装更新,请稍候"] = "An update check or install is in progress. Please wait.",
|
||||
["稍后"] = "Later",
|
||||
["自动检查书源插件更新"] = "Check source plugin updates automatically",
|
||||
["%1 更新"] = "%1 updates",
|
||||
["书源插件检查更新与自动更新"] = "Source plugin update checks and auto-update",
|
||||
["书源插件"] = "Source plugins",
|
||||
},
|
||||
zh_TW = {
|
||||
["下载并安装"] = "下載並安裝",
|
||||
["「%1」已是最新(%2)。重新下载并替换已安装的版本?"] = "「%1」已是最新(%2)。重新下載並替換已安裝的版本?",
|
||||
["书源插件更新"] = "書源外掛更新",
|
||||
["书源插件更新失败:%1"] = "書源外掛更新失敗:%1",
|
||||
["关"] = "關",
|
||||
["发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"] = "發現「%1」新版本 %2(目前 %3),更新後重新啟動 KOReader。",
|
||||
["已是最新版本(%1)"] = "已是最新版本(%1)",
|
||||
["开"] = "開",
|
||||
["只更新本书源,其它书源各自独立更新"] = "只更新本書源,其它書源各自獨立更新",
|
||||
["新版本缺少插件包,请稍后再试"] = "新版本缺少外掛包,請稍後再試",
|
||||
["更新书源插件"] = "更新書源外掛",
|
||||
["更新信息无效,请稍后再试"] = "更新資訊無效,請稍後再試",
|
||||
["更新包校验失败,请稍后再试"] = "更新包校驗失敗,請稍後再試",
|
||||
["更新包过大,已中止下载"] = "更新包過大,已中止下載",
|
||||
["更新完成,正在重启 KOReader…"] = "更新完成,正在重新啟動 KOReader…",
|
||||
["检查书源插件更新"] = "檢查書源外掛更新",
|
||||
["检查更新"] = "檢查更新",
|
||||
["检查更新失败:%1"] = "檢查更新失敗:%1",
|
||||
["正在下载…"] = "正在下載…",
|
||||
["正在检查书源插件更新…"] = "正在檢查書源外掛更新…",
|
||||
["正在检查或安装更新,请稍候"] = "正在檢查或安裝更新,請稍候",
|
||||
["稍后"] = "稍後",
|
||||
["自动检查书源插件更新"] = "自動檢查書源外掛更新",
|
||||
["%1 更新"] = "%1 更新",
|
||||
["书源插件检查更新与自动更新"] = "書源外掛檢查更新與自動更新",
|
||||
["书源插件"] = "書源外掛",
|
||||
},
|
||||
}
|
||||
|
||||
local function _(msgid)
|
||||
local lang = tostring(GetText.current_lang or ""):gsub("%.[%w%-]+$", "")
|
||||
local catalog
|
||||
if lang:match("^zh_TW") or lang:match("^zh_HK") then
|
||||
catalog = CATALOG.zh_TW
|
||||
elseif not lang:match("^zh") then
|
||||
catalog = CATALOG.en
|
||||
end
|
||||
return catalog and catalog[msgid] or GetText(msgid)
|
||||
end
|
||||
|
||||
local Update = {}
|
||||
|
||||
-- 按包名分状态:各书源互不影响,各自只自动检查一次、同一版本只弹一次
|
||||
local function shared()
|
||||
local all = rawget(_G, "__moonsource_update")
|
||||
if not all then
|
||||
all = {}
|
||||
rawset(_G, "__moonsource_update", all)
|
||||
end
|
||||
all[PKG] = all[PKG] or {}
|
||||
return all[PKG]
|
||||
end
|
||||
|
||||
local function pluginsDir()
|
||||
local root = debug.getinfo(1, "S").source:match("^@(.*)/[^/]+/[^/]+$")
|
||||
return root and root:match("^(.*)/[^/]+$")
|
||||
end
|
||||
|
||||
---@return string
|
||||
function Update.version()
|
||||
return require(PKG .. ".version")
|
||||
end
|
||||
|
||||
local function sourceId()
|
||||
return require(PKG .. "._meta").id
|
||||
end
|
||||
|
||||
--- 数字段依次比较;同数字时预发布比正式版旧;已装版本解析不出(开发版)一律视为更旧。
|
||||
---@param candidate string
|
||||
---@param installed string
|
||||
---@return boolean
|
||||
function Update.newer(candidate, installed)
|
||||
local function parts(version)
|
||||
local out = {}
|
||||
for n in version:gmatch("%d+") do out[#out + 1] = tonumber(n) end
|
||||
return out
|
||||
end
|
||||
local left, right = parts(candidate), parts(installed)
|
||||
if #right == 0 or installed:find("dev", 1, true) then return true end
|
||||
for i = 1, 3 do
|
||||
local l, r = left[i] or 0, right[i] or 0
|
||||
if l ~= r then return l > r end
|
||||
end
|
||||
return installed:find("%-") ~= nil and candidate:find("%-") == nil
|
||||
end
|
||||
|
||||
local function parseSums(body)
|
||||
local sums = {}
|
||||
for hash, name in tostring(body):gmatch("(%x+)%s+%*?(%S+)") do
|
||||
if #hash == 64 then sums[name] = hash:lower() end
|
||||
end
|
||||
return sums
|
||||
end
|
||||
|
||||
---@return table|nil release, string|nil err
|
||||
local function parse(body)
|
||||
local ok, data = pcall(require("json").decode, body)
|
||||
if not ok or type(data) ~= "table" or data.draft or data.prerelease then
|
||||
return nil, "invalid release response"
|
||||
end
|
||||
local tag = data.tag_name
|
||||
local version = type(tag) == "string" and tag:match("^v?(%d+%.%d+%.%d+[%w%.%+%-]*)$")
|
||||
if not version then return nil, "invalid release version" end
|
||||
local base = SITE .. "/" .. REPO .. "/releases/download/" .. tag .. "/"
|
||||
local assets, sums_url = {}, nil
|
||||
for _idx, asset in ipairs(type(data.assets) == "table" and data.assets or {}) do
|
||||
if type(asset) == "table" and type(asset.name) == "string" and asset.browser_download_url == base .. asset.name then
|
||||
if asset.name == "SHA256SUMS" then
|
||||
sums_url = asset.browser_download_url
|
||||
else
|
||||
assets[asset.name] = { url = asset.browser_download_url, size = tonumber(asset.size) }
|
||||
end
|
||||
end
|
||||
end
|
||||
if not sums_url then return nil, "release has no plugin checksum" end
|
||||
local notes = type(data.body) == "string" and require("utils.text").trim(data.body) or ""
|
||||
return { version = version, assets = assets, sums_url = sums_url, notes = notes ~= "" and notes or nil }
|
||||
end
|
||||
|
||||
local job
|
||||
|
||||
--- 发起一个异步请求并登记为在途;回调若同步触发,不会把已结束的请求挂回去占着「忙」。
|
||||
---@param start fun(wrap: fun(cb: function): function): any
|
||||
local function launch(start)
|
||||
local settled = false
|
||||
local function wrap(cb)
|
||||
return function(...)
|
||||
settled = true
|
||||
job = nil
|
||||
return cb(...)
|
||||
end
|
||||
end
|
||||
local handle = start(wrap)
|
||||
if not settled then job = handle end
|
||||
end
|
||||
|
||||
--- 查询最新稳定版,并配好本插件的安装包 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
|
||||
local Request = require("http.request")
|
||||
local timeout = require("http.timeout").net(1)
|
||||
local function finish(release, sums_body)
|
||||
local sums = parseSums(sums_body)
|
||||
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")
|
||||
local cfg = settings.getSource(sourceId())
|
||||
cfg.update_last_checked_at = os.time()
|
||||
settings.saveSource(sourceId(), cfg)
|
||||
cb(release)
|
||||
end
|
||||
launch(function(wrap)
|
||||
return Request.get(API, { timeout = timeout, headers = { Accept = "application/json" } }, wrap(function(body, err)
|
||||
if err then return cb(nil, err) end
|
||||
local release, perr = parse(body)
|
||||
if not release then return cb(nil, perr) end
|
||||
launch(function(wrap_sums)
|
||||
return Request.get(release.sums_url, { timeout = timeout }, wrap_sums(function(sums_body, sums_err)
|
||||
if sums_err then return cb(nil, sums_err) end
|
||||
finish(release, sums_body)
|
||||
end))
|
||||
end)
|
||||
end))
|
||||
end)
|
||||
end
|
||||
|
||||
--- 下载、校验并换掉已装的本插件目录。
|
||||
---@param release table Update.check 的结果
|
||||
---@param cb fun(ok: boolean, err: any)
|
||||
---@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 target = release.target
|
||||
Paths.ensureDir(Paths.root())
|
||||
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
|
||||
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)
|
||||
local text = tostring(err)
|
||||
local known = {
|
||||
{ "^busy$", _("正在检查或安装更新,请稍候") },
|
||||
{ "^invalid release", _("更新信息无效,请稍后再试") },
|
||||
{ "^release has no", _("新版本缺少插件包,请稍后再试") },
|
||||
{ "update checksum mismatch", _("更新包校验失败,请稍后再试") },
|
||||
{ "download too large", _("更新包过大,已中止下载") },
|
||||
}
|
||||
for _idx, entry in ipairs(known) do
|
||||
if text:find(entry[1]) then return entry[2] end
|
||||
end
|
||||
return text:match("[^\n]*")
|
||||
end
|
||||
|
||||
local function startInstall(release)
|
||||
local ProgressDialog = require("ui.themes.widgets.progress_dialog")
|
||||
local dialog = ProgressDialog:new{ title = _("更新书源插件"), text = _("正在下载…"), dismissable = false }
|
||||
dialog:show()
|
||||
Update.install(release, function(ok, err)
|
||||
if ok then
|
||||
dialog:set(_("更新完成,正在重启 KOReader…"), 100)
|
||||
local UIManager = require("ui/uimanager")
|
||||
UIManager:nextTick(function()
|
||||
UIManager:broadcastEvent(require("ui/event"):new("Restart"))
|
||||
end)
|
||||
return
|
||||
end
|
||||
dialog:close()
|
||||
require("ui.themes.widgets.toast"):new{ text = T(_("书源插件更新失败:%1"), errorText(err)), timeout = 5 }:show()
|
||||
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(_("正在下载…"), math.floor(fraction * 100))
|
||||
end)
|
||||
end
|
||||
|
||||
--- 弹更新说明与安装确认。同版本只提示一次,force 跳过这层记忆。
|
||||
---@param release table
|
||||
---@param force boolean|nil
|
||||
function Update.prompt(release, force)
|
||||
local state = shared()
|
||||
if not force and state.offered == release.version then return end
|
||||
state.offered = release.version
|
||||
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)。重新下载并替换已安装的版本?"), name, release.version)
|
||||
end
|
||||
local dialog
|
||||
dialog = require("ui.themes.widgets.text_viewer"):new{
|
||||
title = _("书源插件更新"),
|
||||
text = release.notes and (header .. "\n\n" .. release.notes) or header,
|
||||
buttons = {
|
||||
{ text = _("稍后"), callback = function() dialog:close() end },
|
||||
{
|
||||
text = _("下载并安装"),
|
||||
variant = "primary",
|
||||
callback = function()
|
||||
dialog:close()
|
||||
startInstall(release)
|
||||
end,
|
||||
},
|
||||
},
|
||||
}
|
||||
dialog:show()
|
||||
end
|
||||
|
||||
--- 用户主动检查,始终给结果。
|
||||
---@param force boolean|nil 为真时已是最新也提示重装
|
||||
function Update.manualCheck(force)
|
||||
local Toast = require("ui.themes.widgets.toast")
|
||||
local loading = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = _("检查更新"), text = _("正在检查书源插件更新…"), dismissable = false,
|
||||
}
|
||||
loading:show()
|
||||
Update.check(function(release, err)
|
||||
loading:close()
|
||||
if err then
|
||||
Toast:new{ text = T(_("检查更新失败:%1"), errorText(err)), timeout = 4 }:show()
|
||||
elseif force or release.available then
|
||||
shared().offered = nil
|
||||
Update.prompt(release, force)
|
||||
else
|
||||
Toast:new{ text = T(_("已是最新版本(%1)"), release.installed), timeout = 3 }:show()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
local function autoEnabled()
|
||||
return require("utils.settings").getSource(sourceId()).update_auto_check ~= false
|
||||
end
|
||||
|
||||
--- 按 24 小时间隔静默检查;只提示,不自动安装。
|
||||
function Update.autoCheck()
|
||||
local state = shared()
|
||||
if state.checked or not autoEnabled() then return end
|
||||
local last = tonumber(require("utils.settings").getSource(sourceId()).update_last_checked_at) or 0
|
||||
if os.time() - last < CHECK_INTERVAL then return end
|
||||
if not require("ui/network/manager"):isConnected() then return end
|
||||
state.checked = true
|
||||
Update.check(function(release)
|
||||
if release and release.available and autoEnabled() then Update.prompt(release) end
|
||||
end)
|
||||
end
|
||||
|
||||
--- 桌面回到前台时自动检查。
|
||||
function Update.register()
|
||||
require("utils.extension").add("desktop.activated", function() Update.autoCheck() end)
|
||||
end
|
||||
|
||||
--- 设置页「书库 → 更新」:每个书源各登记一项,只管自己。
|
||||
---@param desktop table
|
||||
---@return table[]
|
||||
function Update.entries(desktop)
|
||||
local name = require(PKG .. "._meta").name
|
||||
local Row = require("ui.themes.widgets.row")
|
||||
return {
|
||||
{
|
||||
group = "library", order = 40, id = "moonsource_update_" .. sourceId(), icon = "system_update",
|
||||
title = T(_("%1 更新"), name),
|
||||
subtitle = _("书源插件检查更新与自动更新"),
|
||||
status = Update.version(),
|
||||
page = {
|
||||
title = T(_("%1 更新"), name),
|
||||
sections = function()
|
||||
return {{
|
||||
title = _("书源插件"),
|
||||
rows = {
|
||||
function(iw)
|
||||
return Row.widget({
|
||||
width = iw, chevron = true, kind = "nav", icon = "system_update",
|
||||
title = _("检查书源插件更新"),
|
||||
subtitle = _("只更新本书源,其它书源各自独立更新"),
|
||||
status = Update.version(),
|
||||
callback = function() Update.manualCheck() end,
|
||||
})
|
||||
end,
|
||||
function(iw)
|
||||
local on = autoEnabled()
|
||||
return Row.widget({
|
||||
width = iw, chevron = false, kind = "toggle", icon = "autorenew",
|
||||
title = _("自动检查书源插件更新"),
|
||||
status = on and _("开") or _("关"), status_on = on,
|
||||
callback = function()
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource(sourceId())
|
||||
cfg.update_auto_check = not on
|
||||
settings.saveSource(sourceId(), cfg)
|
||||
desktop:updateView()
|
||||
end,
|
||||
})
|
||||
end,
|
||||
},
|
||||
}}
|
||||
end,
|
||||
},
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
return Update
|
||||
@@ -1,84 +0,0 @@
|
||||
--[[-- 书源插件在线更新:版本比较、只配本插件的包、校验下载地址、设置页追加更新行。本文件由模板仓库同步,勿手改。 @module tests.update.update_spec --]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
package.preload["json"] = function() return require("support.json_stub") end
|
||||
local Assert = require("support.assert")
|
||||
local Json = require("support.json_stub")
|
||||
|
||||
local PKG = "moonsource_dedao"
|
||||
local DIR = "moonsource-dedao.koplugin"
|
||||
local REPO = "moon-source/" .. DIR:gsub("%.koplugin$", "")
|
||||
|
||||
local cfg = {}
|
||||
package.preload["utils.settings"] = function()
|
||||
return { getSource = function() return cfg end, saveSource = function() end }
|
||||
end
|
||||
package.preload["utils.text"] = function()
|
||||
return { trim = function(s) return (s:gsub("^%s+", ""):gsub("%s+$", "")) end }
|
||||
end
|
||||
package.preload["http.timeout"] = function() return { net = function() return 5 end } end
|
||||
|
||||
local BASE = "https://git.garfieldho.me/" .. REPO .. "/releases/download/v1.2.3/"
|
||||
local zip = DIR .. "-1.2.3.zip"
|
||||
local other = "moonsource-other.koplugin-1.2.3.zip"
|
||||
local release = {
|
||||
tag_name = "v1.2.3", draft = false, prerelease = false, body = " 更新说明 ",
|
||||
assets = {
|
||||
{ name = "SHA256SUMS", size = 10, browser_download_url = BASE .. "SHA256SUMS" },
|
||||
{ name = zip, size = 100, browser_download_url = BASE .. zip },
|
||||
{ name = other, size = 200, browser_download_url = "https://evil.example/x.zip" },
|
||||
},
|
||||
}
|
||||
local sums = ("a"):rep(64) .. " " .. zip .. "\n" .. ("b"):rep(64) .. " " .. other .. "\n"
|
||||
local requested = {}
|
||||
package.preload["http.request"] = function()
|
||||
return {
|
||||
get = function(url, _opts, cb)
|
||||
requested[#requested + 1] = url
|
||||
if url:find("/releases/latest", 1, true) then cb(Json.encode(release)) else cb(sums) end
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local Update = require(PKG .. ".update")
|
||||
|
||||
-- 版本比较
|
||||
Assert.is_true(Update.newer("0.6.1", "0.6.0"))
|
||||
Assert.is_false(Update.newer("0.6.0", "0.6.0"))
|
||||
Assert.is_false(Update.newer("0.5.9", "0.6.0"))
|
||||
Assert.is_true(Update.newer("1.0.0", "1.0.0-rc.1"))
|
||||
Assert.is_true(Update.newer("0.0.1", "0.0.0-dev"))
|
||||
|
||||
-- 只查自己仓库的 Release,只配自己的包
|
||||
local got, err
|
||||
Update.check(function(r, e) got, err = r, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(requested[1], "https://git.garfieldho.me/api/v1/repos/" .. REPO .. "/releases/latest")
|
||||
Assert.eq(got.version, "1.2.3")
|
||||
Assert.eq(got.notes, "更新说明")
|
||||
Assert.is_true(got.available)
|
||||
Assert.eq(got.target.dir, DIR)
|
||||
Assert.eq(got.target.sha256, ("a"):rep(64))
|
||||
Assert.is_true(cfg.update_last_checked_at ~= nil)
|
||||
|
||||
-- 下载地址不在本仓库 Release 下的资产被拒
|
||||
release.assets[2].browser_download_url = "https://evil.example/x.zip"
|
||||
local rejected
|
||||
Update.check(function(_r, e) rejected = e end)
|
||||
Assert.matches(rejected, "no plugin archive")
|
||||
release.assets[2].browser_download_url = BASE .. zip
|
||||
|
||||
-- 没有 SHA256SUMS 的 Release 无效
|
||||
release.assets[1] = nil
|
||||
local bad_err
|
||||
Update.check(function(_r, e) bad_err = e end)
|
||||
Assert.matches(bad_err, "no plugin checksum")
|
||||
|
||||
-- 设置页「书库 → 更新」每个书源登记自己的一项
|
||||
package.preload["ui.themes.widgets.row"] = function() return { widget = function(spec) return spec end } end
|
||||
local entries = Update.entries({ updateView = function() end })
|
||||
Assert.len(entries, 1)
|
||||
Assert.eq(entries[1].group, "library")
|
||||
Assert.matches(entries[1].id, "^moonsource_update_")
|
||||
Assert.len(entries[1].page.sections()[1].rows, 2)
|
||||
Reference in new issue
Block a user