Repository navigation
Expand file tree
/
Copy pathcli.tl
More file actions
176 lines (148 loc) · 5.34 KB
/
Copy pathcli.tl
File metadata and controls
176 lines (148 loc) · 5.34 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
---@brief
--- The command line driver
---
--- This is not a module to be used and requiring it will attempt to start the cli and call os.exit
local argparse <const> = require("argparse")
local tl <const> = require("tl")
local command <const> = require("cyan.command")
local common <const> = require("cyan.tlcommon")
local config <const> = require("cyan.config")
local fs <const> = require("cyan.fs")
local log <const> = require("cyan.log")
local script <const> = require("cyan.script")
local util <const> = require("cyan.util")
local keys <const>, from <const>, sort <const>, ivalues <const>
= util.tab.keys, util.tab.from, util.tab.sort_in_place, util.tab.ivalues
local parser <const> = argparse("cyan", "The Teal build system")
parser:add_help(false)
parser:option("-l --preload", "Execute the equivalent of require('modulename') before processing Teal files.")
:argname("<modulename>")
:count("*")
parser:option("--global-env-def", "Load <module-name> before typechecking. Use this to define types provided by your environment.")
:argname("<module-name>")
:count("?")
parser:option("-I --include-dir", "Prepend this directory to the module search path.")
:argname("<directory>")
:count("*")
local warnings <const>: {string} = sort(from(keys(tl.warning_kinds))) as {string}
table.insert(warnings, "all")
parser:option("--wdisable", "Disable the given kind of warning. Use '--wdisable all' to disable all warnings")
:argname("<warning>")
:choices(warnings)
:count("*")
parser:option("--werror", "Promote the given kind of warning to an error. Use '--werror all' to promote all warnings to errors")
:argname("<warning>")
:choices(warnings)
:count("*")
parser:option("--gen-compat", "Generate compatibility code for targeting different Lua VM versions.")
:choices({ "off", "optional", "required" })
:default("optional")
:defmode("a")
parser:option("--gen-target", "Minimum targeted Lua version for generated code.")
:choices({ "5.1", "5.3", "5.4" })
parser:flag("--no-script", "Do not run any scripts.")
:action(script.disable)
parser:mutex(
parser:flag("-q --quiet", "Do not print information messages to stdout. Errors may still be printed to stderr. (Same as --verbosity quiet)."),
parser:option("-v --verbosity", "Set verbosity of logging.")
:choices(log.verbosities)
:action(function(_: argparse.Args, __: string, val: string|boolean|{string})
log.set_verbosity(val as log.Verbosity)
end)
)
parser:option("-s --source-dir", "Override the source directory.")
parser:option("-b --build-dir", "Override the build directory.")
parser:command_target("command")
command.new{
name = "help",
description = [[Show this message and exit]],
exec = function(): integer
log.info(parser:get_help())
return 0
end,
}
parser:flag("-h --help", "Show this help message and exit")
:action(function()
os.exit(command.get("help").exec())
end)
command.new{
name = "version",
description = [[Print version information and exit]],
exec = function(): integer
log.info(
"Cyan version: ", require("cyan.meta").version, "\n",
"Teal version: ", tl.version(), "\n",
" Lua version: ", _VERSION
)
return 0
end
}
require("cyan.commands.initialize")
require("cyan.commands.check-gen")
require("cyan.commands.run")
require("cyan.commands.build")
require("cyan.commands.warnings")
command.register_all(parser)
local type Args = command.Args
local args: Args
do
local ok <const>, res <const> = parser:pparse()
if not ok then
log.err(res)
log.info(parser:get_usage())
os.exit(1)
end
args = res as Args
end
local cmd <const> = assert(command.get(args.command))
command.running = cmd
log.debug("Arguments: ", args)
local exit: integer = 1
local starting_dir <const> = fs.cwd()
local config_path <const> = config.find()
if config_path then
local config_dir <const> = config_path:copy()
table.remove(config_dir)
log.debug("Changing directory into: ", config_dir)
fs.chdir(config_dir)
end
local loaded_config, config_errors <const>, config_warnings <const>
= config.load()
if common.report_config_errors(config_errors, config_warnings) then
os.exit(1)
end
if not loaded_config then
loaded_config = {}
end
command.merge_args_into_config(loaded_config, args)
if loaded_config.scripts then
for hook, filenames in pairs(loaded_config.scripts[command.running.name] or {}) do
if filenames is string then
filenames = { filenames }
end
for f in ivalues(filenames as {string}) do
log.debug("registering file '", f, "' for ", command.running.name, ":", hook)
script.register(f, command.running.name, hook)
end
end
end
do
local ok <const>, err <const> = script.ensure_loaded_for_command(command.running.name)
if not ok then
if err is tl.Result then
common.report_result(err, loaded_config)
else
log.err("Could not load script: ", err)
end
os.exit(1)
end
log.debug("loaded scripts for command: ", command.running.name)
end
local ok <const>, res <const> = xpcall(function()
exit = cmd.exec(args, loaded_config, starting_dir)
end, debug.traceback as function()) as (boolean, string)
if not ok then
log.err("Error executing command\n ", res)
os.exit(2)
end
os.exit(exit)