class Rake::Ant

Constants

VERSION

Attributes

run[RW]
current_target[RW]
location[R]
log[R]
project[R]

Public Class Methods

ant_script() click to toggle source
# File lib/rake/ant.rb, line 59
def self.ant_script
  require 'rbconfig'
  RbConfig::CONFIG['host_os'] =~ /Windows|mswin/ ? 'ant.bat' : 'ant'
end
load() click to toggle source
# File lib/rake/ant.rb, line 36
def self.load
  if ENV['ANT_HOME'] && File.exist?(ENV['ANT_HOME'])
    const_set(:ANT_HOME, ENV['ANT_HOME'])
    Dir["#{ANT_HOME}/lib/*.jar"].each {|j| $CLASSPATH << j }
  else
    load_from_ant
  end

  # Explicitly add javac path to classpath.
  # 'java.home' usually contains the JRE home on Windows and Linux. We
  # want the directory above that which contains lib/tools.jar.
  java_home = ENV['JAVA_HOME'] || ENV_JAVA['java.home'].sub(/\/jre$/,'')
  if java_home && File.exist?(java_home)
    const_set(:JAVA_HOME, java_home)
    load_if_exist "#{java_home}/lib/tools.jar"
    load_if_exist "#{java_home}/lib/classes.zip"
  end
end
load_from_ant() click to toggle source
# File lib/rake/ant.rb, line 5
def self.load_from_ant
  IO.popen("#{ant_script} -diagnostics") do |diag|
    classpath_jars = []
    listing_path = nil
    jar_path = nil
    diag.readlines.each do |line|

      # workaround for JRUBY-4814 (IO.popen doesnt convert CRLF to LF on Windows)
      line.chomp!

      if line =~ /^ant\.home: (.*)$/ && !defined?(ANT_HOME)
        const_set(:ANT_HOME, $1)
      elsif line =~ /Tasks availability/
        break
      elsif line =~ /^ (.*) jar listing$/
        listing_path = $1
      elsif line =~ /^(.*\.home): (.*)$/
        home_var, path = $1, $2
        jar_path = listing_path.sub(home_var.upcase.sub('.','_'), path) if listing_path
      elsif line =~ /^ant\.core\.lib: (.*)$/
        classpath_jars << $1
      elsif line =~ /^(.*\.jar) \(\d+ bytes\)/
        classpath_jars << File.join(jar_path, $1)
      end
    end
    classpath_jars.uniq.each {|j| $CLASSPATH << j }
  end
rescue Errno::ENOENT
  raise RuntimeError, "Could not execute `#{ant_script}`. Make sure Ant is installed on the local system."
end
load_if_exist(jar) click to toggle source
# File lib/rake/ant.rb, line 55
def self.load_if_exist(jar)
  $CLASSPATH << jar if File.exist?(jar)
end
new(options={}, &block) click to toggle source
# File lib/rake/ant/ant.rb, line 20
def initialize(options={}, &block)
  @options = options
  @location = Ant.location_from_caller
  @project = create_project options
  @current_target = nil
  generate_methods @project.data_type_definitions
  generate_methods @project.task_definitions
  process_arguments unless options[:run] == false || Ant.run || @location.file_name != $0
  define_tasks(&block)
end

Private Class Methods

ant(options={}, &code) click to toggle source
# File lib/rake/ant/ant.rb, line 171
def ant(options={}, &code)
  if options.respond_to? :to_hash
    @ant ||= Ant.new options.to_hash
    @ant.define_tasks(&code)
    @ant
  else
    options = options.join(" ") if options.respond_to? :to_ary
    ant_bin = ENV['ANT_HOME'] ? File.join(ENV['ANT_HOME'], 'bin', 'ant') : 'ant' # find one on $PATH
    system "#{ant_bin} #{options.to_s}" # FIXME: Make this more secure if using array form
  end
rescue Exception => e
  warn e.message
  warn e.backtrace.join("\n")
end
location_from_caller() click to toggle source
# File lib/rake/ant/ant.rb, line 166
def location_from_caller
  file, line = caller.detect{|el| el !~ /^#{File.dirname(__FILE__)}/ && el !~ /\.java:/}.split(/:(\d+):?/)
  Location.new(file, line.to_i, 1)
end
safe_method_name(element_name) click to toggle source
# File lib/rake/ant/ant.rb, line 158
def safe_method_name(element_name)
  if element_name =~ /\A(and|or|not|do|end|if|else)\z/m
    "_#{element_name}"
  else
    element_name
  end
end

Public Instance Methods

[](name) click to toggle source
# File lib/rake/ant/ant.rb, line 48
def [](name)
  if @project.targets.containsKey(name.to_s)
    TargetWrapper.new(@project, name)
  else
    MissingWrapper.new(@project, name)
  end
end
_element(name, args = {}, &block) click to toggle source
# File lib/rake/ant/ant.rb, line 83
def _element(name, args = {}, &block)
  Element.new(self, name).call(@current_target, args, &block)
end
add_target(*options, &block) click to toggle source

Add a target (two forms)

  1. Execute a block as a target: add_target “foo-target” { echo :message => “I am cool” }

  2. Execute a rake task as a target: add_target Rake.application

# File lib/rake/ant/ant.rb, line 42
def add_target(*options, &block)
  target = options.first.respond_to?(:name) ? RakeTarget.new(self, options.first) : BlockTarget.new(self, *options, &block)
  @project.add_target target
end
Also aliased as: target
ant(*args) click to toggle source
# File lib/rake/ant/ant.rb, line 75
def ant(*args)
  raise "ant is known to be broken and is unsupported in the ant library"
end
antcall(*args) click to toggle source
# File lib/rake/ant/ant.rb, line 79
def antcall(*args)
  raise "antcall is known to be broken and is unsupported in the ant library"
end
define_tasks(&code) click to toggle source
# File lib/rake/ant/ant.rb, line 35
def define_tasks(&code)
  code.arity == 1 ? code[self] : instance_eval(&code) if code
end
execute_default() click to toggle source
# File lib/rake/ant/ant.rb, line 60
def execute_default
  @project.execute_target(@project.default_target)
end
execute_target(name) click to toggle source
# File lib/rake/ant/ant.rb, line 56
def execute_target(name)
  self[name].execute
end
method_missing(name, *args, &block) click to toggle source
# File lib/rake/ant/ant.rb, line 87
def method_missing(name, *args, &block)
  project.log "invoking method_missing: #{name} on Ant instance", 5
  _element(name, *args, &block)
end
process_arguments(argv = ARGV, run_at_exit = true) click to toggle source
# File lib/rake/ant/ant.rb, line 100
def process_arguments(argv = ARGV, run_at_exit = true)
  properties = []
  targets = []
  argv.each {|a| a =~ /^-D/ ? properties << a[2..-1] : targets << a }
  properties.each do |p|
    key, value = p.split('=', 2)
    value ||= "true"
    @project.set_user_property(key, value)
  end
  at_exit do
    begin
      run(*targets) if (!targets.empty? || @project.default_target) && !Ant.run
    rescue => e
      warn e.message
      puts e.backtrace.join("\n") if $DEBUG
      exit 1
    end
  end if run_at_exit
end
project_help() click to toggle source
# File lib/rake/ant/ant.rb, line 64
def project_help
  max_width = @project.targets.keys.max {|a,b| a.length <=> b.length}.length
  @project.targets.values.select {|t|
    t.description
  }.sort{|a,b|
    a.name <=> b.name
  }.map {|t|
    "%-#{max_width}s - %s" % [t.name, t.description]
  }.join("\n")
end
properties() click to toggle source
# File lib/rake/ant/ant.rb, line 31
def properties
  @project.properties
end
run(*targets) click to toggle source
# File lib/rake/ant/ant.rb, line 92
def run(*targets)
  if targets.length > 0
    targets.each {|t| execute_target(t) }
  else
    execute_default
  end
end
target(*options, &block)
Alias for: add_target

Private Instance Methods

create_project(options) click to toggle source
# File lib/rake/ant/ant.rb, line 121
def create_project(options)
  # If we are calling into a rakefile from ant then we already have a project to use
  return $project if defined?($project) && $project

  options[:basedir] ||= '.'
  output_level = options.delete(:output_level) || 2

  Project.new.tap do |p|
    p.init
    p.add_build_listener(DefaultLogger.new.tap do |log|
      log.output_print_stream = Java::java.lang.System.out
      log.error_print_stream = Java::java.lang.System.err
      log.emacs_mode = true
      log.message_output_level = output_level
      @log = log
    end)
    helper = ProjectHelper.getProjectHelper
    helper.import_stack.add(Java::java.io.File.new(@location.file_name))
    p.addReference(ProjectHelper::PROJECTHELPER_REFERENCE, helper)
    options.each_pair {|k,v| p.send("set_#{k}", v) if p.respond_to?("set_#{k}") }
  end
end
generate_methods(collection) click to toggle source
# File lib/rake/ant/ant.rb, line 144
def generate_methods(collection)
  existing_methods = Ant.instance_methods(false)
  collection.each do |name, clazz|
    element = Element.new(self, name, clazz)
    method_name = Ant.safe_method_name(name)
    (class << self; self; end).send(:define_method, method_name) do |*a, &b|
      element.call(@current_target, *a, &b)
    end unless existing_methods.include?(method_name)
  end
end