Class: Antwrap::AntProject

Inherits:
Object
  • Object
show all
Defined in:
lib/ant_project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Hash.new) ⇒ AntProject

Create an AntProject. Parameters are specified via a hash: :ant_home=>Ant basedir

-A String indicating the location of the ANT_HOME directory. If provided, Antwrap will
load the classes from the ANT_HOME/lib dir. If ant_home is not provided, the Ant jar files
must be available in the CLASSPATH.

:name=>project_name

-A String indicating the name of this project.

:basedir=>project_basedir

-A String indicating the basedir of this project. Corresponds to the 'basedir' attribute 
on an Ant project.

:declarative=>declarative_mode

-A boolean value indicating wether Ant tasks created by this project instance should 
have their execute() method invoked during their creation. For example, with 
the option :declarative=>true the following task would execute; 
@antProject.echo(:message => "An Echo Task")
However, with the option :declarative=>false, the programmer is required to execute the 
task explicitly; 
echoTask = @antProject.echo(:message => "An Echo Task")
echoTask.execute()
Default value is <em>true</em>.

:logger=>Logger

-A Logger instance. Defaults to Logger.new(STDOUT)

:loglevel=>The level to set the logger to

-Defaults to Logger::ERROR


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ant_project.rb', line 72

def initialize(options=Hash.new)
  
  @logger = options[:logger] || Logger.new(STDOUT)
  @logger.level = options[:loglevel] || Logger::ERROR
  
  if(!@@classes_loaded && options[:ant_home])
    @logger.debug("loading ant jar files. Ant_Home: #{options[:ant_home]}")
    AntwrapClassLoader.load_ant_libs(options[:ant_home])
    @@classes_loaded = true
  end
  
  @logger.debug(Antwrap::ApacheAnt::Main.getAntVersion())
  @ant_version = Antwrap::ApacheAnt::Main.getAntVersion()[/\d\.\d\.\d/].to_f
  init_project(options)
  
  @task_stack = Array.new
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ant_project.rb', line 95

def method_missing(sym, *args)
  
  begin
    task = AntTask.new(sym.to_s, self, args[0])
    
    parent_task = @task_stack.last
    @task_stack << task
    
    yield self if block_given?
    
    parent_task.add(task) if parent_task
    
    if @task_stack.size == 1 
      if declarative == true
        @logger.debug("Executing #{task}")
        task.execute 
      else 
        @logger.debug("Returning #{task}")
        return task
      end  
    end
    
  rescue
    @logger.error("Error instantiating '#{sym.to_s}' task: " + $!.to_s)
    raise
  ensure
    @task_stack.pop
  end
  
end

Instance Attribute Details

#ant_versionObject

Returns the value of attribute ant_version.



46
47
48
# File 'lib/ant_project.rb', line 46

def ant_version
  @ant_version
end

#declarativeObject

Returns the value of attribute declarative.



46
47
48
# File 'lib/ant_project.rb', line 46

def declarative
  @declarative
end

#loggerObject

Returns the value of attribute logger.



46
47
48
# File 'lib/ant_project.rb', line 46

def logger
  @logger
end

#projectObject

Returns the value of attribute project.



46
47
48
# File 'lib/ant_project.rb', line 46

def project
  @project
end

Instance Method Details

#basedirObject

The Ant Project’s basedir. Default is ‘.’



132
133
134
# File 'lib/ant_project.rb', line 132

def basedir
  return @project.getBaseDir().getAbsolutePath();
end

#java(*args, &block) ⇒ Object



91
92
93
# File 'lib/ant_project.rb', line 91

def java(*args, &block)
  method_missing(:java, *args, &block)
end

#nameObject

The Ant Project’s name. Default is ”



127
128
129
# File 'lib/ant_project.rb', line 127

def name
  return @project.getName
end

#to_sObject

Displays the Class name followed by the AntProject name -e.g. AntProject



138
139
140
# File 'lib/ant_project.rb', line 138

def to_s
  return self.class.name + "[#{name}]"
end