Version 2.0006

1) Refactored update framework and removed duplicate code
master
Durel 7 years ago
parent e9e6299eae
commit 4d5cc00fea

@ -2,6 +2,13 @@
dbot.changelog = {}
dbot.changelog[2.0006] =
{
{ change = drlDbotChangeLogTypeMisc,
desc = "Refactored update framework and removed duplicate code"
}
}
dbot.changelog[2.0005] =
{
{ change = drlDbotChangeLogTypeNew,
@ -16,7 +23,7 @@ dbot.changelog[2.0004] =
{
{ change = drlDbotChangeLogTypeMisc,
desc = "Refresh code now forces your first refresh in a new session to use a full refresh scan. " ..
"This is necessary because items have have been moved or modified in a different client and " ..
"This is necessary because items might have moved or changed outside of this client and " ..
"we need to know the current location of all items. This also allows us to detect if a no-" ..
"save item was removed after the last session."
},

@ -87,7 +87,7 @@ dbot.version : Module to track version and changelog information and update the
save_state="y"
date_written="2017-08-12 08:45:15"
requires="4.98"
version="2.0005"
version="2.0006"
>
<description trim="y">
<![CDATA[
@ -4410,13 +4410,13 @@ function inv.cli.version.fn(name, line, wildcards)
elseif (command == "changelog") then
dbot.info("Full changelog:")
retval = dbot.version.changelog(0, line) -- show changelog from version 0 to the latest
retval = dbot.version.changelog.get(0, line) -- show changelog from version 0 to the latest
elseif (command == "check") then
retval = dbot.update.version(drlDbotUpdateCheck, line)
retval = dbot.version.update.release(drlDbotUpdateCheck, line)
else
retval = dbot.update.version(drlDbotUpdateInstall, line)
retval = dbot.version.update.release(drlDbotUpdateInstall, line)
end -- if
@ -19562,54 +19562,64 @@ end -- dbot.remote.getCR
----------------------------------------------------------------------------------------------------
-- dbot.version: Track the plugin's version and changelog and update the plugin
--
-- dbot.version.changelog(minVersion, endTag)
-- dbot.version.changelogCR()
-- dbot.version.displayChanges(minVersion, changeLog)
-- dbot.version.displayChange(changeLogEntries)
-- dbot.version.changelog.get(minVersion, endTag)
-- dbot.version.changelog.getCR()
-- dbot.version.changelog.displayChanges(minVersion, changeLog)
-- dbot.version.changelog.displayChange(changeLogEntries)
--
--
-- Note: This code is derived from a plugin written by Arcidayne. Thanks Arcidayne!
-- dbot.version.update.release(mode, endTag)
-- dbot.version.update.releaseCR()
-- Note: dbot.version.update is derived from a plugin written by Arcidayne. Thanks Arcidayne!
----------------------------------------------------------------------------------------------------
dbot.version = {}
dbot.version.changeLogPkg = nil
dbot.version = {}
dbot.version.changelog = {}
dbot.version.changelog.pkg = nil
dbot.version.update = {}
dbot.version.update.pkg = nil
drlDbotUpdateCheck = "check"
drlDbotUpdateInstall = "install"
drlDbotChangeLogTypeFix = "@RFix@W"
drlDbotChangeLogTypeNew = "@GNew@W"
drlDbotChangeLogTypeMisc = "@yMsc@W"
drlDbotChangeLogTypeFix = "@RFix@W"
drlDbotChangeLogTypeNew = "@GNew@W"
drlDbotChangeLogTypeMisc = "@yMsc@W"
function dbot.version.changelog(minVersion, endTag)
function dbot.version.changelog.get(minVersion, endTag)
local url = "https://raw.githubusercontent.com/Aardurel/aard-plugins/master/aard_inventory.changelog"
local protocol = "HTTPS"
if (dbot.version.changeLogPkg ~= nil) then
if (dbot.version.changelog.pkg ~= nil) then
dbot.info("Skipping changelog request: another request is in progress")
return inv.tags.stop(invTagsVersion, endTag, DRL_RET_BUSY)
end -- if
dbot.version.changeLogPkg = {}
dbot.version.changeLogPkg.url = url
dbot.version.changeLogPkg.protocol = protocol
dbot.version.changeLogPkg.minVersion = minVersion or 0
dbot.version.changeLogPkg.endTag = endTag
dbot.version.changelog.pkg = {}
dbot.version.changelog.pkg.url = url
dbot.version.changelog.pkg.protocol = protocol
dbot.version.changelog.pkg.minVersion = minVersion or 0
dbot.version.changelog.pkg.endTag = endTag
wait.make(dbot.version.changelogCR)
wait.make(dbot.version.changelog.getCR)
return DRL_RET_SUCCESS
end -- dbot.version.changelog
end -- dbot.version.changelog.get
function dbot.version.changelogCR()
function dbot.version.changelog.getCR()
if (dbot.version.changeLogPkg == nil) then
dbot.error("dbot.version.changelogCR: Change log package is missing!")
if (dbot.version.changelog.pkg == nil) then
dbot.error("dbot.version.changelog.getCR: Change log package is missing!")
return inv.tags.stop(invTagsVersion, "missing end tag", DRL_RET_INTERNAL_ERROR)
end -- if
local fileData, retval = dbot.remote.get(dbot.version.changeLogPkg.url, dbot.version.changeLogPkg.protocol)
local fileData, retval = dbot.remote.get(dbot.version.changelog.pkg.url,
dbot.version.changelog.pkg.protocol)
if (retval ~= DRL_RET_SUCCESS) then
dbot.warn("dbot.version.changelogCR: Failed to retrieve remote changelog file: " ..
dbot.warn("dbot.version.changelog.getCR: Failed to retrieve remote changelog file: " ..
dbot.retval.getString(retval))
elseif (fileData == nil) then
dbot.info("No changelog information was found.")
@ -19617,48 +19627,47 @@ function dbot.version.changelogCR()
else
loadstring(fileData)()
if (dbot.changelog == nil) then
dbot.warn("dbot.version.changelogCR: Invalid changelog format detected")
dbot.warn("dbot.version.changelog.getCR: Invalid changelog format detected")
retval = DRL_RET_INTERNAL_ERROR
else
retval = dbot.version.displayChanges(dbot.version.changeLogPkg.minVersion, dbot.changelog)
retval = dbot.version.changelog.displayChanges(dbot.version.changelog.pkg.minVersion, dbot.changelog)
end -- if
end -- if
dbot.version.changeLogPkg = nil
dbot.version.changelog.pkg = nil
return inv.tags.stop(invTagsVersion, endTag, retval)
end -- dbot.version.changelogCR
end -- dbot.version.changelog.getCR
function dbot.version.displayChanges(minVersion, changeLog)
function dbot.version.changelog.displayChanges(minVersion, changeLog)
local sortedLog = {}
for k, v in pairs(changeLog) do
table.insert(sortedLog, { version = tonumber(k) or 0, changes = v})
end -- for
table.sort(sortedLog, function (v1, v2) return v1.version > v2.version end)
table.sort(sortedLog, function (v1, v2) return v1.version < v2.version end)
for _, clog in ipairs(sortedLog) do
if (clog.version > minVersion) then
dbot.version.displayChange(clog)
dbot.version.changelog.displayChange(clog)
end -- if
end -- for
return DRL_RET_SUCCESS
end -- dbot.version.displayChanges
end -- dbot.version.changelog.displayChanges
-- Format of entry is: { version = 2.0004,
-- changes = { { change = drlDbotChangeLogTypeXYZ, desc = "what changed" }
-- }
-- }
function dbot.version.displayChange(changeLogEntries)
function dbot.version.changelog.displayChange(changeLogEntries)
local retval = DRL_RET_SUCCESS
if (changeLogEntries == nil) then
dbot.warn("dbot.version.displayChange: Change entries are missing!")
dbot.warn("dbot.version.changelog.displayChange: Change entries are missing!")
return DRL_RET_INVALID_PARAM
end -- if
@ -19668,84 +19677,59 @@ function dbot.version.displayChange(changeLogEntries)
end -- for
return retval
end -- dbot.version.displayChange
--FIXME: rename this version.update???
dbot.update = {}
dbot.update.url = "https://raw.githubusercontent.com/Aardurel/aard-plugins/master/aard_inventory.xml"
dbot.update.protocol = "HTTPS"
dbot.update.pkg = nil
end -- dbot.version.changelog.displayChange
drlDbotUpdateCheck = "check"
drlDbotUpdateInstall = "install"
function dbot.update.version(mode, endTag)
local retval = DRL_RET_SUCCESS
function dbot.version.update.release(mode, endTag)
local url = "https://raw.githubusercontent.com/Aardurel/aard-plugins/master/aard_inventory.xml"
local protocol = "HTTPS"
local retval = DRL_RET_SUCCESS
if (mode == nil) or ((mode ~= drlDbotUpdateCheck) and (mode ~= drlDbotUpdateInstall)) then
dbot.warn("dbot.update.version: Missing or invalid mode parameter")
dbot.warn("dbot.version.update.release: Missing or invalid mode parameter")
return inv.tags.stop(invTagsVersion, endTag, DRL_RET_INVALID_PARAM)
end -- if
if (dbot.update.pkg ~= nil) then
if (dbot.version.update.pkg ~= nil) then
dbot.info("Skipping update request: another update request is in progress")
return inv.tags.stop(invTagsVersion, endTag, DRL_RET_BUSY)
end -- if
-- Pull in the async package if it exists
local asyncOk, async = pcall (require, "async")
if (not asyncOk) or (async == nil) then
dbot.warn("dbot.update.version: Failed to find \"async\" package, skipping update request")
return inv.tags.stop(invTagsVersion, endTag, DRL_RET_UNSUPPORTED)
end -- if
dbot.version.update.pkg = {}
dbot.version.update.pkg.mode = mode
dbot.version.update.pkg.url = url
dbot.version.update.pkg.protocol = protocol
dbot.version.update.pkg.endTag = endTag
dbot.update.pkg = {}
dbot.update.pkg.mode = mode
dbot.update.pkg.endTag = endTag
-- Make a request to grab the latest plugin file. Newer versions of mush have nice async
-- capabilities and we use that if possible. Otherwise, we fall back to an old-style request.
-- Both methods of making the request will call dbot.update.callback upon completion.
if (async.doAsyncRemoveRequest ~= nil) then
async.doAsyncRemoteRequest(dbot.update.url, dbot.update.callback, dbot.update.protocol)
else
wait.make(dbot.updateCR) -- Fall back to a co-routine running the old-style async code
end
wait.make(dbot.version.update.releaseCR)
return retval
end -- dbot.update
end -- dbot.version.update.release
-- Scan the file we just (hopefully) downloaded and check the file's version. If the user
-- requested a version check, we report the current and latest available versions. If the
-- user requested an installation of the latest plugin, do the upgrade if everything looks
-- sane.
function dbot.update.callback(retval, page, status, headers, fullStatus, requestUrl)
local retval = DRL_RET_SUCCESS
function dbot.version.update.releaseCR()
if (dbot.update.pkg == nil) or (dbot.update.pkg.mode == nil) then
dbot.error("dbot.update.callback: Missing or invalid update package detected")
if (dbot.version.update.pkg == nil) or (dbot.version.update.pkg.mode == nil) then
dbot.error("dbot.version.update.releaseCR: Missing or invalid update package detected")
return inv.tags.stop(invTagsVersion, "end tag is nil", DRL_RET_INVALID_PARAM)
end -- if
local endTag = dbot.update.pkg.endTag
local endTag = dbot.version.update.pkg.endTag
if (status ~= 200) then
dbot.warn("dbot.update.callback: Failed to retrieve remote plugin information")
retval = DRL_RET_INTERNAL_ERROR
-- This blocks until the plugin file is returned, an error is detected, or we time out
local pluginData, retval = dbot.remote.get(dbot.version.update.pkg.url, dbot.version.update.pkg.protocol)
if (retval ~= DRL_RET_SUCCESS) then
dbot.warn("dbot.version.update.releaseCR: Failed to retrieve latest plugin file: " ..
dbot.retval.getString(retval))
elseif (pluginData == nil) then
dbot.info("Could not find a remote plugin release")
retval = DRL_RET_MISSING_ENTRY
else
local currentVersion = GetPluginInfo(GetPluginID(), 19) or 0
local currentVerStr = string.format("%1.4f", currentVersion)
local remoteVerStr = string.match(page, '%s%s+version="([0-9%.]+)"')
local remoteVerStr = string.match(pluginData, '%s%s+version="([0-9%.]+)"')
local remoteVersion = tonumber(remoteVerStr or "") or 0
if (remoteVersion == currentVersion) then
@ -19756,68 +19740,34 @@ function dbot.update.callback(retval, page, status, headers, fullStatus, request
"is newer than the latest official release (v" .. remoteVerStr .. ")")
retval = DRL_RET_VER_MISMATCH
elseif (dbot.update.pkg.mode == drlDbotUpdateCheck) then
elseif (dbot.version.update.pkg.mode == drlDbotUpdateCheck) then
dbot.info("You are running v" .. currentVerStr .. ", latest version is v" .. remoteVerStr)
dbot.info("Changes since your last update:")
dbot.update.pkg = nil
return dbot.version.changelog(currentVersion, endTag)
dbot.version.update.pkg = nil
return dbot.version.changelog.get(currentVersion, endTag)
elseif (dbot.update.pkg.mode == drlDbotUpdateInstall) then
elseif (dbot.version.update.pkg.mode == drlDbotUpdateInstall) then
dbot.info("Updating plugin from version " .. currentVerStr .. " to version " .. remoteVerStr)
dbot.info("Please do not enter anything until the update completes")
local pluginFile = GetPluginInfo(GetPluginID(), 6)
local file = io.open(pluginFile, "w")
file:write(page)
file:write(pluginData)
file:close()
dbot.reload()
else
dbot.error("dbot.update.callback: Detected invalid mode \"@R" .. (dbot.update.pkg.mode or "nil") ..
"@W\"")
dbot.error("dbot.version.update.callback: Detected invalid mode \"@R" ..
(dbot.version.update.pkg.mode or "nil") .. "@W\"")
end -- if
end -- if
dbot.update.pkg = nil
return inv.tags.stop(invTagsVersion, endTag, retval)
end -- dbot.update.callback
function dbot.updateCR()
local urlThread = async.request(dbot.update.url, dbot.update.protocol)
local updateRet, page, status, headers, fullStatus = -1, nil, -1, nil, nil, dbot.update.url
if (urlThread == nil) then
dbot.warn("dbot.updateCR: Failed to create thread requesting update")
else
local timeout = 10
local totTime = 0
while (urlThread:alive()) do
if (totTime > timeout) then
retval = DRL_RET_TIMEOUT
break
end -- if
wait.time(drlSpinnerPeriodDefault)
totTime = totTime + drlSpinnerPeriodDefault
end -- while
--FIXME: this is broken!!! retval is never defined!!!
if (retval ~= DRL_RET_SUCCESS) then
updateRet, page, status, headers, fullStatus = urlThread:join()
end -- if
end -- if
dbot.update.callback(updateRet, page, status, headers, fullStatus)
end -- dbot.updateCR
dbot.version.update.pkg = nil
return inv.tags.stop(invTagsVersion, endTag, retval)
end -- dbot.version.update.releaseCR
]]>

Loading…
Cancel
Save