lib/maven/tools/dsl.rb in maven-tools-0.34.5 vs lib/maven/tools/dsl.rb in maven-tools-1.0.0.rc2

- old
+ new

@@ -1,30 +1,31 @@ require 'fileutils' require 'maven/tools/gemspec_dependencies' require 'maven/tools/artifact' require 'maven/tools/jarfile' require 'maven/tools/versions' +require 'maven/tools/gemfile_lock' module Maven module Tools module DSL def tesla( &block ) @model = Model.new @model.model_version = '4.0.0' - @model.name = File.basename( basedir ) + @model.name = ::File.basename( basedir ) @model.group_id = 'dummy' @model.artifact_id = model.name @model.version = '0.0.0' @context = :project nested_block( :project, @model, block ) if block if @needs_torquebox if ! @model.repositories.detect { |r| r.id == 'rubygems-prereleases' } && @model.dependencies.detect { |d| d.group_id == 'rubygems' && d.version.match( /[a-zA-Z]/ ) } @current = @model - snapshot_repository( 'http://rubygems-proxy.torquebox.org/prereleases', - :id => 'rubygems-prereleases' ) + snapshot_repository( 'rubygems-prereleases', + 'http://rubygems-proxy.torquebox.org/prereleases' ) @current = nil end @needs_torquebox = nil end result = @model @@ -45,23 +46,23 @@ @model end def eval_pom( src, reference_file = '.' ) @source = reference_file - eval( src ) + eval( src, nil, ::File.expand_path( @source ) ) ensure @source = nil @basedir = nil end def basedir( basedir = nil ) - @basedir ||= basedir if basedir + @basedir = basedir if basedir if @source - @basedir ||= File.directory?( @source ) ? @source : - File.dirname( File.expand_path( @source ) ) + @basedir ||= ::File.directory?( @source ) ? @source : + ::File.dirname( ::File.expand_path( @source ) ) end - @basedir ||= File.expand_path( '.' ) + @basedir ||= ::File.expand_path( '.' ) end def artifact( a ) if a.is_a?( String ) a = Maven::Tools::Artifact.from_coordinate( a ) @@ -106,62 +107,96 @@ def gemfile( name = 'Gemfile', options = {} ) if name.is_a? Hash options = name name = 'Gemfile' end - name = File.join( basedir, name ) unless File.exists?( name ) - basedir = File.dirname( name ) unless basedir + name = ::File.join( basedir, name ) unless ::File.exists?( name ) + basedir = ::File.dirname( name ) unless basedir - @gemfile_options = options - FileUtils.cd( basedir ) do - eval( File.read( File.expand_path( name ) ) ) + @inside_gemfile = true + # the eval might need those options for gemspec declaration + pr = profile :gemfile do + activation do + file( :missing => name + '.lock' ) + end + + FileUtils.cd( basedir ) do + f = ::File.expand_path( name ) + eval( ::File.read( f ), nil, f ) + end end - if @gemfile_options - @gemfile_options = nil + if @gemspec_args + @inside_gemfile = :gemfile + case @gemspec_args[ 0 ] + when Hash + gemspec( @gemspec_args[ 0 ].merge( options ) ) + when NilClass + gemspec( @gemspec_args[ 0 ], options ) + else + @gemspec_args[ 1 ].merge!( options ) + gemspec( *@gemspec_args ) + end + else setup_gem_support( options ) end + if pr.dependencies.empty? + @current.profiles.delete( pr ) + end + + lockfile = ::File.expand_path( name + '.lock' ) + if File.exists? lockfile + profile :gemfile_lock do + activation do + file( :exists => name + '.lock' ) + end + locked = GemfileLock.new( lockfile ) + locked.hull.each do |name, version| + gem name, version unless model.artifact_id == name && model.group_id == 'rubygems' + end + end + end + if @has_path or @has_git gem 'bundler', :scope => :provided unless gem? 'bundler' jruby_plugin :gem do execute_goal :exec, :filename => 'bundle', :args => 'install' end end ensure + @inside_gemfile = nil + @gemspec_args = nil @has_path = nil @has_git = nil end def setup_gem_support( options, spec = nil, config = {} ) - unless model.properties.member?( 'project.build.sourceEncoding' ) - properties( 'project.build.sourceEncoding' => 'utf-8' ) - end if spec.nil? require_path = '.' - name = File.basename( File.expand_path( '.' ) ) + name = ::File.basename( ::File.expand_path( '.' ) ) else require_path = spec.require_path name = spec.name end unless options[ :only_metadata ] unless model.repositories.detect { |r| r.id == 'rubygems-releases' } - repository( 'http://rubygems-proxy.torquebox.org/releases', - :id => 'rubygems-releases' ) + repository( 'rubygems-releases', + 'http://rubygems-proxy.torquebox.org/releases' ) end @needs_torquebox = true setup_jruby_plugins_version end if options.key?( :jar ) || options.key?( 'jar' ) jarpath = options[ :jar ] || options[ 'jar' ] if jarpath - jar = File.basename( jarpath ).sub( /.jar$/, '' ) - output = File.dirname( "#{require_path}/#{jarpath}" ) + jar = ::File.basename( jarpath ).sub( /.jar$/, '' ) + output = ::File.dirname( "#{require_path}/#{jarpath}" ) output.sub!( /\/$/, '' ) end else jar = "#{name}" output = "#{require_path}" @@ -171,11 +206,11 @@ build do source_directory source end end if jar && ( source || - File.exists?( File.join( basedir, 'src', 'main', 'java' ) ) ) + ::File.exists?( ::File.join( basedir, 'src', 'main', 'java' ) ) ) plugin( :jar, VERSIONS[ :jar_plugin ], :outputDirectory => output, :finalName => jar ) do execute_goals :jar, :phase => 'prepare-package' end @@ -192,31 +227,33 @@ if jruby.match( /-SNAPSHOT/ ) != nil snapshot_repository( 'http://ci.jruby.org/snapshots/maven', :id => 'jruby-snapshots' ) end scope( jruby_scope ) do - if jruby < '1.7' + if ( jruby < '1.6' ) + raise 'jruby before 1.6 are not supported' + elsif ( jruby < '1.7' ) warn 'jruby version below 1.7 uses jruby-complete' jar 'org.jruby:jruby-core', jruby - elsif jruby.sub( /1\.7\./, '').to_i < 5 + elsif ( jruby.sub( /1\.7\./, '').to_i < 5 ) jar 'org.jruby:jruby-core', jruby elsif jruby =~ /-no_asm$/ pom 'org.jruby:jruby-noasm', jruby.sub( /-no_asm$/, '' ) - else + else pom 'org.jruby:jruby', jruby end end end - #private :setup_jruby + private :setup_jruby def jarfile( file = 'Jarfile', options = {} ) if file.is_a? Hash options = file file = 'Jarfile' end unless file.is_a?( Maven::Tools::Jarfile ) - file = Maven::Tools::Jarfile.new( File.expand_path( file ) ) + file = Maven::Tools::Jarfile.new( ::File.expand_path( file ) ) end if options[ :skip_locked ] or not file.exists_lock? file.populate_unlocked do |dsl| setup_jruby( dsl.jruby ) @@ -237,39 +274,47 @@ end end end end - def gemspec( name = nil, options = @gemfile_options || {} ) + def gemspec( name = nil, options = {} ) + if @inside_gemfile == true + @gemspec_args = [ name, options ] + return + end if name.is_a? Hash options = name name = nil end if name - name = File.join( basedir, name ) + name = ::File.join( basedir, name ) else name - gemspecs = Dir[ File.join( basedir, "*.gemspec" ) ] + gemspecs = Dir[ ::File.join( basedir, "*.gemspec" ) ] raise "more then one gemspec file found" if gemspecs.size > 1 raise "no gemspec file found" if gemspecs.size == 0 name = gemspecs.first end spec = nil - spec_file = File.read( File.expand_path( name ) ) + f = ::File.expand_path( name ) + spec_file = ::File.read( f ) begin + spec = Gem::Specification.from_yaml( spec_file ) + rescue Gem::Exception FileUtils.cd( basedir ) do # TODO jruby java user.dir - spec = eval( spec_file ) + spec = eval( spec_file, nil, f ) end - rescue - spec = Gem::Specification.from_yaml( spec_file ) end self.spec( spec, name, options ) end def spec( spec, name = nil, options = {} ) name ||= "#{spec.name}-#{spec.version}.gemspec" + unless model.properties.member?( 'project.build.sourceEncoding' ) + properties( 'project.build.sourceEncoding' => 'utf-8' ) + end @gemfile_options = nil if @context == :project build.directory = '${basedir}/pkg' @@ -293,11 +338,28 @@ if options[ :include_jars ] || options[ 'include_jars' ] config[ :includeDependencies ] = true end plugin( 'de.saumya.mojo:gem-maven-plugin:${jruby.plugins.version}', config ) + + deps = nil + if @inside_gemfile + profile! @inside_gemfile do + deps = all_deps( spec ) + end + else + deps = all_deps( spec ) + end + unless deps.java_runtime.empty? + deps.java_runtime.each do |d| + dependency Maven::Tools::Artifact.new( *d ) + end + end + end + + def all_deps( spec ) deps = Maven::Tools::GemspecDependencies.new( spec ) deps.runtime.each do |d| gem d end unless deps.development.empty? @@ -305,16 +367,13 @@ deps.development.each do |d| gem d end end end - unless deps.java_runtime.empty? - deps.java_runtime.each do |d| - dependency Maven::Tools::Artifact.new( *d ) - end - end + deps end + private :all_deps def build( &block ) build = @current.build ||= Build.new nested_block( :build, build, block ) if block build @@ -366,33 +425,85 @@ else @current.id = args[ 0 ] end end - def site( url = nil, options = {} ) + def site( *args, &block ) site = Site.new - options.merge!( :url => url ) + args, options = args_and_options( *args ) + site.id = args[ 0 ] + site.url = args[ 1 ] + site.name = args[ 2 ] fill_options( site, options ) + nested_block( :site, site, block) if block @current.site = site end - def source_control( url = nil, options = {} ) + def source_control( *args, &block ) scm = Scm.new - options.merge!( :url => url ) + args, options = args_and_options( *args ) + scm.connection = args[ 0 ] + scm.developer_connection = args[ 1 ] + scm.url = args[ 2 ] fill_options( scm, options ) + nested_block( :scm, scm, block ) if block @current.scm = scm end alias :scm :source_control - def issue_management( url, system = nil ) + def issue_management( *args, &block ) issues = IssueManagement.new - issues.url = url - issues.system = system + args, options = args_and_options( *args ) + issues.url = args[ 0 ] + fill_options( issues, options ) + nested_block( :issue_management, issues, block ) if block @current.issue_management = issues - issues end + alias :issues :issue_management + def ci_management( *args, &block ) + ci = CiManagement.new + args, options = args_and_options( *args ) + ci.url = args[ 0 ] + fill_options( ci, options ) + nested_block( :ci_management, ci, block ) if block + @current.ci_management = ci + end + alias :ci :ci_management + + def distribution_management( *args, &block ) + di = DistributionManagement.new + args, options = args_and_options( *args ) + di.status = args[ 0 ] + di.download_url = args[ 1 ] + fill_options( di, options ) + nested_block( :distribution_management, di, block ) if block + @current.distribution_management = di + end + + def relocation( *args, &block ) + args, options = args_and_options( *args ) + relocation = fill_gav( Relocation, args.join( ':' ) ) + fill_options( relocation, options ) + nested_block( :relocation, relocation, block ) if block + @current.relocation = relocation + end + + def system( arg ) + @current.system = arg + end + + def notifier( *args, &block ) + n = Notifier.new + args, options = args_and_options( *args ) + n.type = args[ 0 ] + n.address = args[ 1 ] + fill_options( n, options ) + nested_block( :notifier, n, block ) if block + @current.notifiers << n + n + end def mailing_list( *args, &block ) list = MailingList.new args, options = args_and_options( *args ) list.name = args[ 0 ] fill_options( list, options ) @@ -466,89 +577,105 @@ activation = Activation.new nested_block( :activation, activation, block ) if block @current.activation = activation end - def distribution( val = nil, &block ) + def distribution( *args, &block ) if @context == :license - @current.distribution = val + args, options = args_and_options( *args ) + @current.distribution = args[ 0 ] + fill_options( @current, options ) else - dist = DistributionManagement.new - nested_block( :distribution, dist, block ) if block - @current.distribution_management = dist + distribution_management( *args, &block ) end end - alias :distribution_management :distribution def includes( *items ) @current.includes = items.flatten end def excludes( *items ) @current.excludes = items.flatten end - def test_resource( &block ) + def test_resource( options = {}, &block ) # strange behaviour when calling specs from Rakefile return if @current.nil? - resource = Resource.new - nested_block( :resource, resource, block ) if block + resource = TestResource.new + fill_options( resource, options ) + nested_block( :test_resource, resource, block ) if block if @context == :project ( @current.build ||= Build.new ).test_resources << resource else @current.test_resources << resource end end - def resource( &block ) + def resource( options = {}, &block ) resource = Resource.new + fill_options( resource, options ) nested_block( :resource, resource, block ) if block if @context == :project ( @current.build ||= Build.new ).resources << resource else @current.resources << resource end end - def repository( url, options = {}, &block ) - do_repository( :repository=, url, options, block ) + def repository( *args, &block ) + do_repository( :repository=, *args, &block ) end - def plugin_repository( url, options = {}, &block ) - do_repository( :plugin, url, options, block ) + def plugin_repository( *args, &block ) + do_repository( :plugin, *args, &block ) end - def snapshot_repository( url, options = {}, &block ) - unless @current.respond_to? :snapshot_repository= - options[ :releases ] = false unless options.key?( :releases ) || options.key?( 'releases' ) - options[ :snapshots ] = true unless options.key?( :snapshots ) || options.key?( 'snapshots' ) + def set_policy( key, enable, options ) + return unless options + if map = options[ key ] || options[ key.to_s ] + map[ :enabled ] = enable + else + options[ key ] = enable end - do_repository( :snapshot_repository=, url, options, block ) end + private :set_policy - def releases( config ) - @current.releases = repository_policy( config ) + def snapshot_repository( *args, &block ) + unless @current.respond_to?( :snapshot_repository= ) + args, options = args_and_options( *args ) + set_policy( :releases, false, options ) + set_policy( :snapshots, true, options ) + args << options + end + do_repository( :snapshot_repository=, *args, &block ) end - def snapshots( config ) - @current.snapshots = repository_policy( config ) + def releases( config = nil, &block ) + @current.releases = repository_policy( @current.releases, + config, &block ) end - def repository_policy( config ) - rp = RepositoryPolicy.new + def snapshots( config = nil, &block) + @current.snapshots = repository_policy( @current.snapshots, + config, &block ) + end + + def repository_policy( rp, config, &block ) + rp ||= RepositoryPolicy.new case config when Hash - rp.enabled = config[ :enabled ] - rp.update_policy = config[ :update ] - rp.checksum_policy = config[ :checksum ] + rp.enabled = config[ :enabled ] unless config[ :enabled ].nil? + rp.update_policy = config[ :update ] || config[ :update_policy ] + rp.checksum_policy = config[ :checksum ] || config[ :checksum_policy ] when TrueClass rp.enabled = true when FalseClass rp.enabled = false else - rp.enabled = 'true' == config + rp.enabled = 'true' == config unless config.nil? end + nested_block( :repository_policy, rp, block ) if block rp end def args_and_options( *args ) if args.last.is_a? Hash @@ -592,18 +719,30 @@ @current.properties[k.to_s] = v.to_s end @current.properties end - def extension( *gav ) - @current.build ||= Build.new - gav = gav.join( ':' ) - ext = fill_gav( Extension, gav) - @current.build.extensions << ext + def extension( *args ) + build = if @context == :build + @current + else + @current.build ||= Build.new + end + args, options = args_and_options( *args ) + ext = fill_gav( Extension, args.join( ':' ) ) + fill_options( ext, options ) + build.extensions << ext ext end + def exclusion( *gav ) + gav = gav.join( ':' ) + ex = fill_gav( Extension, gav) + @current.exclusions << ex + ex + end + def setup_jruby_plugins_version unless @current.properties.key?( 'jruby.plugins.version' ) properties( 'jruby.plugins.version' => VERSIONS[ :jruby_plugins ] ) end end @@ -704,11 +843,11 @@ else options[ :id ] = id end options[ :taskId ] = options[ :id ] || options[ 'id' ] if @source - options[ :nativePom ] = File.expand_path( @source ).sub( /#{basedir}./, '' ) + options[ :nativePom ] = ::File.expand_path( @source ).sub( /#{basedir}./, '' ) end add_execute_task( options, &block ) else # just act like execute_goals @@ -764,16 +903,16 @@ @current.executions << exec nested_block(:execution, exec, block) if block exec end - def dependency( type, *args ) - do_dependency( false, type, *args ) + def dependency( type, *args, &block ) + do_dependency( false, type, *args, &block ) end - def dependency!( type, *args ) - do_dependency( true, type, *args ) + def dependency!( type, *args, &block ) + do_dependency( true, type, *args, &block ) end def dependency?( type, *args ) find_dependency( dependency_container, retrieve_dependency( type, *args ) ) != nil @@ -806,32 +945,36 @@ elsif args[ 0 ].is_a?( ::Maven::Tools::Artifact ) a = args[ 0 ] type = a[ :type ] options = a else + args, options = args_and_options( *args ) a = ::Maven::Tools::Artifact.from( type, *args ) end options ||= {} d = fill_gav( Dependency, a ? a.gav : args.join( ':' ) ) d.type = type.to_s # TODO maybe copy everything from options ? d.scope = options[ :scope ] if options[ :scope ] d.system_path = options[ :system_path ] if options[ :system_path ] + d end def dependency_container if @context == :overrides @current.dependency_management ||= DependencyManagement.new @current.dependency_management.dependencies + #elsif @context == :build + # @current. else @current.dependencies end end - def do_dependency( bang, type, *args ) + def do_dependency( bang, type, *args, &block ) d = retrieve_dependency( type, *args ) container = dependency_container if bang dd = find_dependency( container, d ) @@ -841,14 +984,13 @@ container << d end else container << d end + + args, options = args_and_options( *args ) - if args.last.is_a?( Hash ) - options = args.last - end if options || @scope options ||= {} if @scope if options[ :scope ] || options[ 'scope' ] raise "scope block and scope option given" @@ -858,40 +1000,59 @@ exclusions = options.delete( :exclusions ) || options.delete( "exclusions" ) case exclusions when Array exclusions.each do |v| - d.exclusions << fill_gav( Exclusion, v ) + v, opts = args_and_options( v ) + ex = fill_gav( Exclusion, *v ) + fill_options( ex, opts ) + d.exclusions << ex end when String d.exclusions << fill_gav( Exclusion, exclusions ) end options.each do |k,v| d.send( "#{k}=".to_sym, v ) unless d.send( k.to_sym ) end end + nested_block( :dependency, d, block ) if block d end def scope( name ) - @scope = name - yield - @scope = nil + if @context == :dependency + @current.scope = name + else + @scope = name + yield + @scope = nil + end end def phase( name ) @phase = name yield @phase = nil end + def profile!( id, &block ) + profile = @current.profiles.detect { |p| p.id.to_s == id.to_s } + if profile + nested_block( :profile, profile, block ) if block + profile + else + profile( id, &block ) + end + end + def profile( id, &block ) profile = Profile.new profile.id = id if id @current.profiles << profile nested_block( :profile, profile, block ) if block + profile end def report_set( *reports, &block ) set = ReportSet.new case reports.last @@ -940,35 +1101,30 @@ unless args[ 0 ].match( /:/ ) args[ 0 ] = "rubygems:#{args[ 0 ] }" end if args.last.is_a?(Hash) options = args.last - elsif @group - options = {} - args << options - end - if options - + # on ruby-maven side we ignore the require option options.delete( :require ) options.delete( 'require' ) if options.key?( :git ) @has_git = true elsif options.key?( :path ) @has_path = true else platform = options.delete( :platform ) || options.delete( 'platform' ) - group = options.delete( :group ) || options.delete( 'group' ) || @group - if group + group = options.delete( :group ) || options.delete( 'group' ) || @group || nil + if group case group.to_sym when :test options[ :scope ] = :test when :development options[ :scope ] = :provided end - end + end if platform.nil? || is_jruby_platform( platform ) options[ :version ] = '[0,)' if args.size == 2 && options[ :version ].nil? && options[ 'version' ].nil? do_dependency( bang, :gem, *args ) end end @@ -977,11 +1133,11 @@ do_dependency( bang, :gem, *args ) end end def local( path, options = {} ) - path = File.expand_path( path ) + path = ::File.expand_path( path ) dependency( :jar, Maven::Tools::Artifact.new_local( path, :jar, options ) ) end def method_missing( method, *args, &block ) @@ -1014,30 +1170,32 @@ end end end @current else - if ( args.size > 0 && - args[0].is_a?( String ) && - args[0] =~ /^[${}0-9a-zA-Z._-]+(:[${}0-9a-zA-Z._-]+)+$/ ) || - ( args.size == 1 && args[0].is_a?( Hash ) ) - mm = method.to_s - case mm[ (mm.size - 1)..-1 ] + begin + # if ( args.size > 0 && + # args[0].is_a?( String ) && + # args[0] =~ /^[${}0-9a-zA-Z._-]+(:[${}0-9a-zA-Z._-]+)+$/ ) || + # ( args.size == 1 && args[0].is_a?( Hash ) ) + case method.to_s[ -1 ] when '?' - dependency?( method.to_s[0..-2].to_sym, *args ) + dependency?( method.to_s[0..-2].to_sym, *args, &block ) when '!' - dependency!( method.to_s[0..-2].to_sym, *args ) + dependency!( method.to_s[0..-2].to_sym, *args, &block ) else - dependency( method, *args ) + dependency( method, *args, &block ) end # elsif @current.respond_to? method # @current.send( method, *args ) # @current - else +# else + rescue => e p @context p m p args + raise e end end else super end @@ -1051,28 +1209,31 @@ receiver.configuration = options end private - def do_repository( method, url = nil, options = {}, block = nil ) + def do_repository( method, *args, &block ) + args, options = args_and_options( *args ) if @current.respond_to?( method ) r = DeploymentRepository.new else r = Repository.new c = options.delete( :snapshots ) c = options.delete( 'snapshots' ) if c.nil? unless c.nil? - r.snapshots = repository_policy( c ) + r.snapshots = repository_policy( r.snapshots, c ) end c = options.delete( :releases ) c = options.delete( 'releases' ) if c.nil? unless c.nil? - r.releases = repository_policy( c ) + r.releases = repository_policy( r.releases, c ) end end - nested_block( :repository, r, block ) if block - options.merge!( :url => url ) + r.id = args[ 0 ] + r.url = args[ 1 ] + r.name = args[ 2 ] fill_options( r, options ) + nested_block( :repository, r, block ) if block case method when :plugin @current.plugin_repositories << r else if @current.respond_to?( method )