File: maven_factory.rb

package info (click to toggle)
ruby-jar-dependencies 0.3.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: ruby: 1,325; xml: 14; makefile: 9
file content (140 lines) | stat: -rw-r--r-- 4,280 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'jar_dependencies'
require 'jars/gemspec_artifacts'

module Jars
  class MavenFactory

    module AttachJars

      def attach_jars( spec, all_dependencies = false )
        @index ||= 0
        @done ||= []

        deps = GemspecArtifacts.new( spec )
        deps.artifacts.each do |a|
          # for this gemspec we want to include all artifacts but
          # for all others we want to exclude provided and test artifacts
          if !@done.include?( a.key ) and (all_dependencies or (a.scope != 'provided' and a.scope != 'test'))

            # ruby dsl is not working reliably for classifier
            self[ "jars.#{@index}" ] = a.to_coord_no_classifier
            if a.exclusions
              jndex = 0
              a.exclusions.each do |ex|
                self[ "jars.#{@index}.exclusions.#{jndex}" ] = ex.to_s
                jndex += 1
              end
            end
            self[ "jars.#{@index}.scope" ] = a.scope if a.scope
            if a.classifier
              self[ "jars.#{@index}.classifier" ] = a.classifier
            end
            @index += 1
            @done << a.key
          end
        end
      end
    end

    attr_reader :debug, :verbose

    def initialize( options = nil,  debug = Jars.debug?, verbose = Jars.verbose? )
      @options = (options || {}).dup
      @options.delete( :ignore_dependencies )
      @debug = debug
      @verbose = verbose
      @installed_maven = false
    end

    def maven_new( pom )
      lazy_load_maven
      maven = setup( Maven::Ruby::Maven.new )

      maven.extend AttachJars
      # TODO copy pom to tmp dir in case it is not a real file
      maven.options[ '-f' ] = pom
      maven
    end

    private

    def setup( maven )
      maven.verbose = @verbose
      if @debug
        maven.options[ '-X' ] = nil
      end
      if @verbose
        maven.options[ '-e' ] = nil
      elsif not @debug
        maven.options[ '--quiet' ] = nil
      end
      maven[ 'verbose' ] = (@debug || @verbose) == true

      if Jars.maven_settings
        maven.options[ '-s' ] = Jars::MavenSettings.effective_settings
      end

      maven[ 'maven.repo.local' ] = "#{java.io.File.new( Jars.local_maven_repo ).absolute_path}"

      maven
    end

    private

    def lazy_load_maven
      add_gem_to_load_path( 'ruby-maven' )
      add_gem_to_load_path( 'ruby-maven-libs' )
      if @installed_maven
        puts
        puts 'using maven for the first time results in maven'
        puts 'downloading all its default plugin and can take time.'
        puts 'as those plugins get cached on disk and further execution'
        puts 'of maven is much faster then the first time.'
        puts
      end
      require 'maven/ruby/maven'
    end

    def find_spec_via_rubygems( name, req )
      require 'rubygems/dependency'
      dep = Gem::Dependency.new( name, req )
      dep.matching_specs( true ).last
    end

    def add_gem_to_load_path( name )
      # if the gem is already activated => good
      return if Gem.loaded_specs[ name ]
      # just install gem if needed and add it to the load_path
      # and leave activated gems as they are
      req = requirement( name )
      unless spec = find_spec_via_rubygems( name, req )
        spec = install_gem( name, req )
      end
      unless spec
        raise "failed to resolve gem '#{name}' if you're using Bundler add it as a dependency"
      end
      path = File.join( spec.full_gem_path, spec.require_path )
      $LOAD_PATH << path unless $LOAD_PATH.include?( path )
    end

    def requirement( name )
      jars = Gem.loaded_specs[ 'jar-dependencies' ]
      dep = jars.nil? ? nil : jars.dependencies.detect { |d| d.name == name }
      dep.nil? ? Gem::Requirement.create( '>0' ) : dep.requirement
    end

    def install_gem( name, req )
      @installed_maven = true
      puts "Installing gem '#{name}' . . ."
      require 'rubygems/dependency_installer'
      inst = Gem::DependencyInstaller.new( @options ||= {} )
      inst.install( name, req ).first
    rescue => e
      if Jars.verbose?
        warn "#{e.inspect}"
        warn e.backtrace.join( "\n" )
      end
      raise "there was an error installing '#{name} (#{req})' #{@options[:domain]}. please install it manually: #{e.inspect}"
    end
  end
end