Class: Maven::Tools::Artifact

Inherits:
Hash
  • Object
show all
Defined in:
lib/maven/tools/artifact.rb

Direct Known Subclasses

DSL::DependencyDSL

Defined Under Namespace

Classes: Helper

Constant Summary collapse

ATTRS =
:type=, :group_id=, :artifact_id=, :version=, :classifier=, :exclusions=, :scope=

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_id, artifact_id, type, version = nil, classifier = nil, exclusions = nil, options = {}) ⇒ Artifact

Returns a new instance of Artifact.



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
# File 'lib/maven/tools/artifact.rb', line 124

def initialize( group_id, artifact_id, type,  
                version = nil, classifier = nil, exclusions = nil,
                options = {} )
  if exclusions.nil?
    if version.nil? and !classifier.nil?
      version = classifier
      classifier = nil
    elsif classifier.is_a?( Array )
      exclusions = classifier#version
      #version = classifier
      classifier = nil
    end
  end
  self[ :type ] = type
  self[ :group_id ] = group_id
  self[ :artifact_id ] = artifact_id
  self[ :version ] = version
  self[ :classifier ] = classifier if classifier
  self[ :exclusions ] = exclusions if exclusions
  if options
    self[ :group_id ] ||= options[ :group_id ]
    self[ :artifact_id ] ||= options[ :artifact_id ]
    self[ :version ] ||= options[ :version ]
    self[ :classifier ] ||= options[ :classifier ] if options[ :classifier ] 
    self[ :exclusions ] ||= prepare( options[ :exclusions ] ) if options[ :exclusions ]
    options.delete( :group_id )
    options.delete( :artifact_id )
    options.delete( :version )
    options.delete( :classifier )
    options.delete( :exclusions )
    options.delete( :scope ) if options[ :scope ] == :compile
    self.merge!( options )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, arg = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/maven/tools/artifact.rb', line 176

def method_missing( m, arg = nil )
  if ATTRS.member? m
    # setter
    self[ m.to_s[ 0..-2].to_sym ] = arg
  elsif ATTRS.member?( "#{m}=".to_sym )
    if arg.nil?
      # getter
      self[ m ]
    else
      # setter
      self[ m ] = arg
    end
  else
    super
  end
end

Class Method Details

.from(type, *args) ⇒ Object



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
# File 'lib/maven/tools/artifact.rb', line 48

def self.from( type, *args )
  if args.last.is_a? Hash
    options = args.last.dup
    args = args[0..-2]
  end
  helper = Helper.new
  case args.size
  when 1
    # jar "asd:Asd:123
    # jar "asd:Asd:123:test"
    # jar "asd:Asd:123:[dsa:rew,fe:fer]"
    # jar "asd:Asd:123:test:[dsa:rew,fe:fer]"
    group_id, artifact_id, version, classifier, exclusions = args[0].split( /:/ )
    self.new( group_id, artifact_id, type,
              version, classifier, exclusions,
              options )
  when 2
    # jar "asd:Asd", 123
    # jar "asd:Asd:test", 123
    # jar "asd:Asd:[dsa:rew,fe:fer]", 123
    # jar "asd:Asd:test:[dsa:rew,fe:fer]", 123
    group_id, artifact_id, classifier, exclusions = args[0].split( /:/ )
    self.new( group_id, artifact_id, type,
              helper.to_version( args[ 1 ] ),
              classifier, exclusions,
              options )
  when 3
    # jar "asd:Asd",'>123', '<345'
    # jar "asd:Asd:test",'>123', '<345'
    # jar "asd:Asd:[dsa:rew,fe:fer]",'>123', '<345'
    # jar "asd:Asd:test:[dsa:rew,fe:fer]",'>123', '<345'
    # jar "asd:Asd:test:[dsa:rew,fe:fer]", '123', 'source'
    if args[ 0 ].match /:/
      v = helper.to_version( *args[1..-1] )         
      case v
      when String
        group_id, artifact_id, classifier, exclusions = args[0].split( /:/ )
        self.new( group_id, artifact_id, type,
                  v, classifier, exclusions,
                  options )
      else
        group_id, artifact_id = args[0].split( /:/ )
        self.new( group_id, artifact_id, type,
                  args[1], args[2], nil,
                  options )
      end
    else
      self.new( args[ 0 ], args[ 1 ], type,
                args[ 2 ], nil, nil,
                options )            
    end
  else
    nil
  end
end

.from_coordinate(coord) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/maven/tools/artifact.rb', line 104

def self.from_coordinate( coord )
  exclusions = nil
  coord.sub!(/:\[([^:]+:[^:]+)+\]/) do |s|
    exclusions = s[1..-1]
    ''
  end
  args = coord.split( /:/ )
  # maven coordinates differ :(
  if args.size == 5
    classifier = args[ 4 ]
    args[ 4 ] = args[ 3 ]
    args[ 3 ] = classifier
  end
  if exclusions
    args[ 4 ] ||= nil
    args << exclusions
  end
  new( *args )
end

.new_local(path, type, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/maven/tools/artifact.rb', line 34

def self.new_local( path, type, options = {} )
  name = ::File.basename( path ).sub( /.#{type}$/, '' )
  if ind = name.rindex( '-' )
    version = name[ind + 1..-1]
    name = name[0..ind - 1]
  else
    version = '0'
  end
  self.new( "ruby.maven-tools.#{type}", name, type,
            nil, version, nil,
            options.merge( { :system_path => path,
                             :scope => :system } ) )
end

Instance Method Details

#exclusionsObject



204
205
206
207
208
# File 'lib/maven/tools/artifact.rb', line 204

def exclusions
  if key?( :exclusions )
    self[:exclusions].inspect.gsub( /[\[\]" ]/, '' ).split( /,/ )
  end
end

#gavObject



196
197
198
# File 'lib/maven/tools/artifact.rb', line 196

def gav
  [ self[:group_id], self[:artifact_id], self[:version], self[:classifier] ].select { |o| o }.join( ':' )
end

#keyObject



200
201
202
# File 'lib/maven/tools/artifact.rb', line 200

def key
  @key ||= [ self[:group_id], self[:artifact_id], self[:classifier] ].select { |o| o }.join( ':' )
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/maven/tools/artifact.rb', line 192

def respond_to?( m )
  ATTRS.member? m
end

#to_coordinateObject



210
211
212
# File 'lib/maven/tools/artifact.rb', line 210

def to_coordinate
  [ self[:group_id], self[:artifact_id], self[:type], self[:classifier], self[:version] ].select { |o| o }.join( ':' )
end

#to_sObject



214
215
216
# File 'lib/maven/tools/artifact.rb', line 214

def to_s
  [ self[:group_id], self[:artifact_id], self[:type], self[:classifier], self[:version], key?( :exclusions )? self[:exclusions].inspect.gsub( /[" ]/, '' ) : nil ].select { |o| o }.join( ':' )
end