/
init.lua
1493 lines (1350 loc) · 47.1 KB
/
init.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local pkgu = require 'pkg'.util
local xsys = require 'xsys'
local lfs = require 'lfs'
local serpent = require 'serpent'
local time = require 'time'
local md5 = require 'md5'
-- TODO: Remove dependency on xsys.
local trim, split = xsys.string.trim, xsys.string.split
local append = xsys.table.append
local jos, jarch = jit.os, jit.arch
-- TODO: Aggregate multiple luarocks into same package to solve some conflicts.
-- TODO: Implement external libraries handling.
-- TODO: Implement proper semantic versioning.
-- TODO: Refactor:
-- TODO: + rock.name, rock.version.
-- TODO: + pass around save_error, save_error_sys, save_pass.
-- TODO: Are all these utilities useful in general (xsys? use pl instead?) ?
-- Create a valid path name:
local function D(...)
return table.concat({ ... }, '/')
end
local function mkdir_all(p)
local sp = split(p, '/')
for i=1,#sp-1 do
local pp = table.concat(sp, '/', 1, i)
lfs.mkdir(pp)
end
end
local function pathfilesattr(p, o)
o = o or { }
for f in lfs.dir(p) do
if f ~= "." and f ~= ".." then
local pf = p..'/'..f
local pfmode = lfs.attributes(pf).mode
if pfmode == 'directory' then
pathfilesattr(pf, o)
elseif pfmode == 'file' then
o[pf] = lfs.attributes(pf)
else
error('unexpected path mode "'..pfmode..'"')
end
end
end
return o
end
-- Files only (ignore empty folders).
local function pathdiff(p0attr, p1attr)
local rem, add, mod = { }, { }, { }
for f,_ in pairs(p0attr) do
if not p1attr[f] then
rem[#rem + 1] = f
end
end
for f,_ in pairs(p1attr) do
if not p0attr[f] then
add[#add + 1] = f
elseif p0attr[f].modification ~= p1attr[f].modification then
mod[#mod + 1] = f
end
end
local new = append(add, mod)
table.sort(rem); table.sort(add); table.sort(mod); table.sort(new)
return new, rem, add, mod
end
local function file_exist(filename)
local f = io.open(filename)
if f then
f:close()
end
return f ~= nil
end
local function file_read(filename, opt)
opt = opt or 'rb'
local f = assert(io.open(filename, opt))
local s = f:read('*a')
assert(f:close())
return s
end
local function file_write(filename, s)
local f = assert(io.open(filename, 'wb'))
f:write(s)
assert(f:close())
end
local function load_config(s)
local env = { }
local f = assert(loadstring(s))
setfenv(f, env)
f()
return env
end
local function deserialize(s)
return assert(loadstring(s))()
end
local function serialize(t)
return 'return '..serpent.block(t, { comment = false })
end
-- TODO: Add example file.
local config_filename = D(os.getenv('LUA_ROOT'), 'host', 'config-dev.lua')
local config = deserialize(file_read(config_filename))
local luarocks_address = config.luarocks_address
local dir = config.dir
local cmd = config.cmd
local show_new_only = config.show_new_only
local function execute(command, fstdout, fstderr)
local toexecute = command..' > '..fstdout..' 2> '..fstderr
return os.execute(toexecute)
end
local function to_unix_eol(paths)
for i=1,#paths do
local command = cmd.dos2unix..' '..paths[i]
assert(execute(command, dir.null, dir.null) == 0)
end
end
--------------------------------------------------------------------------------
local function copy_table(t)
if type(t) ~= 'table' then
return t
end
local o = { }
for k, v in pairs(t) do
o[k] = copy_table(v)
end
return o
end
local function keys_to_array(t)
local o = { }
for k in pairs(t) do
o[#o + 1] = k
end
return o
end
local function array_to_true(x)
local o = { }
for i=1,#x do
o[x[i]] = true
end
return o
end
local function setvarg(t, v, k, ...)
local nextk = ...
if not nextk then
t[k] = v
else
t[k] = type(t[k]) ~= 'nil' and t[k] or { }
setvarg(t[k], v, ...)
end
end
local function getvarg(t, k, ...)
local nextk = ...
if not nextk then
return t[k]
elseif type(t[k]) == 'nil' then
return nil
else
return getvarg(t[k], ...)
end
end
--------------------------------------------------------------------------------
local write_persisted = { }
local function persistent(filename)
local serialized = file_exist(filename) and deserialize(file_read(filename))
local persisted = serialized or { }
write_persisted[persisted] = function()
serialized = serialize(persisted)
file_write(filename, serialized)
end
return persisted
end
-- rockname -> upkgversion -> os -> arch -> true/false:
local rock_pass = persistent(D(dir.luarocks_state, 'pass.lua'))
-- rockname -> rockversion -> modules -> true:
local rock_modules = persistent(D(dir.luarocks_state, 'modules.lua'))
-- TODO
local module_bundles = persistent(D(dir.luarocks_state, 'bundles.lua'))
-- excludeid -> rockname -> rockversion -> { rock_ name, rock_ version }:
local rock_exclude_sys = persistent(D(dir.luarocks_state_sys,
'exclude_sys.lua'))
-- errorid -> rockname -> rockversion -> errormessage:
local rock_error = persistent(D(dir.luarocks_state, 'error.lua'))
-- errorid -> rockname -> rockversion -> errormessage:
local rock_error_sys = persistent(D(dir.luarocks_state_sys, 'error_sys.lua'))
local function notnil(x)
return type(x) ~= 'nil'
end
local function logger(kind, name, version)
assert(kind and name and version)
return function(message, ...)
assert(notnil(message))
setvarg(kind, message, name, version, ...)
write_persisted[kind]()
end
end
local function id_logger(kind, name, version)
assert(kind and name and version)
return function(id, message, ...)
assert(notnil(id) and notnil(message))
setvarg(kind, message, id, name, version, ...)
write_persisted[kind]()
end
end
local function has_error(kind, targetrockname, targetrockversion)
for id,rocknames in pairs(kind) do
for rockname,rockversions in pairs(rocknames) do
for rockversion,info in pairs(rockversions) do
if rockname == targetrockname and (rockversion == targetrockversion or rockversion..'-0' == targetrockversion) then
return id, info
end
end
end
end
end
local function module_exist(kind, targetmodulename)
for rockname,rockversions in pairs(kind) do
for rockversion,modules in pairs(rockversions) do
for modulename in pairs(modules) do
-- Case insensitive as some OS are so:
if modulename:lower() == targetmodulename:lower() then
return rockname, rockversion
end
end
end
end
end
--------------------------------------------------------------------------------
local function is_valid_version(version)
local ok = pcall(pkgu.versplit, version)
if not ok then
return nil, version..' is not a valid module version'
end
return true
end
-- Rock and upkg versions only differ in the release number.
-- TODO: Change
local PKG_VER = 3
local PKG_MUL = 100
assert(PKG_VER <= PKG_MUL - 1)
local function upkg_release(rockname, pre_release, release)
release = release and assert(tonumber(release)) or 0
assert(release >= 0, release)
return tostring(release*PKG_MUL + PKG_VER)
end
local function rock_release(rockname, pre_release, release)
release = assert(tonumber(release))
assert(release >= 0, release)
return tostring(math.floor(release/PKG_MUL))
end
local function to_upkg_version(rockname, rockversion)
local pre_release, release = unpack(split(rockversion, '-'))
return pre_release..'-'..upkg_release(rockname, pre_release, release)
end
local function to_rock_version(rockname, upkgversion)
local pre_release, release = unpack(split(upkgversion, '-'))
return pre_release..'-'..rock_release(rockname, pre_release, release)
end
--------------------------------------------------------------------------------
local function download_rockspec(rockname, rockversion)
local rockspec_filename = rockname..'-'..rockversion..'.rockspec'
local rockspec_path = D(dir.luarocks_repository, 'spec', rockspec_filename)
if not file_exist(rockspec_path) then
local ok, rockspec_code = pcall(pkgu.download, luarocks_address,
rockspec_filename)
if not ok then
return nil, rockspec_code
end
file_write(rockspec_path, rockspec_code)
end
return true
end
local function load_downloaded_rockspec(rockname, rockversion)
local rockspec_filename = rockname..'-'..rockversion..'.rockspec'
local rockspec_path = D(dir.luarocks_repository, 'spec', rockspec_filename)
local rockspec_code = file_read(rockspec_path)
local ok, rockspec = pcall(load_config, rockspec_code)
if not ok then
return nil, rockspec
end
return rockspec
end
local function is_lua_dependency(s)
return s:find('^lua ') or s == 'lua'
end
-- TODO: Use Luarocks version-parser.
local function get_unsupported_lua(v)
if v:find('lua >= 5.2')
or v:find('lua >= 5.3')
or v:find('lua ~> 5.2')
or v:find('lua ~> 5.3')
or v:find('lua == 5.2')
or v:find('lua == 5.3')
or v:find('lua > 5.1') then
return v
end
end
local function is_supported_lua(rockspec)
local unsupported_lua = false
for _,dependency in ipairs(rockspec.dependencies or { }) do
if is_lua_dependency(dependency) then
unsupported_lua = unsupported_lua or get_unsupported_lua(dependency)
end
end
if unsupported_lua then
return nil, unsupported_lua
end
return true
end
local luarocksos_to_luaos = {
['!windows'] = 'Linux,OSX',
windows = 'Windows',
win32 = 'Windows',
macosx = 'OSX',
linux = 'Linux',
unix = 'OSX,Linux',
bsd = 'OSX',
}
local function is_supported_os(rockspec)
if not rockspec.supported_platforms then
return true
end
local supported_os = false
for _,os in ipairs(rockspec.supported_platforms) do
supported_os = supported_os or (luarocksos_to_luaos[os] or ''):find(jos)
end
if not supported_os then
return nil, rockspec.supported_platforms
end
return true
end
local function is_supported_external_libraries(rockspec)
if rockspec.external_dependencies then
return nil, rockspec.external_dependencies
end
return true
end
-- TODO: Use Luarocks version-parser.
local function name_version_dependency(s)
local first = unpack(split(s, ','))
-- We consider all the below equal and impose semantic versioning:
first = first:gsub(' ~> ', ' >= ')
:gsub(' ~= ', ' >= ')
:gsub(' == ', ' >= ')
:gsub(' > ', ' >= ')
:gsub(' = ', ' >= ')
-- Deal with unsupported but valid spec:
first = first:gsub('lua < 5.3', 'lua >= 5.1')
-- Deal with badly formatted dependencies, just a space between name and ver:
if not first:find('>=') then
first = first:gsub(' ', '>=')
end
local name, version = unpack(split(first, '>='))
name = trim(name)
if version then
version = trim(version)
local ok, err = is_valid_version(version)
if not ok then
return nil, err
end
return name, version
else
return name
end
end
local function is_valid_version_dependencies(rockspec)
for _,dependency in ipairs(rockspec.dependencies or { }) do
if not name_version_dependency(dependency) then
return nil, dependency
end
end
return true
end
local function upkg_dependencies(rockspec, all_valid_versions)
local out = { luajit = '2.0' }
for _,dependency in ipairs(rockspec.dependencies or { }) do
if not is_lua_dependency(dependency) then
local name, version = name_version_dependency(dependency)
if not version then
local info = pkgu.infobest(all_valid_versions, name)
if info then
version = info.version
else
-- False is not a valid version string format of course, but it will
-- result in correct logic when trying to install dependencies, i.e.
-- 'dependency_unavailable' in install():
version = false
end
end
out[name] = version
end
end
return out
end
local function upkg_repo_all_valid_versions(manifest_repository)
local all_valid_versions = { }
for rockname,rockversions in pairs(manifest_repository) do
for rockversion,_ in pairs(rockversions) do
if is_valid_version(rockversion) then
pkgu.infoinsert(all_valid_versions, rockname, {
version = rockversion
})
end
end
end
return all_valid_versions
end
local function description_field(description, field, rockname)
local desc
if description and description[field] then
desc = description[field]
else
desc = ''
end
if rockname then
return rockname..' : '..desc
else
return desc
end
end
local function save_pass(rockname, rockversion, status)
local upkgversion = to_upkg_version(rockname, rockversion)
local save = logger(rock_pass, rockname, upkgversion)
save(status, jos, jarch)
return status
end
local function upkg_repo_valid(manifest_repository)
local all_valid_versions = upkg_repo_all_valid_versions(manifest_repository)
local valid = { }
for rockname,rockversions in pairs(manifest_repository) do
for rockversion,_ in pairs(rockversions) do
local save_error = id_logger(rock_error, rockname, rockversion)
local save_error_sys = id_logger(rock_error_sys, rockname, rockversion)
local ok = is_valid_version(rockversion)
if not ok then
save_error('not_valid_version_format', rockversion)
save_pass(rockname, rockversion, false)
break
end
local err
ok, err = download_rockspec(rockname, rockversion)
if not ok then
save_error('rock_download_error', err)
save_pass(rockname, rockversion, false)
break
end
ok, err = load_downloaded_rockspec(rockname, rockversion)
if not ok then
save_error('rock_loadstring_error', err)
save_pass(rockname, rockversion, false)
break
end
local rockspec = ok
ok, err = is_supported_os(rockspec)
if not ok then
save_error_sys('unsupported_os', err)
save_pass(rockname, rockversion, false)
break
end
ok, err = is_supported_external_libraries(rockspec)
if not ok then
save_error('unsupported_external_library', err)
save_pass(rockname, rockversion, false)
break
end
ok, err = is_supported_lua(rockspec)
if not ok then
save_error('unsupported_lua_version', err)
save_pass(rockname, rockversion, false)
break
end
ok, err = is_valid_version_dependencies(rockspec)
if not ok then
save_error('not_valid_version_format_of_dependency', err)
save_pass(rockname, rockversion, false)
break
end
local dependencies = upkg_dependencies(rockspec, all_valid_versions)
local description = rockspec.description
-- To get actual __meta.lua it is still necessary to modify 'require' to
-- account for rockname -> { modules } and to add intra-dependencies
-- (same reason); it is also necessary to set a version to the upkgversion
-- , to remove rockspec and to add name.
local info = {
version = rockversion,
require = dependencies,
-- These are not going to be modified:
description = description_field(description, 'summary', rockname),
license = description_field(description, 'license'),
homepage = description_field(description, 'homepage'),
}
pkgu.infoinsert(valid, rockname, info)
end
end
return valid
end
local function init_manifest()
local manifest_path = D(dir.luarocks_repository, 'manifest.lua')
local manifest_code = file_read(manifest_path) -- Saved without byte opt.
local manifest = load_config(manifest_code)
return manifest
end
local function repo_from_manifest()
local manifest = init_manifest()
return upkg_repo_valid(manifest.repository)
end
--------------------------------------------------------------------------------
local function is_in_boundle(modulename, rockname)
if module_bundles[modulename] then
for _,p in ipairs(module_bundles[modulename].pattern) do
if rockname:find(p) then
return true
end
end
end
return false
end
local function bundle_hash(components)
local hash = { }
for _,c in pairs(components) do
hash[#hash + 1] = c.meta.name
hash[#hash + 1] = c.meta.version
end
return md5.sumhexa(table.concat(hash))
end
local function bundle_require(components)
local require = { }
for _,c in pairs(components) do
for name,version in pairs(c.meta.require) do
if require[name] then
-- Simplistic approach: same major, take highest:
assert(pkgu.versplit(require[name]) == pkgu.versplit(version))
if pkgu.verlt(require[name], version) then
require[name] = version
end
else
require[name] = version
end
end
end
return require
end
local function bundle_license(components)
local license = { }
for _,c in pairs(components) do
license[c.meta.license] = true
end
return table.concat(keys_to_array(license), ' + ')
end
-- TODO: More visual friendly: alphabetical order + \n:
local function bundle_description(components, bundlename)
local description = { }
for _,c in pairs(components) do
local desc = c.meta.description:gsub(' :', '~'..c.meta.version..' :')
description[#description + 1] = desc
end
return bundlename..'-bundle: '..table.concat(description, ' + ')
end
local function bundle_meta(components, bundlename)
return {
name = bundlename,
version = '0.'..tostring(#module_bundles[bundlename].hash),
require = bundle_require(components),
license = bundle_license(components),
homepage = module_bundles[bundlename].homepage,
description = bundle_description(components, bundlename),
}
end
local function bundle_dir(bundlename, bundleversion)
return D(dir.luarocks_package, '__bundle', bundlename, bundleversion)
end
local function bundle_copy_paths(paths, bundlename, bundleversion)
for from in pairs(paths) do
local _,_,rockname,subpath = from:find(dir.luarocks_package..'/([^/]+)/[^/]+/[^/]+/(.+)')
if subpath ~= '__meta.lua' then
local to = D(bundle_dir(bundlename, bundleversion), bundlename)
if subpath:sub(1,2) == '__' and subpath:sub(1,5) ~= '__bin' then
to = D(to, (subpath:gsub('^__(.-)/', '__%1/'..rockname..'/')))
else
to = D(to, subpath)
end
local content = file_read(from)
if file_exist(to) then
local existing_content = file_read(to)
if existing_content ~= content then
print('SKIPPING '..bundlename..' due to conflict in '..to)
return false
end
else
mkdir_all(to)
file_write(to, content)
end
end
end
return true
end
--------------------------------------------------------------------------------
local function install_rock(rockname, rockversion)
local luarockscmd = cmd.luarocks..'install '..rockname..' '..rockversion
local logpath = D(dir.luarocks_state_sys, 'log', rockname..'~'..rockversion)
local logstdout = logpath..'_out.txt'
local logstderr = logpath..'_err.txt'
local errorcode = execute(luarockscmd, logstdout, logstderr)
if errorcode ~= 0 then -- TODO: Check ok to do binary in windows for this.
local msg = {
stdout = rockname..'~'..rockversion..'_out.txt',
stderr = rockname..'~'..rockversion..'_err.txt',
}
local save_error_sys = id_logger(rock_error_sys, rockname, rockversion)
save_error_sys('luarocks_install_error', msg)
return nil, file_read(logstderr)
end
return true
end
local function changed_paths(directory, f, ...)
local dir_before = pathfilesattr(directory)
local ok, err = f(...)
if not ok then
return nil, err
end
local dir_after = pathfilesattr(directory)
return (pathdiff(dir_before, dir_after))
end
------------
local function content_if_find(path, tomatch)
local _, last = path:find(tomatch, 1, true) -- Plain matching.
return last and path:sub(last + 2)
end
local function discard_2_args(_, _, ...)
return ...
end
local function captures(s, pattern)
return discard_2_args(s:find(pattern))
end
local function match_modulenames_content_lua(path)
local content = content_if_find(path, dir.luarockstree_lua)
if content then
local plain_modulename = captures(content, '^([^/]+)%.lua')
if plain_modulename then
return true, { plain_modulename }, 'init.lua'
end
local modulename, lua_content = captures(content, '^([^/]+)/(.+)')
if modulename and lua_content then
return true, { modulename }, lua_content
end
-- Plain files not ending in '.lua' in dir.luarockstree_lua are skipped:
return true
end
end
-- TODO: OSX: .dylib or .so?
local os_clib_extension = {
Windows = 'dll',
Linux = 'so',
OSX = 'so',
}
local clib_extension = os_clib_extension[jos]
local function match_modulenames_content_clua(path)
local content = content_if_find(path, dir.luarockstree_clua)
if content then
assert(content:find('%.'..clib_extension), content)
local clua_content = D(jos, jarch, '-'..content:gsub('/', '_'))
local plain_modulename = captures(content, '^([^/]+)%.'..clib_extension)
if plain_modulename then
return true, { plain_modulename }, clua_content
end
local modulename = captures(content, '^([^/]+)')
return true, { modulename }, clua_content
end
end
local function match_modulenames_content_extra(path, rockname, rockversion,
modules)
local tomatch = D(dir.luarockstree_extra, rockname, rockversion)
local content = content_if_find(path, tomatch)
if content then
local rockspec = rockname..'-'..rockversion..'.rockspec'
if content ~= 'rock_manifest' and content ~= rockspec then
if content:find('^bin') then -- Handle executable scripts case.
local script_content = file_read(path)
local script_parse_ok = loadstring(script_content)
if not script_parse_ok then
return true -- Not valid Lua scripts are skipped.
end
end
content = content and '__'..content
-- TODO: Use sys_rock_error here.
assert(content ~= '__meta.lua') -- Reserved for package manager.
local modulenames = keys_to_array(modules)
-- If no lua or clua files have been installed modules will be an empty
-- table (this can happen for pure-script luarocks for instance; this is
-- not desiderable as the extra files would be lost, hence use rockname:
if #modulenames == 0 then
modulenames[1] = rockname
end
return true, modulenames, content
end
return true
end
end
local function fixed_width(s, nchar)
return s:sub(1, math.min(#s, nchar))..(' '):rep(math.max(nchar - #s, 0))
end
local function remove_expected(unmatched)
unmatched[D(dir.luarockstree_extra, 'manifest')] = nil
for file in pairs(unmatched) do
if file:find(dir.luarockstree_bin, 1, true) then
unmatched[file] = nil
end
end
end
local function has_module_conflict(modules, rockname, rockversion)
for modulename in pairs(modules) do
if not is_in_boundle(modulename, rockname) then
local in_rockname, in_rockversion = module_exist(rock_modules, modulename)
if in_rockname and in_rockname ~= rockname then
local save_error = id_logger(rock_error, rockname, rockversion)
save_error('module_conflict', {
module_name = modulename,
rock_name = in_rockname,
rock_version = in_rockversion
})
return modulename
end
end
end
return false
end
local function module_dir_upkg(rockname, upkgversion, modulename)
assert(rockname and upkgversion and modulename)
return D(dir.luarocks_package, rockname, upkgversion, modulename)
end
local function module_dir(rockname, rockversion, modulename)
return module_dir_upkg(rockname, to_upkg_version(rockname, rockversion),
modulename)
end
local function handle(modules, unmatched, installed_paths, handler,
rockname, rockversion)
for _, path in ipairs(installed_paths) do
local match, modulenames, content = handler(path, rockname, rockversion,
modules)
if match then
assert(unmatched[path]) -- Multiple match is matching logic error!
unmatched[path] = nil
end
if content then
for _, modulename in ipairs(modulenames) do
local to = D(module_dir(rockname, rockversion, modulename), content)
modules[modulename] = modules[modulename] or { }
if modules[modulename][to] then
local save_error_sys = id_logger(rock_error_sys, rockname,
rockversion)
save_error_sys('destination_already_set', to)
return nil, 'destination already set '..to
end
modules[modulename][to] = path
end
end
end
return true
end
local function modules_links(installed_paths, rockname, rockversion)
local modules, unmatched = { }, array_to_true(installed_paths)
local lua_ok, lua_err = handle(modules, unmatched, installed_paths,
match_modulenames_content_lua, rockname, rockversion)
if not lua_ok then
return nil, lua_err
end
local clua_ok, clua_err = handle(modules, unmatched, installed_paths,
match_modulenames_content_clua, rockname, rockversion)
if not clua_ok then
return nil, clua_err
end
local extra_ok, extra_err = handle(modules, unmatched, installed_paths,
match_modulenames_content_extra, rockname, rockversion)
if not extra_ok then
return nil, extra_err
end
remove_expected(unmatched)
local unmatched_array = keys_to_array(unmatched)
if #unmatched_array > 0 then
local save_error_sys = id_logger(rock_error_sys, rockname, rockversion)
save_error_sys('unexpected_files', unmatched_array)
local unmatched_text = table.concat(unmatched_array, '\n')
return nil, 'unexpected files: '..unmatched_text
end
return modules
end
local function copy_links(links, rockname, rockversion)
for to, from in pairs(links) do
local content = file_read(from)
if file_exist(to) then
local existing_content = file_read(to)
if existing_content ~= content then
local save_error = id_logger(rock_error, rockname, rockversion)
save_error('file_os_conflict', {
from = from,
to = to,
current_os = jos,
})
return nil, 'file os conflict: '..to
end
else
mkdir_all(to)
file_write(to, content)
end
end
return true
end
local function copy_modules_links(modules, rockname, rockversion)
for _, links in pairs(modules) do
local ok, err = copy_links(links, rockname, rockversion)
if not ok then
return nil, err
end
end
return true
end
local function copy_modules_meta(meta, rockname, rockversion)
for modulename, modulemeta in pairs(meta) do
local content = serialize(modulemeta)
local to = D(module_dir(rockname, rockversion, modulename), '__meta.lua')
if file_exist(to) then
local existing_content = file_read(to)
if existing_content ~= content then
error(existing_content..' ~= '..content)
end
else
file_write(to, content)
end
end
end
local function fix_require_rocknames_to_modules(modulemeta, repo)
local fixed_require = { }
for dep_name, dep_version in pairs(modulemeta.require) do
if dep_name ~= 'luajit' and dep_name ~= 'pkg' then
local info = assert(pkgu.infobest(repo, dep_name, dep_version))
local target_version = info.version
local modules = assert(getvarg(rock_modules, dep_name, target_version))
for dep_modulename in pairs(modules) do
if dep_modulename ~= modulemeta.name then -- No bundle self-dependency.
if is_in_boundle(dep_modulename, dep_name) then
dep_version = '0' -- Simplistic approach: no versioning for bundles.
end
fixed_require[dep_modulename] = dep_version
end
end
else
fixed_require[dep_name] = dep_version
end
end
modulemeta.require = fixed_require
end
local function add_intra_dependencies(modulename, modulemeta, meta, upkgversion)
for othermodulename in pairs(meta) do
if modulename ~= othermodulename then
modulemeta.require[othermodulename] = upkgversion
end
end
end
local function get_meta(info, modules, rockname, rockversion, repo)
local upkgversion = to_upkg_version(rockname, rockversion)
local meta_common = {
version = upkgversion, -- Info.version is rockversion.
require = info.require,
description = info.description,
license = info.license,
homepage = info.homepage,
}
local meta = { }
for modulename in pairs(modules) do
meta[modulename] = copy_table(meta_common)
meta[modulename].name = modulename
fix_require_rocknames_to_modules(meta[modulename], repo)
end
for modulename, modulemeta in pairs(meta) do
add_intra_dependencies(modulename, modulemeta, meta, upkgversion)
end
return meta
end
local function check_has_files(paths, rockname, rockversion)
if #paths == 0 then
local save_error_sys = id_logger(rock_error_sys, rockname, rockversion)
save_error_sys('no_installed_files', true)
return nil
end
return true
end
local function status_printer(rockname, rockversion, level)
assert(rockname and rockversion)
return function(status, message, exclude)
assert(status)
if not exclude then
local fixed_status = fixed_width((' '):rep(level)..status, 10)
local fixed_name_version = fixed_width(rockname..'~'..rockversion, 32)
io.write(fixed_status, ' : ', fixed_name_version)
if message then
io.write(' : ', message)
end
io.write('\n')
end
end
end