-
Notifications
You must be signed in to change notification settings - Fork 24
/
updater.lua
214 lines (177 loc) · 7.46 KB
/
updater.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
local utils = require("utils")
local config = require("config")
local threader = require("threader")
local notify = require("notify")
local alert = require("alert")
local scener = require("scener")
local sharp = require("sharp")
local ffi = require("ffix")
local fs = require("fs")
local updater = {}
local userOS = love.system.getOS()
if userOS == "Linux" then
-- Let's assume that the owner of olympus.love is the only one who can self-update it.
local sourceAttrs = fs.attributes(love.filesystem.getSource())
if sourceAttrs and sourceAttrs.permissions:match("rw.......") then
ffi.cdef[[
int getuid();
int geteuid();
]]
local owner = tonumber(sourceAttrs.uid)
updater.available = owner == tonumber(ffi.C.getuid()) or owner == tonumber(ffi.C.geteuid())
else
updater.available = false
end
else
updater.available = true
end
function updater.check(auto)
if not updater.available then
return
end
if updater.checking then
return updater.checking
end
updater.checking = threader.routine(function()
updater.latest = nil
local versionOld, extraOld = utils.load("version.txt"):match("(.*)-(.*-.*-[^\n]*)")
local srcOld, idOld = (extraOld or "?"):match("(.*)-(.*)-.*")
idOld = tonumber(idOld)
if not idOld then
notify("Cannot determine currently running version of Olympus!")
end
if auto and srcOld == "dev" then
updater.checking = nil
return
end
local options = scener.preload("options")
local changelog, updatebtn = options.root:findChild("changelog", "updatebtn")
changelog.text = "Checking for updates..."
updatebtn.enabled = false
updatebtn:reflow()
local utilsAsync = threader.wrap("utils")
local builds, buildsError = utilsAsync.downloadJSON("https://dev.azure.com/EverestAPI/Olympus/_apis/build/builds"):result()
if not builds then
notify("Error downloading builds list: " .. tostring(buildsError))
return false
end
builds = builds.value
for bi = 1, #builds do
local build = builds[bi]
if (build.status == "completed" or build.status == "succeeded") and (build.reason == "manual" or build.reason == "individualCI") then
local id = build.id
local branch = build.sourceBranch:gsub("refs/heads/", "")
if id <= idOld then
break
elseif config.updates:match(branch, 1, false) then
local latest = {
id = id,
branch = branch,
version = build.buildNumber
}
updater.latest = latest
local cb = function()
if srcOld ~= "dev" then
updater.update(tostring(build.id))
return
end
alert({
body = "One does not simply update a devbuild.",
buttons = {
{ "OK", function(container)
if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
updater.update(tostring(build.id))
end
container:close("OK")
end }
},
init = function(container)
container:findChild("buttons").children[1]:hook({
update = function(orig, self, dt)
orig(self, dt)
if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
self.text = "I know what I'm doing."
else
self.text = "OK"
end
end
})
end
})
end
if auto then
alert({
body = string.format([[
There's a new version of Olympus available.
Do you want to update to %s now?]], build.buildNumber),
buttons = {
{ "Yes", function(container)
cb()
container:close("OK")
end},
{ "No" }
}
})
else
notify(string.format([[
There's a new version of Olympus available: %s]], build.buildNumber))
end
local changelogTask = utilsAsync.download(string.format("https://raw.githubusercontent.com/EverestAPI/Olympus/%s/changelog.txt", build.sourceVersion))
local function setChangelog(changelogText)
changelog.text = {{ 1, 1, 1, 1 },
"Currently installed:\n" .. versionOld, { 1, 1, 1, 0.5 }, "-" .. extraOld .. "\n\n", { 1, 1, 1, 1 },
"Newest available:\n" .. build.buildNumber, { 1, 1, 1, 0.5 }, string.format("-azure-%s-%s", build.id, build.sourceVersion and build.sourceVersion:sub(1, 5) or "?????") .. "\n\n", { 1, 1, 1, 1 },
"Changelog:\n" .. changelogText
}
end
setChangelog("Downloading...")
changelogTask:calls(function(task, data, error)
data = data and data:match("#changelog#\n(.*)")
if not data then
setChangelog("Failed to download:\n" .. tostring(error))
return
end
latest.changelog = data
setChangelog(data)
end)
updatebtn.enabled = true
updatebtn.cb = cb
updatebtn:reflow()
updater.checking = nil
return
end
end
end
changelog.text = {{ 1, 1, 1, 1 },
"Currently installed:\n" .. versionOld, { 1, 1, 1, 0.5 }, "-" .. extraOld .. "\n\n", { 1, 1, 1, 1 },
"No updates found."
}
updatebtn.enabled = false
updatebtn:reflow()
updater.checking = nil
end)
return updater.checking
end
function updater.update(id)
if not updater.available then
return
end
local installer = scener.push("installer")
installer.update("Preparing update of Olympus", false, "")
installer.sharpTask("installOlympus", id):calls(function(task, last)
if not last then
return
end
installer.update("Olympus successfully updated", 1, "done")
installer.done({
{
"Restart Olympus",
function()
sharp.restart(love.filesystem.getSource()):result()
love.event.quit()
end
}
})
end)
end
return updater