#-*- mode: ruby -*-

properties( 'tesla.dump.pom' => 'pom.xml',
            'tesla.dump.readOnly' => true,
            'jruby.version' => '@project.version@',
            'jruby.plugins.version' => '1.0.10' )

gemfile

gem 'bundler', '1.7.7'

pom 'org.jruby:jruby', '${jruby.version}'

jar 'de.saumya.mojo:jruby-mains', '0.3.1'

build do
  directory 'pkg'
end

files = [ '.rspec', '*.rb', 'config.ru', '*file', '*file.lock', '.jbundler/classpath.rb',
          'lib/**', 'app/**', 'config/**', 'vendor/**', 'spec/**' ]
jruby_plugin!( :gem,
               # need a jruby-complete from maven central here
               # TODO fix plugin to use the one from compile-artifacts
               :jrubyVersion => '1.7.19',
               :includeRubygemsInResources => true ) do
  execute_goals( 'generate-resources', 'process-resources', :includeBinStubs => true, :includeRubyResources => files )
end

plugin :jar, '2.4', :outputDirectory => '.', :finalName => 'runnable' do
  execute_goal :jar, :phase => :'prepare-package'
end
plugin :clean, '2.4', :filesets => [ { :directory => '.', :includes => ['runnable.jar', '*/**/*.jar'] } ]

if File.file?('Jarfile.lock')
  phase 'generate-resources' do
    plugin :dependency do
      items = []
      File.read( 'Jarfile.lock' ).each_line do |l|
        data = l.sub(/-\ /, '').strip.split(':')
        if data.size > 3
          data = Hash[ [:groupId, :artifactId, :type, :version, :classifier].zip( data ) ]
          data[ :outputDirectory ] = File.join( '${project.build.outputDirectory}/jars',
                                                data[:groupId].gsub(/[.]/, '/'),
                                                data[:artifactId],
                                                data[:version] )
          items << data
        end
      end
      execute_goal( :copy,
                    :id => 'copy jar dependencies',
                    :artifactItems => items )
    end
  end
end

plugin :shade do
  execute_goals( 'shade',
                 :id => 'pack',
                 :artifactSet => { :excludes => ['rubygems:*'] },
                 :transformers => [ { :@implementation => 'org.apache.maven.plugins.shade.resource.ManifestResourceTransformer',
                                      :mainClass => 'de.saumya.mojo.mains.JarMain' } ] )
end

# test bits

phase :package do
  plugin( 'org.codehaus.mojo:exec-maven-plugin:1.2',
          :executable => 'java',
          :environmentVariables => {
            'HOME' => '${basedir}',
            'GEM_PATH' => '${basedir}',
            'GEM_HOME' => '${basedir}'
          } ) do
    
    execute_goal( :exec, :id => 'rake -T',
                  :arguments => [ '-jar', 'runnable.jar', '-S', 'rake', '-T' ] )
    
    execute_goal( :exec, :id => 'rake spec',
                  :arguments => [ '-jar', 'runnable.jar', '-S', 'rake', 'spec' ] )

    execute_goal( :exec, :id => 'rspec',
                  :arguments => [ '-jar', 'runnable.jar', '-S', 'rspec' ] )
    
    execute_goal( :exec, :id => 'nested IsolatedScriptingContainer',
                  :arguments => [ '-jar', 'runnable.jar', 'nested.rb' ] )

  end
end
 
