1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008-2012 Jean Privat <jean@pryen.org>
5 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # Common command-line tool infractructure than handle options and error messages
28 redef type OTHER: Message
30 var location
: nullable Location
33 # Comparisons are made on message locations.
34 redef fun <(other
: OTHER): Bool do
35 if location
== null then return true
36 if other
.location
== null then return false
38 return location
.as(not null) < other
.location
.as(not null)
41 redef fun to_s
: String
51 fun to_color_string
: String
54 var red
= "{esc}[0;31m"
55 var bred
= "{esc}[1;31m"
56 var green
= "{esc}[0;32m"
57 var yellow
= "{esc}[0;33m"
63 else if l
.file
== null then
64 return "{yellow}{l}{def}: {text}"
66 return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}"
71 # Global context for tools
74 var error_count
: Int = 0
77 var warning_count
: Int = 0
79 # Directory where to generate log files
80 var log_directory
: String = "logs"
83 private var messages
: Array[Message] = new Array[Message]
84 private var message_sorter
: ComparableSorter[Message] = new ComparableSorter[Message]
88 if messages
.length
> 0 then
89 message_sorter
.sort
(messages
)
92 if opt_no_color
.value
then
95 stderr
.write
("{m.to_color_string}\n")
102 if error_count
> 0 then exit
(1)
106 fun error
(l
: nullable Location, s
: String)
108 messages
.add
(new Message(l
,s
))
109 error_count
= error_count
+ 1
110 if opt_stop_on_first_error
.value
then check_errors
113 # Add an error, show errors and quit
114 fun fatal_error
(l
: nullable Location, s
: String)
121 fun warning
(l
: nullable Location, s
: String)
123 if opt_warn
.value
== 0 then return
124 messages
.add
(new Message(l
,s
))
125 warning_count
= warning_count
+ 1
126 if opt_stop_on_first_error
.value
then check_errors
130 fun info
(s
: String, level
: Int)
132 if level
<= verbose_level
then
137 fun exec_and_check
(args
: Array[String], error
: String)
139 var prog
= args
.first
142 # Is the wanted program available?
143 var proc_which
= new IProcess.from_a
("which", [prog
])
145 var res
= proc_which
.status
147 print
"{error}: executable \"{prog}\
" not found"
151 # Execute the wanted program
152 var proc
= new Process.from_a
(prog
, args
)
156 print
"{error}: execution of \"{prog} {args.join(" ")}\
" failed"
161 # Global OptionContext
162 var option_context
: OptionContext = new OptionContext
165 var opt_warn
: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
168 var opt_quiet
: OptionBool = new OptionBool("Do not show warnings", "-q", "--quiet")
171 var opt_log
: OptionBool = new OptionBool("Generate various log files", "--log")
174 var opt_log_dir
: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
177 var opt_help
: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
180 var opt_version
: OptionBool = new OptionBool("Show version and exit", "--version")
183 var opt_verbose
: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
185 # Option --stop-on-first-error
186 var opt_stop_on_first_error
: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
189 var opt_no_color
: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
192 var verbose_level
: Int = 0
196 option_context
.add_option
(opt_warn
, opt_quiet
, opt_stop_on_first_error
, opt_no_color
, opt_log
, opt_log_dir
, opt_help
, opt_version
, opt_verbose
)
199 # Name, usage and synopsis of the tool.
200 # It is mainly used in `usage`.
201 # Should be correctly set by the client before calling `process_options`
202 # A multi-line string is recommmended.
204 # eg. `"Usage: tool [OPTION]... [FILE]...\nDo some things."`
205 var tooldescription
: String writable = "Usage: [OPTION]... [ARG]..."
207 # Does `process_options` should accept an empty sequence of arguments.
208 # ie. nothing except options.
209 # Is `false` by default.
211 # If required, if should be set by the client before calling `process_options`
212 var accept_no_arguments
writable = false
214 # print the full usage of the tool.
215 # Is called by `process_option` on `--help`.
216 # It also could be called by the client.
219 print tooldescription
223 # Parse and process the options given on the command line
224 fun process_options
(args
: Sequence[String])
226 self.opt_warn
.value
= 1
229 option_context
.parse
(args
)
231 if opt_help
.value
then
236 if opt_version
.value
then
241 var errors
= option_context
.get_errors
242 if not errors
.is_empty
then
243 for e
in errors
do print
"Error: {e}"
244 print tooldescription
245 print
"Use --help for help"
249 if option_context
.rest
.is_empty
and not accept_no_arguments
then
250 print tooldescription
251 print
"Use --help for help"
256 verbose_level
= opt_verbose
.value
258 if self.opt_quiet
.value
then self.opt_warn
.value
= 0
260 if opt_log_dir
.value
!= null then log_directory
= opt_log_dir
.value
.as(not null)
261 if opt_log
.value
then
262 # Make sure the output directory exists
266 nit_dir
= compute_nit_dir
269 # The identified root directory of the Nit project
270 var nit_dir
: nullable String
272 private fun compute_nit_dir
: nullable String
274 # a environ variable has precedence
275 var res
= "NIT_DIR".environ
276 if not res
.is_empty
then return res
278 # find the runpath of the program from argv[0]
279 res
= "{sys.program_name.dirname}/.."
280 if res
.file_exists
and "{res}/src/nit.nit".file_exists
then return res
.simplify_path
282 # find the runpath of the process from /proc
283 var exe
= "/proc/self/exe"
284 if exe
.file_exists
then
286 res
= res
.dirname
.join_path
("..")
287 if res
.file_exists
and "{res}/src/nit.nit".file_exists
then return res
.simplify_path