File: gemspec_artifacts.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 (222 lines) | stat: -rw-r--r-- 5,670 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
module Jars

  class MavenVersion < String
    def self.new(*args)
      if args.size == 0 || (args.size == 1 && args[0].nil?)
        nil
      else
        low, high = convert(args[0])
        low, high = convert(args[1], low, high) if args[1] =~ /[=~><]/
        if low == high
          low
        else
          super "#{low || '[0'},#{high || ')'}"
        end
      end
    end

    private

    def self.convert(arg, low = nil, high = nil)
      if arg =~ /~>/
        val = arg.sub(/~>\s*/, '')
        last = val=~/\./ ? val.sub(/\.[0-9]*[a-z]+.*$/, '').sub(/\.[^.]+$/, '.99999') : '99999'
        ["[#{snapshot_version(val)}", "#{snapshot_version(last)}]"]
      elsif arg =~ />=/
        val = arg.sub(/>=\s*/, '')
        ["[#{snapshot_version(val)}", (nil || high)]
      elsif arg =~ /<=/
        val = arg.sub(/<=\s*/, '')
        [(nil || low), "#{snapshot_version(val)}]"]
      # treat '!' the same way as '>' since maven can not describe such range
      elsif arg =~ /[!>]/  
        val = arg.sub(/[!>]\s*/, '')
        ["(#{snapshot_version(val)}", (nil || high)]
      elsif arg =~ /</
        val = arg.sub(/<\s*/, '')
        [(nil || low), "#{snapshot_version(val)})"]
      elsif arg =~ /\=/
        val = arg.sub(/=\s*/, '')
        # for prereleased version pick the maven version (no version range)
        if val.match( /[a-z]|[A-Z]/ )
          [ val, val ]
        else
          ["[#{val}", "#{val}.0.0.0.0.1)"]
        end
      else
        # no conversion here, i.e. assume maven version
        [arg, arg]
      end
    end

    def self.snapshot_version( val )
      if val.match(/[a-z]|[A-Z]/) && !val.match(/-SNAPSHOT|[${}]/)
        val + '-SNAPSHOT'
      else
        val
      end
    end
  end

  class GemspecArtifacts

    class Exclusion
      attr_reader :group_id, :artifact_id

      def initialize(line)
        @group_id, @artifact_id = line.gsub(/['"]/, '').strip.split( ':' )
        @artifact_id.strip!
      end

      def to_s
        "#{@group_id}:#{@artifact_id}"
      end
    end
    
    class Exclusions < Array

      def to_s
        "[#{join(', ')}]"
      end

      def initialize( line )
        super()
        line.gsub(/'"|^\s*\[|\]\s*$/, '').split( /,\s*/ ).each do |exclusion|
          self.<< Exclusion.new( exclusion )
        end
        freeze
      end
    end

    class Artifact
 
      attr_reader :type, :group_id, :artifact_id, :classifier, :version, :scope, :exclusions

      ALLOWED_TYPES = ['jar', 'pom']
      
      def initialize( options, *args )
        @type, @group_id, @artifact_id, @classifier, @version, @exclusions = *args
        options.each do |k,v|
          instance_variable_set( "@#{k}", v )
        end
      end

      def self.new( line )
        line = line.strip
        index = line.index( /\s/ )
        if index.nil?
          return nil
        end
        type = line[0..index].strip
        unless ALLOWED_TYPES.member?( type )
          return nil
        end
        line = line[index..-1]
        line.gsub!(/['"]/, '')
        line.strip!

        options = {}
        line.sub!(/,\s*:exclusions\s*(:|=>)\s*(\[[^\]]+\])/) do
          options[ :exclusions ] = Exclusions.new( $2.strip )
          ''
        end
        line.sub!(/,\s*:([a-z]+)\s*(:|=>)\s*(:?[a-zA-Z0-9_]+)/) do
          options[ $1.to_sym ] = $3.sub(/^:/, '')
          ''
        end
        exclusions = nil
        line.sub!(/[,:]\s*\[(.+:.+,?\s*)+\]$/) do |a|
          exclusions = Exclusions.new( a[1..-1].strip )
          ''
        end

        line.strip!
        line.gsub!(/,\s*/, ':')

        if line.match(/[\[\(\)\]]/)
          index = line.index(/[\[\(].+$/)
          version = line[index..-1].sub(/:/, ', ')
          line = line[0..index - 1].strip.sub(/:$/, '')
        else
          index = line.index(/[:][^:]+$/)
          version = line[index + 1..-1]
          line = line[0..index - 1].strip
        end

        case line.count(':')
        when 2
          group_id, artifact_id, classifier = line.split(':')
        when 1
          group_id, artifact_id = line.split(':')
          classifier = nil
        else
          warn line
          return nil
        end
        super( options, type, group_id, artifact_id, classifier, version, exclusions )
      end

      def to_s
        args = [@group_id, @artifact_id]
        args << @classifier if @classifier
        args << @version
        args << @exclusions.to_s if @exclusions
        "#{@type} #{group_id}:#{args[1..-1].join(', ')}"
      end

      def to_gacv
        args = [@group_id, @artifact_id]
        args << @classifier if @classifier
        args << @version
        args.join(':')
      end

      def to_coord_no_classifier
        args = [@group_id, @artifact_id]
        args << @type
        args << MavenVersion.new( @version )
        args.join(':')
      end

      def to_coord
        args = [@group_id, @artifact_id]
        args << @classifier if @classifier
        args << @type
        args << MavenVersion.new( @version )
        args.join(':')
      end

      def key
        args = [@group_id, @artifact_id]
        args << @classifier if @classifier
        args.join(':')
      end
    end

    attr_reader :artifacts

    def initialize( spec )
      @artifacts = []
      spec.requirements.each do |req|
        req.split( /\n/ ).each do |line|
          if ( a = Artifact.new( line ) )
            @artifacts << a
          end
        end
      end
      @artifacts.freeze
    end

    def [](index)
      @artifacts[index]
    end

    def each(&block)
      @artifacts.each(&block)
    end

    def size
      @artifacts.size
    end
  end
end