maven-tools 0.29.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,343 @@
1
+ module Maven
2
+ module Model
3
+ class Tag
4
+
5
+ def self.prepend_tags(*tags)
6
+ _tags(true, *tags)
7
+ end
8
+
9
+ def self.tags(*tags)
10
+ _tags(false, *tags)
11
+ end
12
+
13
+ def self._tags(prepend, *tags)
14
+ if tags.size == 0
15
+ @tags
16
+ else
17
+ #self.send :attr_accessor, *tags
18
+ tags.each do |tag|
19
+ eval <<-EOF
20
+ def #{tag.to_s}(val = nil)
21
+ @#{tag.to_s} = val if val
22
+ @#{tag.to_s}
23
+ end
24
+ def #{tag.to_s}=(val)
25
+ @#{tag.to_s} = val
26
+ end
27
+ EOF
28
+ end
29
+ if self.superclass.respond_to?:tags
30
+ @tags ||= (self.superclass.tags || []).dup
31
+ else
32
+ @tags ||= []
33
+ end
34
+ unless @tags.include? tags[0]
35
+ if prepend
36
+ @tags.replace([tags, @tags].flatten)
37
+ else
38
+ @tags.replace([@tags, tags].flatten)
39
+ end
40
+ end
41
+ @tags
42
+ end
43
+ end
44
+
45
+ def _name
46
+ self.class.to_s.downcase.sub(/.*::/, '')
47
+ end
48
+
49
+ def initialize(args = {})
50
+ warn "deprecated #{args.inspect}" if args.size > 0
51
+ args.each do |k,v|
52
+ send("#{k}=".to_sym, v)
53
+ end
54
+ end
55
+
56
+ def comment(c)
57
+ @comment = c if c
58
+ @comment
59
+ end
60
+
61
+ def to_xml(buf = "", indent = "")
62
+ buf << "#{indent}<#{_name}>\n"
63
+ buf << "#{indent}<!--\n#{indent}#{@comment}\n#{indent}-->\n" if @comment
64
+ self.class.tags.each do |var|
65
+ val = instance_variable_get("@#{var}".to_sym)
66
+ var = var.to_s.gsub(/_(.)/) { $1.upcase }
67
+ case val
68
+ when Array
69
+ val.flatten!
70
+ if val.size > 0
71
+ buf << "#{indent} <#{var}>\n"
72
+ val.each do |v|
73
+ if v.is_a? Tag
74
+ v.to_xml(buf, indent + " ")
75
+ else
76
+ buf << "#{indent} <#{var.to_s.sub(/s$/, '')}>#{v}</#{var.to_s.sub(/s$/, '')}>\n"
77
+ end
78
+ end
79
+ buf << "#{indent} </#{var}>\n"
80
+ end
81
+ when Hash
82
+ if val.size > 0
83
+ buf << "#{indent} <#{var}>\n"
84
+ val.keys.each do |k|
85
+ v = val[k]
86
+ if v.is_a? Tag
87
+ v.to_xml(buf, indent + " ")
88
+ else
89
+ buf << "#{indent} <#{k}>#{v}</#{k}>\n"
90
+ end
91
+ end
92
+ buf << "#{indent} </#{var}>\n"
93
+ end
94
+ when Tag
95
+ val.to_xml(buf, indent + " ")
96
+ else
97
+ #when String
98
+ buf << "#{indent} <#{var}>#{val}</#{var}>\n" if val
99
+ end
100
+ end
101
+ buf << "#{indent}</#{_name.sub(/ .*$/, '')}>\n"
102
+ buf
103
+ end
104
+ end
105
+
106
+ class NamedArray < Array
107
+ attr_reader :name
108
+ def initialize(name, &block)
109
+ @name = name.to_s
110
+ if block
111
+ block.call(self)
112
+ end
113
+ self
114
+ end
115
+ end
116
+
117
+ class ModelHash < Hash
118
+
119
+ def initialize(clazz)
120
+ @clazz = clazz
121
+ end
122
+
123
+ def keys
124
+ @keys ||= []
125
+ end
126
+
127
+ def get(key, &block)
128
+ key = key.to_sym if key
129
+ result = self[key]
130
+ if result.nil?
131
+ keys << key
132
+ result = (self[key] = @clazz.new(key))
133
+ end
134
+ if block
135
+ block.call(result)
136
+ end
137
+ result
138
+ end
139
+ alias :new :get
140
+ alias :add :get
141
+
142
+ def default_model
143
+ @default_model ||=
144
+ begin
145
+ model = @clazz.new
146
+ self[nil] = model
147
+ model
148
+ end
149
+ end
150
+
151
+ def method_missing(method, *args, &block)
152
+ default_model.send(method, *args, &block)
153
+ end
154
+ end
155
+
156
+ class DeveloperHash < Hash
157
+
158
+ def keys
159
+ @keys ||= []
160
+ end
161
+
162
+ def get(*args, &block)
163
+ developer = if args.size == 1 && args[0].is_a?(Developer)
164
+ args[0]
165
+ else
166
+ Developer.new(*args)
167
+ end
168
+ keys << developer.id unless keys.member? developer.id
169
+ self[developer.id] = developer
170
+ if block
171
+ block.call(developer)
172
+ end
173
+ developer
174
+ end
175
+ alias :new :get
176
+ alias :add :get
177
+ end
178
+
179
+ class LicenseHash < Hash
180
+
181
+ def keys
182
+ @keys ||= []
183
+ end
184
+
185
+ def get(*args, &block)
186
+ license = if args.size == 1 && args[0].is_a?(License)
187
+ args[0]
188
+ else
189
+ License.new(*args)
190
+ end
191
+ keys << license.name unless keys.member? license.name
192
+ self[license.name] = license
193
+ if block
194
+ block.call(license)
195
+ end
196
+ license
197
+ end
198
+ alias :new :get
199
+ alias :add :get
200
+ end
201
+
202
+ class PluginHash < Hash
203
+
204
+ def adjust_key(name)
205
+ name = name.to_s
206
+ if (name =~ /\:/).nil?
207
+ if [:jruby, :gem, :rspec, :rake, :minitest, :rails3, :gemify, :cucumber, :runit, :bundler].member? name.to_sym
208
+ "de.saumya.mojo:#{name}-maven-plugin"
209
+ else
210
+ "maven-#{name}-plugin"
211
+ end
212
+ else
213
+ name
214
+ end
215
+ end
216
+
217
+ def key?(k)
218
+ super( adjust_key(k).to_sym )
219
+ end
220
+
221
+ def keys
222
+ @keys ||= []
223
+ end
224
+
225
+ def get(*args, &block)
226
+ case args.size
227
+ when 3
228
+ name = "#{args[0]}:#{args[1]}"
229
+ version = args[2]
230
+ when 2
231
+ name = args[0].to_s
232
+ version = args[1]
233
+ when 1
234
+ name = args[0].to_s
235
+ else
236
+ raise "need name"
237
+ end
238
+
239
+ name = adjust_key(name)
240
+ group_id = name =~ /\:/ ? name.sub(/:.+$/, '') : nil
241
+ artifact_id = name.sub(/^.+:/, '')
242
+
243
+ k = "#{group_id}:#{artifact_id}".to_sym
244
+ result = self[k]
245
+ if result.nil?
246
+ keys << k
247
+ result = (self[k] = Plugin.new(group_id, artifact_id, version))
248
+ end
249
+ result.version = version if version
250
+ if block
251
+ block.call(result)
252
+ end
253
+ result
254
+ end
255
+ alias :new :get
256
+ alias :add :get
257
+
258
+ end
259
+
260
+ class ListItems < Tag
261
+
262
+ def initialize(name = nil)
263
+ @name = name
264
+ end
265
+
266
+ def add(item)
267
+ @items ||= Array.new
268
+ @items << item
269
+ end
270
+ alias :<< :add
271
+
272
+ def to_xml(buf = "", indent = "")
273
+ buf << "#{indent}<#{@name}>\n" if @name
274
+ buf << "#{indent}<!--\n#{indent}#{@comment}\n#{indent}-->\n" if @comment
275
+ @items.each do |i|
276
+ i.to_xml(buf, indent)
277
+ end
278
+ buf << "#{indent}</#{@name}>\n" if @name
279
+ end
280
+
281
+ end
282
+
283
+ class HashTag < Tag
284
+
285
+ def initialize(name, args = {})
286
+ @name = name
287
+ @props = args
288
+ end
289
+
290
+ def [](key)
291
+ @props ||= {}
292
+ @props[key]
293
+ end
294
+
295
+ def []=(key, value)
296
+ @props ||= {}
297
+ @props[key] = value
298
+ end
299
+
300
+ def to_xml(buf = "", indent = "")
301
+ buf << "#{indent}<#{@name}>\n"
302
+ buf << "#{indent}<!--\n#{indent}#{@comment}\n#{indent}-->\n" if @comment
303
+ map_to_xml(buf, indent, @props)
304
+ buf << "#{indent}</#{@name}>\n"
305
+ end
306
+
307
+ def map_to_xml(buf, indent, map)
308
+ # sort the hash over the keys
309
+ map.collect { |k,v| [k.to_s, v]}.sort.each do |k,v|
310
+ case v
311
+ when Hash
312
+ buf << "#{indent} <#{k}>\n"
313
+ map_to_xml(buf, indent + " ", v)
314
+ buf << "#{indent} </#{k}>\n"
315
+ when NamedArray
316
+ buf << "#{indent} <#{k}>\n"
317
+ v.each do|i|
318
+ buf << "#{indent} <#{v.name}>\n"
319
+ case i
320
+ when Hash
321
+ map_to_xml(buf, indent + " ", i)
322
+ end
323
+ buf << "#{indent} </#{v.name}>\n"
324
+ end
325
+ buf << "#{indent} </#{k}>\n"
326
+ when Array
327
+ buf << "#{indent} <#{k}>\n"
328
+ singular = k.to_s.sub(/s$/, '')
329
+ v.each do |i|
330
+ buf << "#{indent} <#{singular}>#{i}</#{singular}>\n"
331
+ end
332
+ buf << "#{indent} </#{k}>\n"
333
+ when /\n$/
334
+ buf << "#{indent} <#{k}>#{v}"
335
+ buf << "#{indent} </#{k}>\n"
336
+ else
337
+ buf << "#{indent} <#{k}>#{v}</#{k}>\n"
338
+ end
339
+ end
340
+ end
341
+ end
342
+ end
343
+ end
@@ -0,0 +1,322 @@
1
+ module Maven
2
+ module Model
3
+ class Tag
4
+
5
+ def self.prepend_tags(*tags)
6
+ _tags(true, *tags)
7
+ end
8
+
9
+ def self.tags(*tags)
10
+ _tags(false, *tags)
11
+ end
12
+
13
+ def self._tags(prepend, *tags)
14
+ if tags.size == 0
15
+ @tags
16
+ else
17
+ #self.send :attr_accessor, *tags
18
+ tags.each do |tag|
19
+ eval <<-EOF
20
+ def #{tag.to_s}(val = nil)
21
+ @#{tag.to_s} = val if val
22
+ @#{tag.to_s}
23
+ end
24
+ def #{tag.to_s}=(val)
25
+ @#{tag.to_s} = val
26
+ end
27
+ EOF
28
+ end
29
+ if self.superclass.respond_to?:tags
30
+ @tags ||= (self.superclass.tags || []).dup
31
+ else
32
+ @tags ||= []
33
+ end
34
+ unless @tags.include? tags[0]
35
+ if prepend
36
+ @tags.replace([tags, @tags].flatten)
37
+ else
38
+ @tags.replace([@tags, tags].flatten)
39
+ end
40
+ end
41
+ @tags
42
+ end
43
+ end
44
+
45
+ def _name
46
+ self.class.to_s.downcase.sub(/.*::/, '')
47
+ end
48
+
49
+ def initialize(args = {})
50
+ warn "deprecated #{args.inspect}" if args.size > 0
51
+ args.each do |k,v|
52
+ send("#{k}=".to_sym, v)
53
+ end
54
+ end
55
+
56
+ def comment(c)
57
+ @comment = c if c
58
+ @comment
59
+ end
60
+
61
+ def to_xml(buf = "", indent = "")
62
+ buf << "#{indent}<#{_name}>\n"
63
+ buf << "#{indent}<!--\n#{indent}#{@comment}\n#{indent}-->\n" if @comment
64
+ self.class.tags.each do |var|
65
+ val = instance_variable_get("@#{var}".to_sym)
66
+ var = var.to_s.gsub(/_(.)/) { $1.upcase }
67
+ case val
68
+ when Array
69
+ val.flatten!
70
+ if val.size > 0
71
+ buf << "#{indent} <#{var}>\n"
72
+ val.each do |v|
73
+ if v.is_a? Tag
74
+ v.to_xml(buf, indent + " ")
75
+ else
76
+ buf << "#{indent} <#{var.to_s.sub(/s$/, '')}>#{v}</#{var.to_s.sub(/s$/, '')}>\n"
77
+ end
78
+ end
79
+ buf << "#{indent} </#{var}>\n"
80
+ end
81
+ when Hash
82
+ if val.size > 0
83
+ buf << "#{indent} <#{var}>\n"
84
+ val.each do |k, v|
85
+ if v.is_a? Tag
86
+ v.to_xml(buf, indent + " ")
87
+ else
88
+ buf << "#{indent} <#{k}>#{v}</#{k}>\n"
89
+ end
90
+ end
91
+ buf << "#{indent} </#{var}>\n"
92
+ end
93
+ when Tag
94
+ val.to_xml(buf, indent + " ")
95
+ else
96
+ #when String
97
+ buf << "#{indent} <#{var}>#{val}</#{var}>\n" if val
98
+ end
99
+ end
100
+ buf << "#{indent}</#{_name.sub(/ .*$/, '')}>\n"
101
+ buf
102
+ end
103
+ end
104
+
105
+ class NamedArray < Array
106
+ attr_reader :name
107
+ def initialize(name, &block)
108
+ @name = name.to_s
109
+ if block
110
+ block.call(self)
111
+ end
112
+ self
113
+ end
114
+ end
115
+
116
+ class ModelHash < Hash
117
+
118
+ def initialize(clazz)
119
+ @clazz = clazz
120
+ end
121
+
122
+ def get(key, &block)
123
+ key = key.to_sym if key
124
+ result = self[key]
125
+ if result.nil?
126
+ result = (self[key] = @clazz.new(key))
127
+ end
128
+ if block
129
+ block.call(result)
130
+ end
131
+ result
132
+ end
133
+ alias :new :get
134
+ alias :add :get
135
+
136
+ def default_model
137
+ @default_model ||=
138
+ begin
139
+ model = @clazz.new
140
+ self[nil] = model
141
+ model
142
+ end
143
+ end
144
+
145
+ def method_missing(method, *args, &block)
146
+ default_model.send(method, *args, &block)
147
+ end
148
+ end
149
+
150
+ class DeveloperHash < Hash
151
+
152
+ def get(*args, &block)
153
+ developer = if args.size == 1 && args[0].is_a?(Developer)
154
+ args[0]
155
+ else
156
+ Developer.new(*args)
157
+ end
158
+ self[developer.id] = developer
159
+ if block
160
+ block.call(developer)
161
+ end
162
+ developer
163
+ end
164
+ alias :new :get
165
+ alias :add :get
166
+ end
167
+
168
+ class LicenseHash < Hash
169
+
170
+ def get(*args, &block)
171
+ license = if args.size == 1 && args[0].is_a?(License)
172
+ args[0]
173
+ else
174
+ License.new(*args)
175
+ end
176
+ self[license.name] = license
177
+ if block
178
+ block.call(license)
179
+ end
180
+ license
181
+ end
182
+ alias :new :get
183
+ alias :add :get
184
+ end
185
+
186
+ class PluginHash < Hash
187
+
188
+ def adjust_key(name)
189
+ name = name.to_s
190
+ if (name =~ /\:/).nil?
191
+ if [:jruby, :gem, :rspec, :rake, :minitest, :rails3, :gemify, :cucumber, :runit, :bundler].member? name.to_sym
192
+ "de.saumya.mojo:#{name}-maven-plugin"
193
+ else
194
+ "maven-#{name}-plugin"
195
+ end
196
+ else
197
+ name
198
+ end
199
+ end
200
+
201
+ def key?(k)
202
+ super( adjust_key(k).to_sym )
203
+ end
204
+
205
+ def get(*args, &block)
206
+ case args.size
207
+ when 3
208
+ name = "#{args[0]}:#{args[1]}"
209
+ version = args[2]
210
+ when 2
211
+ name = args[0].to_s
212
+ version = args[1]
213
+ when 1
214
+ name = args[0].to_s
215
+ else
216
+ raise "need name"
217
+ end
218
+
219
+ name = adjust_key(name)
220
+ group_id = name =~ /\:/ ? name.sub(/:.+$/, '') : nil
221
+ artifact_id = name.sub(/^.+:/, '')
222
+
223
+ k = "#{group_id}:#{artifact_id}".to_sym
224
+ result = self[k]
225
+ if result.nil?
226
+ result = (self[k] = Plugin.new(group_id, artifact_id, version))
227
+ end
228
+ result.version = version if version
229
+ if block
230
+ block.call(result)
231
+ end
232
+ result
233
+ end
234
+ alias :new :get
235
+ alias :add :get
236
+
237
+ end
238
+
239
+ class ListItems < Tag
240
+
241
+ def initialize(name = nil)
242
+ @name = name
243
+ end
244
+
245
+ def add(item)
246
+ @items ||= Array.new
247
+ @items << item
248
+ end
249
+ alias :<< :add
250
+
251
+ def to_xml(buf = "", indent = "")
252
+ buf << "#{indent}<#{@name}>\n" if @name
253
+ buf << "#{indent}<!--\n#{indent}#{@comment}\n#{indent}-->\n" if @comment
254
+ @items.each do |i|
255
+ i.to_xml(buf, indent)
256
+ end
257
+ buf << "#{indent}</#{@name}>\n" if @name
258
+ end
259
+
260
+ end
261
+
262
+ class HashTag < Tag
263
+
264
+ def initialize(name, args = {})
265
+ @name = name
266
+ @props = args
267
+ end
268
+
269
+ def [](key)
270
+ @props ||= {}
271
+ @props[key]
272
+ end
273
+
274
+ def []=(key, value)
275
+ @props ||= {}
276
+ @props[key] = value
277
+ end
278
+
279
+ def to_xml(buf = "", indent = "")
280
+ buf << "#{indent}<#{@name}>\n"
281
+ buf << "#{indent}<!--\n#{indent}#{@comment}\n#{indent}-->\n" if @comment
282
+ map_to_xml(buf, indent, @props)
283
+ buf << "#{indent}</#{@name}>\n"
284
+ end
285
+
286
+ def map_to_xml(buf, indent, map)
287
+ # sort the hash over the keys
288
+ map.collect { |k,v| [k.to_s, v]}.sort.each do |k,v|
289
+ case v
290
+ when Hash
291
+ buf << "#{indent} <#{k}>\n"
292
+ map_to_xml(buf, indent + " ", v)
293
+ buf << "#{indent} </#{k}>\n"
294
+ when NamedArray
295
+ buf << "#{indent} <#{k}>\n"
296
+ v.each do|i|
297
+ buf << "#{indent} <#{v.name}>\n"
298
+ case i
299
+ when Hash
300
+ map_to_xml(buf, indent + " ", i)
301
+ end
302
+ buf << "#{indent} </#{v.name}>\n"
303
+ end
304
+ buf << "#{indent} </#{k}>\n"
305
+ when Array
306
+ buf << "#{indent} <#{k}>\n"
307
+ singular = k.to_s.sub(/s$/, '')
308
+ v.each do |i|
309
+ buf << "#{indent} <#{singular}>#{i}</#{singular}>\n"
310
+ end
311
+ buf << "#{indent} </#{k}>\n"
312
+ when /\n$/
313
+ buf << "#{indent} <#{k}>#{v}"
314
+ buf << "#{indent} </#{k}>\n"
315
+ else
316
+ buf << "#{indent} <#{k}>#{v}</#{k}>\n"
317
+ end
318
+ end
319
+ end
320
+ end
321
+ end
322
+ end