Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. module Maven
  2. module Tools
  3. class Gemify
  4.  
  5. BASE_GOAL = "de.saumya.mojo:gemify-maven-plugin:0.22.0"
  6.  
  7. require 'java'
  8.  
  9. bin = nil
  10. if ENV['M2_HOME'] # use M2_HOME if set
  11. bin = File.join(ENV['M2_HOME'], "bin")
  12. else
  13. ENV['PATH'].split(java.lang.System.getProperty("path.separator")).detect do |path|
  14. mvn = File.join(path, "mvn")
  15. if File.exists?(mvn)
  16. if File.symlink?(mvn)
  17. link = File.readlink(mvn)
  18. if link =~ /^\// # is absolute path
  19. bin = File.dirname(File.expand_path(link))
  20. else # is relative path so join with dir of the maven command
  21. bin = File.dirname(File.expand_path(File.join(File.dirname(mvn), link)))
  22. end
  23. else # is no link so just expand it
  24. bin = File.expand_path(path)
  25. end
  26. else
  27. nil
  28. end
  29. end
  30. end
  31. bin = "/usr/share/maven2/bin" if bin.nil? # OK let's try debian default
  32. if File.exists?(bin) &&
  33. Dir.glob(File.join(bin, "..", "lib", "maven-core-3.*jar")).size == 0
  34. begin
  35. gem 'ruby-maven', ">=0"
  36. bin = File.dirname(Gem.bin_path('ruby-maven', "rmvn"))
  37. rescue LoadError
  38. bin = nil
  39. end
  40. end
  41. raise "can not find maven3 installation. install ruby-maven with\n\n\tjruby -S gem install ruby-maven --pre\n\n" if bin.nil?
  42. warn "using #{bin}"
  43. boot = File.join(bin, "..", "boot")
  44. lib = File.join(bin, "..", "lib")
  45. ext = File.join(bin, "..", "ext")
  46. classpath = (Dir.glob(lib + "/*jar") + Dir.glob(boot + "/*jar"))
  47.  
  48. java.lang.System.setProperty("classworlds.conf", File.join(bin, "m2.conf"))
  49.  
  50. java.lang.System.setProperty("maven.home", File.join(bin, ".."))
  51. classpath.each do |path|
  52. require path
  53. end
  54.  
  55. private
  56.  
  57. def self.create_maven
  58. puts "create maven instance"
  59.  
  60. import "org.codehaus.plexus.classworlds"
  61. classWorld = ClassWorld.new("plexus.core", java.lang.Thread.currentThread().getContextClassLoader());
  62. config = org.codehaus.plexus.DefaultContainerConfiguration.new
  63. config.setClassWorld(classWorld)
  64. config.setName("ruby-tools")
  65. container = org.codehaus.plexus.DefaultPlexusContainer.new(config);
  66. container.lookup(org.apache.maven.Maven.java_class)
  67. end
  68.  
  69. def self.maven
  70. @maven ||= create_maven
  71. end
  72.  
  73. def self.temp_dir
  74. @temp_dir ||=
  75. begin
  76. f = java.io.File.createTempFile("gemify", "")
  77. f.delete
  78. f.mkdir
  79. f.deleteOnExit
  80. f.absolute_path
  81. end
  82. end
  83.  
  84. def self.execute(goal, gemname, version, props = {})
  85. r = org.apache.maven.execution.DefaultMavenExecutionRequest.new
  86. r.setShowErrors( true )
  87. r.getUserProperties.put("gemify.skipDependencies", "true")
  88. r.getUserProperties.put("gemify.tempDir", temp_dir)
  89. r.getUserProperties.put("gemify.gemname", gemname)
  90. r.getUserProperties.put("gemify.version", version.to_s) if version
  91. props.each do |k,v|
  92. r.getUserProperties.put(k.to_s, v.to_s)
  93. end
  94. r.setGoals( [goal] )
  95. r.setLoggingLevel(0)
  96.  
  97. #p r.goals.to_s
  98. #p r.getUserProperties.map
  99. out = java.lang.System.out
  100. string_io = java.io.ByteArrayOutputStream.new
  101. java.lang.System.setOut(java.io.PrintStream.new(string_io))
  102. result = maven.execute( r );
  103. java.lang.System.setOut(out)
  104. result.getExceptions.each { |e| e.printStackTrace }
  105. string_io.to_s
  106. end
  107.  
  108. public
  109.  
  110. def self.get_versions(gemname)
  111. # p "versions"
  112. # p gemname
  113. result = execute("#{BASE_GOAL}:versions", gemname, nil)
  114.  
  115. result.gsub(/\n/, '').sub(/.*\[/, "").sub(/\]/, '').gsub(/ /, '').split(',')
  116. end
  117.  
  118. def self.generate_spec(gemname, version)
  119. # puts "generate spec"
  120. # p gemname
  121. # p version
  122. result = execute("#{BASE_GOAL}:gemify", gemname, version, {"gemify.onlySpecs" => true })
  123. path = result.gsub(/\n/, '')
  124. if path =~ /gemspec: /
  125. path = path.sub(/.*gemspec: /, '')
  126. if path.size > 0
  127. result = File.expand_path(path)
  128. java.io.File.new(result).deleteOnExit
  129. result
  130. end
  131. end
  132. end
  133.  
  134. def self.generate_gem(gemname, version)
  135. # p "generate gem"
  136. # p gemname
  137. # p version.to_s
  138.  
  139. result = execute("#{BASE_GOAL}:gemify", gemname, version)
  140. path = result.gsub(/\n/, '')
  141. if path =~ /gem: /
  142.  
  143. path = path.sub(/.*gem: /, '')
  144. if path.size > 0
  145. result = File.expand_path(path)
  146. java.io.File.new(result).deleteOnExit
  147. result
  148. end
  149. end
  150. end
  151. end
  152. end
  153. end
Add Comment
Please, Sign In to add comment