Author: boisvert
Date: Wed Aug  4 05:46:33 2010
New Revision: 982126

URL: http://svn.apache.org/viewvc?rev=982126&view=rev
Log:
BUILDR-488 artifact poms not reinstalled

Also:  it's now possible to provide artifact content with,
        artifact(...).content some_string

Also:  artifact(...).pom.from(file) works as expected.

Also:  poms associated with packages are no longer created directly
       in the local repository.  they are created alongside packages
       under 'target' which is more consistent.  the local repository
       is therefore only written when installing/downloading artifacts
       -- not during packaging.
       

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/lib/buildr/core/util.rb
    buildr/trunk/lib/buildr/packaging/artifact.rb
    buildr/trunk/lib/buildr/packaging/package.rb
    buildr/trunk/spec/core/util_spec.rb
    buildr/trunk/spec/packaging/artifact_spec.rb
    buildr/trunk/spec/packaging/packaging_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: 
http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Wed Aug  4 05:46:33 2010
@@ -23,6 +23,7 @@
 * Fixed:  BUILDR-472 ECJ dependency now required to build any java project
 * Fixed:  BUILDR-479 Enforce using a minimal version of jruby
 * Fixed:  BUILDR-481 Antwrap monkey-patching in core.rb
+* Fixed:  BUILDR-488 artifact poms not reinstalled
 
 1.4.1 (2010-07-07)
 * Added:  BUILDR-420 Support external compiler

Modified: buildr/trunk/lib/buildr/core/util.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/core/util.rb?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/core/util.rb (original)
+++ buildr/trunk/lib/buildr/core/util.rb Wed Aug  4 05:46:33 2010
@@ -115,6 +115,22 @@ module Buildr
       FileList[dirs.map { |dir| File.join(dir, '/**/{*,.*}') }].reject { 
|file| File.basename(file) =~ /^[.]{1,2}$/ }
     end
 
+    # :call-seq:
+    #   replace_extension(filename) => filename_with_updated_extension
+    #
+    # Replace the file extension, e.g.,
+    #   replace_extension("foo.zip", "txt") => "foo.txt"
+    def replace_extension(filename, new_ext)
+      ext = File.extname(filename)
+      if filename =~ /\.$/
+        filename + new_ext
+      elsif ext == ""
+        filename + "." + new_ext
+      else
+        filename[0..-ext.length] + new_ext
+      end
+    end
+
     # Utility methods for running gem commands
     module Gems #:nodoc:
       extend self

Modified: buildr/trunk/lib/buildr/packaging/artifact.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/packaging/artifact.rb?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/packaging/artifact.rb (original)
+++ buildr/trunk/lib/buildr/packaging/artifact.rb Wed Aug  4 05:46:33 2010
@@ -143,13 +143,16 @@ module Buildr
     end
 
     def install
-      pom.install if pom && pom != self
       invoke
-      installed = Buildr.repositories.locate(self)
-      unless installed == name # If not already in local repository.
-        mkpath File.dirname(installed)
-        cp name, installed
-        info "Installed #{installed}"
+      in_local_repository = Buildr.repositories.locate(self)
+      if name != in_local_repository
+        if pom && pom != self
+          pom.invoke
+          pom.install
+        end
+        mkpath File.dirname(in_local_repository)
+        cp name, in_local_repository
+        info "Installed #{name} to #{in_local_repository}"
       end
     end
 
@@ -230,7 +233,7 @@ module Buildr
     # The default artifact type.
     DEFAULT_TYPE = :jar
 
-    include ActsAsArtifact
+    include ActsAsArtifact, Buildr
 
     class << self
 
@@ -332,7 +335,7 @@ module Buildr
         # if the artifact knows how to build itself (e.g. download from a 
different location),
         # so don't perform it if the task found a different way to create the 
artifact.
         task.enhance do
-          unless File.exist?(name)
+          if !File.exist?(name)
             info "Downloading #{to_spec}"
             download
             pom.invoke rescue nil if pom && pom != self
@@ -349,30 +352,52 @@ module Buildr
     #   install test
     # See also Buildr#install and Buildr#upload.
     def from(path)
-      path = path.is_a?(Rake::Task) ? path : File.expand_path(path.to_s)
-      unless exist?
-        enhance [path] do
-          path = File.expand_path(path.to_s)
-          mkpath File.dirname(name)
-          pom.invoke unless type == :pom
+      @from = path.is_a?(Rake::Task) ? path : File.expand_path(path.to_s)
+      enhance [...@from] do
+        mkpath File.dirname(name)
+        cp @from.to_s, name
+      end
+      pom.content pom_xml unless pom == self || pom.has_content?
+      self
+    end
 
-          cp path, name
-          info "Installed #{path} as #{to_spec}"
+    # :call-seq:
+    #   content(string) => self
+    #
+    # Use this when you want to install or upload an artifact from a given 
content, for example:
+    #   readme = artifact('com.example:readme:txt:1.0').content(<<-EOF
+    #     Please visit our website at http://example.com/readme
+    #   <<EOF
+    #   install readme
+    #
+    # If the argument is not a string, it will be converted to a string using 
to_s
+    def content(string = nil)
+      return @content unless string
+
+      unless @content
+        enhance do
+          write name, @content
         end
-      end
-      unless type == :pom
-        pom.enhance do
-          unless pom.exist?
-            mkpath File.dirname(pom.name)
-            File.open(pom.name, 'w') { |file| file.write pom.pom_xml }
+
+        class << self
+          # Force overwriting target since we don't have source file
+          # to check for timestamp modification
+          def needed?
+            true
           end
         end
       end
+      @content = string
+      pom.content pom_xml unless pom == self || pom.has_content?
       self
     end
 
   protected
 
+    def has_content?
+      @from || @content
+    end
+
     # :call-seq:
     #   download
     #
@@ -639,11 +664,11 @@ module Buildr
   # To specify an artifact and the means for creating it:
   #   download(artifact('dojo:dojo-widget:zip:2.0')=>
   #     'http://download.dojotoolkit.org/release-2.0/dojo-2.0-widget.zip')
-  def artifact(spec, &block) #:yields:task
+  def artifact(spec, path = nil, &block) #:yields:task
     spec = artifact_ns.fetch(spec) if spec.kind_of?(Symbol)
     spec = Artifact.to_hash(spec)
     unless task = Artifact.lookup(spec)
-      task = Artifact.define_task(repositories.locate(spec))
+      task = Artifact.define_task(path || repositories.locate(spec))
       task.send :apply_spec, spec
       Rake::Task['rake:artifacts'].enhance [task]
       Artifact.register(task)
@@ -748,7 +773,7 @@ module Buildr
   end
 
   # :call-seq:
-  #   install(artifacts)
+  #   install(artifacts) => install_task
   #
   # Installs the specified artifacts in the local repository as part of the 
install task.
   #
@@ -759,9 +784,12 @@ module Buildr
     artifacts = artifacts(args)
     raise ArgumentError, 'This method can only install artifacts' unless 
artifacts.all? { |f| f.respond_to?(:to_spec) }
     all = (artifacts + artifacts.map { |artifact| artifact.pom }).uniq
-    task('install').tap do |task|
-      task.enhance all, &block
-      task 'uninstall' do
+    task('install').tap do |install|
+      install.enhance(all) do
+        all.each(&:install)
+      end
+      install.enhance &block if block
+      task('uninstall') do
         all.map(&:to_s ).each { |file| rm file if File.exist?(file) }
       end
     end

Modified: buildr/trunk/lib/buildr/packaging/package.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/packaging/package.rb?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/packaging/package.rb (original)
+++ buildr/trunk/lib/buildr/packaging/package.rb Wed Aug  4 05:46:33 2010
@@ -168,7 +168,6 @@ module Buildr
         task 'package'=>package
         package.enhance [task('build')]
         package.enhance { info "Packaging #{File.basename(file_name)}" }
-
         if spec[:file]
           class << package ; self ; end.send(:define_method, :type) { 
spec[:type] }
           class << package ; self ; end.send(:define_method, :id) { nil }
@@ -176,19 +175,27 @@ module Buildr
           # Make it an artifact using the specifications, and tell it how to 
create a POM.
           package.extend ActsAsArtifact
           package.send :apply_spec, spec.only(*Artifact::ARTIFACT_ATTRIBUTES)
-          # Another task to create the POM file.
-          pom = package.pom
-          pom.enhance do
-            mkpath File.dirname(pom.name)
-            File.open(pom.name, 'w') { |file| file.write pom.pom_xml }
+
+          # Create pom associated with package
+          class << package
+            def pom
+              unless @pom
+                pom_filename = Util.replace_extension(self.name, 'pom')
+                spec = {:group=>group, :id=>id, :version=>version, :type=>:pom}
+                @pom = Buildr.artifact(spec, pom_filename)
+                @pom.content @pom.pom_xml
+              end
+              @pom
+            end
           end
+
           file(Buildr.repositories.locate(package)=>package) { package.install 
}
 
           # Add the package to the list of packages created by this project, 
and
           # register it as an artifact. The later is required so if we look up 
the spec
           # we find the package in the project's target directory, instead of 
finding it
           # in the local repository and attempting to install it.
-          Artifact.register package, pom
+          Artifact.register package, package.pom
         end
 
         task('install')   { package.install if package.respond_to?(:install) }

Modified: buildr/trunk/spec/core/util_spec.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/spec/core/util_spec.rb?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/spec/core/util_spec.rb (original)
+++ buildr/trunk/spec/core/util_spec.rb Wed Aug  4 05:46:33 2010
@@ -16,6 +16,22 @@
 
 require File.join(File.dirname(__FILE__), '../spec_helpers')
 
+describe Buildr do
+  describe "#replace_extension" do
+    it "should replace filename extensions" do
+      replace = lambda { |filename, ext| Util.replace_extension(filename, ext) 
}
+
+      replace["foo.zip", "txt"].should eql("foo.txt")
+      replace["foo.", "txt"].should eql("foo.txt")
+      replace["foo", "txt"].should eql("foo.txt")
+
+      replace["bar/foo.zip", "txt"].should eql("bar/foo.txt")
+      replace["bar/foo.", "txt"].should eql("bar/foo.txt")
+      replace["bar/foo", "txt"].should eql("bar/foo.txt")
+    end
+  end
+end
+
 describe Hash do
   describe "#only" do
     it "should find value for one key" do
@@ -68,7 +84,7 @@ end
 
 describe File do
   # Quite a few of the other specs depend on File#utime working correctly.
-  # These specs validate that utime is working as expected. 
+  # These specs validate that utime is working as expected.
   describe "#utime" do
     it "should update mtime of directories" do
       mkpath 'tmp'

Modified: buildr/trunk/spec/packaging/artifact_spec.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/spec/packaging/artifact_spec.rb?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/spec/packaging/artifact_spec.rb (original)
+++ buildr/trunk/spec/packaging/artifact_spec.rb Wed Aug  4 05:46:33 2010
@@ -129,6 +129,14 @@ describe Artifact do
     Artifact.list.should include(@classified.to_spec)
     Artifact.list.should include('foo:foo:jar:1.0')
   end
+
+  it 'should accept user-defined string content' do
+    a = artifact(@spec)
+    a.content 'foo'
+    install a
+    lambda { install.invoke }.should change { File.exist?(a.to_s) && 
File.exist?(repositories.locate(a)) }.to(true)
+    read(repositories.locate(a)).should eql('foo')
+  end
 end
 
 
@@ -604,9 +612,20 @@ describe Buildr, '#install' do
   end
 
   it 'should install POM alongside artifact' do
+    pom = artifact(@spec).pom
     write @file
     install artifact(@spec).from(@file)
-    lambda { install.invoke }.should change { 
File.exist?(artifact(@spec).pom.to_s) }.to(true)
+    lambda { install.invoke }.should change { 
File.exist?(repositories.locate(pom)) }.to(true)
+  end
+
+  it 'should reinstall POM alongside artifact' do
+    pom = artifact(@spec).pom
+    write @file
+    write repositories.locate(pom)
+    sleep 1
+
+    install artifact(@spec).from(@file)
+    lambda { install.invoke }.should change { 
File.mtime(repositories.locate(pom)) }
   end
 end
 

Modified: buildr/trunk/spec/packaging/packaging_spec.rb
URL: 
http://svn.apache.org/viewvc/buildr/trunk/spec/packaging/packaging_spec.rb?rev=982126&r1=982125&r2=982126&view=diff
==============================================================================
--- buildr/trunk/spec/packaging/packaging_spec.rb (original)
+++ buildr/trunk/spec/packaging/packaging_spec.rb Wed Aug  4 05:46:33 2010
@@ -263,7 +263,7 @@ describe Project, '#package' do
     end
     project('foo').packages.uniq.size.should be(5)
   end
-  
+
   it 'should create different tasks for package with different ids' do
     define 'foo', :version=>'1.0' do
       package(:jar, :id=>'bar')
@@ -271,7 +271,7 @@ describe Project, '#package' do
     end
     project('foo').packages.uniq.size.should be(2)
   end
-    
+
   it 'should create different tasks for package with classifier' do
     define 'foo', :version=>'1.0' do
       package(:jar)
@@ -347,10 +347,10 @@ describe Project, '#package' do
     end
   end
 
-  it 'should create a POM artifact in local repository' do
+  it 'should create a POM artifact in target directory' do
     define 'foo', :version=>'1.0' do
       package.pom.should be(artifact('foo:foo:pom:1.0'))
-      repositories.locate('foo:foo:pom:1.0').should eql(package.pom.to_s)
+      package.pom.to_s.should point_to_path('target/foo-1.0.pom')
     end
   end
 
@@ -662,15 +662,15 @@ describe Rake::Task, ' upload' do
       read(upload).should eql(read(package))
     end
   end
-  
+
   it 'should not upload twice the pom when artifacts are uploaded from a 
project' do
     write 'src/main/java/Foo.java', 'public class Foo {}'
     repositories.release_to = 'sftp://example.com/base'
-    define 'foo' do 
-      project.group = "attached" 
-      project.version = "1.0" 
-      package(:jar) 
-      package(:sources) 
+    define 'foo' do
+      project.group = "attached"
+      project.version = "1.0"
+      package(:jar)
+      package(:sources)
     end
      URI.should_receive(:upload).exactly(:once).
          
with(URI.parse('sftp://example.com/base/attached/foo/1.0/foo-1.0-sources.jar'), 
project("foo").package(:sources).to_s, anything)


Reply via email to