-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassist.lua
195 lines (162 loc) · 5.02 KB
/
assist.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
local here = arg[1]
local g_here = here
local g_branchname = "main"
local g_platform = None
package.path = package.path .. ';'..here..'/?.lua;'..here..'/deps/alloui/lib/pl/lua/?.lua'
local json = require "json"
local tablex = require"pl.tablex"
local g_platform_file_map = {
["linux-x64"] = { file = "liballonet.so" },
["windows-x64"] = { file = "allonet.dll" },
["mac-universal"] = { file = "liballonet.dylib" }
}
--------------------------------------------------------------
local s3_root = "http://alloverse-downloads-prod.s3-eu-north-1.amazonaws.com/allonet" --alloverse-downloads-prod/allonet/"
--- Check lock file and download binaries for that version
-- If lockfile is empty do upgrade
function fetch(version)
version = version or get_locked_version()
if not version then
return print("Could not determine version to fetch")
end
print("Downloading Allonet " .. version)
local file = g_platform_file_map[g_platform].file
download(s3_root .. "/" .. version .. "/" .. g_platform .. "/" .. file, g_here .. "/lib/" .. file)
save_current_version(version)
save_locked_version(version)
end
--- Download the latest meta. If versions differ then save version to lock file, fetch the version
function upgrade()
local latest_version = get_latest_version()
if not latest_version then
return print("Failed to read latest version.")
end
local current_version = get_current_version()
if not current_version or (current_version ~= latest_version) then
print("Found new version.")
fetch(latest_version)
return
end
print("You are already on the latest version (" .. latest_version .. ")")
end
function download(url, dest)
system("curl -fsSL \"" .. url .. "\" > " .. dest)
end
-------------------------------------------------------------------
local g_lockfile = here .. "/allonet.lock"
function save_locked_version(version)
writefile(g_lockfile, version)
end
function get_locked_version()
return trim(readfile(g_lockfile))
end
local g_cachefilepath = here .. "/lib/allonet.cache"
function get_current_version()
return trim(readfile(g_cachefilepath))
end
function save_current_version(version)
writefile(g_cachefilepath, version)
end
--- Returns the meta json for the latest available build
-- {
-- "version": "${VERSION}",
-- "platform": "${PLATFORM}",
-- "branch": "${BUILD_SOURCEBRANCHNAME}",
-- "buildid": "${BUILD_BUILDID}",
-- "buildnumber": "${BUILD_BUILDNUMBER}",
-- "githash": "${BUILD_SOURCEVERSION}",
-- "changemsg": "${BUILD_SOURCEVERSIONMESSAGE}"
-- }
function get_latest_json(branch)
local path = "latest_" .. (branch or g_branchname) .. "_" .. g_platform .. ".json"
local url = s3_root .. "/" .. path
local jsons = system("curl -fsSL \"" .. url .. "\"")
local json = json.decode(jsons)
return json
end
function get_latest_version(branch)
local latest = get_latest_json(branch)
if latest then
return latest["version"]
end
end
function system(cmd)
--print("system("..cmd..")")
local handle = io.popen(cmd)
local result = handle:read("*a")
handle:close()
return trim(result)
end
function readfile(path)
local f = io.open(path, "r")
if f == nil then
return ""
end
local s = f:read("*a")
f:close()
return s
end
function writefile(path, content)
local f = io.open(path, "w")
f:write(content)
f:close()
end
function file_exists(path)
local f=io.open(path,"r")
if f~=nil then
io.close(f)
return true
else
return false
end
end
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function all(t, f)
for k, v in ipairs(t) do
if f(v) == false then
return false
end
end
return true
end
function get_current_platform()
local uname = system("uname -s")
if uname == "Darwin" then
return "mac-universal"
elseif uname == "Linux" then
return "linux-x64"
elseif uname == "CYGWIN" or uname == "MINGW" then
return "windows-x64"
else
return None
end
end
function isversionstring(str)
return str:match("%d+%.%d+%.%d+%.g[a-z0-9]+") ~= nil
end
--------------------------------------------------------------
--------------------------------------------------------------
g_platform = get_current_platform()
if not g_platform then
print("Could not determine your platform. Please reach out to us via email or our Discord. Details on alloverse.com")
return
end
print("Current platform: " .. g_platform)
if arg[2] == "fetch" then
fetch()
elseif arg[2] == "upgrade" then
local version_or_branch = arg[3]
if version_or_branch and version_or_branch ~= "" then
if isversionstring(version_or_branch) then
print("Requested version " .. version_or_branch)
fetch(version_or_branch)
else
print("Requested branch " .. version_or_branch)
fetch(get_latest_version(version_or_branch))
end
else
upgrade()
end
end