Buildr C0 Coverage Information - RCov

lib/buildr/packaging/package.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/buildr/packaging/package.rb 249 115
18.47%
27.83%

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 
17 require 'buildr/core/project'
18 require 'buildr/core/compile'
19 require 'buildr/packaging/artifact'
20 
21 
22 module Buildr
23   # Methods added to Project to support packaging and tasks for packaging,
24   # installing and uploading packages.
25   module Package
26 
27     include Extension
28 
29     first_time do
30       desc 'Create packages'
31       Project.local_task('package'=>'build') { |name| "Packaging #{name}" }
32       desc 'Install packages created by the project'
33       Project.local_task('install'=>'package') { |name| "Installing packages from #{name}" }
34       desc 'Remove previously installed packages'
35       Project.local_task('uninstall') { |name| "Uninstalling packages from #{name}" }
36       desc 'Upload packages created by the project'
37       Project.local_task('upload'=>'package') { |name| "Deploying packages from #{name}" }
38       # Anything that comes after local packaging (install, upload) executes the integration tests,
39       # which do not conflict with integration invoking the project's own packaging (package=>
40       # integration=>foo:package is not circular, just confusing to debug.)
41       task 'package' do
42         task('integration').invoke if Buildr.options.test && Buildr.application.original_dir == Dir.pwd
43       end
44     end
45 
46     before_define(:package => :build) do |project|
47       [ :package, :install, :uninstall, :upload ].each { |name| project.recursive_task name }
48       # Need to run build before package, since package is often used as a dependency by tasks that
49       # expect build to happen.
50       project.task('package'=>project.task('build'))
51       project.group ||= project.parent && project.parent.group || project.name
52       project.version ||= project.parent && project.parent.version
53     end
54 
55     after_define(:package)
56 
57     # The project's identifier. Same as the project name, with colons replaced by dashes.
58     # The ID for project foo:bar is foo-bar.
59     def id
60       name.gsub(':', '-')
61     end
62 
63     # Group used for packaging. Inherited from parent project. Defaults to the top-level project name.
64     attr_accessor :group
65 
66     # Version used for packaging. Inherited from parent project.
67     attr_accessor :version
68 
69     # :call-seq:
70     #   package(type, spec?) => task
71     #
72     # Defines and returns a package created by this project.
73     #
74     # The first argument declares the package type. For example, :jar to create a JAR file.
75     # The package is an artifact that takes its artifact specification from the project.
76     # You can override the artifact specification by passing various options in the second
77     # argument, for example:
78     #   package(:zip, :classifier=>'sources')
79     #
80     # Packages that are ZIP files provides various ways to include additional files, directories,
81     # and even merge ZIPs together. Have a look at ZipTask for more information. In case you're
82     # wondering, JAR and WAR packages are ZIP files.
83     #
84     # You can also enhance a JAR package using the ZipTask#with method that accepts the following options:
85     # * :manifest -- Specifies how to create the MANIFEST.MF. By default, uses the project's
86     #   #manifest property.
87     # * :meta_inf -- Specifies files to be included in the META-INF directory. By default,
88     #   uses the project's #meta-inf property.
89     #
90     # The WAR package supports the same options and adds a few more:
91     # * :classes -- Directories of class files to include in WEB-INF/classes. Includes the compile
92     #   target directory by default.
93     # * :libs -- Artifacts and files to include in WEB-INF/libs. Includes the compile classpath
94     #   dependencies by default.
95     #
96     # For example:
97     #   define 'project' do
98     #     define 'beans' do
99     #       package :jar
100     #     end
101     #     define 'webapp' do
102     #       compile.with project('beans')
103     #       package(:war).with :libs=>MYSQL_JDBC
104     #     end
105     #     package(:zip, :classifier=>'sources').include path_to('.')
106     #  end
107     #
108     # Two other packaging types are:
109     # * package :sources -- Creates a JAR file with the source code and classifier 'sources', for use by IDEs.
110     # * package :javadoc -- Creates a ZIP file with the Javadocs and classifier 'javadoc'. You can use the
111     #   javadoc method to further customize it.
112     #
113     # A package is also an artifact. The following tasks operate on packages created by the project:
114     #   buildr upload     # Upload packages created by the project
115     #   buildr install    # Install packages created by the project
116     #   buildr package    # Create packages
117     #   buildr uninstall  # Remove previously installed packages
118     #
119     # If you want to add additional packaging types, implement a method with the name package_as_[type]
120     # that accepts a file name and returns an appropriate Rake task.  For example:
121     #   def package_as_zip(file_name) #:nodoc:
122     #     ZipTask.define_task(file_name)
123     #   end
124     #
125     # The file name is determined from the specification passed to the package method, however, some
126     # packagers need to override this.  For example, package(:sources) produces a file with the extension
127     # 'jar' and the classifier 'sources'.  If you need to overwrite the default implementation, you should
128     # also include a method named package_as_[type]_spec.  For example:
129     #   def package_as_sources_spec(spec) #:nodoc:
130     #     # Change the source distribution to .zip extension
131     #     spec.merge({ :type=>:zip, :classifier=>'sources' })
132     #   end
133     def package(*args)
134       spec = Hash === args.last ? args.pop.dup : {}
135       no_options = spec.empty? # since spec is mutated
136       if spec[:file]
137         rake_check_options spec, :file, :type
138         spec[:type] = args.shift || spec[:type] || spec[:file].split('.').last.to_sym
139         file_name = spec[:file]
140       else
141         rake_check_options spec, *ActsAsArtifact::ARTIFACT_ATTRIBUTES
142         spec[:id] ||= self.id
143         spec[:group] ||= self.group
144         spec[:version] ||= self.version
145         spec[:type] = args.shift || spec[:type] || compile.packaging || :zip
146       end
147 
148       packager = method("package_as_#{spec[:type]}") rescue fail("Don't know how to create a package of type #{spec[:type]}")
149       if packager.arity == 1
150         unless file_name
151           spec = send("package_as_#{spec[:type]}_spec", spec) if respond_to?("package_as_#{spec[:type]}_spec")
152           file_name = path_to(:target, Artifact.hash_to_file_name(spec))
153         end
154         package = (no_options && packages.detect { |pkg| pkg.type == spec[:type] && (pkg.id.nil? || pkg.id == spec[:id]) &&
155           (pkg.respond_to?(:classifier) ? pkg.classifier : nil) == spec[:classifier]}) ||
156           packages.find { |pkg| pkg.name == file_name } ||
157           packager.call(file_name)
158       else
159         Buildr.application.deprecated "We changed the way package_as methods are implemented.  See the package method documentation for more details."
160         file_name ||= path_to(:target, Artifact.hash_to_file_name(spec))
161         package = packager.call(file_name, spec)
162       end
163 
164       # First time: prepare package for install, uninstall and upload tasks.
165       unless packages.include?(package)
166         # We already run build before package, but we also need to do so if the package itself is
167         # used as a dependency, before we get to run the package task.
168         task 'package'=>package
169         package.enhance [task('build')]
170         package.enhance { info "Packaging #{File.basename(file_name)}" }
171         if spec[:file]
172           class << package ; self ; end.send(:define_method, :type) { spec[:type] }
173           class << package ; self ; end.send(:define_method, :id) { nil }
174         else
175           # Make it an artifact using the specifications, and tell it how to create a POM.
176           package.extend ActsAsArtifact
177           package.send :apply_spec, spec.only(*Artifact::ARTIFACT_ATTRIBUTES)
178 
179           # Create pom associated with package
180           class << package
181             def pom
182               unless @pom
183                 pom_filename = Util.replace_extension(self.name, 'pom')
184                 spec = {:group=>group, :id=>id, :version=>version, :type=>:pom}
185                 @pom = Buildr.artifact(spec, pom_filename)
186                 @pom.content @pom.pom_xml
187               end
188               @pom
189             end
190           end
191 
192           file(Buildr.repositories.locate(package)=>package) { package.install }
193 
194           # Add the package to the list of packages created by this project, and
195           # register it as an artifact. The later is required so if we look up the spec
196           # we find the package in the project's target directory, instead of finding it
197           # in the local repository and attempting to install it.
198           Artifact.register package, package.pom
199         end
200 
201         task('install')   { package.install if package.respond_to?(:install) }
202         task('uninstall') { package.uninstall if package.respond_to?(:uninstall) }
203         task('upload')    { package.upload if package.respond_to?(:upload) }
204 
205         packages << package
206       end
207       package
208     end
209 
210     # :call-seq:
211     #   packages => tasks
212     #
213     # Returns all packages created by this project. A project may create any number of packages.
214     #
215     # This method is used whenever you pass a project to Buildr#artifact or any other method
216     # that accepts artifact specifications and projects. You can use it to list all packages
217     # created by the project. If you want to return a specific package, it is often more
218     # convenient to call #package with the type.
219     def packages
220       @packages ||= []
221     end
222 
223   protected
224 
225     def package_as_zip(file_name) #:nodoc:
226       ZipTask.define_task(file_name)
227     end
228 
229     def package_as_tar(file_name) #:nodoc:
230       TarTask.define_task(file_name)
231     end
232     alias :package_as_tgz :package_as_tar
233 
234     def package_as_sources_spec(spec) #:nodoc:
235       spec.merge(:type=>:jar, :classifier=>'sources')
236     end
237 
238     def package_as_sources(file_name) #:nodoc:
239       ZipTask.define_task(file_name).tap do |zip|
240         zip.include :from=>[compile.sources, resources.target].compact
241       end
242     end
243 
244   end
245 end
246 
247 class Buildr::Project
248   include Buildr::Package
249 end

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