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