-
Notifications
You must be signed in to change notification settings - Fork 2
/
packagemanager.lua
651 lines (541 loc) · 15.7 KB
/
packagemanager.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
---
-- Package management module
-- Copyright (c) 2014-2017 Blizzard Entertainment
---
local p = premake
p.modules.packagemanager = {}
p.modules.packagemanager._VERSION = p._VERSION
local m = p.modules.packagemanager
--[ Global package manager namespace ] ----------------------------------------
p.packagemanager = {
cache_location = nil,
folders = {},
aliases = {},
servers = {
'http://***REMOVED***',
'http://***REMOVED***'
},
systems = {
dofile("package_v2.lua"),
dofile("package_v1.lua"),
},
}
local pm = p.packagemanager
-- include the rest of the module ---------------------------------------------
include 'util.lua'
include 'context.lua'
include 'httpext.lua'
include 'variant.lua'
include 'package.lua'
include 'package_base.lua'
include 'package_project.lua'
include 'packageresolver.lua'
include 'deprecated.lua'
--[ local private variables ] -------------------------------------------------
local __loaded = {}
local __aliases = {}
local __loadedLockFile = nil
--[ private functions ]----------------------------------------------------
---
-- Get the alias information from the package server.
---
local function __getAliasTable(name)
-- if we don't want server queries, just return the local results.
if _OPTIONS['no-http'] or (pm.servers == nil) then
if pm.aliases[name] then
local aliases = {}
for _, alias in ipairs(pm.aliases[name]) do
table.insert(aliases, alias:lower())
print(alias)
end
return name:lower(), aliases
end
return name:lower(), {}
end
-- query servers for alias information.
local link = '/aliases?name=' .. http.escapeUrlParam(name)
local tbl = http.getJson(pm.servers, link)
if tbl ~= nil then
local aliases = {}
for _, alias in ipairs(tbl.Aliases or {}) do
table.insert(aliases, alias:lower())
end
return tbl.RealName:lower(), aliases
end
return name:lower(), {}
end
local function __getRealname(name)
local realname, aliases = __getAliasTable(name)
if name:lower() ~= realname:lower() then
p.warn("Using the alias '%s' is deprecated, use the real name '%s'.", name, realname)
end
return realname
end
---
-- Load lockfile.
---
local function __loadLockFile()
if __loadedLockFile ~= nil then
return __loadedLockFile
end
local g = p.api.rootContainer()
local fn = g.lockfile or path.join(_MAIN_SCRIPT_DIR, ".lockfile")
local text = io.readfile(fn)
if text == nil then
__loadedLockFile = {}
else
local result, err = json.decode(text)
if result == nil then
p.error(err)
end
__loadedLockFile = result
end
return __loadedLockFile
end
---
-- Find the first version that matches the pattern.
---
local function __findVersion(name, pattern)
local versions = {}
-- if we don't want to do any http queries, exit.
if _OPTIONS['no-http'] then
return nil
end
-- ask server for a sorted list of available versions
local link = '/api/v1/versions/' .. http.escapeUrlParam(name)
local tbl = http.getJson(pm.servers, link)
if tbl ~= nil then
for _, entry in ipairs(tbl) do
local v = entry.version
if v:match(pattern) == v then
table.insert(versions, v)
end
end
end
-- no matching results?
if #versions <= 0 then
return nil
end
-- first result is best available match.
return versions[1]
end
---
-- Find the first version that matches the pattern.
---
local function __getGitVersion(name, version)
-- if we don't want to do any http queries, exit.
if _OPTIONS['no-http'] then
return nil
end
-- ask for the type of package, which returns a pre-cached release.
local link = '/api/v1/type/' .. http.escapeUrlParam(name) .. '/' .. http.escapeUrlParam(version)
local tbl = http.getJson(pm.servers, link)
if tbl ~= nil then
return tbl.version
end
return nil
end
---
-- Import a single package...
---
local function __importPackage(name, version)
-- create the --use-<name> option.
local optionname = 'use-' .. name
p.option.add({
trigger = optionname,
value = '<path>',
default = version,
description = 'Path or Version for ' .. name .. ' package.',
category = 'Package Manager'
})
-- option overrides version from table.
version = _OPTIONS[optionname] or version
-- test if there is a '*' in the version number...
if version:contains('*') then
local lockfile = __loadLockFile()
local pattern = path.wildcards(version)
local id = name .. '/' .. version
-- Find in LockFile
local v = lockfile[id]
-- If we didn't find it in the lockfile, find a matching version.
if v == nil then
v = __findVersion(name, pattern)
if v == nil then
p.error("No version matching the pattern '%s', was found for package '%s'.", version, name)
else
p.info("Found '%s' for '%s', pattern: '%s'.", v, name, version)
end
else
p.info("Using '%s' for '%s', pattern: '%s'.", v, name, version)
end
-- Update version.
version = v
-- and store in global lockfile.
local g = p.api.rootContainer()
g.__lockfile = g.__lockfile or {}
g.__lockfile[id] = v
end
-- deal with translating git| versions.
if version:startswith("git|") then
local lockfile = __loadLockFile()
local id = name .. '/' .. version
-- Find in LockFile
local v = lockfile[id]
-- If we didn't find it in the lockfile, we need to ask the server for a hash.
if v == nil then
v = __getGitVersion(name, version)
if v == nil then
p.error("No git hash was found for package '%s' using reference '%s'.", name, version:sub(5))
else
p.info("Found '%s' for '%s', reference: '%s'.", v, name, version:sub(5))
end
else
p.info("Using '%s' for '%s', reference: '%s'.", v, name, version:sub(5))
end
-- Update version.
version = 'git|' .. v
-- and store in global lockfile.
local g = p.api.rootContainer()
g.__lockfile = g.__lockfile or {}
g.__lockfile[id] = v
end
-- now initialize the package.
for _, pkgsys in ipairs(pm.systems) do
local pkg = pkgsys(name, version)
if pkg ~= nil then
return pkg
end
end
p.error("No package '%s' version '%s' was found.", name, version)
end
local function __setPackageOptions(pkg, options)
-- initialize options table with defaults.
if pkg.options then
for name, f in pairs(pkg.options) do
f._proc = premake.field.accessor({_kind=f.kind}, "store")
if f.default ~= nil then
pkg.optionValues[name] = f._proc(nil, nil, f.default, nil)
end
end
end
-- set options set through import.
if options ~= nil then
if not pkg.options then
p.error("Package '%s' has no options.", pkg.name)
end
for name, value in pairs(options) do
local f = pkg.options[name]
if f == nil then
p.error("Package '%s' has no '%s' option.", pkg.name, name)
end
pkg.optionValues[name] = f._proc(nil, nil, value, nil)
end
end
-- check if all required options have a value.
if pkg.options then
for name, f in pairs(pkg.options) do
if f.required and pkg.optionValues[name] == nil then
p.error("Option '%s' is required for package '%s'.", name, pkg.name)
end
end
end
end
--[ public functions ]----------------------------------------------------
---
-- Get the location of where the package manager should download packages.
---
function pm.getCacheLocation()
local folder = os.getenv('PACKAGE_CACHE_PATH')
if folder then
return folder
else
if pm.cache_location then
return pm.cache_location
end
return path.join(_MAIN_SCRIPT_DIR, _OPTIONS.to or 'build', 'package_cache')
end
end
---
-- Import packages.
---
function pm.import(tbl)
if (not tbl) or (type(tbl) ~= "table") then
local caller = filelineinfo(2)
p.error("Invalid argument to import.\n @%s\n", caller)
end
-- we always need to have a workspace.
local scope = p.api.scope.current
local wks = p.api.scope.workspace
if wks == nil then
local caller = filelineinfo(2)
p.error("No workspace is currently in scope.\n @%s\n", caller)
end
-- override the 'group' function, packages shouldn't call it.
local groupFunc = _G['group']
_G['group'] = function(name)
local caller = filelineinfo(2)
p.warn("Using group '%s' inside package script.\n @%s\n", name, caller)
end
-- import packages.
local init_table = {}
for name, version in pairs(tbl) do
local realname, aliases = __getAliasTable(name)
if name:lower() ~= realname:lower() then
local caller = filelineinfo(2)
p.warn("Using the alias '%s' is deprecated, use the real name '%s'.\n @%s\n", name, realname, caller)
end
-- is there an options table?
local options = nil
if type(version) == 'table' then
options = version
version = options.version
options.version = nil
end
if not wks.package_cache[realname] then
local pkg = __importPackage(realname, version)
__setPackageOptions(pkg, options)
table.insert(init_table, pkg)
wks.package_cache[realname] = pkg
for _, alias in ipairs(aliases) do
verbosef("ALIAS: '%s' aliased to '%s'.", realname, alias)
wks.package_cache[alias] = pkg
__aliases[alias] = realname
end
else
p.warn("Package '%s' already imported.", realname)
end
end
-- setup a filter with the action and system 'filters', we don't know the rest yet.
local filter = {
action = _ACTION,
system = os.getSystemTags(os.target()),
}
-- load all variants that match the filter.
for _, pkg in ipairs(init_table) do
pkg:loadvariants(filter)
end
-- initialize.
for _, pkg in ipairs(init_table) do
if not __loaded[pkg.name] then
__loaded[pkg.name] = true
else
p.api._isIncludingExternal = true
end
-- and initialize all loaded variants.
pkg:initialize()
p.api._isIncludingExternal = nil
end
-- restore 'group' function.
_G['group'] = groupFunc
-- restore current scope.
p.api.scope.current = scope
end
---
-- Get a package by name from the current workspace.
---
function pm.getPackage(name)
local wks = p.api.scope.workspace
if wks == nil then
error("No workspace in scope.", 3)
end
local realname = __aliases[name:lower()]
if realname ~= nil then
p.warn("Using the alias '%s' is deprecated, use the real name '%s'.", name, realname)
end
return wks.package_cache[name:lower()]
end
---
-- Get an option for a package.
---
function pm.getPackageOption(name, option)
local pkg
if option == nil then
local project = p.api.scope.project or prj
if project == nil then
error("No project in scope.", 3)
end
option = name
pkg = project.package
else
local realname = __getRealname(name):lower()
pkg = pm.getPackage(realname)
end
if pkg == nil then
p.error("Package options can't be read.", 3)
end
return pkg.optionValues[option]
end
---
-- Resets the state of the package manager to it's default state.
---
function pm.reset()
__loaded = {}
pm.cache_location = nil
pm.folders = {}
pm.aliases = {}
pm.servers = {
'http://***REMOVED***',
'http://***REMOVED***'
}
end
---
-- override 'workspace' so that we can initialize a package cache.
---
p.override(p.workspace, 'new', function(base, name)
local wks = base(name)
wks.package_cache = wks.package_cache or {}
return wks
end)
---
-- override 'context.compile' so that we can inject the package options into the filters
---
p.override(p.context, 'compile', function(base, ctx)
-- if this is a project and it was generated from a package, inject package options
-- into filter context.
local prj = ctx.project
if prj ~= nil and prj.frompackage then
for name, value in pairs(prj.package.optionValues) do
p.context.addFilter(ctx, name, value)
end
end
return base(ctx)
end)
---
-- private callback for the package targetdir.
---
function pm._selectTargetDir(cfg)
if cfg.kind == 'StaticLib' then
return cfg.package_libdir
else
return cfg.package_bindir
end
end
function pm._printPackageOptions(pkg)
if not pkg.options then
return
end
printf(' Package: %s-%s', pkg.name, pkg.version)
printf(' --------------------------')
local k_length = 0
local n_length = 0
for name, f in pairs(pkg.options) do
if (#f.kind > k_length) then k_length = #f.kind end
if (#name > n_length) then n_length = #name end
end
local fmt = " %-" .. k_length .. "s %-" .. n_length .. "s %s %s"
for name, f in spairs(pkg.options) do
local k = string.format("%-" .. k_length .. "s", f.kind)
local n = string.format("%-" .. n_length .. "s", name)
local r = iif(f.required, "[required]", "[optional]")
if f.default ~= nil then
printf(" %s %s (default: %s) %s", k, n, tostring(f.default), f.description or '');
else
printf(" %s %s %s %s", k, n, r, f.description or '');
end
end
end
---
-- override 'project' so that when a package defines a new project we initialize it with some default values.
---
p.override(p.project, 'new', function(base, name, parent)
local prj = base(name, parent)
if not prj.package then
if package.current then
-- set package on project.
prj.package = package.current.package
prj.variant = package.current
prj.frompackage = true
-- set some default package values.
prj.blocks[1].targetdir = "%{premake.packagemanager._selectTargetDir(cfg)}"
prj.blocks[1].objdir = "%{cfg.package_objdir}/" .. name
prj.blocks[1].buildlog = "%{cfg.package_buildlog}"
prj.blocks[1].location = "%{prj.package_location}"
elseif parent ~= nil then
name = name:lower()
local p = parent.package_cache[name]
if p then
error("Package '" .. name .. "' already exists in the workspace.")
end
-- set package on project.
prj.package = m.createProjectPackage(name)
parent.package_cache[name] = prj.package
end
-- add project to package.
table.insert(prj.package.projects, prj)
end
return prj
end)
---
-- Set metatable on global package manager, locking it down.
---
setmetatable(pm, {
__metatable = false,
__index = function(tbl, key)
return rawget(tbl, key)
end,
__newindex = function(tbl, key, value)
if (key == "cache_location") then
assert(value == nil or type(value) == "string", "cache_location must be a string or nil.")
rawset(tbl, key, value)
elseif (key == "folders") then
assert(type(value) == "table", "folders must be a table.")
rawset(tbl, key, value)
elseif (key == "servers") then
assert(type(value) == "table", "servers must be a table.")
rawset(tbl, key, value)
elseif (key == "aliases") then
assert(type(value) == "table", "aliases must be a table.")
rawset(tbl, key, value)
else
error("Attempt to modify packagemanager field: " .. key)
end
end,
__tostring = function()
return "Package Manager"
end,
})
---
--- inject lockfile store into p.main.preBake
---
p.override(p.main, "postBake", function (base)
-- write the lockfile.
local g = p.api.rootContainer()
local fn = g.lockfile or path.join(_MAIN_SCRIPT_DIR, ".lockfile")
if g.__lockfile ~= nil then
local lockfile = json.encode_pretty(g.__lockfile)
if #lockfile > 2 then
os.mkdir(path.getdirectory(fn))
local f, err = os.writefile_ifnotequal(lockfile, fn);
else
os.remove(fn)
end
else
os.remove(fn)
end
-- now run the prebake.
base()
end)
---
--- provide help for package options.
---
p.override(p.main, 'processCommandLine', function(base)
if (_OPTIONS["package-help"]) then
print('Package options:')
print();
for wks in p.global.eachWorkspace() do
printf('Workspace: %s', wks.name)
printf('--------------------------')
for name, pkg in pairs(wks.package_cache) do
if not __aliases[name] then
pm._printPackageOptions(pkg)
end
end
end
print('--------------------------')
print('done.');
os.exit(1)
end
base()
end)
-- return the module pointer.
return p.modules.packagemanager