Class: Ant

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/ant/ant.rb,
lib/rake/ant/target.rb,
lib/rake/ant/element.rb,
lib/rake/ant/project_converter.rb

Defined Under Namespace

Classes: BlockTarget, Element, MissingWrapper, ProjectConverter, RakeTarget, TargetWrapper, UnknownElement

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Ant



20
21
22
23
24
25
26
27
28
29
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



87
88
89
90
# 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

Class Attribute Details

.runObject

Returns the value of attribute run.



156
157
158
# File 'lib/rake/ant/ant.rb', line 156

def run
  @run
end

Instance Attribute Details

#current_targetObject

Returns the value of attribute current_target.



18
19
20
# File 'lib/rake/ant/ant.rb', line 18

def current_target
  @current_target
end

#locationObject (readonly)

Returns the value of attribute location.



17
18
19
# File 'lib/rake/ant/ant.rb', line 17

def location
  @location
end

#logObject (readonly)

Returns the value of attribute log.



17
18
19
# File 'lib/rake/ant/ant.rb', line 17

def log
  @log
end

#projectObject (readonly)

Returns the value of attribute project.



17
18
19
# File 'lib/rake/ant/ant.rb', line 17

def project
  @project
end

Class Method Details

.ant(options = {}, &code) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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_callerObject



166
167
168
169
# 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) ⇒ Object



158
159
160
161
162
163
164
# 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

Instance Method Details

#[](name) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



83
84
85
# 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) ⇒ Object Also known as: target

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



42
43
44
45
# 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

#ant(*args) ⇒ Object



75
76
77
# 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) ⇒ Object



79
80
81
# 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) ⇒ Object



35
36
37
# File 'lib/rake/ant/ant.rb', line 35

def define_tasks(&code)
  code.arity == 1 ? code[self] : instance_eval(&code) if code
end

#execute_defaultObject



60
61
62
# File 'lib/rake/ant/ant.rb', line 60

def execute_default
  @project.execute_target(@project.default_target)
end

#execute_target(name) ⇒ Object



56
57
58
# File 'lib/rake/ant/ant.rb', line 56

def execute_target(name)
  self[name].execute
end

#process_arguments(argv = ARGV, run_at_exit = true) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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_helpObject



64
65
66
67
68
69
70
71
72
73
# 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

#propertiesObject



31
32
33
# File 'lib/rake/ant/ant.rb', line 31

def properties
  @project.properties
end

#run(*targets) ⇒ Object



92
93
94
95
96
97
98
# 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