Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/bee_console.rb | 286 | 245 | 97.55%
|
97.14%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 # Copyright 2006-2012 Michel Casabianca <michel.casabianca@gmail.com> |
2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at |
6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # |
9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, |
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 # See the License for the specific language governing permissions and |
13 # limitations under the License. |
14 |
15 require 'rubygems' |
16 require 'bee_build' |
17 require 'bee_util' |
18 require 'bee_listener' |
19 require 'bee_console_formatter' |
20 require 'getoptlong' |
21 require 'syck' |
22 require 'yaml' |
23 |
24 module Bee |
25 |
26 module Console |
27 |
28 # Command line help. |
29 HELP = <<'EOF' |
30 Usage: bee [options] [targets] |
31 -V Print version and exit. |
32 -h Print help about usage and exit. |
33 -b Print help about build and exit. |
34 -n Don't actually run any commands; just print them. |
35 -k task Print help about tasks in a package (writing "foo.?") or a |
36 given one (writing "foo.bar") and exit. |
37 -e egg Print help about templates in a given package (writing |
38 "foo.?") or a given one (writing "foo.bar") and exit. |
39 -p name=value Set a named property with a given value. |
40 -t egg Run a given egg to generate a template project. |
41 -v Enable verbose mode. |
42 -s style Define style for output (see documentation). |
43 -c Use color scheme for output (if running in color terminal). |
44 -w Use black and white output (default). |
45 -f file Build file to run (defaults to "build.yml"). |
46 -r Look for build file recursively up in file system. |
47 -l Print bee logo on console. |
48 -R resource Print given resource (such as ':bee:clean.yml') on console. |
49 -a Print list of available targets. |
50 -o Print list of available options. |
51 -x Print list of available tasks. |
52 -y Print list of available templates. |
53 targets Targets to run (default target if omitted). |
54 EOF |
55 # Options descriptions. |
56 OPTIONS = [ |
57 ['--version', '-V', GetoptLong::NO_ARGUMENT], |
58 ['--help', '-h', GetoptLong::NO_ARGUMENT], |
59 ['--help-build', '-b', GetoptLong::NO_ARGUMENT], |
60 ['--help-task','-k', GetoptLong::REQUIRED_ARGUMENT], |
61 ['--help-template','-e', GetoptLong::REQUIRED_ARGUMENT], |
62 ['--dry-run', '-n', GetoptLong::NO_ARGUMENT], |
63 ['--property', '-p', GetoptLong::REQUIRED_ARGUMENT], |
64 ['--template', '-t', GetoptLong::REQUIRED_ARGUMENT], |
65 ['--verbose', '-v', GetoptLong::NO_ARGUMENT], |
66 ['--style', '-s', GetoptLong::REQUIRED_ARGUMENT], |
67 ['--color', '-c', GetoptLong::NO_ARGUMENT], |
68 ['--black-and-white', '-w', GetoptLong::NO_ARGUMENT], |
69 ['--file', '-f', GetoptLong::REQUIRED_ARGUMENT], |
70 ['--recursive', '-r', GetoptLong::NO_ARGUMENT], |
71 ['--logo', '-l', GetoptLong::NO_ARGUMENT], |
72 ['--resource', '-R', GetoptLong::REQUIRED_ARGUMENT], |
73 ['--targets', '-a', GetoptLong::NO_ARGUMENT], |
74 ['--options', '-o', GetoptLong::NO_ARGUMENT], |
75 ['--tasks', '-x', GetoptLong::NO_ARGUMENT], |
76 ['--templates', '-y', GetoptLong::NO_ARGUMENT], |
77 ] |
78 # Name for default build file. |
79 DEFAULT_BUILD_FILE = 'build.yml' |
80 # Exit value on error parsing command line |
81 EXIT_PARSING_CMDLINE = 1 |
82 # Exit value on build error |
83 EXIT_BUILD_ERROR = 2 |
84 # Exit value on unknown error |
85 EXIT_UNKNOWN_ERROR = 3 |
86 # Exit value on user interruption |
87 EXIT_INTERRUPT_ERROR = 4 |
88 # Bee options environment variable. |
89 BEE_OPT_ENV = 'BEEOPT' |
90 # Bee text logo (generated with http://www.network-science.de/ascii/) |
91 BEE_LOGO = <<"EOF" |
92 _ |
93 | |__ ___ ___ |
94 ____ | '_ \\ / _ \\/ _ \\ _____ _____ _____ _____ _____ _____ _____ _____ _____ |
95 |____| | |_) | __/ __/ |_____|_____|_____|_____|_____|_____|_____|_____|_____| |
96 |_.__/ \\___|\\___| #{Bee.version.ljust(8)} http://bee.rubyforge.org |
97 |
98 EOF |
99 |
100 # Parse command line and return parsed arguments. |
101 # - arguments: command line arguments. |
102 def self.parse_command_line(arguments) |
103 arguments = arguments.clone |
104 version = false |
105 help = false |
106 help_build = false |
107 help_task = false |
108 help_template = false |
109 properties = {} |
110 task = nil |
111 dry_run = false |
112 template = nil |
113 verbose = false |
114 style = nil |
115 color = false |
116 file = DEFAULT_BUILD_FILE |
117 recursive = false |
118 logo = false |
119 resource = nil |
120 print_targets = false |
121 print_options = false |
122 print_tasks = false |
123 print_templates = false |
124 targets = [] |
125 # read options in BEEOPT environment variable |
126 options = ENV[BEE_OPT_ENV] |
127 options.split(' ').reverse.each { |option| arguments.unshift(option) } if |
128 options |
129 # parse command line arguments |
130 old_argv = ARGV |
131 ARGV.replace(arguments) |
132 opts = GetoptLong.new(*OPTIONS) |
133 opts.each do |opt, arg| |
134 case opt |
135 when '--version' |
136 version = true |
137 when '--help' |
138 help = true |
139 when '--help-build' |
140 help_build = true |
141 when '--help-task' |
142 help_task = true |
143 task = arg |
144 when '--help-template' |
145 help_template = true |
146 template = arg |
147 when '--dry-run' |
148 dry_run = true |
149 verbose = true |
150 when '--property' |
151 name, value = parse_property(arg) |
152 properties[name] = value |
153 when '--template' |
154 template = arg |
155 when '--verbose' |
156 verbose = true |
157 when '--style' |
158 style = arg |
159 when '--color' |
160 color = true |
161 when '--black-and-white' |
162 color = false |
163 when '--file' |
164 file = arg |
165 when '--recursive' |
166 recursive = true |
167 when '--logo' |
168 logo = true |
169 when '--resource' |
170 resource = arg |
171 when '--targets' |
172 print_targets = true |
173 when '--options' |
174 print_options = true |
175 when '--tasks' |
176 print_tasks = true |
177 when '--templates' |
178 print_templates = true |
179 end |
180 end |
181 targets = Array.new(ARGV) |
182 ARGV.replace(old_argv) |
183 return version, help, help_build, help_task, task, help_template, |
184 template, properties, dry_run, verbose, style, color, file, |
185 recursive, logo, resource, print_targets, print_options, |
186 print_tasks, print_templates, targets |
187 end |
188 |
189 # Parse a command line property. |
190 # - property: property definition as "name=value". |
191 # Return: name and value of the property. |
192 def self.parse_property(property) |
193 begin |
194 index = property.index('=') |
195 raise "No = sign (should be 'name=value')" if not index |
196 name = property[0..index-1] |
197 value = YAML::load(property[index+1..-1]) |
198 return name, value |
199 rescue |
200 raise "Error parsing property '#{property}': #{$!}" |
201 end |
202 end |
203 |
204 # Start build from command line. |
205 # - arguments: command line arguments. |
206 def self.start_command_line(arguments) |
207 STDOUT.sync = true |
208 begin |
209 version, help, help_build, help_task, task, help_template, |
210 template, properties, dry_run, verbose, style, color, file, |
211 recursive, logo, resource, print_targets, print_options, |
212 print_tasks, print_templates, targets = parse_command_line(arguments) |
213 rescue |
214 puts "ERROR: parsing command line: #{$!}" |
215 exit(EXIT_PARSING_CMDLINE) |
216 end |
217 begin |
218 formatter = Formatter.new(style, color, verbose) |
219 rescue |
220 puts "ERROR: bad format string '#{style}'" |
221 exit(EXIT_PARSING_CMDLINE) |
222 end |
223 begin |
224 puts BEE_LOGO if logo |
225 if version |
226 puts Bee.version |
227 elsif help |
228 puts HELP |
229 elsif help_build |
230 build = Build.load(file, recursive, properties) |
231 puts formatter.help_build(build) |
232 elsif help_task |
233 puts formatter.help_task(task) |
234 elsif help_template |
235 puts formatter.help_template(template) |
236 elsif template |
237 file = Bee::Util::find_template(template) |
238 listener = Bee::Listener.new(formatter) |
239 build = Build.load(file, false, properties) |
240 build.run(targets, listener, dry_run) |
241 elsif resource |
242 raise Bee::Util::BuildError.new("'#{resource}' is not a valid resource") if |
243 !Util::resource?(resource) |
244 begin |
245 puts File.read(Bee::Util::resource_path(resource)) |
246 rescue Exception |
247 raise Bee::Util::BuildError.new("Resource '#{resource}' not found") |
248 end |
249 elsif print_targets |
250 begin |
251 build = Build.load(file) |
252 targets = build.targets.hash.keys |
253 targets += build.targets.alias.keys if build.targets.alias |
254 rescue Exception |
255 targets = [] |
256 end |
257 print targets.sort.join(' ') |
258 elsif print_options |
259 print OPTIONS.map {|o| o[0]}.sort.join(' ') |
260 elsif print_tasks |
261 print Bee::Task::PackageManager.list_tasks.join(' ') |
262 elsif print_templates |
263 print Bee::Task::PackageManager.list_templates.join(' ') |
264 else |
265 listener = Listener.new(formatter) |
266 build = Build.load(file, recursive, properties) |
267 build.run(targets, listener, dry_run) |
268 puts formatter.format_success('OK') |
269 end |
270 rescue Bee::Util::BuildError |
271 puts formatter.format_error_message($!) |
272 exit(EXIT_BUILD_ERROR) |
273 rescue Interrupt |
274 puts "\n#{formatter.format_error('ERROR')}: Build was interrupted!" |
275 puts $!.backtrace.join("\n") if verbose |
276 exit(EXIT_INTERRUPT_ERROR) |
277 rescue Exception |
278 puts "#{formatter.format_error('ERROR')}: #{$!}" |
279 puts $!.backtrace.join("\n") |
280 exit(EXIT_UNKNOWN_ERROR) |
281 end |
282 end |
283 |
284 end |
285 |
286 end |
Generated on Fri Oct 09 02:07:48 +0200 2015 with rcov 1.0.0