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,498 @@
1
+ # TODO make nice require after ruby-maven uses the same ruby files
2
+ require File.join(File.dirname(File.dirname(__FILE__)), 'model', 'model.rb')
3
+ require File.join(File.dirname(__FILE__), 'gemfile_lock.rb')
4
+ require File.join(File.dirname(__FILE__), 'versions.rb')
5
+ require File.join(File.dirname(__FILE__), 'jarfile.rb')
6
+
7
+ module Maven
8
+ module Tools
9
+
10
+ class ArtifactPassthrough
11
+
12
+ def initialize(&block)
13
+ @block = block
14
+ end
15
+
16
+ def add_artifact(a)
17
+ @block.call(a)
18
+ end
19
+
20
+ def add_repository(name, url)
21
+ end
22
+ end
23
+
24
+ class GemProject < Maven::Model::Project
25
+ tags :dummy
26
+
27
+ def initialize(artifact_id = dir_name, version = "0.0.0", &block)
28
+ super("rubygems", artifact_id, version, &block)
29
+ packaging "gem"
30
+ end
31
+
32
+ def add_param(config, name, list, default = [])
33
+ if list.is_a? Array
34
+ config[name] = list.join(",").to_s unless (list || []) == default
35
+ else
36
+ # list == nil => (list || []) == default is true
37
+ config[name] = list.to_s unless (list || []) == default
38
+ end
39
+ end
40
+ private :add_param
41
+
42
+ def load_gemspec(specfile)
43
+ require 'rubygems'
44
+ if specfile.is_a? ::Gem::Specification
45
+ spec = specfile
46
+ else
47
+ spec = ::Gem::Specification.load(specfile)
48
+ @gemspec = specfile
49
+ end
50
+ raise "file not found '#{specfile}'" unless spec
51
+ @current_file = specfile
52
+ artifact_id spec.name
53
+ version spec.version
54
+ name spec.summary || "#{self.artifact_id} - gem"
55
+ description spec.description if spec.description
56
+ url spec.homepage if spec.homepage
57
+ (spec.email || []).zip(spec.authors || []).map do |email, author|
58
+ self.developers.new(author, email)
59
+ end
60
+
61
+ # TODO work with collection of licenses - there can be more than one !!!
62
+ (spec.licenses + spec.files.select {|file| file.to_s =~ /license|gpl/i }).each do |license|
63
+ # TODO make this better, i.e. detect the right license name from the file itself
64
+ self.licenses.new(license)
65
+ end
66
+
67
+ config = {}
68
+ if @gemspec
69
+ relative = File.expand_path(@gemspec).sub(/#{File.expand_path('.')}/, '').sub(/^\//, '')
70
+ add_param(config, "gemspec", relative)
71
+ end
72
+ add_param(config, "autorequire", spec.autorequire)
73
+ add_param(config, "defaultExecutable", spec.default_executable)
74
+ add_param(config, "testFiles", spec.test_files)
75
+ #has_rdoc always gives true => makes not sense to keep it then
76
+ #add_param(config, "hasRdoc", spec.has_rdoc)
77
+ add_param(config, "extraRdocFiles", spec.extra_rdoc_files)
78
+ add_param(config, "rdocOptions", spec.rdoc_options)
79
+ add_param(config, "requirePaths", spec.require_paths, ["lib"])
80
+ add_param(config, "rubyforgeProject", spec.rubyforge_project)
81
+ add_param(config, "requiredRubygemsVersion",
82
+ spec.required_rubygems_version && spec.required_rubygems_version != ">= 0" ? "<![CDATA[#{spec.required_rubygems_version}]]>" : nil)
83
+ add_param(config, "bindir", spec.bindir, "bin")
84
+ add_param(config, "requiredRubyVersion",
85
+ spec.required_ruby_version && spec.required_ruby_version != ">= 0" ? "<![CDATA[#{spec.required_ruby_version}]]>" : nil)
86
+ add_param(config, "postInstallMessage",
87
+ spec.post_install_message ? "<![CDATA[#{spec.post_install_message}]]>" : nil)
88
+ add_param(config, "executables", spec.executables)
89
+ add_param(config, "extensions", spec.extensions)
90
+ add_param(config, "platform", spec.platform, 'ruby')
91
+
92
+ # # TODO maybe calculate extra files
93
+ # files = spec.files.dup
94
+ # (Dir['lib/**/*'] + Dir['spec/**/*'] + Dir['features/**/*'] + Dir['test/**/*'] + spec.licenses + spec.extra_rdoc_files).each do |f|
95
+ # files.delete(f)
96
+ # if f =~ /^.\//
97
+ # files.delete(f.sub(/^.\//, ''))
98
+ # else
99
+ # files.delete("./#{f}")
100
+ # end
101
+ # end
102
+ #add_param(config, "extraFiles", files)
103
+ add_param(config, "files", spec.files)
104
+
105
+ plugin('gem').with(config) if config.size > 0
106
+
107
+ spec.dependencies.each do |dep|
108
+ scope =
109
+ case dep.type
110
+ when :runtime
111
+ "compile"
112
+ when :development
113
+ "test"
114
+ else
115
+ warn "unknown scope: #{dep.type}"
116
+ "compile"
117
+ end
118
+
119
+ versions = dep.requirement.requirements.collect do |req|
120
+ # use this construct to get the same result in 1.8.x and 1.9.x
121
+ req.collect{ |i| i.to_s }.join
122
+ end
123
+ gem(dep.name, versions).scope = scope
124
+ end
125
+
126
+ spec.requirements.each do |req|
127
+ begin
128
+ eval req
129
+ rescue => e
130
+ # TODO requirements is a list !!!
131
+ add_param(config, "requirements", req)
132
+ warn e
133
+ rescue SyntaxError => e
134
+ # TODO requirements is a list !!!
135
+ add_param(config, "requirements", req)
136
+ end
137
+ end
138
+ end
139
+
140
+ def load_mavenfile(file)
141
+ file = file.path if file.is_a?(File)
142
+ if File.exists? file
143
+ @current_file = file
144
+ content = File.read(file)
145
+ eval content
146
+ else
147
+ self
148
+ end
149
+ end
150
+
151
+ def load_gemfile(file)
152
+ file = file.path if file.is_a?(File)
153
+ if File.exists? file
154
+ @current_file = file
155
+ content = File.read(file)
156
+ #loaded_files << file
157
+ if @lock.nil?
158
+ @lock = GemfileLock.new(file + ".lock")
159
+ if @lock.size == 0
160
+ @lock = nil
161
+ else
162
+ # loaded_files << file + ".lock"
163
+ # just make sure bundler is there and has a version
164
+ @lock.hull.each do |dep|
165
+ dependency_management.gem dep
166
+ end
167
+ end
168
+ end
169
+ eval content
170
+
171
+ # we have a Gemfile so we add the bundler plugin
172
+ plugin(:bundler)
173
+ else
174
+ self
175
+ end
176
+ end
177
+
178
+ def load_jarfile(file)
179
+ jars = Jarfile.new(file)
180
+ if jars.exists?
181
+ container = ArtifactPassthrough.new do |a|
182
+ artifactId, groupId, extension, version = a.split(/:/)
183
+ send(extension.to_sym, "#{artifactId}:#{groupId}", version)
184
+ end
185
+ if !jars.exists_lock? || jars.mtime > jars.mtime_lock
186
+ jars.populate_unlocked container
187
+ end
188
+ jars.populate_locked container
189
+ end
190
+ end
191
+
192
+ def dir_name
193
+ File.basename(File.expand_path("."))
194
+ end
195
+ private :dir_name
196
+
197
+ def add_defaults(args = {})
198
+ versions = VERSIONS
199
+ versions = versions.merge(args) if args
200
+
201
+ name "#{dir_name} - gem" unless name
202
+
203
+ packaging "gem" unless packaging
204
+
205
+ repository("rubygems-releases").url = "http://rubygems-proxy.torquebox.org/releases" unless repository("rubygems-releases").url
206
+
207
+ unless repository("rubygems-prereleases").url
208
+ repository("rubygems-prereleases") do |r|
209
+ r.url = "http://rubygems-proxy.torquebox.org/prereleases"
210
+ r.releases(:enabled => false)
211
+ r.snapshots(:enabled => true)
212
+ end
213
+ end
214
+
215
+ # TODO go through all plugins to find out any SNAPSHOT version !!
216
+ if versions[:jruby_plugins] =~ /-SNAPSHOT$/ || properties['jruby.plugins.version'] =~ /-SNAPSHOT$/
217
+ plugin_repository("sonatype-snapshots") do |nexus|
218
+ nexus.url = "http://oss.sonatype.org/content/repositories/snapshots"
219
+ nexus.releases(:enabled => false)
220
+ nexus.snapshots(:enabled => true)
221
+ end
222
+ end
223
+
224
+ if packaging =~ /gem/ || plugin?(:gem)
225
+ gem = plugin(:gem)
226
+ gem.version = "${jruby.plugins.version}" unless gem.version
227
+ if packaging =~ /gem/
228
+ gem.extensions = true
229
+ if @gemspec && !(self.gem?('jruby-openssl') || self.gem?('jruby-openssl-maven'))
230
+ gem.gem('jruby-openssl-maven')
231
+ end
232
+ end
233
+ if File.exists?('lib') && File.exists?(File.join('src', 'main', 'java'))
234
+ plugin(:jar) do |j|
235
+ j.version = versions[:jar_plugin] unless j.version
236
+ j.in_phase('prepare-package').execute_goal(:jar).with :outputDirectory => '${project.basedir}/lib', :finalName => '${project.artifactId}'
237
+ end
238
+ end
239
+ end
240
+
241
+ if @bundler_deps && @bundler_deps.size > 0
242
+ plugin(:bundler)
243
+ bdeps = []
244
+ # first get the locked gems
245
+ @bundler_deps.each do |args, dep|
246
+ if @lock
247
+ # add its dependencies as well to have the version
248
+ # determine by the dependencyManagement
249
+ @lock.dependency_hull(dep.artifact_id).map.each do |d|
250
+ bdeps << d unless has_gem? d[0]
251
+ end
252
+ end
253
+ end
254
+ # any unlocked gems now
255
+ @bundler_deps.each do |args, dep|
256
+ bdeps << args unless has_gem? args[0]
257
+ end
258
+
259
+ # now add the deps to bundler plugin
260
+ # avoid to setup bundler if it has no deps
261
+ if bdeps.size > 0
262
+ plugin(:bundler) do |bundler|
263
+ # install will be triggered on initialize phase
264
+ bundler.executions.goals << "install"
265
+ bdeps.each do |d|
266
+ bundler.gem(d)
267
+ end
268
+ end
269
+ end
270
+ end
271
+
272
+ if plugin?(:bundler)
273
+ bundler = plugin(:bundler)
274
+ bundler.version = "${jruby.plugins.version}" unless bundler.version
275
+ unless gem?(:bundler)
276
+ gem("bundler")
277
+ end
278
+ end
279
+
280
+ if gem?('bundler') && !gem('bundler').version?
281
+ gem('bundler').version = nil
282
+ dependency_management.gem 'bundler', versions[:bundler_version]
283
+ end
284
+
285
+ if versions[:jruby_plugins]
286
+ #add_test_plugin(nil, "test")
287
+ add_test_plugin("rspec", "spec")
288
+ add_test_plugin("cucumber", "features")
289
+ add_test_plugin("minitest", "test")
290
+ add_test_plugin("minitest", "spec", 'spec')
291
+ end
292
+
293
+ self.properties = {
294
+ "project.build.sourceEncoding" => "UTF-8",
295
+ "gem.home" => "${project.build.directory}/rubygems",
296
+ "gem.path" => "${project.build.directory}/rubygems",
297
+ "jruby.plugins.version" => versions[:jruby_plugins]
298
+ }.merge(self.properties)
299
+
300
+ has_plugin_gems = build.plugins.detect do |k, pl|
301
+ pl.dependencies.detect { |d| d.type.to_sym == :gem } if pl.dependencies
302
+ end
303
+
304
+ if has_plugin_gems
305
+ plugin_repository("rubygems-releases").url = "http://rubygems-proxy.torquebox.org/releases" unless plugin_repository("rubygems-releases").url
306
+
307
+ unless plugin_repository("rubygems-prereleases").url
308
+ plugin_repository("rubygems-prereleases") do |r|
309
+ r.url = "http://rubygems-proxy.torquebox.org/prereleases"
310
+ r.releases(:enabled => false)
311
+ r.snapshots(:enabled => true)
312
+ end
313
+ end
314
+ end
315
+ # TODO
316
+ configs = {
317
+ :gem => [:initialize],
318
+ :rails3 => [:initialize, :pom],
319
+ :bundler => [:install]
320
+ }.collect do |name, goals|
321
+ if plugin?(name)
322
+ {
323
+ :pluginExecutionFilter => {
324
+ :groupId => 'de.saumya.mojo',
325
+ :artifactId => "#{name}-maven-plugin",
326
+ :versionRange => '[0,)',
327
+ :goals => goals
328
+ },
329
+ :action => { :ignore => nil }
330
+ }
331
+ end
332
+ end
333
+ configs.delete_if { |c| c.nil? }
334
+ if configs.size > 0
335
+ build.plugin_management do |pm|
336
+ options = {
337
+ :lifecycleMappingMetadata => {
338
+ :pluginExecutions => Maven::Model::NamedArray.new(:pluginExecution) do |e|
339
+ configs.each { |c| e << c }
340
+ end
341
+ }
342
+ }
343
+ pm.plugins.get('org.eclipse.m2e:lifecycle-mapping', '1.0.0').configuration(options)
344
+ end
345
+ end
346
+
347
+ profile('executable') do |exe|
348
+ exe.jar('de.saumya.mojo:gem-assembly-descriptors', '${jruby.plugins.version}').scope :runtime
349
+ exe.plugin(:assembly, '2.2-beta-5') do |a|
350
+ options = {
351
+ :descriptorRefs => ['jar-with-dependencies-and-gems'],
352
+ :archive => {:manifest => { :mainClass => 'de.saumya.mojo.assembly.Main' } }
353
+ }
354
+ a.configuration(options)
355
+ a.in_phase(:package).execute_goal(:assembly)
356
+ a.jar 'de.saumya.mojo:gem-assembly-descriptors', '${jruby.plugins.version}'
357
+ end
358
+ end
359
+ end
360
+
361
+ def has_gem?(gem)
362
+ self.gem?(gem)
363
+ end
364
+
365
+ def add_test_plugin(name, test_dir, goal = 'test')
366
+ unless plugin?(name)
367
+ has_gem = name.nil? ? true : gem?(name)
368
+ if has_gem && File.exists?(test_dir)
369
+ plugin(name || 'runit', "${jruby.plugins.version}").execution.goals << goal
370
+ end
371
+ else
372
+ pl = plugin(name || 'runit')
373
+ pl.version = "${jruby.plugins.version}" unless pl.version
374
+ end
375
+ end
376
+ private :add_test_plugin
377
+
378
+ def stack
379
+ @stack ||= [[:default]]
380
+ end
381
+ private :stack
382
+
383
+ def group(*args, &block)
384
+ stack << args
385
+ block.call if block
386
+ stack.pop
387
+ end
388
+
389
+ def gemspec(name = nil)
390
+ if name
391
+ load_gemspec(File.join(File.dirname(@current_file), name))
392
+ else
393
+ Dir[File.join(File.dirname(@current_file), "*.gemspec")].each do |file|
394
+ load_gemspec(file)
395
+ end
396
+ end
397
+ end
398
+
399
+ def source(*args)
400
+ warn "ignore source #{args}" if !(args[0].to_s =~ /^https?:\/\/rubygems.org/) && args[0] != :rubygems
401
+ end
402
+
403
+ def path(*args)
404
+ end
405
+
406
+ def git(*args)
407
+ end
408
+
409
+ def is_jruby_platform(*args)
410
+ args.detect { |a| :jruby == a.to_sym }
411
+ end
412
+ private :is_jruby_platform
413
+
414
+ def platforms(*args, &block)
415
+ if is_jruby_platform(*args)
416
+ block.call
417
+ end
418
+ end
419
+
420
+ def gem(*args, &block)
421
+ dep = nil
422
+ if args.last.is_a?(Hash)
423
+ options = args.delete(args.last)
424
+ unless options.key?(:git) || options.key?(:path)
425
+ if options[:platforms].nil? || is_jruby_platform(*(options[:platforms] || []))
426
+ group = options[:group] || options[:groups]
427
+ if group
428
+ [group].flatten.each do |g|
429
+ if dep
430
+ profile(g).dependencies << dep
431
+ else
432
+ dep = profile(g).gem(args, &block)
433
+ end
434
+ end
435
+ else
436
+ self.gem(args, &block)
437
+ end
438
+ end
439
+ end
440
+ else
441
+ stack.last.each do |c|
442
+ if c == :default
443
+ if @lock.nil? || args[0]== 'bundler'
444
+ dep = add_gem(args, &block)
445
+ else
446
+ dep = add_gem(args[0], &block)
447
+
448
+ # add its dependencies as well to have the version
449
+ # determine by the dependencyManagement
450
+ @lock.dependency_hull(args[0]).map.each do |d|
451
+ add_gem d[0], nil
452
+ end
453
+ end
454
+ else
455
+ if @lock.nil?
456
+ if dep
457
+ profile(c).dependencies << dep
458
+ else
459
+ dep = profile(c).gem(args, &block)
460
+ end
461
+ else
462
+ if dep
463
+ profile(c).dependencies << dep
464
+ else
465
+ dep = profile(c).gem(args[0], nil, &block)
466
+ end
467
+ # add its dependencies as well to have the version
468
+ # determine by the dependencyManagement
469
+ @lock.dependency_hull(args[0]).map.each do |d|
470
+ profile(c).gem d[0], nil unless gem? d[0]
471
+ end
472
+ end
473
+ end
474
+ end
475
+ end
476
+ if dep
477
+ # first collect the missing deps it any
478
+ @bundler_deps ||= []
479
+ # use a dep with version so just create it from the args
480
+ @bundler_deps << [args, dep]
481
+ end
482
+ dep
483
+ end
484
+ end
485
+ end
486
+ end
487
+
488
+ if $0 == __FILE__
489
+ proj = Maven::Tools::GemProject.new("test_gem")
490
+ if ARGV[0] =~ /\.gemspec$/
491
+ proj.load_gemspec(ARGV[0])
492
+ else
493
+ proj.load(ARGV[0] || 'Gemfile')
494
+ end
495
+ proj.load(ARGV[1] || 'Mavenfile')
496
+ proj.add_defaults
497
+ puts proj.to_xml
498
+ end