lib/java/xmlbeans.rb in buildr-0.18.0 vs lib/java/xmlbeans.rb in buildr-0.19.0

- old
+ new

@@ -1,66 +1,72 @@ +require "java/java" + module Buildr + module Java + module XMLBeans - module XMLBeans + REQUIRES = [ "xmlbeans:xbean:jar:2.2.0", "stax:stax-api:jar:1.0" ] - REQUIRES = [ "xmlbeans:xbean:jar:2.2.0", "stax:stax-api:jar:1.0" ] + class << self - def self.compile(*args) - options = Hash === args.last ? args.pop : {} - options[:verbose] ||= Rake.application.options.trace || false - fu_check_options options, :verbose, :noop, :javasource, :jar, :classes, :output + def compile(*args) + options = Hash === args.last ? args.pop : {} + options[:verbose] ||= Rake.application.options.trace || false + fu_check_options options, :verbose, :noop, :javasource, :jar, :compile, :output, :xsb - # Options for SchemaCompiler. - cmd_args = [ options[:verbose] ? "-verbose" : "-quiet" ] - cmd_args << "-javasource" << options[:javasource].collect.join(File::PATH_SEPARATOR) if options[:javasource] - cmd_args << "-out" << options[:jar] if options[:jar] - cmd_args << "-d" << options[:classes] if options[:classes] - cmd_args << "-src" << options[:output] if options[:output] - cmd_args += args - cmd_args << { :verbose=>options[:verbose] } + # Options for SchemaCompiler. + output = options[:output].to_s + cmd_args = [ options[:verbose] ? "-verbose" : "-quiet" ] + cmd_args << "-javasource" << options[:javasource].collect.join(File::PATH_SEPARATOR) if options[:javasource] + cmd_args << "-out" << options[:jar] if options[:jar] + cmd_args << "-d" << output + cmd_args << "-srconly" unless options[:compile] + cmd_args << "-src" << output + cmd_args += args.flatten + cmd_args << { :verbose=>options[:verbose] } - unless options[:noop] - puts "Running XMLBeans schema compiler" if verbose - mkpath options[:classes], :verbose=>false rescue nil if options[:classes] - mkpath options[:output], :verbose=>false rescue nil if options[:output] - sh(Java.path_to_bin("java"), "-Xmx256m", "-cp", requires.join(File::PATH_SEPARATOR), - "org.apache.xmlbeans.impl.tool.SchemaCompiler", *cmd_args) { |ok, res| - fail "Failed to compile schemas, see errors above" unless ok } - # Touch paths to let other tasks know there's an update. - touch options[:classes], :verbose=>false if options[:classes] - touch options[:output], :verbose=>false if options[:output] - end - end + unless options[:noop] + puts "Running XMLBeans schema compiler" if verbose + mkpath output, :verbose=>false rescue nil + sh(Java.path_to_bin("java"), "-Xmx256m", "-cp", requires.join(File::PATH_SEPARATOR), + "org.apache.xmlbeans.impl.tool.SchemaCompiler", *cmd_args) { |ok, res| + fail "Failed to compile schemas, see errors above" unless ok } + # Touch paths to let other tasks know there's an update. + touch output, :verbose=>false + end + end - def self.requires() - @requires ||= artifacts(REQUIRES).each { |artifact| artifact.invoke }.map(&:to_s) - end - - class CompileTask < Rake::FileTask + private - def initialize(*args) - super - enhance do |task| - XMLBeans.compile *task.prerequisites + [options.merge(:output=>task.name)] + def requires() + @requires ||= artifacts(REQUIRES).each { |artifact| artifact.invoke }.map(&:to_s) end + end - def options() - @options ||= {} + def compile_xml_beans(*args) + # Generate sources and add them to the compile task. + generated = file(path_to("target/generated/xmlbeans")=>FileList[args.flatten]) do |task| + Java::XMLBeans.compile task.prerequisites, :output=>task.name, + :javasource=>compile.options.source, :xsb=>compile.target + end + compile.from(generated).with(JAVAX.stream, XMLBEANS) + # Once compiled, we need to copy the generated XSB/XSD and one (magical?) class file + # into the target directory, or the rest is useless. + compile do |task| + verbose(false) do + base = Pathname.new(generated.to_s) + FileList["#{base}/**/*.{class,xsb,xsd}"].each do |file| + target = File.join(compile.target.to_s, Pathname.new(file).relative_path_from(base)) + mkpath File.dirname(target) ; cp file, target + end + end + end end - def using(options) - self.options.merge!(options) - self - end - end - - def self.compile_task(args) - output = args.keys.first - files = args.values.first.collect { |f| File.directory?(f) ? FileList[f + "/**/*"] : f }.flatten - CompileTask.define_task(output=>files) - end - end + class Project + include Java::XMLBeans + end end