Class: Builder::AntBuilder

Inherits:
XmlBase show all
Defined in:
lib/builder/antbuilder.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XmlBase

#<<, #nil?, #tag!, #text!

Methods inherited from BlankSlate

hide

Constructor Details

#initialize(options = {}) ⇒ AntBuilder

Create an ant markup builder. Parameters are specified by an option hash.

:target=>target_object

Object receiving the markup. out must respond to the << operator. The default is a plain string target.

:indent=>indentation

Number of spaces used for indentation. The default is no indentation and no line breaks.

:margin=>initial_indentation_level

Amount of initial indentation (specified in levels, not spaces).



32
33
34
35
36
37
38
39
# File 'lib/builder/antbuilder.rb', line 32

def initialize(options={})
  indent = options[:indent] || 0
  margin = options[:margin] || 0
  super(indent, margin)
  @target = options[:target] || ""
  @debug = options[:debug] 
  @debug = false if @debug.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



192
193
194
195
196
# File 'lib/builder/antbuilder.rb', line 192

def method_missing(sym, *args, &block)
  ret = super
  _execute if @level==0
  ret
end

Class Method Details

.run_targets_from_command_lineObject

Subclasses ONLY can use this at the end of their class definition to run targets from the command line like ant does. For example: “jruby build.rb clean compile release” is like saying “ant clean compile release # this is any comment”



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/builder/antbuilder.rb', line 96

def AntBuilder.run_targets_from_command_line
  # self would be expected to be a subclass here, because AntBuilder doesnt define any 'ant_target'
  builder = self.new
  ARGV.each do |ant_target| 
    return if ant_target == "#"
    if builder.respond_to?(ant_target)
      builder.instance_eval("builder.#{ant_target}") 
    else
      raise RuntimeError, "\nBUILD FAILED\nTarget '#{ant_target}' does not exist in this project.", []
    end
  end
end

Instance Method Details

#_check_for_circular_dependencies(call_stack, some_method_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/builder/antbuilder.rb', line 46

def _check_for_circular_dependencies(call_stack, some_method_name) 
  # remove noise from the stack - all we want left are the ant task methods
  method_name_on_stack_regexp = /`\w+'/ 
  call_stack.reject!{|c| c =~ /#{__FILE__}/ or c=~ /`instance_eval'/ or c=~ /`each'/ or not c=~ method_name_on_stack_regexp }
  
  # collect just those lines that look like method names
  call_stack.collect!{|c| (method_name_on_stack_regexp.match(c))[0] }
  
  # check whether some_method_name is on the stack
  call_stack.each do |c|
      if c=~ /`#{some_method_name}'/
        # circular dependecy detected 
        err = call_stack.inject(some_method_name){|message,method_name|  "#{message} <- #{method_name}"}
        raise RuntimeError, "\nBUILD FAILED\nCircular dependency: #{err}", []
      end
  end
end

#depends(*ant_task_dependecies) ⇒ Object

implement the ant depends clause



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/builder/antbuilder.rb', line 65

def depends(*ant_task_dependecies) 
  
  # who called me?
  my_caller_matcher = caller[0].match(/`(\w+)'/)
  my_caller = my_caller_matcher[1] if not my_caller_matcher.nil?

  # Call the dependencies if they haven't been called already
  for ant_task_dependency in ant_task_dependecies
  
        if not respond_to?(ant_task_dependency) 
          raise RuntimeError, "\nBUILD FAILED\nTarget '#{ant_task_dependency}' does not exist in this project. It is used from target `#{my_caller}'.", []
        end
        _check_for_circular_dependencies(caller, ant_task_dependency.to_s) 
        
        cmd = "puts \"\n#{ant_task_dependency}:\" if  not @__#{ant_task_dependency}__.nil?; @__#{ant_task_dependency}__ ||= #{ant_task_dependency} || true"
        puts "#{cmd}" if @debug 
        instance_eval(cmd)
  end
  
  # Now print the name of the calling task, from the first line of stack
  puts
  puts "#{my_caller}:"
  
  # and make sure we also record the fact that the caller has been called (as we have done for its dependencies)
  instance_eval("@__#{my_caller}__ = true ")

end

#target!Object

Return the target of the builder.



42
43
44
# File 'lib/builder/antbuilder.rb', line 42

def target!
  @target
end