C0 code coverage information

Generated on Wed Oct 07 08:33:55 -0700 2009 with rcov 0.8.2.1


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.
Name Total lines Lines of code Total coverage Code coverage
lib/buildr/core/application.rb 656 456
69.5%  
57.0%  
  1 # Licensed to the Apache Software Foundation (ASF) under one or more
  2 # contributor license agreements.  See the NOTICE file distributed with this
  3 # work for additional information regarding copyright ownership.  The ASF
  4 # licenses this file to you under the Apache License, Version 2.0 (the
  5 # "License"); you may not use this file except in compliance with the License.
  6 # You may obtain a copy of the License at
  7 #
  8 #    http://www.apache.org/licenses/LICENSE-2.0
  9 #
 10 # Unless required by applicable law or agreed to in writing, software
 11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 13 # License for the specific language governing permissions and limitations under
 14 # the License.
 15 
 16 
 17 # Portion of this file derived from Rake.
 18 # Copyright (c) 2003, 2004 Jim Weirich
 19 #
 20 # Permission is hereby granted, free of charge, to any person obtaining a copy
 21 # of this software and associated documentation files (the "Software"), to deal
 22 # in the Software without restriction, including without limitation the rights
 23 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 24 # copies of the Software, and to permit persons to whom the Software is
 25 # furnished to do so, subject to the following conditions:
 26 #
 27 # The above copyright notice and this permission notice shall be included in
 28 # all copies or substantial portions of the Software.
 29 #
 30 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 31 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 32 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 33 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 34 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 35 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 36 # SOFTWARE.
 37 
 38 
 39 require 'rake'
 40 require 'highline/import'
 41 require 'rubygems/source_info_cache'
 42 require 'buildr/core/util'
 43 Gem.autoload :SourceInfoCache, 'rubygems/source_info_cache'
 44 
 45 
 46 # Gem::user_home is nice, but ENV['HOME'] lets you override from the environment.
 47 ENV['HOME'] ||= File.expand_path(Gem::user_home)
 48 ENV['BUILDR_ENV'] ||= 'development'
 49 
 50 
 51 module Buildr
 52 
 53   # Provide settings that come from three sources.
 54   #
 55   # User settings are placed in the .buildr/settings.yaml file located in the user's home directory.
 56   # They should only be used for settings that are specific to the user and applied the same way
 57   # across all builds.  Example for user settings are preferred repositories, path to local repository,
 58   # user/name password for uploading to remote repository.
 59   #
 60   # Build settings are placed in the build.yaml file located in the build directory.  They help keep
 61   # the buildfile and build.yaml file simple and readable, working to the advantages of each one.
 62   # Example for build settings are gems, repositories and artifacts used by that build.
 63   #
 64   # Profile settings are placed in the profiles.yaml file located in the build directory.  They provide
 65   # settings that differ in each environment the build runs in.  For example, URLs and database
 66   # connections will be different when used in development, test and production environments.
 67   # The settings for the current environment are obtained by calling #profile.
 68   class Settings
 69 
 70     def initialize(application) #:nodoc:
 71       @application = application
 72     end
 73 
 74     # User settings loaded from setting.yaml file in user's home directory.
 75     def user
 76       @user ||= load_from('settings', @application.home_dir)
 77     end
 78 
 79     # Build settings loaded from build.yaml file in build directory.
 80     def build
 81       @build ||= load_from('build')
 82     end
 83 
 84     # Profiles loaded from profiles.yaml file in build directory.
 85     def profiles
 86       @profiles ||= load_from('profiles')
 87     end
 88 
 89     # :call-seq:
 90     #    profile => hash
 91     #
 92     # Returns the profile for the current environment.
 93     def profile
 94       profiles[@application.environment] ||= {}
 95     end
 96 
 97   private
 98 
 99     def load_from(name, path = nil)
100       unless path
101         fail "Internal error: attempting to access local setting before buildfile located" unless @application.rakefile
102         path = File.dirname(@application.rakefile)
103       end
104       file_name = ['yaml', 'yml'].map { |ext| File.join(path, "#{name}.#{ext}") }.find { |fn| File.exist?(fn) }
105       return {} unless file_name
106       yaml = YAML.load(File.read(file_name)) || {}
107       fail "Expecting #{file_name} to be a map (name: value)!" unless Hash === yaml
108       @application.buildfile.enhance [file_name]
109       yaml
110     end
111 
112   end
113 
114 
115   class Application < Rake::Application #:nodoc:
116 
117     # Deprecated: rakefile/Rakefile, removed in 1.5
118     DEFAULT_BUILDFILES = ['buildfile', 'Buildfile'] + DEFAULT_RAKEFILES
119     
120     attr_reader :rakefiles, :requires
121     private :rakefiles, :requires
122 
123     def initialize
124       super
125       @rakefiles = DEFAULT_BUILDFILES.dup
126       @top_level_tasks = []
127       @home_dir = File.expand_path('.buildr', ENV['HOME'])
128       mkpath @home_dir unless File.exist?(@home_dir)
129       @settings = Settings.new(self)
130       @on_completion = []
131       @on_failure = []
132     end
133 
134     def run
135       standard_exception_handling do
136         init 'Buildr'
137         load_buildfile
138         top_level
139       end
140     end
141 
142     # Not for external consumption.
143     def switch_to_namespace(names) #:nodoc:
144       current, @scope = @scope, names
145       begin
146         yield
147       ensure
148         @scope = current
149       end
150     end
151 
152     # Returns list of Gems associated with this buildfile, as listed in build.yaml.
153     # Each entry is of type Gem::Specification.
154     attr_reader :gems
155 
156     # Buildr home directory, .buildr under user's home directory.
157     attr_reader :home_dir
158 
159     # Copied from BUILD_ENV.
160     def environment
161       ENV['BUILDR_ENV']
162     end
163 
164     # Returns the Settings associated with this build.
165     attr_reader :settings
166 
167     # :call-seq:
168     #   buildfile
169     # Returns the buildfile as a task that you can use as a dependency.
170     def buildfile
171       @buildfile_task ||= BuildfileTask.define_task(File.expand_path(rakefile))
172     end
173 
174     # Files that complement the buildfile itself
175     def build_files #:nodoc:
176       deprecated 'Please call buildfile.prerequisites instead' 
177       buildfile.prerequisites
178     end
179 
180     # Yields to block on successful completion. Primarily used for notifications.
181     def on_completion(&block)
182       @on_completion << block
183     end
184 
185     # Yields to block on failure with exception. Primarily used for notifications.
186     def on_failure(&block)
187       @on_failure << block
188     end
189 
190     # :call-seq:
191     #   deprecated(message)
192     #
193     # Use with deprecated methods and classes. This method automatically adds the file name and line number,
194     # and the text 'Deprecated' before the message, and eliminated duplicate warnings. It only warns when
195     # running in verbose mode.
196     #
197     # For example:
198     #   deprecated 'Please use new_foo instead of foo.'
199     def deprecated(message) #:nodoc:
200       return unless verbose
201       "#{caller[1]}: Deprecated: #{message}".tap do |message|
202         @deprecated ||= {}
203         unless @deprecated[message]
204           @deprecated[message] = true
205           warn message
206         end
207       end
208     end
209 
210   protected
211     
212     def load_buildfile # replaces load_rakefile
213       standard_exception_handling do
214         find_buildfile
215         load_gems
216         load_artifact_ns
217         load_tasks
218         raw_load_buildfile
219       end
220     end
221 
222     def top_level # adds on_completion hook
223       standard_exception_handling do
224         if options.show_tasks
225           display_tasks_and_comments
226         elsif options.show_prereqs
227           display_prerequisites
228         elsif options.execute
229           eval options.execute
230         else
231           @start = Time.now
232           top_level_tasks.each { |task_name| invoke_task(task_name) }
233           if verbose
234             elapsed = Time.now - @start
235             real = []
236             real << ('%ih' % (elapsed / 3600)) if elapsed >= 3600
237             real << ('%im' % ((elapsed / 60) % 60)) if elapsed >= 60
238             real << ('%.3fs' % (elapsed % 60))
239             puts $terminal.color("Completed in #{real.join}", :green)
240           end
241           # On OS X this will load Cocoa and Growl which takes half a second we
242           # don't want to measure, so put this after the console message.
243           title, message = "Your build has completed", "#{Dir.pwd}\nbuildr #{@top_level_tasks.join(' ')}"
244           @on_completion.each do |block|
245             block.call(title, message) rescue nil
246           end
247         end
248       end
249     end
250 
251     def handle_options
252       options.rakelib = ['tasks']
253 
254       OptionParser.new do |opts|
255         opts.banner = "buildr [-f rakefile] {options} targets..."
256         opts.separator ""
257         opts.separator "Options are ..."
258         
259         opts.on_tail("-h", "--help", "-H", "Display this help message.") do
260           puts opts
261           exit
262         end
263         standard_buildr_options.each { |args| opts.on(*args) }
264       end.parse!
265     end
266 
267     def standard_buildr_options # replaces standard_rake_options
268       [
269         ['--describe', '-D [PATTERN]', "Describe the tasks (matching optional PATTERN), then exit.",
270           lambda { |value|
271             options.show_tasks = true
272             options.full_description = true
273             options.show_task_pattern = Regexp.new(value || '')
274           }
275         ],
276         ['--execute',  '-E CODE',
277           "Execute some Ruby code after loading the buildfile",
278           lambda { |value| options.execute = value }            
279         ],
280         ['--environment',  '-e ENV',
281           "Environment name (e.g. development, test, production).",
282           lambda { |value| ENV['BUILDR_ENV'] = value }            
283         ],
284         ['--generate [PATH]',
285          "Generate buildfile from either pom.xml file or directory path.",
286           lambda { |value|
287             value ||= File.exist?('pom.xml') ? 'pom.xml' : Dir.pwd
288             raw_generate_buildfile value
289             exit
290           }
291         ],
292         ['--libdir', '-I LIBDIR', "Include LIBDIR in the search path for required modules.",
293           lambda { |value| $:.push(value) }
294         ],
295         ['--prereqs', '-P [PATTERN]', "Display the tasks and dependencies (matching optional PATTERN), then exit.",
296           lambda { |value|
297             options.show_prereqs = true
298             options.show_task_pattern = Regexp.new(value || '')
299           }
300         ],
301         ['--quiet', '-q', "Do not log messages to standard output.",
302           lambda { |value| verbose(false) }
303         ],
304         ['--buildfile', '-f FILE', "Use FILE as the buildfile.",
305           lambda { |value| 
306             @rakefiles.clear 
307             @rakefiles << value
308           }
309         ],
310         ['--rakelibdir', '--rakelib', '-R PATH',
311           "Auto-import any .rake files in PATH. (default is 'tasks')",
312           lambda { |value| options.rakelib = value.split(':') }
313         ],
314         ['--require', '-r MODULE', "Require MODULE before executing rakefile.",
315           lambda { |value|
316             begin
317               require value
318             rescue LoadError => ex
319               begin
320                 rake_require value
321               rescue LoadError => ex2
322                 raise ex
323               end
324             end
325           }
326         ],
327         ['--rules', "Trace the rules resolution.",
328           lambda { |value| options.trace_rules = true }
329         ],
330         ['--no-search', '--nosearch', '-N', "Do not search parent directories for the Rakefile.",
331           lambda { |value| options.nosearch = true }
332         ],
333         ['--silent', '-s', "Like --quiet, but also suppresses the 'in directory' announcement.",
334           lambda { |value|
335             verbose(false)
336             options.silent = true
337           }
338         ],
339         ['--tasks', '-T [PATTERN]', "Display the tasks (matching optional PATTERN) with descriptions, then exit.",
340           lambda { |value|
341             options.show_tasks = true
342             options.show_task_pattern = Regexp.new(value || '')
343             options.full_description = false
344           }
345         ],
346         ['--trace', '-t', "Turn on invoke/execute tracing, enable full backtrace.",
347           lambda { |value|
348             options.trace = true
349             verbose(true)
350           }
351         ],
352         ['--verbose', '-v', "Log message to standard output (default).",
353           lambda { |value| verbose(true) }
354         ],
355         ['--version', '-V', "Display the program version.",
356           lambda { |value|
357             puts "Buildr #{Buildr::VERSION} #{RUBY_PLATFORM[/java/] && '(JRuby '+JRUBY_VERSION+')'}"
358             exit
359           }
360         ]
361       ]
362     end
363 
364     def find_buildfile
365       buildfile, location = find_rakefile_location || (tty_output? && ask_generate_buildfile)
366       fail "No Buildfile found (looking for: #{@rakefiles.join(', ')})" if buildfile.nil?
367       @rakefile = buildfile
368       Dir.chdir(location)
369     end
370 
371     def ask_generate_buildfile
372       source = choose do |menu|
373         menu.header = "To use Buildr you need a buildfile. Do you want me to create one?"
374         menu.choice("From Maven2 POM file") { 'pom.xml' } if File.exist?('pom.xml')
375         menu.choice("From directory structure") { Dir.pwd }
376         menu.choice("Cancel") { }
377       end
378       if source
379         buildfile = raw_generate_buildfile(source)
380         [buildfile, File.dirname(buildfile)]
381       end
382     end
383 
384     def raw_generate_buildfile(source)
385       # We need rakefile to be known, for settings.build to be accessible.
386       @rakefile = File.expand_path(DEFAULT_BUILDFILES.first)
387       fail "Buildfile already exists" if File.exist?(@rakefile) && !(tty_output? && agree('Buildfile exists, overwrite?'))
388       script = File.directory?(source) ? Generate.from_directory(source) : Generate.from_maven2_pom(source)
389       File.open @rakefile, 'w' do |file|
390         file.puts script
391       end
392       puts "Created #{@rakefile}" if verbose
393       @rakefile
394     end
395 
396     def raw_load_buildfile # replaces raw_load_rakefile
397       puts "(in #{Dir.pwd}, #{environment})" unless options.silent
398       load File.expand_path(@rakefile) if @rakefile && @rakefile != ''
399       load_imports
400       Buildr.projects
401     end
402 
403     # Load/install all Gems specified in build.yaml file.
404     def load_gems #:nodoc:
405       missing_deps, installed = listed_gems.partition { |gem| gem.is_a?(Gem::Dependency) }
406       unless missing_deps.empty?
407         newly_installed = Util::Gems.install(*missing_deps)
408         installed += newly_installed
409       end
410       installed.each do |spec|
411         if gem(spec.name, spec.version.to_s)
412           # TODO: is this intended to load rake tasks from the installed gems?
413           # We should use a convention like .. if the gem has a _buildr.rb file, load it.
414 
415           #FileList[spec.require_paths.map { |path| File.expand_path("#{path}/*.rb", spec.full_gem_path) }].
416           #  map { |path| File.basename(path) }.each { |file| require file }
417           #FileList[File.expand_path('tasks/*.rake', spec.full_gem_path)].each do |file|
418           #  Buildr.application.add_import file
419           #end
420         end
421       end
422       @gems = installed
423     end
424 
425     # Returns Gem::Specification for every listed and installed Gem, Gem::Dependency
426     # for listed and uninstalled Gem, which is the installed before loading the buildfile.
427     def listed_gems #:nodoc:
428       Array(settings.build['gems']).map do |dep|
429         name, trail = dep.scan(/^\s*(\S*)\s*(.*)\s*$/).first
430         versions = trail.scan(/[=><~!]{0,2}\s*[\d\.]+/)
431         versions = ['>= 0'] if versions.empty?
432         dep = Gem::Dependency.new(name, versions)
433         Gem::SourceIndex.from_installed_gems.search(dep).last || dep
434       end
435     end
436 
437     # Load artifact specs from the build.yaml file, making them available 
438     # by name ( ruby symbols ).
439     def load_artifact_ns #:nodoc:
440       hash = settings.build['artifacts']
441       return unless hash
442       raise "Expected 'artifacts' element to be a hash" unless Hash === hash
443       # Currently we only use one artifact namespace to rule them all. (the root NS)
444       Buildr::ArtifactNamespace.load(:root => hash)
445     end
446     
447     # Loads buildr.rb files from users home directory and project directory.
448     # Loads custom tasks from .rake files in tasks directory.
449     def load_tasks #:nodoc:
450       # TODO: this might need to be split up, look for deprecated features, better method name.
451       files = [ File.expand_path('buildr.rb', ENV['HOME']), 'buildr.rb' ].select { |file| File.exist?(file) }
452       files += [ File.expand_path('buildr.rake', ENV['HOME']), File.expand_path('buildr.rake') ].
453         select { |file| File.exist?(file) }.each { |file| warn "Please use '#{file.ext('rb')}' instead of '#{file}'" }
454       files += (options.rakelib || []).collect { |rlib| Dir["#{rlib}/*.rake"] }.flatten
455 
456       files.each do |file|
457         unless $LOADED_FEATURES.include?(file)
458           load file
459           $LOADED_FEATURES << file
460         end
461       end
462       buildfile.enhance files
463       true
464     end
465 
466     def display_tasks_and_comments
467       displayable_tasks = tasks.select { |t| t.comment && t.name =~ options.show_task_pattern }
468       if options.full_description
469         displayable_tasks.each do |t|
470           puts "buildr #{t.name_with_args}"
471           t.full_comment.split("\n").each do |line|
472             puts "    #{line}"
473           end
474           puts
475         end
476       else
477         width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10
478         max_column = truncate_output? ? terminal_width - name.size - width - 7 : nil
479         displayable_tasks.each do |t|
480           printf "#{name} %-#{width}s  # %s\n",
481             t.name_with_args, max_column ? truncate(t.comment, max_column) : t.comment
482         end
483       end
484     end
485 
486     def display_prerequisites
487       displayable_tasks = tasks.select { |t| t.name =~ options.show_task_pattern }
488       displayable_tasks.each do |t|
489         puts "buildr #{t.name}"
490         t.prerequisites.each { |pre| puts "    #{pre}" }
491       end
492     end
493 
494     def standard_exception_handling # adds on_failure hook
495       begin
496         yield
497       rescue SystemExit => ex
498         # Exit silently with current status
499         exit(ex.status)
500       rescue OptionParser::ParseError => ex
501         $stderr.puts $terminal.color(ex.message, :red)
502         exit(1)
503       rescue Exception => ex
504         title, message = "Your build failed with an error", "#{Dir.pwd}:\n#{ex.message}"
505         @on_failure.each do |block|
506           block.call(title, message, ex) rescue nil
507         end
508         # Exit with error message
509         $stderr.puts "Buildr aborted!"
510         $stderr.puts $terminal.color(ex.message, :red)
511         if options.trace
512           $stderr.puts ex.backtrace.join("\n")
513         else
514           $stderr.puts ex.backtrace.select { |str| str =~ /#{rakefile}/ }.map { |line| $terminal.color(line, :red) }.join("\n") if rakefile
515           $stderr.puts "(See full trace by running task with --trace)"
516         end
517         exit(1)
518       end
519     end
520 
521   end
522   
523   
524   # This task stands for the buildfile and all its associated helper files (e.g., buildr.rb, build.yaml).
525   # By using this task as a prerequisite for other tasks, you can ensure these tasks will be needed
526   # whenever the buildfile changes.
527   class BuildfileTask < Rake::FileTask #:nodoc:
528     
529     def timestamp
530       ([name] + prerequisites).map { |f| File.stat(f).mtime }.max rescue Time.now
531     end
532   end
533 
534 
535   class << self
536 
537     # Returns the Buildr::Application object.
538     def application
539       Rake.application
540     end
541 
542     def application=(app) #:nodoc:
543       Rake.application = app
544     end
545 
546     # Returns the Settings associated with this build.
547     def settings
548       Buildr.application.settings
549     end
550 
551     # Copied from BUILD_ENV.
552     def environment
553       Buildr.application.environment
554     end
555 
556   end
557 
558   Buildr.application = Buildr::Application.new
559 
560 end
561 
562 
563 # Add a touch of color when available and running in terminal.
564 if $stdout.isatty
565   begin
566     require 'Win32/Console/ANSI' if Config::CONFIG['host_os'] =~ /mswin/
567     HighLine.use_color = true
568   rescue LoadError
569   end
570 else
571   HighLine.use_color = false
572 end
573 
574 
575 alias :warn_without_color :warn
576 
577 # Show warning message.
578 def warn(message)
579   warn_without_color $terminal.color(message.to_s, :blue) if verbose
580 end
581 
582 # Show error message.  Use this when you need to show an error message and not throwing
583 # an exception that will stop the build.
584 def error(message)
585   puts $terminal.color(message.to_s, :red)
586 end
587 
588 # Show optional information.  The message is printed only when running in verbose
589 # mode (the default).
590 def info(message)
591   puts message if verbose
592 end
593 
594 # Show message.  The message is printed out only when running in trace mode.
595 def trace(message)
596   puts message if Buildr.application.options.trace
597 end
598 
599 
600 module Rake #:nodoc
601   # Rake's circular dependency checks (InvocationChain) only applies to task prerequisites,
602   # all other cases result in the non too-descriptive thread sleeping error. This change can
603   # deal with circular dependencies that occur from direct task invocation, e.g:
604   #   task 'foo'=>'bar'
605   #   task 'bar' do
606   #     task('foo').invoke
607   #   end
608   class Task #:nodoc:
609     def invoke(*args)
610       task_args = TaskArguments.new(arg_names, args)
611       invoke_with_call_chain(task_args, Thread.current[:rake_chain] || InvocationChain::EMPTY)
612     end
613 
614     def invoke_with_call_chain(task_args, invocation_chain)
615       new_chain = InvocationChain.append(self, invocation_chain)
616       @lock.synchronize do
617         if application.options.trace
618           puts "** Invoke #{name} #{format_trace_flags}"
619         end
620         return if @already_invoked
621         @already_invoked = true
622         begin
623           invoke_prerequisites(task_args, new_chain)
624         rescue
625           trace "Exception while invoking prerequisites of task #{self.inspect}"
626           raise
627         end
628         begin
629           old_chain, Thread.current[:rake_chain] = Thread.current[:rake_chain], new_chain
630           execute(task_args) if needed?
631         ensure
632           Thread.current[:rake_chain] = nil
633         end
634       end
635     end
636   end
637 end
638 
639 
640 module RakeFileUtils #:nodoc:
641   FileUtils::OPT_TABLE.each do |name, opts|
642     default_options = []
643     if opts.include?(:verbose) || opts.include?("verbose")
644       default_options << ':verbose => RakeFileUtils.verbose_flag == true'
645     end
646     next if default_options.empty?
647     module_eval(<<-EOS, __FILE__, __LINE__ + 1)
648     def #{name}( *args, &block )
649       super(
650         *rake_merge_option(args,
651           #{default_options.join(', ')}
652           ), &block)
653     end
654     EOS
655   end
656 end

Generated using the rcov code coverage analysis tool for Ruby version 0.8.2.1.

Valid XHTML 1.0! Valid CSS!