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
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file option.lua
--
-- define module: option
local option = {}
-- load modules
local cli = require("base/cli")
local table = require("base/table")
local tty = require("base/tty")
local colors = require("base/colors")
local text = require("base/text")
-- translate the menu
function option._translate(menu)
-- translate the menus if exists function
local submenus_all = {}
for k, submenu in pairs(menu) do
if type(submenu) == "function" then
local ok, _submenus_or_errors = pcall(submenu)
if ok and _submenus_or_errors then
for k, m in pairs(_submenus_or_errors) do
submenus_all[k] = m
end
else
return false, (_submenus_or_errors or "translate option menu failed!")
end
else
submenus_all[k] = submenu
end
end
table.copy2(menu, submenus_all)
-- save menu
option._MENU = menu
return true
end
function option._modenarrow()
-- show menu in narrow mode?
local width = os.getwinsize().width
return width > 0 and width < 60
end
-- get the top context
function option._context()
local contexts = option._CONTEXTS
if contexts then
return contexts[#contexts]
end
end
-- save context
function option.save(taskname)
option._CONTEXTS = option._CONTEXTS or {}
local context = {options = {}, defaults = {}, taskname = taskname}
if taskname then
context.defaults = option.defaults(taskname) or context.defaults
end
table.insert(option._CONTEXTS, context)
return context
end
-- restore context
function option.restore()
if option._CONTEXTS then
table.remove(option._CONTEXTS)
end
end
-- the command line
function option.cmdline()
if not xmake._ARGS then
xmake._ARGS = os.args(xmake._ARGV)
end
return "xmake " .. xmake._ARGS
end
-- init the option
function option.init(menu)
-- check
assert(menu)
-- translate menu
local ok, errors = option._translate(menu)
if not ok then
return false, errors
end
-- the main menu
local main = option.taskmenu("main")
assert(main)
-- new top context
local context = option.save()
assert(context)
-- check command
if xmake._COMMAND then
-- find the current task and save it
for taskname, taskinfo in pairs(main.tasks) do
if taskname == xmake._COMMAND or taskinfo.shortname == xmake._COMMAND then
context.taskname = taskname
break
end
end
-- not found?
if not context.taskname or not menu[context.taskname] then
option.show_main()
return false, "invalid task: " .. xmake._COMMAND
end
end
local options = table.wrap(option.taskmenu().options)
-- parse remain parts
local results, err = option.parse(xmake._COMMAND_ARGV, options, { populate_defaults = false })
if not results then
option.show_menu(context.taskname)
return false, err
end
-- finish parsing
context.options = results
-- init the default value
option.populate_defaults(options, context.defaults)
-- ok
return true
end
-- parse arguments with the given options
function option.parse(argv, options, opt)
-- check
assert(argv and options)
opt = opt or { populate_defaults = true }
-- parse arguments
local results = {}
local flags = {}
for _, o in ipairs(options) do
-- get mode and name
local mode = o[3]
local name = o[2]
assert(o and ((mode ~= "v" and mode ~= "vs") or name))
-- fill short flags
if o[3] == "k" and o[1] then
table.insert(flags, o[1])
end
end
-- run parser
local pargs = cli.parsev(argv, flags)
-- save parse results
for i, arg in ipairs(pargs) do
if arg.type == "option" or arg.type == "flag" then
-- find option or flag
local name_idx = arg.short and 1 or 2
local match_opt = nil
for _, o in pairs(options) do
local name = o[name_idx]
if name == arg.key then
match_opt = o
break
end
end
-- save option
if match_opt and ((arg.type == "option" and match_opt[3] ~= "k") or (arg.type == "flag" and match_opt[3] == "k")) then
results[match_opt[2] or match_opt[1]] = option.boolean(arg.value)
else
if opt.allow_unknown then
results[arg.key] = option.boolean(arg.value)
else
return nil, string.format("Invalid %s: %s", arg.type, arg)
end
end
elseif arg.type == "arg" then
-- find a value option with name
local match_opt = nil
for _, o in ipairs(options) do
-- get mode and name
local mode = o[3]
local name = o[2]
-- is value and with name?
if mode == "v" and name and not results[name] then
match_opt = o
break
-- is values and with name?
elseif mode == "vs" and name then
match_opt = o
break
end
end
-- ok? save this value with name opt[2]
if match_opt then
-- get mode and name
local mode = match_opt[3]
local name = match_opt[2]
-- save value
if mode == "v" then
results[name] = arg.value
elseif mode == "vs" then
-- the option
local o = results[name]
if not o then
results[name] = {}
o = results[name]
end
-- append value
table.insert(o, arg.value)
end
else
-- failed
if opt.allow_unknown then
if arg.key then
results[arg.key] = arg.value
else
-- the option
local o = results["$ARGS"]
if not o then
results["$ARGS"] = {}
o = results["$ARGS"]
end
-- append value
table.insert(o, arg.value)
end
else
return nil, "invalid argument: " .. arg.value
end
end
end
end
-- init the default value
if opt.populate_defaults then
option.populate_defaults(options, results)
end
-- ok
return results
end
-- fill defined with option's default value, in place
function option.populate_defaults(options, defined)
-- check
assert(options and defined)
-- populate the default value
for _, o in ipairs(options) do
-- the long name
local longname = o[2]
-- key=value?
if o[3] == "kv" then
local shortname = o[1]
-- the key
local key = longname or shortname
assert(key)
-- move value to key if needed
if shortname and defined[shortname] ~= nil then
defined[key], defined[shortname] = defined[shortname], nil
end
-- save the default value
if defined[key] == nil then
defined[key] = o[4]
end
-- value with name?
elseif o[3] == "v" and longname then
-- save the default value
if defined[longname] == nil then
defined[longname] = o[4]
end
end
end
end
-- get the current task name
function option.taskname()
return option._context().taskname
end
-- get the task menu
function option.taskmenu(task)
-- check
assert(option._MENU)
-- the current task
task = task or option.taskname() or "main"
-- get the task menu
local taskmenu = option._MENU[task]
if type(taskmenu) == "function" then
-- load this task menu
taskmenu = taskmenu()
-- save this task menu
option._MENU[task] = taskmenu
end
-- get it
return taskmenu
end
-- get the given option value for the current task
function option.get(name)
-- check
assert(name)
-- the options
local options = option.options()
if options then
local value = options[name]
if value == nil then
value = option.default(name)
end
return value
end
end
-- set the given option for the current task
function option.set(name, value)
-- check
assert(name)
-- cannot be the first context for menu
assert(#option._CONTEXTS > 1)
-- the options
local options = option.options()
assert(options)
-- set it
options[name] = value
end
-- get the boolean value
function option.boolean(value)
if type(value) == "string" then
local v = value:lower()
if v == "true" or v == "yes" or v == "y" then value = true
elseif v == "false" or v == "no" or v == "n" then value = false
end
end
return value
end
-- get the given default option value for the current task
function option.default(name)
assert(name)
-- the defaults
local defaults = option.defaults()
assert(defaults)
-- get it
return defaults[name]
end
-- get the current options
function option.options()
-- get it
local context = option._context()
if context then
return context.options
end
end
-- get all default options for the current or given task
function option.defaults(task)
-- get the default options for the current task
if task == nil then
return option._context().defaults
end
-- the task menu
local taskmenu = option.taskmenu(task)
-- get the default options for the given task
local defaults = {}
if taskmenu then
option.populate_defaults(taskmenu.options, defaults)
end
return defaults
end
-- show update tips
function option.show_update_tips()
-- show latest version
local versionfile = path.join(os.tmpdir(), "latest_version")
if os.isfile(versionfile) then
local versioninfo = io.load(versionfile)
if versioninfo and versioninfo.version and semver.compare(versioninfo.version, xmake._VERSION_SHORT) > 0 then
local updatetips = nil
if os.host() == "windows" then
updatetips = string.format([[
==========================================================================
| ${bright yellow}A new version of xmake is available!${clear} |
| |
| To update to the latest version ${bright}%s${clear}, run "xmake update". |
==========================================================================
]], versioninfo.version)
else
updatetips = string.format([[
╔════════════════════════════════════════════════════════════════════════════╗
║ ${bright yellow}A new version of xmake is available!${clear} ║
║ ║
║ To update to the latest version ${bright}%s${clear}, run "xmake update". ║
╚════════════════════════════════════════════════════════════════════════════╝
]], versioninfo.version)
end
io.print(colors.translate(updatetips))
end
end
end
-- show logo
function option.show_logo(logo, opt)
-- define logo
logo = logo or [[
_
__ ___ __ __ __ _| | ______
\ \/ / | \/ |/ _ | |/ / __ \
> < | \__/ | /_| | < ___/
/_/\_\_|_| |_|\__ \|_|\_\____|
by ruki, xmake.io
]]
-- make rainbow for logo
opt = opt or {}
if tty.has_color24() or tty.has_color256() then
local lines = {}
local seed = opt.seed or 236
for _, line in ipairs(logo:split("\n")) do
local i = 0
local line2 = ""
line:gsub(".", function (c)
local code = tty.has_color24() and colors.rainbow24(i, seed) or colors.rainbow256(i, seed)
line2 = string.format("%s${bright %s}%s", line2, code, c)
i = i + 1
end)
table.insert(lines, line2)
end
logo = table.concat(lines, "\n")
end
-- show logo
io.print(colors.translate(logo))
-- define footer
local footer = [[
${point_right} ${bright}Manual${clear}: ${underline}https://xmake.io/#/getting_started${clear}
${pray} ${bright}Donate${clear}: ${underline}https://xmake.io/#/sponsor${clear}
]]
-- show footer
io.print(colors.translate(footer))
-- show update tips
option.show_update_tips()
end
-- show the menu
function option.show_menu(task)
-- no task? print main menu
if not task then
option.show_main()
return
end
-- the menu
local menu = option._MENU
assert(menu)
-- the task menu
local taskmenu = option.taskmenu(task)
assert(taskmenu)
-- print title
if menu.title then
io.print(colors.translate(menu.title))
end
-- print copyright
if menu.copyright then
io.print(colors.translate(menu.copyright))
end
-- show logo
option.show_logo()
-- print usage
if taskmenu.usage then
io.print("")
io.print(colors.translate("${bright}Usage: $${default color.menu.usage}" .. taskmenu.usage .. "${clear}"))
end
-- print description
if taskmenu.description then
io.print("")
io.print(colors.translate(taskmenu.description))
end
-- print options
if taskmenu.options then
option.show_options(taskmenu.options, task)
end
end
-- show the main menu
function option.show_main()
-- the menu
local menu = option._MENU
assert(menu)
-- the main menu
local main = option.taskmenu("main")
assert(main)
-- print title
if menu.title then
io.print(colors.translate(menu.title))
end
-- print copyright
if menu.copyright then
io.print(colors.translate(menu.copyright))
end
-- show logo
option.show_logo()
-- print usage
if main.usage then
io.print("")
io.print(colors.translate("${bright}Usage: $${default color.menu.usage}" .. main.usage .. "${clear}"))
end
-- print description
if main.description then
io.print("")
io.print(colors.translate(main.description))
end
-- print tasks
if main.tasks then
local narrow = option._modenarrow()
-- make task categories
local categories = {}
for taskname, taskinfo in pairs(main.tasks) do
-- the category name
local categoryname = taskinfo.category or "task"
if categoryname == "main" then
categoryname = "action"
end
-- the category task
local category = categories[categoryname] or { name = categoryname, tasks = {} }
categories[categoryname] = category
-- add task to the category
category.tasks[taskname] = taskinfo
end
-- sort categories
categories = table.values(categories)
table.sort(categories, function (a, b)
if a.name == "action" then
return true
end
return a.name < b.name
end)
-- dump tasks by categories
local tablecontent = {}
for _, category in ipairs(categories) do
-- the category name and task
assert(category.name and category.tasks)
-- print category name
table.insert(tablecontent, {})
table.insert(tablecontent, {{string.format("%s%ss: ", string.sub(category.name, 1, 1):upper(), string.sub(category.name, 2)), style="${reset bright}"}})
-- print tasks
for taskname, taskinfo in table.orderpairs(category.tasks) do
-- init the task line
local taskline = string.format(narrow and " %s%s" or " %s%s",
taskinfo.shortname and (taskinfo.shortname .. ", ") or " ",
taskname)
table.insert(tablecontent, {taskline, colors.translate(taskinfo.description or "")})
end
end
-- set table styles
tablecontent.style = {"${color.menu.main.task.name}"}
tablecontent.width = {nil, "auto"}
tablecontent.sep = narrow and " " or " "
-- print table
io.write(text.table(tablecontent))
end
-- print options
if main.options then
option.show_options(main.options, "build")
end
end
-- show the options menu
function option.show_options(options, taskname)
assert(options)
-- remove repeat empty lines
local is_action = false
local emptyline_count = 0
local printed_options = {}
for _, opt in ipairs(options) do
if opt.category and printed_options[#printed_options].category then
table.remove(printed_options)
end
table.insert(printed_options, opt)
if opt.category and opt.category == "action" then
is_action = true
end
end
if printed_options[#printed_options].category then
table.remove(printed_options)
end
-- narrow mode?
local narrow = option._modenarrow()
-- print header
local tablecontent = {}
table.insert(tablecontent, {})
if is_action then
table.insert(tablecontent, {{"Common options:", style="${reset bright}"}})
else
table.insert(tablecontent, {{"Options:", style="${reset bright}"}})
end
-- print options
local categories = {}
for _, opt in ipairs(printed_options) do
if opt.category and opt.category == "action" then
-- the following options are belong action? show command section
--
-- @see core/base/task.lua: translate menu
--
table.insert(tablecontent, {})
table.insert(tablecontent, {{"Command options (" .. taskname .. "):", style="${reset bright}"}})
elseif opt.category and opt.category ~= "." then
local category_root = opt.category:split("/")[1]
if not categories[category_root] then
table.insert(tablecontent, {})
table.insert(tablecontent, {{"Command options (" .. opt.category .. "):", style="${reset bright}"}})
categories[category_root] = true
end
elseif opt[3] == nil then
table.insert(tablecontent, {})
else
-- append the shortname
local shortname = opt[1]
local name = opt[2]
local mode = opt[3]
local default = opt[4]
local title1, title2
local kvplaceholder = mode == "kv" and ((type(default) == "boolean") and "[y|n]" or(name and name:upper() or "XXX"))
if shortname then
if mode == "kv" then
title1 = "-" .. shortname .. " " .. kvplaceholder
else
title1 = "-" .. shortname
end
end
-- append the name
if name then
local leading = mode:startswith("k") and "--" or " "
local kv
if mode == "k" then
kv = name
elseif mode == "kv" then
kv = name .. "=" .. kvplaceholder
elseif mode == "vs" then
kv = name .. " ..."
else
kv = name
end
title2 = leading .. kv
elseif mode == "v" or mode == "vs" then
title2 = " ..."
end
-- get description
local optdespn = table.maxn(opt)
local description = table.move(opt, 5, optdespn, 1, table.new(optdespn - 5 + 1, 0))
if #description == 0 then
description[1] = ""
end
-- transform description
local desp_strs = table.new(#description, 0)
for _, v in ipairs(description) do
if type(v) == "function" then
v = v()
end
if type(v) == "string" then
table.insert(desp_strs, colors.translate(v))
elseif type(v) == "table" then
table.move(v, 1, #v, #desp_strs + 1, desp_strs)
end
end
-- append the default value
if default then
local defaultval = tostring(default)
if type(default) == "boolean" then
defaultval = default and "y" or "n"
end
local def_desp = colors.translate(string.format(" (default: ${bright}%s${clear})", defaultval))
desp_strs[1] = desp_strs[1] .. def_desp
end
-- append values
local values = opt.values
if type(values) == "function" then
values = values(false, {helpmenu = true})
end
if values then
for _, value in ipairs(table.wrap(values)) do
table.insert(desp_strs, " - " .. tostring(value))
end
end
-- insert row
if narrow then
if title1 then
table.insert(tablecontent, {{" " .. title1, " " .. title2 }, desp_strs})
else
table.insert(tablecontent, {{" " .. title2 }, desp_strs})
end
else
if title1 then
table.insert(tablecontent, {" " .. title1 .. ", " .. title2 , desp_strs})
else
table.insert(tablecontent, {" " .. title2 , desp_strs})
end
end
end
end
-- set table styles
tablecontent.style = {"${color.menu.option.name}"}
tablecontent.width = {nil, "auto"}
tablecontent.sep = narrow and " " or " "
-- print table
io.write(text.table(tablecontent))
end
-- return module: option
return option
|