-
Notifications
You must be signed in to change notification settings - Fork 2
/
package_v1.lua
350 lines (283 loc) · 9.67 KB
/
package_v1.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
---
-- Package management module
-- Version 1.0 Package API.
-- Copyright (c) 2017 Blizzard Entertainment
---
local p = premake
local m = p.modules.packagemanager
-- Package API ----------------------------------------------------------------
local function __createVariant(name, location, server)
local variant = m.createVariant(name)
variant.filter = p.packagemanager.filterFromVariant(name)
variant.location = location
variant.server = server
return variant
end
local function __getVariants(name, version)
local result = {}
-- test if version is a folder name.
if os.isdir(version) then
for i, dir in pairs(os.matchdirs(version .. '/*')) do
local n, variant = string.match(dir, '(.+)[\\|/](.+)')
variant = variant:lower()
result[variant] = __createVariant(variant, dir, nil)
end
return result
end
-- test if we have the package locally.
for i, folder in ipairs(p.packagemanager.folders) do
local location = m.createPackageLocation(folder, name, version)
if os.isdir(location) then
for i, dir in pairs(os.matchdirs(location .. '/*')) do
local n, variant = string.match(dir, '(.+)[\\|/](.+)')
variant = variant:lower()
result[variant] = __createVariant(variant, dir, nil)
end
return result
end
end
-- if we don't want server queries, just return the local results.
if _OPTIONS['no-http'] then
return result
end
-- Query the server for variant information.
local link = '/archives?name=' .. http.escapeUrlParam(name) .. '&version=' .. http.escapeUrlParam(version)
for _, hostname in ipairs(p.packagemanager.servers) do
local content, result_str, result_code = http.get(hostname .. link, { headers = http.getHttpHeader() })
if content then
local variant_tbl = json.decode(content)
for i, variant in pairs(variant_tbl) do
variant = path.getbasename(variant):lower()
if not result[variant] then
verbosef('[%s/%s] Adding variant: %s from %s.', name, version, variant, hostname)
result[variant] = __createVariant(variant, nil, hostname)
end
end
else
p.warn('A problem occured trying to contact %s.\n%s(%d).', hostname, result_str, result_code)
end
end
return result
end
local function __download(pkg, var)
local name = pkg.name
local version = pkg.version
local variant = var.name
local hostname = var.server
-- first see if we can find the package locally.
for i, folder in pairs(p.packagemanager.folders) do
local location = m.createPackageLocation(folder, name, version, variant)
if os.isdir(location) then
location = path.getabsolute(location)
verbosef('LOCAL: %s', location)
return location
end
end
-- then try the package cache.
local location = m.createPackageLocation(p.packagemanager.getCacheLocation(), name, version, variant)
if os.isdir(location) then
location = path.getabsolute(location)
verbosef('CACHED: %s', location)
return location
end
-- if we don't have a host name, we can't download it.
if not hostname then
premake.error("Package '" .. name .. "/" .. version .. "' not found on any server.")
end
-- calculate standard file_url.
local file = http.escapeUrlParam(name) .. '/' .. http.escapeUrlParam(version) .. '/' .. http.escapeUrlParam(variant) .. '.zip'
local file_url = hostname .. '/' .. file
-- get link information from server.
local link_url = '/link?name=' .. http.escapeUrlParam(name) .. '&version=' .. http.escapeUrlParam(version) .. '&variant=' .. http.escapeUrlParam(variant)
local content, result_str, result_code = http.get(hostname .. link_url, { headers = http.getHttpHeader() })
if content then
local info_tbl = json.decode(content)
if info_tbl.url then
file_url = info_tbl.url
end
if type(info_tbl.state) == "string" and info_tbl.state:lower() ~= 'active' then
premake.warn('"%s/%s" is marked "%s", consider upgrading to a known good version.', name, version, info_tbl.state)
end
if info_tbl.version then
location = m.createPackageLocation(p.packagemanager.getCacheLocation(), name, info_tbl.version, variant)
if os.isdir(location) then
verbosef('CACHED: %s', location)
return location
end
end
end
-- Download the file.
local destination = location .. '.zip'
print(' DOWNLOAD: ' .. file_url)
os.mkdir(path.getdirectory(destination))
local result_str, response_code = http.download(file_url, destination,
{
headers = http.getHttpHeader(),
progress = iif(_OPTIONS.verbose, http.reportProgress, nil)
})
if result_str ~= "OK" then
premake.error('Download of %s failed (%d)\n%s', file_url, response_code, result_str)
end
-- Unzip it
verbosef(' UNZIP : %s', destination)
zip.extract(destination, location)
os.remove(destination)
return location
end
local function __makeExecutable(directory_bin)
if os.ishost('linux') or os.ishost('macosx') then
for num, file in pairs(os.matchfiles(path.join(directory_bin, '*'))) do
os.execute("chmod +x \"" .. file .. "\"")
end
end
end
local function __getFrameworkFolders(dir)
if os.istarget('macosx') then
local pattern = path.join(dir, '*.framework')
return os.matchdirs(pattern)
else
return {}
end
end
-- Package API ----------------------------------------------------------------
local packageAPI = {}
---
-- load all the variant compatible with a specific config.
---
function packageAPI:loadvariants(filter)
local options = self.variants
local check = { }
-- Check if there is a build_custom_variant method, and use that.
if filter.cfg ~= nil and type(bnet.build_custom_variant) == 'function' then
--p.warnOnce("build_custom_variant", "'bnet.build_custom_variant(cfg, options)' is deprecated, use the 'variantmap' API instead.")
table.insertflat(check, bnet.build_custom_variant(filter.cfg, options))
end
-- Check the default variants.
table.insertflat(check, p.packagemanager.buildVariantsFromFilter(filter))
table.insert(check, 'noarch')
table.insert(check, 'universal')
-- load all those variants, if they exists.
local result = {}
for _, v in ipairs(check) do
local r = self:loadvariant(v)
if r then
table.insert(result, r)
end
end
return result
end
---
-- load a specific variant if not already loaded.
---
function packageAPI:loadvariant(variant)
-- if it's a string, find it.
if type(variant) == "string" then
variant = self.variants[variant:lower()]
end
-- if the variant doesn't exist, or is already loaded, return.
if not variant or variant.loaded then
return variant
end
verbosef(' LOAD %s/%s', self.name, variant.name)
-- download it first, in case that hasn't happened yet?
local directory = __download(self, variant)
-- register package as loaded
variant.loaded = true
variant.package = self
variant.location = directory
-- does it contain an include directory?
local directory_include = path.join(directory, 'include')
if os.isdir(directory_include) then
verbosef(' INC ' .. directory_include)
variant.includes = variant.includes or {}
table.insert(variant.includes, 'include')
end
-- does it contain an bin directory?
local directory_bin = path.join(directory, 'bin')
if os.isdir(directory_bin) then
verbosef(' BIN ' .. directory_bin)
__makeExecutable(directory_bin)
variant.bindirs = variant.bindirs or {}
table.insert(variant.bindirs, 'bin')
end
-- does it contain an runtime directory?
local directory_runtime = path.join(directory, 'runtime')
if os.isdir(directory_runtime) then
verbosef(' BIN ' .. directory_runtime)
__makeExecutable(directory_runtime)
variant.bindirs = variant.bindirs or {}
table.insert(variant.bindirs, 'runtime')
end
-- does it contain a library directory?
local directory_lib = path.join(directory, 'lib')
if os.isdir(directory_lib) then
verbosef(' LIB ' .. directory_lib)
variant.libdirs = variant.libdirs or {}
table.insert(variant.libdirs, 'lib')
variant.links = variant.links or {}
table.insertflat(variant.links, _get_lib_files(directory_lib, variant.filter.system))
end
-- on mac does it contain a framework directory?
if m.isSystem('macosx', variant.filter.system) then
local directory_fw = path.join(directory, 'framework')
if os.isdir(directory_fw) then
verbosef(' FRAMEWORK ' .. directory_fw)
variant.libdirs = variant.libdirs or {}
table.insert(variant.libdirs, 'framework')
variant.links = variant.links or {}
table.insertflat(variant.links, __getFrameworkFolders(directory_fw))
end
end
-- does it contain a package premake directive?
local path_premake = path.join(directory, 'premake5-package.lua')
if os.isfile(path_premake) then
-- store current package context.
local previous_package = package.current
package.current = v
-- load the script.
verbosef('dofile(%s)', path_premake)
variant.script = path_premake;
variant.initializer = dofile(path_premake)
-- restore package context.
package.current = previous_package
end
return variant
end
function packageAPI:generateManifest(tbl, wks)
local vtbl = {}
for _, v in pairs(self.variants) do
v:generateManifest(vtbl, wks)
end
if next(vtbl) == nil then
vtbl = nil
end
tbl[self.name] = {
type = "1.0",
version = self.version,
variants = vtbl
}
end
-- Import function ------------------------------------------------------------
return function (name, version)
local available_variants = __getVariants(name, version)
if next(available_variants) == nil then
return nil
end
local pkg = m.createPackageBase(name, version)
-- bind the found variants to the package.
pkg.variants = available_variants
for name, variant in ipairs(available_variants) do
variant.package = pkg
end
-- set metadata.
return setmetatable(pkg, {
__metatable = false,
__index = packageAPI,
__newindex = function(tbl, key, value)
error("Attempt to modify readonly table.")
end,
__tostring = function()
return "Package V1"
end,
})
end