File: maven_settings.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 (154 lines) | stat: -rw-r--r-- 5,135 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'rubygems/request'
require 'rubygems/uri_formatter'
module Jars
  class MavenSettings

    LINE_SEPARATOR = ENV_JAVA['line.separator']

    class << self

      def local_settings
        unless instance_variable_defined?(:@_jars_maven_local_settings_)
          @_jars_maven_local_settings_ = nil
        end
        if @_jars_maven_local_settings_.nil?
          if settings = Jars.absolute( 'settings.xml' )
            if File.exists?(settings)
              @_jars_maven_local_settings_ = settings
            end
          end
        end
        @_jars_maven_local_settings_ || nil
      end

      def user_settings
        unless instance_variable_defined?(:@_jars_maven_user_settings_)
          @_jars_maven_user_settings_ = nil
        end
        if @_jars_maven_user_settings_.nil?
          if settings = Jars.absolute( Jars.to_prop( MAVEN_SETTINGS ) )
            unless File.exists?(settings)
              Jars.warn { "configured ENV['#{MAVEN_SETTINGS}'] = '#{settings}' not found" }
              settings = false
            end
          else # use maven default (user) settings
            settings = File.join( Jars.user_home, '.m2', 'settings.xml' )
            settings = false unless File.exists?(settings)
          end
          @_jars_maven_user_settings_ = settings
        end
        @_jars_maven_user_settings_ || nil
      end

      def effective_settings
        unless instance_variable_defined?(:@_jars_effective_maven_settings_)
          @_jars_effective_maven_settings_ = nil
        end
        if @_jars_effective_maven_settings_.nil?
          begin
            http = Gem::Request.proxy_uri(Gem.configuration[:http_proxy] || Gem::Request.get_proxy_from_env('http'))
            https = Gem::Request.proxy_uri(Gem.configuration[:https_proxy] || Gem::Request.get_proxy_from_env('https'))
          rescue NoMethodError
            Jars.debug('ignore rubygems proxy configuration as rubygems is too old')
          end
          if http.nil? && https.nil?
            @_jars_effective_maven_settings_ = settings
          else
            @_jars_effective_maven_settings_ =
              setup_interpolated_settings(http, https) || settings
          end
        end
        @_jars_effective_maven_settings_
      end

      def cleanup
        if effective_settings != settings
          File.unlink(effective_settings)
        end
      ensure
        reset
      end

      def reset
        instance_variables.each { |var| instance_variable_set(var, nil) }
      end

      def settings
        unless instance_variable_defined?(:@_jars_maven_settings_)
          @_jars_maven_settings_ = nil
        end
        if @_jars_maven_settings_.nil?
          local_settings || user_settings
        end
      end

      def global_settings
        unless instance_variable_defined?(:@_jars_maven_global_settings_)
          @_jars_maven_global_settings_ = nil
        end
        if @_jars_maven_global_settings_.nil?
          if mvn_home = ENV[ 'M2_HOME' ] || ENV[ 'MAVEN_HOME' ]
            settings = File.join( mvn_home, 'conf/settings.xml' )
            settings = false unless File.exists?(settings)
          else
            settings = false
          end
          @_jars_maven_global_settings_ = settings
        end
        @_jars_maven_global_settings_ || nil
      end

      def debian_settings
        unless instance_variable_defined?(:@_jars_maven_debian_settings_)
          @_jars_maven_debian_settings_ = nil
        end
        if @_jars_maven_debian_settings_.nil?
          settings = File.join( Jars.debian_mvn_home, 'conf/settings-debian.xml' )
          settings = false unless File.exists?(settings)
          @_jars_maven_debian_settings_ = settings
        end
        @_jars_maven_debian_settings_ || nil
      end

      private

      def setup_interpolated_settings(http, https)
        proxy = raw_proxy_settings_xml(http, https).gsub("\n", LINE_SEPARATOR)
        if settings.nil?
          raw = "<settings>#{LINE_SEPARATOR}#{proxy}</settings>"
        else
          raw = File.read(settings)
          if raw.include?('<proxy>')
            Jars.warn("can not interpolated proxy info for #{settings}")
            return
          else
            raw.sub!("<settings>", "<settings>#{LINE_SEPARATOR}#{proxy}")
          end
        end
        tempfile = java.io.File.create_temp_file('settings', '.xml')
        tempfile.delete_on_exit
        File.write(tempfile.path, raw)
        tempfile.path
      end

      def raw_proxy_settings_xml(http, https)
        raw = File.read(File.join(File.dirname(__FILE__), 'settings.xml'))
        if http
          raw.sub!('__HTTP_ACTIVE__', 'true')
          raw.sub!('__HTTP_SERVER__', http.host)
          raw.sub!('__HTTP_PORT__', http.port.to_s)
        else
          raw.sub!('__HTTP_ACTIVE__', 'false')
        end
        if https
          raw.sub!('__HTTPS_ACTIVE__', 'true')
          raw.sub!('__HTTPS_SERVER__', https.host)
          raw.sub!('__HTTPS_PORT__', https.port.to_s)
        else
          raw.sub!('__HTTPS_ACTIVE__', 'false')
        end
        raw
      end
    end
  end
end