-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
539 lines (471 loc) · 13.1 KB
/
init.lua
File metadata and controls
539 lines (471 loc) · 13.1 KB
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
olua = {}
function __TRACEBACK__(message)
print(debug.traceback(message))
end
---@type 'windows'|'macos'|'linux'
olua.os = package.cpath:find('?.dll') and 'windows' or
((io.popen('uname'):read("*l"):find('Darwin')) and 'macos' or 'linux')
function olua.is_windows()
return olua.os == "windows"
end
local _ipairs = ipairs
function ipairs(t)
local mt = getmetatable(t)
return (mt and mt.__ipairs or _ipairs)(t)
end
local _pairs = pairs
function pairs(t)
local mt = getmetatable(t)
return (mt and mt.__pairs or _pairs)(t)
end
function olua.use(...)
end
-------------------------------------------------------------------------------
-- error
-------------------------------------------------------------------------------
local willdo = ''
function olua.willdo(exp)
willdo = olua.format(exp)
end
local function throw_error(msg)
if #willdo > 0 then
print(willdo)
end
error(msg)
end
function olua.error(exp)
throw_error(olua.format(exp))
end
function olua.assert(cond, exp)
if not cond then
olua.error(exp or '<no assert info>')
end
return cond
end
function olua.print(exp)
print(olua.format(exp))
end
-------------------------------------------------------------------------------
-- io
-------------------------------------------------------------------------------
function olua.exist(path)
path = olua.format(path)
local ok, err, code = os.rename(path, path)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
function olua.mkdir(dir)
dir = olua.format(dir)
if not olua.exist("${dir}/") then
if olua.os == "windows" then
dir = string.gsub(dir, "/", "\\")
olua.exec("mkdir ${dir}")
else
olua.exec("mkdir -p ${dir}")
end
end
end
function olua.pwd()
local dir
if olua.os == "windows" then
dir = io.popen("cd"):read("*l")
dir = dir:gsub("\\", "/")
else
dir = io.popen("pwd"):read("*l")
end
return dir
end
function olua.rmdir(dir)
dir = olua.format(dir)
if olua.os == "windows" then
dir = string.gsub(dir, "/", "\\")
olua.exec("rmdir /s /q ${dir}")
else
olua.exec("rm -rf ${dir}")
end
end
function olua.rm(path)
path = olua.format(path)
print('rm ' .. path)
os.remove(path)
end
function olua.exec(expr)
expr = olua.format(expr)
local flag = false
for cmd in string.gmatch(expr, '[^\n\r]+') do
if string.find(cmd, '[\\] *$') then
if flag then
print(" " .. cmd)
else
print("exec: " .. cmd)
end
flag = true
else
flag = false
print("exec: " .. cmd)
end
end
os.execute(expr)
end
function olua.load_manifest(path)
path = olua.format(path)
local f = io.open(path, "r")
if not f then
olua.print("manifest not found: ${path}")
return
else
f:close()
return dofile(path)
end
end
function olua.write(path, data)
path = olua.format(path)
local f = assert(io.open(path, "w+b"), path)
f:write(data)
f:close()
end
function olua.git_clone(git_dir, git, branch, commit)
if not olua.exist("${git_dir}/.git/config") then
if branch then
olua.exec("git clone --branch ${branch} ${git} ${git_dir}")
else
olua.exec("git clone ${git} ${git_dir}")
end
else
if branch then
olua.exec("git -C ${git_dir} checkout ${branch}")
end
olua.exec("git -C ${git_dir} pull")
end
if commit then
olua.exec("git -C ${git_dir} checkout ${commit}")
end
olua.exec("git -C ${git_dir} submodule init")
olua.exec("git -C ${git_dir} submodule update")
end
-------------------------------------------------------------------------------
-- format args
-------------------------------------------------------------------------------
local function lookup(level, key)
assert(key and #key > 0, key)
local value
for i = 1, 256 do
local k, v = debug.getlocal(level, i)
if k == key then
value = v
elseif not k then
break
end
end
if value then
return value
end
local info1 = debug.getinfo(level, 'Sn')
local info2 = debug.getinfo(level + 1, 'Sn')
if info1.source == info2.source or
string.find(info1.source, "init.lua$")
then
return lookup(level + 1, key)
end
end
local function eval(line)
return string.gsub(line, '${[%w_.?]+}', function (str)
-- search caller file path
local level = 1
local path
while true do
local info = debug.getinfo(level, 'Sfn')
if info then
if string.find(info.source, "init.lua$") and
info.func == olua.format
then
break
else
level = level + 1
end
else
break
end
end
-- search in the functin local value
local indent = string.match(line, ' *')
local key = string.match(str, '[%w_]+')
local opt = string.match(str, '%?+')
local value = lookup(level + 1, key) or _G[key]
for field in string.gmatch(string.match(str, "[%w_.]+"), '[^.]+') do
if not value then
break
elseif field ~= key then
value = value[field]
end
end
if value == nil and not opt then
error("value not found for '" .. str .. "'")
end
-- indent the value if value has multiline
local prefix, posfix = '', ''
if type(value) == 'table' then
local mt = getmetatable(value)
if mt and mt.__tostring then
value = tostring(value)
else
error("no meta method '__tostring' for " .. str)
end
elseif value == nil then
value = 'nil'
elseif type(value) == 'string' then
value = value:gsub('[\n]*$', '')
if opt then
value = olua.trim(value)
if string.find(value, '[\n\r]') then
value = '\n' .. value
prefix = '[['
posfix = '\n' .. indent .. ']]'
indent = indent .. ' '
elseif string.find(value, '[\'"]') then
value = '[[' .. value .. ']]'
else
value = "'" .. value .. "'"
end
end
else
value = tostring(value)
end
return prefix .. string.gsub(value, '\n', '\n' .. indent) .. posfix
end)
end
local function doeval(expr)
local arr = {}
local idx = 1
while idx <= #expr do
local from, to = string.find(expr, '[\n\r]', idx)
if not from then
from = #expr + 1
to = from
end
arr[#arr + 1] = eval(string.sub(expr, idx, from - 1))
idx = to + 1
end
return table.concat(arr, '\n')
end
function olua.trim(expr, indent)
if type(expr) == 'string' then
expr = string.gsub(expr, '[\n\r]', '\n')
expr = string.gsub(expr, '^[\n]*', '') -- trim head '\n'
expr = string.gsub(expr, '[ \n]*$', '') -- trim tail '\n' or ' '
local space = string.match(expr, '^[ ]*')
indent = string.rep(' ', indent or 0)
expr = string.gsub(expr, '^[ ]*', '') -- trim head space
expr = string.gsub(expr, '\n' .. space, '\n' .. indent)
expr = indent .. expr
end
return expr
end
function olua.format(expr, indent)
expr = doeval(olua.trim(expr, indent))
while true do
local s, n = string.gsub(expr, '\n[ ]+\n', '\n\n')
expr = s
if n == 0 then
break
end
end
while true do
local s, n = string.gsub(expr, '\n\n\n', '\n\n')
expr = s
if n == 0 then
break
end
end
expr = string.gsub(expr, '{\n\n', '{\n')
expr = string.gsub(expr, '\n\n}', '\n}')
return expr
end
-------------------------------------------------------------------------------
-- command misc
-------------------------------------------------------------------------------
function olua.ipairs(t, walk)
for i, v in ipairs(t) do
walk(i, v)
end
end
function olua.pairs(t, walk)
for k, v in pairs(t) do
walk(k, v)
end
end
function olua.newarray(sep, prefix, posfix)
local mt = {}
mt.__index = mt
function mt:clear()
for i = 1, #self do
self[i] = nil
end
return self
end
function mt:push(v)
if v ~= nil then
self[#self + 1] = v
end
return self
end
function mt:pushf(v)
if v ~= nil then
self[#self + 1] = olua.format(v)
end
return self
end
function mt:insert(v)
if v ~= nil then
table.insert(self, 1, v)
end
end
function mt:insertf(v)
if v ~= nil then
table.insert(self, 1, olua.format(v))
end
end
function mt:merge(t)
for _, v in ipairs(t) do
self[#self + 1] = v
end
return self
end
function mt:__tostring()
sep = sep or '\n'
prefix = prefix or ''
posfix = posfix or ''
return prefix .. table.concat(self, sep) .. posfix
end
return setmetatable({}, mt)
end
function olua.clone(t, newt)
newt = newt or {}
for k, v in pairs(t) do
newt[k] = v
end
return newt
end
function olua.toarray(map)
local arr = {}
for k, v in pairs(map) do
arr[#arr + 1] = {key = k, value = v}
end
table.sort(arr, function (a, b) return a.key < b.key end)
return arr
end
function olua.newhash(map_only)
local hash = {values = {}, map = {}}
function hash:clear()
hash.values = {}
hash.map = {}
end
function hash:clone()
local new = olua.newhash(map_only)
new.values = olua.clone(hash.values, new.values)
new.map = olua.clone(hash.map, new.map)
return new
end
function hash:get(key)
return hash.map[key]
end
function hash:has(key)
return hash.map[key] ~= nil
end
function hash:push(key, value, message)
if hash.map[key] then
error(string.format('key conflict: %s %s', key, message or ''))
end
assert(value ~= nil, 'no value')
hash.map[key] = value
if not map_only then
hash.values[#hash.values + 1] = value
end
end
function hash:push_if_not_exist(key, value)
if not hash:has(key) then
hash:push(key, value)
end
end
function hash:insert(where, curr, key, value, idx)
assert(not map_only, 'insert not allowed for map only')
if where == 'front' then
idx = 1
elseif where == 'after' then
for i, v in ipairs(hash.values) do
if v == curr then
idx = i + 1
break
end
end
elseif where == 'before' then
for i, v in ipairs(hash.values) do
if v == curr then
idx = i
break
end
end
elseif where == 'back' then
idx = #hash.values + 1
end
if idx then
table.insert(hash.values, idx, value)
hash.map[key] = value
else
error(string.format("can't insert value: %s, because current value not found", key))
end
end
function hash:replace(key, value)
local old = hash.map[key]
hash.map[key] = value
if not map_only then
assert(value ~= nil, "value is nil")
if old then
for i, v in ipairs(hash.values) do
if v == old then
hash.values[i] = value
break
end
end
else
hash.values[#hash.values + 1] = value
end
end
end
function hash:take(key)
local value = hash.map[key]
hash.map[key] = nil
if value and not map_only then
for i, v in ipairs(hash.values) do
if value == v then
table.remove(hash.values, i)
break
end
end
end
return value
end
function hash:__len()
assert(not map_only, '__leng not allowed for map only')
return #hash.values
end
function hash:__index(key)
error('use get')
end
function hash:__newindex(key, value)
error('use push')
end
function hash:__pairs()
return pairs(hash.map)
end
function hash:__ipairs()
assert(not map_only, 'ipairs not allowed for map only')
return ipairs(hash.values)
end
return setmetatable(hash, hash)
end