Antwrap 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,133 @@
1
+ # ant_project.rb
2
+ #
3
+ # Copyright Caleb Powell 2007
4
+ #
5
+ # Licensed under the LGPL, see the file COPYING in the distribution
6
+ #
7
+
8
+ class AntProject
9
+ require 'logger'
10
+ require 'ant_task'
11
+
12
+ private
13
+ @@classes_loaded = false
14
+
15
+ def init_project(options)
16
+
17
+ @project= ApacheAnt::Project.new
18
+ @project.setName(options[:name] || '')
19
+ @project.setDefault('')
20
+ @project.setBasedir(options[:basedir] || FileUtils::pwd)
21
+ @project.init
22
+ if options[:declarative] == nil
23
+ @logger.debug("declarative is nil")
24
+ self.declarative= true
25
+ else
26
+ @logger.debug("declarative is #{options[:declarative]}")
27
+ self.declarative= options[:declarative]
28
+ end
29
+ default_logger = ApacheAnt::DefaultLogger.new
30
+ default_logger.setMessageOutputLevel(2)
31
+ default_logger.setOutputPrintStream(options[:outputstr] || JavaLang::System.out)
32
+ default_logger.setErrorPrintStream(options[:errorstr] || JavaLang::System.err)
33
+ default_logger.setEmacsMode(false)
34
+ @project.addBuildListener(default_logger)
35
+
36
+ end
37
+
38
+ public
39
+ attr :project, false
40
+ attr :ant_version, false
41
+ attr_accessor(:declarative, :logger)
42
+
43
+ # Create an AntProject. Parameters are specified via a hash:
44
+ # :ant_home=><em>Ant basedir</em>
45
+ # -A String indicating the location of the ANT_HOME directory. If provided, Antwrap will
46
+ # load the classes from the ANT_HOME/lib dir. If ant_home is not provided, the Ant jar files
47
+ # must be available in the CLASSPATH.
48
+ # :name=><em>project_name</em>
49
+ # -A String indicating the name of this project.
50
+ # :basedir=><em>project_basedir</em>
51
+ # -A String indicating the basedir of this project. Corresponds to the 'basedir' attribute
52
+ # on an Ant project.
53
+ # :declarative=><em>declarative_mode</em>
54
+ # -A boolean value indicating wether Ant tasks created by this project instance should
55
+ # have their execute() method invoked during their creation. For example, with
56
+ # the option :declarative=>true the following task would execute;
57
+ # @antProject.echo(:message => "An Echo Task")
58
+ # However, with the option :declarative=>false, the programmer is required to execute the
59
+ # task explicitly;
60
+ # echoTask = @antProject.echo(:message => "An Echo Task")
61
+ # echoTask.execute()
62
+ # Default value is <em>true</em>.
63
+ # :logger=><em>Logger</em>
64
+ # -A Logger instance. Defaults to Logger.new(STDOUT)
65
+ # :loglevel=><em>The level to set the logger to</em>
66
+ # -Defaults to Logger::ERROR
67
+ def initialize(options=Hash.new)
68
+
69
+ @logger = options[:logger] || Logger.new(STDOUT)
70
+ @logger.level = options[:loglevel] || Logger::ERROR
71
+
72
+ if(!@@classes_loaded && options[:ant_home])
73
+ @logger.debug("loading ant jar files. Ant_Home: #{options[:ant_home]}")
74
+ AntwrapClassLoader.load_ant_libs(options[:ant_home])
75
+ @@classes_loaded = true
76
+ end
77
+
78
+ @logger.debug(ApacheAnt::Main.getAntVersion())
79
+ @ant_version = ApacheAnt::Main.getAntVersion()[/\d\.\d\.\d/].to_f
80
+ init_project(options)
81
+
82
+ @task_stack = Array.new
83
+
84
+ end
85
+
86
+ def method_missing(sym, *args)
87
+
88
+ begin
89
+ task = AntTask.new(sym.to_s, self, args[0])
90
+
91
+ parent_task = @task_stack.last
92
+ @task_stack << task
93
+
94
+ yield self if block_given?
95
+
96
+ parent_task.add(task) if parent_task
97
+
98
+ if @task_stack.nitems == 1
99
+ if declarative == true
100
+ @logger.debug("Executing #{task}")
101
+ task.execute
102
+ else
103
+ @logger.debug("Returning #{task}")
104
+ return task
105
+ end
106
+ end
107
+
108
+ rescue
109
+ @logger.error("Error instantiating '#{sym.to_s}' task: " + $!)
110
+ raise
111
+ ensure
112
+ @task_stack.pop
113
+ end
114
+
115
+ end
116
+
117
+ #The Ant Project's name. Default is ''
118
+ def name
119
+ return @project.getName
120
+ end
121
+
122
+ #The Ant Project's basedir. Default is '.'
123
+ def basedir
124
+ return @project.getBaseDir().getAbsolutePath();
125
+ end
126
+
127
+ #Displays the Class name followed by the AntProject name
128
+ # -e.g. AntProject[BigCoProject]
129
+ def to_s
130
+ return self.class.name + "[#{name}]"
131
+ end
132
+
133
+ end
@@ -0,0 +1,118 @@
1
+ # ant_task.rb
2
+ #
3
+ # Copyright Caleb Powell 2007
4
+ #
5
+ # Licensed under the LGPL, see the file COPYING in the distribution
6
+ #
7
+
8
+ class AntTask
9
+ attr_accessor :unknown_element, :project, :taskname, :logger, :executed
10
+
11
+ public
12
+ # Creates an AntTask
13
+ # taskname
14
+ # -A String representing the name of an Ant Task. This name should correspond to
15
+ # the element name of the Ant xml task (e.g. javac, jar, war, etc).
16
+ # antProject
17
+ # -An instance of an AntProject
18
+ # attributes
19
+ # -A Hash of task name/values to be applied to the task.
20
+ #
21
+ # For example:
22
+ # antProject = AntProject.new()
23
+ # antTask = AntTask.new('javac', antProject, {:debug => 'on', :verbose => 'no', :fork => 'no'})
24
+ def initialize(taskname, antProject, attributes)
25
+
26
+ taskname = taskname[1, taskname.length-1] if taskname[0,1] == "_"
27
+ @logger = antProject.logger
28
+ @taskname = taskname
29
+ @project_wrapper = antProject
30
+ @project = antProject.project()
31
+ @logger.debug(antProject.to_s)
32
+ @unknown_element = create_unknown_element(@project, taskname)
33
+ @logger.debug(to_s)
34
+
35
+ add_attributes(attributes)
36
+
37
+ end
38
+
39
+ # Displays the Class name followed by the Task name
40
+ # -e.g. AntTask[javac]
41
+ def to_s
42
+ return self.class.name + "[#{@taskname}]"
43
+ end
44
+
45
+ def create_unknown_element(project, taskname)
46
+
47
+ element = ApacheAnt::UnknownElement.new(taskname)
48
+ element.setProject(project)
49
+ element.setOwningTarget(ApacheAnt::Target.new())
50
+ element.setTaskName(taskname)
51
+
52
+ #DNR. This initializes the Task's Wrapper object and prevents NullPointerExeption upon execution of the task
53
+ element.getRuntimeConfigurableWrapper()
54
+
55
+ if(@project_wrapper.ant_version >= 1.6)
56
+ element.setTaskType(taskname)
57
+ element.setNamespace('')
58
+ element.setQName(taskname)
59
+ end
60
+
61
+ return element
62
+
63
+ end
64
+
65
+ # Sets each attribute on the AntTask instance.
66
+ # :attributes - is a Hash.
67
+ def add_attributes(attributes)
68
+
69
+ return if attributes == nil
70
+
71
+ wrapper = ApacheAnt::RuntimeConfigurable.new(@unknown_element, @unknown_element.getTaskName());
72
+
73
+ if(@project_wrapper.ant_version >= 1.6)
74
+ attributes.each do |key, val|
75
+ apply_to_wrapper(wrapper, key.to_s, val){ |k,v| wrapper.setAttribute(k, v)}
76
+ end
77
+ else
78
+ @unknown_element.setRuntimeConfigurableWrapper(wrapper)
79
+ attribute_list = XmlSax::AttributeListImpl.new()
80
+ attributes.each do |key, val|
81
+ apply_to_wrapper(wrapper, key.to_s, val){ |k,v| attribute_list.addAttribute(k, 'CDATA', v)}
82
+ end
83
+ wrapper.setAttributes(attribute_list)
84
+ end
85
+
86
+ end
87
+
88
+ def apply_to_wrapper(wrapper, key, value)
89
+
90
+ raise ArgumentError, "ArgumentError: You cannot use an Array as an argument. Use the :join method instead; i.e ['file1', 'file2'].join(File::PATH_SEPARATOR)." if value.is_a?(Array)
91
+
92
+ begin
93
+ if(key == 'pcdata')
94
+ wrapper.addText(value.to_s)
95
+ else
96
+ yield key, value.to_s
97
+ end
98
+ rescue StandardError
99
+ raise ArgumentError, "ArgumentError: Unable to set :#{key} attribute with value => '#{value}'"
100
+ end
101
+
102
+ end
103
+
104
+ # Add <em>child</em> as a child of this task.
105
+ def add(child)
106
+ @unknown_element.addChild(child.unknown_element())
107
+ @unknown_element.getRuntimeConfigurableWrapper().addChild(child.unknown_element().getRuntimeConfigurableWrapper())
108
+ end
109
+
110
+ # Invokes the AntTask.
111
+ def execute
112
+ @unknown_element.maybeConfigure
113
+ @unknown_element.execute
114
+ @executed = true
115
+ return nil
116
+ end
117
+
118
+ end
@@ -4,220 +4,20 @@
4
4
  #
5
5
  # Licensed under the LGPL, see the file COPYING in the distribution
6
6
  #
7
- require 'antwrap_utilities'
8
- require 'dsl'
9
-
10
- class AntTask
11
- @@task_stack = Array.new
12
- attr_accessor :unknown_element, :project, :taskname, :logger, :executed
13
-
14
- public
15
- def initialize(taskname, antProject, attributes, proc)
16
- taskname = taskname[1, taskname.length-1] if taskname[0,1] == "_"
17
- @logger = antProject.logger
18
- @taskname = taskname
19
- @project_wrapper = antProject
20
- @project = antProject.project()
21
- @logger.debug(antProject.to_s)
22
- @unknown_element = create_unknown_element(@project, taskname)
23
- @logger.debug(to_s)
24
-
25
- add_attributes(attributes)
26
-
27
- if proc
28
- @logger.debug("task_stack.push #{taskname} >> #{@@task_stack}")
29
- @@task_stack.push self
30
-
31
- singleton_class = class << proc; self; end
32
- singleton_class.module_eval{
33
- def method_missing(m, *a, &proc)
34
- @@task_stack.last().send(m, *a, &proc)
35
- end
36
- }
37
- proc.instance_eval &proc
38
-
39
- @@task_stack.pop
40
- end
41
-
42
- end
43
-
44
- def to_s
45
- return self.class.name + "[#{@taskname}]"
46
- end
47
-
48
-
49
- def create_unknown_element(project, taskname)
50
-
51
- element = ApacheAnt::UnknownElement.new(taskname)
52
- element.setProject(project)
53
- element.setOwningTarget(ApacheAnt::Target.new())
54
- element.setTaskName(taskname)
55
-
56
- #DNR. This initializes the Task's Wrapper object and prevents NullPointerExeption upon execution of the task
57
- element.getRuntimeConfigurableWrapper()
58
-
59
- if(@project_wrapper.ant_version >= 1.6)
60
- element.setTaskType(taskname)
61
- element.setNamespace('')
62
- element.setQName(taskname)
63
- end
64
-
65
- return element
66
-
67
- end
68
-
69
- def send(sym, *args)
70
- begin
71
- @logger.debug("AntTask.method_missing sym[#{sym.to_s}]")
72
- task = AntTask.new(sym.to_s, @project_wrapper, args[0], block_given? ? Proc.new : nil)
73
- self.add(task)
74
- rescue StandardError
75
- @logger.error("AntTask.method_missing error:" + $!)
76
- end
77
- end
78
-
79
- # Sets each attribute on the AntTask instance.
80
- # :attributes - is a Hash.
81
- def add_attributes(attributes)
82
-
83
- return if attributes == nil
84
-
85
- wrapper = ApacheAnt::RuntimeConfigurable.new(@unknown_element, @unknown_element.getTaskName());
86
-
87
- if(@project_wrapper.ant_version >= 1.6)
88
- attributes.each do |key, val|
89
- apply_to_wrapper(wrapper, key.to_s, val){ |k,v| wrapper.setAttribute(k, v)}
90
- end
91
- else
92
- @unknown_element.setRuntimeConfigurableWrapper(wrapper)
93
- attribute_list = XmlSax::AttributeListImpl.new()
94
- attributes.each do |key, val|
95
- apply_to_wrapper(wrapper, key.to_s, val){ |k,v| attribute_list.addAttribute(k, 'CDATA', v)}
96
- end
97
- wrapper.setAttributes(attribute_list)
98
- end
99
- end
100
-
101
- def apply_to_wrapper(wrapper, key, value)
102
-
103
- raise ArgumentError, "ArgumentError: You cannot use an Array as an argument. Use the :join method instead; i.e ['file1', 'file2'].join(File::PATH_SEPARATOR)." if value.is_a?(Array)
104
-
105
- begin
106
- if(key == 'pcdata')
107
- wrapper.addText(value.to_s)
108
- else
109
- yield key, value.to_s
110
- end
111
- rescue StandardError
112
- raise ArgumentError, "ArgumentError: Unable to set :#{key} attribute with value => '#{value}'"
113
- end
114
- end
115
-
116
- #Add <em>child</em> as a child of this task.
117
- def add(child)
118
- @unknown_element.addChild(child.unknown_element())
119
- @unknown_element.getRuntimeConfigurableWrapper().addChild(child.unknown_element().getRuntimeConfigurableWrapper())
120
- end
121
7
 
122
- #Invokes the AntTask.
123
- def execute
124
- @unknown_element.maybeConfigure
125
- @unknown_element.execute
126
- @executed = true
127
- end
128
-
8
+ if(RUBY_PLATFORM == 'java')
9
+ require 'java'
10
+ autoload :ApacheAnt, 'jruby_modules.rb'
11
+ autoload :JavaLang, 'jruby_modules.rb'
12
+ autoload :XmlOrg, 'jruby_modules.rb'
13
+ else
14
+ require 'rubygems'
15
+ require 'rjb'
16
+ autoload :ApacheAnt, 'rjb_modules.rb'
17
+ autoload :JavaLang, 'rjb_modules.rb'
18
+ autoload :XmlOrg, 'rjb_modules.rb'
129
19
  end
130
20
 
131
- class AntProject
132
- require 'logger'
133
-
134
- private
135
- @@classes_loaded = false
136
-
137
- def init_project(options)
138
- @project= ApacheAnt::Project.new
139
- @project.setName(options[:name] || '')
140
- @project.setDefault('')
141
- @project.setBasedir(options[:basedir] || '.')
142
- @project.init
143
- self.declarative= options[:declarative] || true
144
- default_logger = ApacheAnt::DefaultLogger.new
145
- default_logger.setMessageOutputLevel(2)
146
- default_logger.setOutputPrintStream(options[:outputstr] || JavaLang::System.out)
147
- default_logger.setErrorPrintStream(options[:errorstr] || JavaLang::System.err)
148
- default_logger.setEmacsMode(false)
149
- @project.addBuildListener(default_logger)
150
- end
151
-
152
- public
153
- attr :project, false
154
- attr :ant_version, false
155
- attr_accessor(:declarative, :logger)
156
-
157
- # Create an AntProject. Parameters are specified via a hash:
158
- # :ant_home=><em>Ant basedir</em>
159
- # -A String indicating the location of the ANT_HOME directory. If provided, Antwrap will
160
- # load the classes from the ANT_HOME/lib dir. If ant_home is not provided, the Ant jar files
161
- # must be available in the CLASSPATH.
162
- # :name=><em>project_name</em>
163
- # -A String indicating the name of this project.
164
- # :basedir=><em>project_basedir</em>
165
- # -A String indicating the basedir of this project. Corresponds to the 'basedir' attribute
166
- # on an Ant project.
167
- # :declarative=><em>declarative_mode</em>
168
- # -A boolean value indicating wether Ant tasks created by this project instance should
169
- # have their execute() method invoked during their creation. For example, with
170
- # the option :declarative=>true the following task would execute;
171
- # @antProject.echo(:message => "An Echo Task")
172
- # However, with the option :declarative=>false, the programmer is required to execute the
173
- # task explicitly;
174
- # echoTask = @antProject.echo(:message => "An Echo Task")
175
- # echoTask.execute()
176
- # Default value is <em>true</em>.
177
- # :logger=><em>Logger</em>
178
- # -A Logger instance. Defaults to Logger.new(STDOUT)
179
- # :loglevel=><em>The level to set the logger to</em>
180
- # -Defaults to Logger::ERROR
181
- def initialize(options=Hash.new)
182
- @logger = options[:logger] || Logger.new(STDOUT)
183
- @logger.level = options[:loglevel] || Logger::ERROR
184
-
185
- if(!@@classes_loaded && options[:ant_home])
186
- @logger.debug("loading ant jar files. Ant_Home: #{options[:ant_home]}")
187
- AntwrapClassLoader.load_ant_libs(options[:ant_home])
188
- @@classes_loaded = true
189
- end
190
-
191
- @logger.debug(ApacheAnt::Main.getAntVersion())
192
- @ant_version = ApacheAnt::Main.getAntVersion()[/\d\.\d\.\d/].to_f
193
- init_project(options)
194
-
195
- end
196
-
197
- def method_missing(sym, *args)
198
- begin
199
- @logger.debug("AntProject.method_missing sym[#{sym.to_s}]")
200
- task = AntTask.new(sym.to_s, self, args[0], block_given? ? Proc.new : nil)
201
- task.execute if declarative
202
- return task
203
- rescue
204
- @logger.error("Error instantiating '#{sym.to_s}' task; " + $!)
205
- raise
206
- end
207
- end
208
-
209
- #The Ant Project's name. Default is ''
210
- def name()
211
- return @project.getName
212
- end
213
-
214
- #The Ant Project's basedir. Default is '.'
215
- def basedir()
216
- return @project.getBaseDir().getAbsolutePath();
217
- end
218
-
219
- def to_s
220
- return self.class.name + "[#{@project.getName()}]"
221
- end
222
-
223
- end
21
+ require 'antwrap_utilities'
22
+ require 'ant_project'
23
+ require 'ant_task'