-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.lua
executable file
·241 lines (202 loc) · 5.64 KB
/
update.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
--
-- Update and install World of Warcraft addons from the command line.
-- Author : Phanx <phanx@phanx.net>
-- Website: https://gitlab.com/phanx/wow-addon-updater
-- License: Zlib - see LICENSE.txt for full text
--
require("lfs") -- luafilesystem
local cleanHTML = dofile("utils/cleanHTML.lua")
local sites = dofile("sites.lua")
local core = dofile("common.lua")
local db = core.getDB()
local args = core.parseArguments(...)
--
-- Logging and message output
--
local function formatMessage(str, ...)
str = tostring(str)
if str:find("%%[dsf%d%.]") then
str = str:format(...)
elseif select("#", ...) > 0 then
for i = 1, select("#", ...) do
str = str .. " " .. tostring(select(i, ...))
end
end
return str
end
local logLines = {}
local function writeLog()
local file = io.open("log.txt", "w")
if not file then return end
file:write(table.concat(logLines, "\n") .. "\n")
file:close()
end
local function log(str, ...)
table.insert(logLines, formatMessage(str, ...))
end
local _print = print
function print(str, ...)
log(formatMessage(str, ...))
_print(formatMessage(str, ...))
end
--
-- Try to match an addon to a project on CurseForge or Wowace
--
local function addSearchResults(site, html, t)
t = t or {}
local parser = sites.parseSearchResults[site]
if parser and type(html) == "string" then
html = cleanHTML(html)
parser(html, t)
end
return t
end
local function getSearchResults(term)
local results = {}
for site, getSearchURL in pairs(sites.getSearchURL) do
local _, _, html = core.request(getSearchURL(term))
addSearchResults(site, html, results)
end
return results
end
local function ignoreAddon(addon)
print("Now ignoring", addon.title)
addon.ignored = true
end
local function saveAddonMatch(addon, site, id)
print("Saving match: %s (%s @ %s)", tostring(addon.title), tostring(id), tostring(site))
addon.site = site
addon.id = id
end
local function matchAddon(addon)
local results = getSearchResults(addon.title)
print("Found %d matches for %s by %s", tostring(#results), tostring(addon.title), tostring(addon.author or "<unknown>"))
if #results > 0 then
for i = 1, #results do
local result = results[i]
local lastUpdated = result.date and os.date("%x", result.date) or "<unknown>"
local siteName = result.site and sites.info[result.site] and sites.info[result.site].name or result.site or "<unknown>"
print("%d. %s (by %s, updated %s on %s)", i, tostring(result.name), tostring(result.author), lastUpdated, siteName)
end
local pick = tonumber(core.prompt("Pick a match (1" .. (#results > 1 and ("-" .. #results) or "") .. ") or enter 0 if none are right:")) or 0
local result = results[pick]
if result then
return saveAddonMatch(addon, result.site, result.id)
end
end
local reply = string.lower(core.prompt("Specify a manual match now? (y/n):"))
if reply == "y" then
local options = ""
for i = 1, #sites.info do
if i > 1 then
options = options .. ", "
elseif i == #sites.info then
options = options .. ", or "
end
options = options .. sites.info[i].id:gsub(sites.info[i].key, "[" .. sites.info[i].key .. "]", 1)
end
reply = core.prompt("Addon site (" .. options ..") or URL:")
if reply:match("^https?://") then
local site, id = sites.parseProjectURL(reply)
if site and id then
return saveAddonMatch(addon, site, id)
end
print("Could not parse URL")
return ignoreAddon(addon)
end
reply = reply:lower()
local site = sites.info[reply] and sites.info[reply].id
if not site then
print("Invalid site")
return ignoreAddon(addon)
end
local id = core.prompt("Addon slug or ID:")
if id:len() == 0 then
print("Missing ID")
return ignoreAddon(addon)
end
return saveAddonMatch(addon, site, id)
end
print("Failed to find a match")
return ignoreAddon(addon)
end
--
-- Scan for added or removed addons, and refresh metadata from TOC files
--
local function scanAddons()
-- Scan for added or changed addons and add them to the DB
for dir in lfs.dir(core.BASEDIR) do
log("Scanning object", dir)
local meta = core.getAddonMetadata(dir)
if meta then
log("Found addon")
local t = db[dir] or {}
for k, v in pairs(meta) do
t[k] = v
end
t.deleted = nil
db[dir] = t
end
end
-- Scan for removed addons and flag them in the DB
for dir, meta in pairs(db) do
if not meta.deleted then
local attr = lfs.attributes(core.BASEDIR .. "/" .. dir)
if not attr or attr.mode ~= "directory" then
log("Deleted:", name)
meta.deleted = true
elseif not meta.site and not (meta.ignored or meta.dev) then
log("New:", dir)
local action = string.lower(core.prompt("New addon '" .. dir .. "' -- [m]atch, [i]gnore, or [s]kip? "))
if action == "m" then
matchAddon(meta)
elseif action == "i" then
ignoreAddon(meta)
else
print("Skipped")
end
end
end
end
-- Write to disk
core.saveDB()
end
--
-- Update all addons
--
local function updateAllAddons()
local dirs = {}
for dir, meta in pairs(db) do
table.insert(dirs, dir)
end
table.sort(dirs)
for i = 1, #dirs do
local dir = dirs[i]
local meta = db[dir]
if meta.site and meta.id and not (meta.dev or meta.ignore or meta.deleted) then
core.updateAddon(meta)
else
local reason = meta.dev and "Working Copy"
or meta.ignore and "Ignored"
or meta.deleted and "Deleted"
or "Unidentified"
log("%s skipped (%s)", dir, reason)
end
end
-- Write to disk
core.saveDB()
end
--
-- Get the party started
--
local function start()
-- Scan for added/removed/updated addons
scanAddons()
-- Check for and download any available updates
updateAllAddons()
-- Clean up any temporary files
core.cleanup()
-- Write the log file
writeLog()
end
start()