extract new module toolcontext from mmloader
[nit.git] / src / toolcontext.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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>
6 #
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18
19 # Common command-line tool infractructure than handle options and error messages
20 package toolcontext
21
22 import opts
23 import location
24
25 class Message
26 super Comparable
27 redef type OTHER: Message
28
29 readable var _location: nullable Location
30 readable var _text: String
31
32 redef fun <(other: OTHER): Bool do
33 if location == null then return true
34 if other.location == null then return false
35
36 return location.as(not null) < other.location.as(not null)
37 end
38
39 redef fun to_s: String
40 do
41 var l = location
42 if l == null then
43 return text
44 else
45 return "{l}: {text}"
46 end
47 end
48
49 fun to_color_string: String
50 do
51 var esc = 27.ascii
52 var red = "{esc}[0;31m"
53 var bred = "{esc}[1;31m"
54 var green = "{esc}[0;32m"
55 var yellow = "{esc}[0;33m"
56 var def = "{esc}[0m"
57
58 var l = location
59 if l == null then
60 return text
61 else if l.file == null then
62 return "{yellow}{l}{def}: {text}"
63 else
64 return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}"
65 end
66 end
67 end
68
69 # Global context for tools
70 class ToolContext
71 # Number of errors
72 readable var _error_count: Int = 0
73
74 # Number of warnings
75 readable var _warning_count: Int = 0
76
77 # Directory where to generate log files
78 readable var _log_directory: String = "logs"
79
80 # Messages
81 var _messages: Array[Message] = new Array[Message]
82 var _message_sorter: ComparableSorter[Message] = new ComparableSorter[Message]
83
84 fun check_errors
85 do
86 if _messages.length > 0 then
87 _message_sorter.sort(_messages)
88
89 for m in _messages do
90 if opt_no_color.value then
91 stderr.write("{m}\n")
92 else
93 stderr.write("{m.to_color_string}\n")
94 end
95 end
96
97 _messages.clear
98 end
99
100 if error_count > 0 then exit(1)
101 end
102
103 # Display an error
104 fun error(l: nullable Location, s: String)
105 do
106 _messages.add(new Message(l,s))
107 _error_count = _error_count + 1
108 if opt_stop_on_first_error.value then check_errors
109 end
110
111 # Add an error, show errors and quit
112 fun fatal_error(l: nullable Location, s: String)
113 do
114 error(l,s)
115 check_errors
116 end
117
118 # Display a warning
119 fun warning(l: nullable Location, s: String)
120 do
121 if _opt_warn.value == 0 then return
122 _messages.add(new Message(l,s))
123 _warning_count = _warning_count + 1
124 if opt_stop_on_first_error.value then check_errors
125 end
126
127 # Display an info
128 fun info(s: String, level: Int)
129 do
130 if level <= verbose_level then
131 print "{s}"
132 end
133 end
134
135 # Global OptionContext
136 readable var _option_context: OptionContext = new OptionContext
137
138 # Option --warn
139 readable var _opt_warn: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
140
141 # Option --quiet
142 readable var _opt_quiet: OptionBool = new OptionBool("Do not show warnings", "-q", "--quiet")
143
144 # Option --log
145 readable var _opt_log: OptionBool = new OptionBool("Generate various log files", "--log")
146
147 # Option --log-dir
148 readable var _opt_log_dir: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
149
150 # Option --help
151 readable var _opt_help: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
152
153 # Option --version
154 readable var _opt_version: OptionBool = new OptionBool("Show version and exit", "--version")
155
156 # Option --verbose
157 readable var _opt_verbose: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
158
159 # Option --stop-on-first-error
160 readable var _opt_stop_on_first_error: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
161
162 # Option --no-color
163 readable var _opt_no_color: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
164
165 # Verbose level
166 readable var _verbose_level: Int = 0
167
168 init
169 do
170 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)
171 end
172
173 # Parse and process the options given on the command line
174 fun process_options
175 do
176 self.opt_warn.value = 1
177
178 # init options
179 option_context.parse(args)
180
181 # Set verbose level
182 _verbose_level = opt_verbose.value
183
184 if self.opt_quiet.value then self.opt_warn.value = 0
185
186 if opt_log_dir.value != null then _log_directory = opt_log_dir.value.as(not null)
187 if _opt_log.value then
188 # Make sure the output directory exists
189 log_directory.mkdir
190 end
191 end
192 end