Buildr C0 Coverage Information - RCov

lib/buildr/scala/compiler.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/buildr/scala/compiler.rb 248 160
33.06%
31.87%

Key

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.

Coverage Details

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 require 'buildr/core/project'
17 require 'buildr/core/common'
18 require 'buildr/core/compile'
19 require 'buildr/packaging'
20 
21 module Buildr::Scala
22   DEFAULT_VERSION = '2.9.0-1'
23 
24   class << self
25 
26     def version_str
27       warn "Use of Scala.version_str is deprecated.  Use Scala.version instead"
28       version
29     end
30 
31     def installed_version
32       unless @installed_version
33         @installed_version = if Scalac.installed?
34           begin
35             # try to read the value from the properties file
36             props = Zip::ZipFile.open(File.expand_path('lib/scala-library.jar', Scalac.scala_home)) do |zipfile|
37               zipfile.read 'library.properties'
38             end
39 
40             version_str = props.match(/version\.number\s*=\s*([^\s]+)/).to_a[1]
41 
42             if version_str
43               md = version_str.match(/\d+\.\d[\d\.]*/) or
44                 fail "Unable to parse Scala version: #{version_str}"
45 
46               md[0].sub(/.$/, "") # remove trailing dot, if any
47             end
48           rescue => e
49             warn "Unable to parse library.properties in $SCALA_HOME/lib/scala-library.jar: #{e}"
50             nil
51           end
52         end
53       end
54 
55       @installed_version
56     end
57 
58     def version
59       Buildr.settings.build['scala.version'] || installed_version || DEFAULT_VERSION
60     end
61 
62     # check if version matches any of the given prefixes
63     def version?(*v)
64       v.any? { |v| version.index(v.to_s) == 0 }
65     end
66 
67     # returns Scala version without build number.
68     # e.g.  "2.9.0-1" => "2.9.0"
69     def version_without_build
70       version.split('-')[0]
71     end
72   end
73 
74   # Scalac compiler:
75   #   compile.using(:scalac)
76   # Used by default if .scala files are found in the src/main/scala directory (or src/test/scala)
77   # and sets the target directory to target/classes (or target/test/classes).
78   #
79   # Accepts the following options:
80   # * :warnings    -- Generate warnings if true (opposite of -nowarn).
81   # * :deprecation -- Output source locations where deprecated APIs are used.
82   # * :optimise    -- Generates faster bytecode by applying optimisations to the program.
83   # * :target      -- Class file compatibility with specified release.
84   # * :debug       -- Generate debugging info.
85   # * :other       -- Array of options to pass to the Scalac compiler as is, e.g. -Xprint-types
86   class Scalac < Buildr::Compiler::Base
87 
88     # The scalac compiler jars are added to classpath at load time,
89     # if you want to customize artifact versions, you must set them on the
90     #
91     #      artifact_ns['Buildr::Compiler::Scalac'].library = '2.7.5'
92     #
93     # namespace before this file is required.  This is of course, only
94     # if SCALA_HOME is not set or invalid.
95     REQUIRES = ArtifactNamespace.for(self) do |ns|
96       version = Buildr.settings.build['scala.version'] || DEFAULT_VERSION
97       ns.library!      'org.scala-lang:scala-library:jar:>=' + version
98       ns.compiler!     'org.scala-lang:scala-compiler:jar:>=' + version
99     end
100 
101     class << self
102       def scala_home
103         env_home = ENV['SCALA_HOME']
104 
105         @home ||= (if !env_home.nil? && File.exists?(env_home + '/lib/scala-library.jar') && File.exists?(env_home + '/lib/scala-compiler.jar')
106           env_home
107         else
108           nil
109         end)
110       end
111 
112       def installed?
113         !scala_home.nil?
114       end
115 
116       def use_installed?
117         if installed? && Buildr.settings.build['scala.version']
118           Buildr.settings.build['scala.version'] == Scala.installed_version
119         else
120           Buildr.settings.build['scala.version'].nil? && installed?
121         end
122       end
123 
124       def dependencies
125         if use_installed?
126           ['scala-library', 'scala-compiler'].map { |s| File.expand_path("lib/#{s}.jar", scala_home) }
127         else
128           REQUIRES.artifacts.map(&:to_s)
129         end
130       end
131 
132       def use_fsc
133         use_installed? && ENV["USE_FSC"] =~ /^(yes|on|true)$/i
134       end
135 
136       def applies_to?(project, task) #:nodoc:
137         paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) }
138         paths.flatten!
139 
140         # Just select if we find .scala files
141         paths.any? { |path| !Dir["#{path}/**/*.scala"].empty? }
142       end
143     end
144 
145     Javac = Buildr::Compiler::Javac
146 
147     OPTIONS = [:warnings, :deprecation, :optimise, :target, :debug, :other, :javac]
148 
149     # Lazy evaluation to allow change in buildfile
150     Java.classpath << lambda { dependencies }
151 
152     specify :language=>:scala, :sources => [:scala, :java], :source_ext => [:scala, :java],
153             :target=>'classes', :target_ext=>'class', :packaging=>:jar
154 
155     def initialize(project, options) #:nodoc:
156       super
157       options[:debug] = Buildr.options.debug if options[:debug].nil?
158       options[:warnings] = verbose if options[:warnings].nil?
159       options[:deprecation] ||= false
160       options[:optimise] ||= false
161       options[:make] ||= :transitivenocp if Scala.version? 2.8
162       options[:javac] ||= {}
163 
164       @java = Javac.new(project, options[:javac])
165     end
166 
167     def compile(sources, target, dependencies) #:nodoc:
168       check_options(options, OPTIONS + (Scala.version?(2.8) ? [:make] : []))
169 
170       java_sources = java_sources(sources)
171       enable_dep_tracing = Scala.version?(2.8) && java_sources.empty?
172 
173       dependencies.unshift target if enable_dep_tracing
174 
175       cmd_args = []
176       cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR)
177       source_paths = sources.select { |source| File.directory?(source) }
178       cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) unless source_paths.empty?
179       cmd_args << '-d' << File.expand_path(target)
180       cmd_args += scalac_args
181 
182       if enable_dep_tracing
183         dep_dir = File.expand_path(target)
184         Dir.mkdir dep_dir unless File.exists? dep_dir
185 
186         cmd_args << '-make:' + options[:make].to_s
187         cmd_args << '-dependencyfile'
188         cmd_args << File.expand_path('.scala-deps', dep_dir)
189       end
190 
191       cmd_args += files_from_sources(sources)
192 
193       unless Buildr.application.options.dryrun
194         trace((['scalac'] + cmd_args).join(' '))
195 
196         if Scalac.use_fsc
197           system(([File.expand_path('bin/fsc', Scalac.scala_home)] + cmd_args).join(' ')) or
198             fail 'Failed to compile, see errors above'
199         else
200           Java.load
201           begin
202             Java.scala.tools.nsc.Main.process(cmd_args.to_java(Java.java.lang.String))
203           rescue => e
204             fail "Scala compiler crashed:\n#{e.inspect}"
205           end
206           fail 'Failed to compile, see errors above' if Java.scala.tools.nsc.Main.reporter.hasErrors
207         end
208 
209         unless java_sources.empty?
210           trace 'Compiling mixed Java/Scala sources'
211 
212           # TODO  includes scala-compiler.jar
213           deps = dependencies + Scalac.dependencies + [ File.expand_path(target) ]
214           @java.compile(java_sources, target, deps)
215         end
216       end
217     end
218 
219   private
220 
221     def java_sources(sources)
222       sources.flatten.map { |source| File.directory?(source) ? FileList["#{source}/**/*.java"] : source } .
223         flatten.reject { |file| File.directory?(file) || File.extname(file) != '.java' }.map { |file| File.expand_path(file) }.uniq
224     end
225 
226     # Returns Scalac command line arguments from the set of options.
227     def scalac_args #:nodoc:
228       args = []
229       args << "-nowarn" unless options[:warnings]
230       args << "-verbose" if trace?(:scalac)
231       if options[:debug] == true
232         args << (Scala.version?(2.7, 2.8) ? "-g" : "-g:vars")
233       elsif options[:debug]
234         args << "-g:#{options[:debug]}"
235       end
236       args << "-deprecation" if options[:deprecation]
237       args << "-optimise" if options[:optimise]
238       args << "-target:jvm-" + options[:target].to_s if options[:target]
239       args + Array(options[:other])
240     end
241 
242   end
243 
244 end
245 
246 # Scala compiler comes first, ahead of Javac, this allows it to pick
247 # projects that mix Scala and Java code by spotting Scala code first.
248 Buildr::Compiler.compilers.unshift Buildr::Scala::Scalac

Generated on 2011-07-06 23:35:38 -0700 with rcov 0.9.8