Class: JRAW::AntProject

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

Direct Known Subclasses

JRawRunner

Constant Summary collapse

@@classes_loaded =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AntProject

Constructor will be called internally and consumes the options hash which can contain infos about a logger



98
99
100
101
102
103
# File 'lib/ant_project.rb', line 98

def initialize(options)
  @logger = options[:logger] || Logger.new(STDOUT)
  logger.level = options[:loglevel] || Logger::INFO

  @task_stack = Array.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ant_project.rb', line 135

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.nitems == 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: " + $!)
    raise
  ensure
    @task_stack.pop
  end

end

Instance Attribute Details

#ant_versionObject

getter and setter for the project instance, the logger, the declarative and the attribute ant_version



24
25
26
# File 'lib/ant_project.rb', line 24

def ant_version
  @ant_version
end

#declarativeObject

getter and setter for the project instance, the logger, the declarative and the attribute ant_version



24
25
26
# File 'lib/ant_project.rb', line 24

def declarative
  @declarative
end

#default_targetObject

getter and setter for the project instance, the logger, the declarative and the attribute ant_version



24
25
26
# File 'lib/ant_project.rb', line 24

def default_target
  @default_target
end

#loggerObject

getter and setter for the project instance, the logger, the declarative and the attribute ant_version



24
25
26
# File 'lib/ant_project.rb', line 24

def logger
  @logger
end

#projectObject

getter and setter for the project instance, the logger, the declarative and the attribute ant_version



24
25
26
# File 'lib/ant_project.rb', line 24

def project
  @project
end

Instance Method Details

#basedirObject

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



171
172
173
# File 'lib/ant_project.rb', line 171

def basedir
  return @project.base_dir.absolute_path;
end

#build_instance_variable(prop) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/ant_project.rb', line 109

def build_instance_variable(prop)
  begin
  instance_variable = "@#{instvar(prop[0])} = '#{prop[1]}'"
  self.instance_eval instance_variable
  logger.debug instance_variable
  rescue SyntaxError => e
    logger.error "Problem with #{instance_variable}. Cannot create valid instance variable."
    raise e
  end
end

#build_propertiesObject



120
121
122
123
124
125
126
127
# File 'lib/ant_project.rb', line 120

def build_properties
  project.properties.each do |prop|
    build_instance_variable(prop)
  end
  logger.debug instance_variables
  # TODO: Hack
  @ant_version = JRAW::ApacheAnt::Main.ant_version[/\d\.\d\.\d/].to_f
end

#init_project(options) ⇒ Object

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, JRAW will
load the classes from the ANT_HOME/lib dir. If ant_home is not provided, the ANT jar files
must be available on 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 => <em>true</em> 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

This is for further initializations inside the constructor and must be called once from the users ANT script Example usage: init_project :basedir => ‘/Users/mjohann/projects/jruby_jraw’,

:name => 'JRuby',
:default => 'jar',
:anthome => ANT_HOME


57
58
59
60
61
62
63
64
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
92
93
94
# File 'lib/ant_project.rb', line 57

def init_project(options)
  # The ANT version used
  logger.info JRAW::ApacheAnt::Main.ant_version
  @ant_version = JRAW::ApacheAnt::Main.ant_version[/\d\.\d\.\d/].to_f
  # instance of ANT project
  @project = JRAW::ApacheAnt::Project.new
  # The default project name taken from the options hash or left blank
  @project.name = options[:name] || ''
  # The default ANT target taken from the options hash or left blank
  @project.default = ''
  # The project's base directory taken from the options hash or the current working directory
  @project.basedir = options[:basedir] || FileUtils::pwd
  # intializing the ANT project
  @default_target = options[:default] if options[:default]
  logger.debug "Default == #{options[:default]}"
  @project.init

  # Sets the task definitions to be declared only or they get executed directly
  # Default is true
  unless options[:declarative]
    logger.debug("declarative is nil")
    self.declarative = true
  else
    logger.debug("declarative is #{options[:declarative]}")
    self.declarative = options[:declarative]
  end

  # Here we setup the default logger instance
  default_logger = ApacheAnt::DefaultLogger.new
  default_logger.message_output_level = Logger::INFO
  # Output is either Standard out or the stream in options hash
  default_logger.output_print_stream = options[:outputstr] || JavaLang::System.out
  default_logger.error_print_stream = options[:errorstr] || JavaLang::System.err
  # Output will be like in log4j.properties configured
  default_logger.emacs_mode = false
  # Set the default logger as the build listener
  @project.add_build_listener(default_logger)
end

#instvar(name) ⇒ Object



129
130
131
132
# File 'lib/ant_project.rb', line 129

def instvar(name)
  name = name.gsub('.', '_')
  name.gsub('-', '_')
end

#nameObject

The Ant AntProject’s name. Default is ”



166
167
168
# File 'lib/ant_project.rb', line 166

def name
  return @project.getName
end

#property_value(name) ⇒ Object



105
106
107
# File 'lib/ant_project.rb', line 105

def property_value(name)
  project.get_property(name)
end

#to_sObject

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



177
178
179
# File 'lib/ant_project.rb', line 177

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