C0 code coverage information
Generated on Wed Oct 07 08:34:01 -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.
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 # Base module for all things Java.
18 module Java
19
20 # JDK commands: java, javac, javadoc, etc.
21 module Commands
22
23 class << self
24
25 # :call-seq:
26 # java(class, *args, options?)
27 #
28 # Runs Java with the specified arguments.
29 #
30 # The last argument may be a Hash with additional options:
31 # * :classpath -- One or more file names, tasks or artifact specifications.
32 # These are all expanded into artifacts, and all tasks are invoked.
33 # * :java_args -- Any additional arguments to pass (e.g. -hotspot, -xms)
34 # * :properties -- Hash of system properties (e.g. 'path'=>base_dir).
35 # * :name -- Shows this name, otherwise shows the first argument (the class name).
36 # * :verbose -- If true, prints the command and all its argument.
37 def java(*args, &block)
38 options = Hash === args.last ? args.pop : {}
39 options[:verbose] ||= Buildr.application.options.trace || false
40 rake_check_options options, :classpath, :java_args, :properties, :name, :verbose
41
42 name = options[:name] || "java #{args.first}"
43 cmd_args = [path_to_bin('java')]
44 classpath = classpath_from(options)
45 cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
46 options[:properties].each { |k, v| cmd_args << "-D#{k}=#{v}" } if options[:properties]
47 cmd_args += (options[:java_args] || (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split).flatten
48 cmd_args += args.flatten.compact
49 unless Buildr.application.options.dryrun
50 info "Running #{name}"
51 block = lambda { |ok, res| fail "Failed to execute #{name}, see errors above" unless ok } unless block
52 puts cmd_args.join(' ') if Buildr.application.options.trace
53 cmd_args = cmd_args.map(&:inspect).join(' ') if Util.win_os?
54 sh(*cmd_args) do |ok, ps|
55 block.call ok, ps
56 end
57 end
58 end
59
60 # :call-seq:
61 # apt(*files, options)
62 #
63 # Runs Apt with the specified arguments.
64 #
65 # The last argument may be a Hash with additional options:
66 # * :compile -- If true, compile source files to class files.
67 # * :source -- Specifies source compatibility with a given JVM release.
68 # * :output -- Directory where to place the generated source files, or the
69 # generated class files when compiling.
70 # * :classpath -- One or more file names, tasks or artifact specifications.
71 # These are all expanded into artifacts, and all tasks are invoked.
72 def apt(*args)
73 options = Hash === args.last ? args.pop : {}
74 rake_check_options options, :compile, :source, :output, :classpath
75
76 files = args.flatten.map(&:to_s).
77 collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten
78 cmd_args = [ Buildr.application.options.trace ? '-verbose' : '-nowarn' ]
79 if options[:compile]
80 cmd_args << '-d' << options[:output].to_s
81 else
82 cmd_args << '-nocompile' << '-s' << options[:output].to_s
83 end
84 cmd_args << '-source' << options[:source] if options[:source]
85 classpath = classpath_from(options)
86 tools = Java.tools_jar
87 classpath << tools if tools
88 cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
89 cmd_args += files
90 unless Buildr.application.options.dryrun
91 info 'Running apt'
92 trace (['apt'] + cmd_args).join(' ')
93 Java.load
94 Java.com.sun.tools.apt.Main.process(cmd_args.to_java(Java.java.lang.String)) == 0 or
95 fail 'Failed to process annotations, see errors above'
96 end
97 end
98
99 # :call-seq:
100 # javac(*files, options)
101 #
102 # Runs Javac with the specified arguments.
103 #
104 # The last argument may be a Hash with additional options:
105 # * :output -- Target directory for all compiled class files.
106 # * :classpath -- One or more file names, tasks or artifact specifications.
107 # These are all expanded into artifacts, and all tasks are invoked.
108 # * :sourcepath -- Additional source paths to use.
109 # * :javac_args -- Any additional arguments to pass (e.g. -extdirs, -encoding)
110 # * :name -- Shows this name, otherwise shows the working directory.
111 def javac(*args)
112 options = Hash === args.last ? args.pop : {}
113 rake_check_options options, :classpath, :sourcepath, :output, :javac_args, :name
114
115 files = args.flatten.each { |f| f.invoke if f.respond_to?(:invoke) }.map(&:to_s).
116 collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten
117 name = options[:name] || Dir.pwd
118
119 cmd_args = []
120 classpath = classpath_from(options)
121 cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
122 cmd_args << '-sourcepath' << options[:sourcepath].join(File::PATH_SEPARATOR) if options[:sourcepath]
123 cmd_args << '-d' << options[:output].to_s if options[:output]
124 cmd_args += options[:javac_args].flatten if options[:javac_args]
125 cmd_args += files
126 unless Buildr.application.options.dryrun
127 info "Compiling #{files.size} source files in #{name}"
128 trace (['javac'] + cmd_args).join(' ')
129 Java.load
130 Java.com.sun.tools.javac.Main.compile(cmd_args.to_java(Java.java.lang.String)) == 0 or
131 fail 'Failed to compile, see errors above'
132 end
133 end
134
135 # :call-seq:
136 # javadoc(*files, options)
137 #
138 # Runs Javadocs with the specified files and options.
139 #
140 # This method accepts the following special options:
141 # * :output -- The output directory
142 # * :classpath -- Array of classpath dependencies.
143 # * :sourcepath -- Array of sourcepaths (paths or tasks).
144 # * :name -- Shows this name, otherwise shows the working directory.
145 #
146 # All other options are passed to Javadoc as following:
147 # * true -- As is, for example, :author=>true becomes -author
148 # * false -- Prefixed, for example, :index=>false becomes -noindex
149 # * string -- Option with value, for example, :windowtitle=>'My project' becomes -windowtitle "My project"
150 # * array -- Option with set of values separated by spaces.
151 def javadoc(*args)
152 options = Hash === args.last ? args.pop : {}
153
154 cmd_args = [ '-d', options[:output], Buildr.application.options.trace ? '-verbose' : '-quiet' ]
155 options.reject { |key, value| [:output, :name, :sourcepath, :classpath].include?(key) }.
156 each { |key, value| value.invoke if value.respond_to?(:invoke) }.
157 each do |key, value|
158 case value
159 when true, nil
160 cmd_args << "-#{key}"
161 when false
162 cmd_args << "-no#{key}"
163 when Hash
164 value.each { |k,v| cmd_args << "-#{key}" << k.to_s << v.to_s }
165 else
166 cmd_args += Array(value).map { |item| ["-#{key}", item.to_s] }.flatten
167 end
168 end
169 [:sourcepath, :classpath].each do |option|
170 options[option].to_a.flatten.tap do |paths|
171 cmd_args << "-#{option}" << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty?
172 end
173 end
174 cmd_args += args.flatten.uniq
175 name = options[:name] || Dir.pwd
176 unless Buildr.application.options.dryrun
177 info "Generating Javadoc for #{name}"
178 trace (['javadoc'] + cmd_args).join(' ')
179 Java.load
180 Java.com.sun.tools.javadoc.Main.execute(cmd_args.to_java(Java.java.lang.String)) == 0 or
181 fail 'Failed to generate Javadocs, see errors above'
182 end
183 end
184
185 protected
186
187 # :call-seq:
188 # path_to_bin(cmd?) => path
189 #
190 # Returns the path to the specified Java command (with no argument to java itself).
191 def path_to_bin(name = nil)
192 home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.'
193 bin = File.expand_path(File.join(home, 'bin'))
194 fail 'JAVA_HOME environment variable does not point to a valid JRE/JDK installation.' unless File.exist? bin
195 File.expand_path(File.join(bin, name.to_s))
196 end
197
198 # :call-seq:
199 # classpath_from(options) => files
200 #
201 # Extracts the classpath from the options, expands it by calling artifacts, invokes
202 # each of the artifacts and returns an array of paths.
203 def classpath_from(options)
204 Buildr.artifacts(options[:classpath] || []).map(&:to_s).
205 map { |t| task(t).invoke; File.expand_path(t) }
206 end
207
208 end
209
210 end
211
212 end
213
Generated using the rcov code coverage analysis tool for Ruby
version 0.8.2.1.