lib/core/generate.rb in buildr-1.2.2 vs lib/core/generate.rb in buildr-1.2.3
- old
+ new
@@ -1,23 +1,36 @@
module Buildr
module Generate #:nodoc:
task "generate" do
- script = Generate.from_directory(true).join("\n")
- buildfile = File.expand_path("buildfile")
- File.open(buildfile, "w") { |file| file.write script }
- puts "Created #{buildfile}"
+ script = nil
+ HighLine.new.choose do |menu|
+ menu.header = "To use Buildr you need a buildfile. Do you want me to create one?"
+
+ menu.choice("From maven2 pom file") { script = Generate.from_maven2_pom(true).join("\n") } if File.exists?("pom.xml")
+ menu.choice("From directory structure") { script = Generate.from_directory(true).join("\n") }
+ menu.choice("Skip") { }
+ end
+
+ if script
+ buildfile = File.expand_path("buildfile")
+ File.open(buildfile, "w") { |file| file.write script }
+ puts "Created #{buildfile}"
+ end
end
class << self
+
+ HEADER = "# Generated by Buildr #{Buildr::VERSION}, change to your liking\n\n"
+
+
def from_directory(root = false)
name = File.basename(Dir.pwd)
if root
+ script = HEADER.split("\n")
header = <<-EOF
-# Generated by Buildr #{Buildr::VERSION}, change to your liking
-
# Version number for this release
VERSION_NUMBER = "1.0.0"
# Version number for the next release
NEXT_VERSION = "1.0.1"
# Group identifier for your projects
@@ -32,11 +45,11 @@
project.version = VERSION_NUMBER
project.group = GROUP
manifest["Implementation-Vendor"] = COPYRIGHT
EOF
- script = header.split("\n")
+ script += header.split("\n")
else
script = [ %{define "#{name}" do} ]
end
script << " compile.with # Add classpath dependencies" if File.exist?("src/main/java")
script << " resources" if File.exist?("src/main/resources")
@@ -57,9 +70,97 @@
end
script << "end"
script.flatten
end
- end
+ def from_maven2_pom(root = false)
+ pom = Buildr::POM.load('pom.xml')
+ project = pom.project
+ artifactId = project['artifactId']
+ description = project['name'] || "The #{artifactId} project"
+
+ if root
+ script = HEADER.split("\n")
+
+ settings_file = ENV["m2_settings"] || File.join(Gem::user_home, ".m2/settings.xml")
+ settings = XmlSimple.xml_in(IO.read(settings_file)) if File.exists?(settings_file)
+
+ if settings
+ proxy = settings['proxies'].first['proxy'].find { |proxy|
+ proxy["active"].nil? || proxy["active"].to_s =~ /true/
+ } rescue nil
+
+ if proxy
+ url = %{#{proxy["protocol"].first}://#{proxy["host"].first}:#{proxy["port"].first}}
+ exclude = proxy["nonProxyHosts"].to_s.gsub("|", ",") if proxy["nonProxyHosts"]
+ script << "options.proxy.http = '#{url}'"
+ script << "options.proxy.exclude << '#{exclude}'" if exclude
+ script << ''
+ # In addition, we need to use said proxies to download artifacts.
+ options.proxy.http = url
+ options.proxy.exclude << exclude if exclude
+ end
+ end
+
+ repositories = project["repositories"].first["repository"].select { |repository|
+ legacy = repository["layout"].to_s =~ /legacy/
+ !legacy
+ } rescue nil
+ unless repositories.nil? || repositories.empty?
+ repositories.each do |repository|
+ name, url = repository["name"], repository["url"]
+ script << "# #{name}"
+ script << "repositories.remote << '#{url}'"
+ # In addition we need to use said repositores to download artifacts.
+ Buildr.repositories.remote << url.to_s
+ end
+ script << ""
+ end
+ else
+ script = []
+ end
+
+ script << "desc '#{description}'"
+ script << "define '#{artifactId}' do"
+
+ groupId = project['groupId']
+ script << " project.group = '#{groupId}'" if groupId
+
+ version = project['version']
+ script << " project.version = '#{version}'" if version
+
+ #get plugins configurations
+ plugins = project['build'].first['plugins'].first['plugin'] rescue {}
+ compile_plugin = plugins.find{|pl| (pl['groupId'].nil? or pl['groupId'].first == 'org.apache.maven.plugins') and pl['artifactId'].first == 'maven-compiler-plugin'}
+ if compile_plugin
+ source = compile_plugin.first['configuration'].first['source'] rescue nil
+ target = compile_plugin.first['configuration'].first['target'] rescue nil
+
+ script << " compile.options.source = '#{source}'" if source
+ script << " compile.options.target = '#{target}'" if target
+ end
+
+ dependencies = pom.dependencies.map{|d| "'#{d}'"}.join(', ')
+ script << " compile.with #{dependencies}" unless dependencies.empty?
+
+ test_dependencies = pom.dependencies(['test']).map{|d| "'#{d}'"}.join(', ')
+ script << " test.compile.with #{test_dependencies}" unless test_dependencies.empty?
+
+ packaging = project['packaging'].first
+ if %w(jar war).include?(packaging)
+ script << " package :#{packaging}, :id => '#{artifactId}'"
+ end
+
+ modules = project['modules'].first['module'] rescue nil
+ if modules
+ modules.each do |mod|
+ chdir(mod) { script << from_maven2_pom.flatten.map { |line| " " + line } << "" }
+ end
+ end
+ script << "end"
+ script.flatten
+ end
+
+ end
end
end