Antwrap 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,29 +53,10 @@ circumstances):
53
53
  <pre><li>There are some reserved words that we have to work around. For example, Ant-Contrib tasks such as 'if' and 'else'
54
54
  conflict with the Ruby reserved words. Under most circumstances, you won't need to use these tasks (indeed, the
55
55
  awkwardness of conditional operations in Ant scripts is likely one of the reasons why you want to move to a build system
56
- such as Rake).
57
-
58
- Nevertheless, there are that occasions demands it. One such situation is the &lt;java&gt; Ant task. The 'java'
59
- symbol conflicts with a symbol in the JRuby library.
60
-
61
- Reserved words like this can be worked around by simply prepending an underscore character ('_') to the task:
56
+ such as Rake). Nevertheless, there are that occasions demands it. Reserved words like this can be worked around by simply prepending an
57
+ underscore character ('_') to the task:
62
58
 
63
59
  <code lang="ruby">
64
- @ant._java(:classname => 'foo.bar.FooBar', :fork => 'false') {
65
- arg(:value => 'argOne')
66
- classpath(){
67
- pathelement(:location => '${output_dir}/classes')
68
- pathelement(:location => '${resource_dir}/parent.jar')
69
- }
70
- arg(:value => 'argTwo')
71
- jvmarg(:value => 'client')
72
- sysproperty(:key=> 'antwrap', :value => 'coolio')
73
- }
74
-
75
- #Because the 'java' Ant task is likely to be very popular, there is an alias you can use called 'jvm'.
76
- #The above task could be rewritten as:
77
- @ant.jvm(:classname => 'foo.bar.FooBar', :fork => 'false') {...
78
-
79
60
  #This is an example of the Ant-Contrib tasks.
80
61
  #note: I added an underscore to 'equals' even though it isn't a reserved word.
81
62
  #This makes the code block more symmetrical (it's not required though). It also
@@ -4,43 +4,39 @@
4
4
  #
5
5
  # Licensed under the LGPL, see the file COPYING in the distribution
6
6
  #
7
- require 'fileutils'
8
- require 'logger'
9
- require 'java'
10
7
 
11
8
  module ApacheAnt
12
- include_class "org.apache.tools.ant.UnknownElement"
13
- include_class "org.apache.tools.ant.RuntimeConfigurable"
14
- include_class "org.apache.tools.ant.Project"
9
+ require 'java'
15
10
  include_class "org.apache.tools.ant.DefaultLogger"
16
- include_class "org.apache.tools.ant.Target"
17
11
  include_class "org.apache.tools.ant.Main"
12
+ include_class "org.apache.tools.ant.Project"
13
+ include_class "org.apache.tools.ant.RuntimeConfigurable"
14
+ include_class "org.apache.tools.ant.Target"
15
+ include_class "org.apache.tools.ant.UnknownElement"
18
16
  end
19
17
 
20
18
  module JavaLang
19
+ require 'java'
21
20
  include_class "java.lang.System"
22
21
  end
23
22
 
24
23
  class AntTask
25
24
  private
26
25
  @@task_stack = Array.new
27
- attr_reader :unknown_element, :project, :taskname, :logger
26
+ attr_reader :unknown_element, :project, :taskname, :logger, :executed
28
27
 
29
28
  public
30
29
  def initialize(taskname, antProject, attributes, proc)
31
- if(taskname[0,1] == "_")
32
- taskname = taskname[1, taskname.length-1]
33
- end
30
+ taskname = taskname[1, taskname.length-1] if taskname[0,1] == "_"
34
31
  @logger = antProject.logger
35
- @logger.debug("AntTask.taskname: #{taskname}, antProject:#{antProject}, atts:#{attributes.to_s}, proc:#{proc.to_s}")
36
32
  @taskname = taskname
37
33
  @project_wrapper = antProject
38
34
  @project = antProject.project()
39
- @logger.debug(@project)
35
+ @logger.debug(antProject.to_s)
40
36
  @unknown_element = create_unknown_element(@project, taskname)
41
- @logger.debug("1")
37
+ @logger.debug(to_s)
38
+
42
39
  addAttributes(attributes)
43
- @logger.debug("2")
44
40
 
45
41
  if proc
46
42
  @logger.debug("task_stack.push #{taskname} >> #{@@task_stack}")
@@ -55,43 +51,52 @@ class AntTask
55
51
  proc.instance_eval &proc
56
52
  @@task_stack.pop
57
53
  end
58
- @logger.debug("3")
59
54
 
60
55
  end
61
56
 
62
57
  def create_unknown_element(project, taskname)
58
+
63
59
  unknown_element = ApacheAnt::UnknownElement.new(taskname)
64
60
  unknown_element.project= project
65
61
  unknown_element.owningTarget= ApacheAnt::Target.new()
66
- unknown_element.namespace= ''
67
- unknown_element.QName= taskname
68
- unknown_element.taskType= taskname
69
62
  unknown_element.taskName= taskname
63
+
64
+ if(@project_wrapper.ant_version >= 1.6)
65
+ unknown_element.taskType= taskname
66
+ unknown_element.namespace= ''
67
+ unknown_element.QName= taskname
68
+ end
69
+
70
70
  return unknown_element
71
+
71
72
  end
72
73
 
73
74
  def addAttributes(attributes)
75
+
76
+ return if attributes == nil
77
+
74
78
  wrapper = ApacheAnt::RuntimeConfigurable.new(@unknown_element, @unknown_element.getTaskName());
75
- attributes.each do |key, val|
76
- if(key.to_s != 'pcdata')
77
- wrapper.setAttribute(key.to_s, val)
78
- else
79
- wrapper.addText(val)
79
+ outer_func = lambda{ |key, val, tfunc| key == 'pcdata' ? wrapper.addText(val) : tfunc.call(key, val) }
80
+
81
+ if(@project_wrapper.ant_version >= 1.6)
82
+ attributes.each do |key, val|
83
+ outer_func.call(key.to_s, val, lambda{|k,v| wrapper.setAttribute(k, val)})
80
84
  end
81
- end unless attributes == nil
85
+ else
86
+ @unknown_element.setRuntimeConfigurableWrapper(wrapper)
87
+ attribute_list = org.xml.sax.helpers.AttributeListImpl.new()
88
+ attributes.each do |key, val|
89
+ outer_func.call(key.to_s, val, lambda{|k,v| attribute_list.addAttribute(k, 'CDATA', v)})
90
+ end
91
+ wrapper.setAttributes(attribute_list)
92
+ end
93
+
82
94
  end
83
95
 
84
96
  def method_missing(sym, *args)
85
- @logger.debug("AntTask.method_missing sym[#{sym.to_s}]")
86
97
  begin
87
- proc = block_given? ? Proc.new : nil
88
- if(@project_wrapper.ant_version < 1.6)
89
- @logger.debug("Creating a 1.5. task")
90
- task = Ant15Task.new(sym.to_s, @project_wrapper, args[0], proc)
91
- else
92
- task = AntTask.new(sym.to_s, @project_wrapper, args[0], proc)
93
- end
94
-
98
+ @logger.debug("AntTask.method_missing sym[#{sym.to_s}]")
99
+ task = AntTask.new(sym.to_s, @project_wrapper, args[0], block_given? ? Proc.new : nil)
95
100
  self.add(task)
96
101
  rescue StandardError
97
102
  @logger.error("AntTask.method_missing error:" + $!)
@@ -99,7 +104,7 @@ class AntTask
99
104
  end
100
105
 
101
106
  def add(child)
102
- @logger.debug("adding child[#{child.unknown_element().getTaskName()}] to [#{@unknown_element.getTaskName()}]")
107
+ @logger.debug("adding child[#{child.taskname}] to [#{@taskname}]")
103
108
  @unknown_element.addChild(child.unknown_element())
104
109
  @unknown_element.getRuntimeConfigurableWrapper().addChild(child.unknown_element().getRuntimeConfigurableWrapper())
105
110
  end
@@ -110,56 +115,14 @@ class AntTask
110
115
  @executed = true
111
116
  end
112
117
 
113
- def was_executed?
114
- @executed
115
- end
116
-
117
- #overridden. 'mkdir' conflicts wth the rake library.
118
- def mkdir(attributes)
119
- create_task('mkdir', attributes, (block_given? ? Proc.new : nil))
120
- end
121
-
122
- #overridden. 'copy' conflicts wth the rake library.
123
- def copy(attributes)
124
- create_task('copy', attributes, (block_given? ? Proc.new : nil))
125
- end
126
-
127
- #overridden. 'java' conflicts wth the JRuby library.
128
- def jvm(attributes=Hash.new)
129
- create_task('java', attributes, (block_given? ? Proc.new : nil))
130
- end
131
-
132
- end
133
-
134
- class Ant15Task < AntTask
135
-
136
- def create_unknown_element(project, taskname)
137
- unknown_element = ApacheAnt::UnknownElement.new(taskname)
138
- unknown_element.project= project
139
- unknown_element.owningTarget= ApacheAnt::Target.new()
140
- unknown_element.taskName= taskname
141
- return unknown_element
142
- end
143
-
144
- def addAttributes(attributes)
145
- wrapper = ApacheAnt::RuntimeConfigurable.new(@unknown_element, @unknown_element.getTaskName());
146
- @unknown_element.setRuntimeConfigurableWrapper(wrapper)
147
- attribute_list = org.xml.sax.helpers.AttributeListImpl.new
148
-
149
- attributes.each do |key, val|
150
- if(key.to_s != 'pcdata')
151
- attribute_list.addAttribute(key.to_s, 'CDATA', val)
152
- else
153
- wrapper.addText(val)
154
- end
155
- end unless attributes == nil
156
- wrapper.setAttributes(attribute_list)
157
- end
118
+ def to_s
119
+ return self.class.name + "[#{@taskname}]"
120
+ end
158
121
 
159
122
  end
160
123
 
161
124
  class AntProject
162
-
125
+ require 'logger'
163
126
  attr :project, false
164
127
  attr :version, false
165
128
  attr :ant_version, true
@@ -196,8 +159,8 @@ class AntProject
196
159
  self.declarative= options[:declarative] || true
197
160
  default_logger = ApacheAnt::DefaultLogger.new
198
161
  default_logger.messageOutputLevel= 2
199
- default_logger.outputPrintStream= JavaLang::System.out
200
- default_logger.errorPrintStream= JavaLang::System.err
162
+ default_logger.outputPrintStream= options[:outputstr] || JavaLang::System.out
163
+ default_logger.errorPrintStream= options[:errorstr] || JavaLang::System.err
201
164
  default_logger.emacsMode= false
202
165
  @project.addBuildListener default_logger
203
166
  @version = ApacheAnt::Main.getAntVersion
@@ -211,25 +174,15 @@ class AntProject
211
174
  @logger.debug("Antproject.create_task.taskname = " + taskname)
212
175
  @logger.debug("Antproject.create_task.attributes = " + attributes.to_s)
213
176
 
214
- task = nil;
215
- if(ant_version < 1.6)
216
- task = Ant15Task.new(taskname, self, attributes, proc)
217
- else
218
- task = AntTask.new(taskname, self, attributes, proc)
219
- end
220
-
177
+ task = AntTask.new(taskname, self, attributes, proc)
221
178
  task.execute if declarative
222
- if taskname == 'macrodef'
223
- @logger.debug("Pushing #{attributes[:name]} to tasks")
224
- end
225
- task
179
+ return task
226
180
  end
227
181
 
228
182
  def method_missing(sym, *args)
229
183
  begin
230
- @logger.info("AntProject.method_missing sym[#{sym.to_s}]")
231
- proc = block_given? ? Proc.new : nil
232
- return create_task(sym.to_s, args[0], proc)
184
+ @logger.debug("AntProject.method_missing sym[#{sym.to_s}]")
185
+ return create_task(sym.to_s, args[0], block_given? ? Proc.new : nil)
233
186
  rescue
234
187
  @logger.error("Error instantiating task[#{sym.to_s}]" + $!)
235
188
  end
@@ -243,19 +196,14 @@ class AntProject
243
196
  return @project.getBaseDir().getAbsolutePath();
244
197
  end
245
198
 
246
- #overridden. 'mkdir' conflicts wth the rake library.
247
- def mkdir(attributes)
248
- create_task('mkdir', attributes, (block_given? ? Proc.new : nil))
249
- end
199
+ def to_s
200
+ return self.class.name + "[#{@project.name}]"
201
+ end
250
202
 
251
- #overridden. 'copy' conflicts wth the rake library.
252
- def copy(attributes)
253
- create_task('copy', attributes, (block_given? ? Proc.new : nil))
254
- end
255
-
256
- #overridden. 'java' conflicts wth the JRuby library.
257
- def jvm(attributes=Hash.new)
203
+ #This method invokes create_task. It is here to prevent conflicts wth the JRuby library
204
+ #over the 'java' symbol.
205
+ def java(attributes=Hash.new)
258
206
  create_task('java', attributes, (block_given? ? Proc.new : nil))
259
207
  end
260
208
 
261
- end
209
+ end
@@ -0,0 +1,101 @@
1
+ require 'antwrap.rb'
2
+
3
+ module Raven
4
+
5
+ # The AntBuilderTask lets you execute Ant tasks directly from
6
+ # your rakefile. For example:
7
+ #
8
+ # ant 'runant' do
9
+ # echo(:text => "Using echo Ant task.")
10
+ # end
11
+ #
12
+ # This task is different from other Raven tasks because its
13
+ # body (or block if you prefer) isn't executed by Rake but
14
+ # within the Ant interpreter (AntBuilder). Therefore the
15
+ # task object isn't available within the code block as with
16
+ # other tasks.
17
+ #
18
+ # For more information about the syntax to use to call Ant
19
+ # tasks see http://antbuilder.rubyforge.org/
20
+ class AntBuilderTask < Rake::Task
21
+ attr_writer :body
22
+
23
+ def execute
24
+ super
25
+ current_dir = application.original_dir()
26
+ ant_project = AntProject.new({:name=>"testProject", :basedir=>current_dir, :declarative=>true,
27
+ :logger=>Logger.new(STDOUT), :loglevel=>Logger::ERROR})
28
+ ant_project.instance_eval(&@body)
29
+ end
30
+ end
31
+ end
32
+
33
+ def ant(args, &block)
34
+ ant_task = Raven::AntBuilderTask.define_task(args)
35
+ ant_task.body = block
36
+ end
37
+
38
+ ant 'runant' do
39
+ mkdir(:dir => 'foo')
40
+ echo(:message => "Using echo Ant task.")
41
+ echo(:pcdata => "Using echo Ant task.")
42
+ end
43
+
44
+ ant :ant_echo do
45
+ echo(:message => "Calling Ant <echo> task")
46
+ end
47
+
48
+ ant 'antcontrib' do
49
+ taskdef(:resource => "net/sf/antcontrib/antlib.xml")
50
+
51
+ property(:name => "bar", :value => "bar")
52
+ _if(){
53
+ equals(:arg1 => "${bar}", :arg2 => "bar")
54
+ _then(){
55
+ echo(:message => "if 1 is equal")
56
+ }
57
+ _else(){
58
+ echo(:message => "if 1 is not equal")
59
+ }
60
+ }
61
+
62
+ property(:name => "baz", :value => "foo")
63
+ _if(){
64
+ equals(:arg1 => "${baz}", :arg2 => "bar")
65
+ _then(){
66
+ echo(:message => "if 2 is equal")
67
+ }
68
+ _else(){
69
+ echo(:message => "if 2 is not equal")
70
+ }
71
+ }
72
+ end
73
+
74
+ ant 'macrodef' do
75
+ dir = 'foo'
76
+
77
+ assert_absent dir
78
+
79
+ macrodef(:name => 'testmacrodef'){
80
+ attribute(:name => 'destination')
81
+ sequential(){
82
+ echo(:message => "Creating @{destination}")
83
+ _mkdir(:dir => "@{destination}")
84
+ }
85
+ }
86
+ testmacrodef(:destination => dir)
87
+ assert_exists dir
88
+
89
+ puts "Cleaning up and deleting #{dir}"
90
+
91
+ Dir.delete dir
92
+
93
+ end
94
+
95
+ def assert_exists(file_path)
96
+ puts "Does not exist[#{file_path}]" if !File.exists?(file_path)
97
+ end
98
+
99
+ def assert_absent(file_path)
100
+ puts "Should not exist[#{file_path}]" if File.exists?(file_path)
101
+ end
@@ -0,0 +1,3 @@
1
+ Manifest-Version: 1.0
2
+ Created-By: 1.5.0_06 (Apple Computer, Inc.)
3
+
@@ -7,6 +7,26 @@
7
7
  require 'test/unit'
8
8
  require 'fileutils'
9
9
  require '../lib/antwrap.rb'
10
+ require 'java'
11
+ class TestStream < java.io.PrintStream
12
+ attr_reader :last_line
13
+
14
+ def initialise(out)
15
+ self.super(out)
16
+ end
17
+
18
+ def println(s)
19
+ puts "s"
20
+ @last_line = s
21
+ self.super(s)
22
+ end
23
+
24
+ def print(s)
25
+ puts "s"
26
+ @last_line = s
27
+ self.super(s)
28
+ end
29
+ end
10
30
 
11
31
  class TestAntwrap < Test::Unit::TestCase
12
32
 
@@ -134,7 +154,7 @@ class TestAntwrap < Test::Unit::TestCase
134
154
 
135
155
  @ant.property(:name => 'output_dir', :value => @output_dir)
136
156
  @ant.property(:name => 'resource_dir', :value =>@resource_dir)
137
- @ant._java(:classname => 'foo.bar.FooBar', :fork => 'false') {
157
+ @ant.java(:classname => 'foo.bar.FooBar', :fork => 'false') {
138
158
  arg(:value => 'argOne')
139
159
  classpath(){
140
160
  pathelement(:location => '${output_dir}/classes')
@@ -147,7 +167,14 @@ class TestAntwrap < Test::Unit::TestCase
147
167
  end
148
168
 
149
169
  def test_echo_task
150
- @ant.echo(:message => "Antwrap is running an Echo task", :level => 'info')
170
+ # stream = TestStream.new(java.lang.System.out)
171
+ #
172
+ # @ant = AntProject.new({:name=>"testProject", :basedir=>@current_dir, :declarative=>true,
173
+ # :logger=>Logger.new(STDOUT), :loglevel=>Logger::DEBUG, :outputstr => stream})
174
+ msg = "Antwrap is running an Echo task"
175
+ @ant.echo(:message => msg, :level => 'info')
176
+ # assert(stream.last_line, stream.last_line == msg)
177
+
151
178
  @ant.echo(:pcdata => "<foo&bar>")
152
179
  end
153
180
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: Antwrap
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.3"
7
- date: 2007-02-19
6
+ version: "0.4"
7
+ date: 2007-03-04
8
8
  summary: "A JRuby module that wraps the Apache Ant build tool, enabling Ant Tasks to be
9
9
  invoked from a JRuby script."
10
10
  require_paths:
@@ -30,12 +30,15 @@ authors:
30
30
  files:
31
31
  - lib/antwrap.rb
32
32
  - lib/convert.rb
33
+ - lib/rakefile.rb
33
34
  - test/output
34
35
  - test/tc_antwrap.rb
35
36
  - test/tc_convert.rb
36
- - test/test
37
37
  - test/test-resources
38
- - test/test/output
38
+ - test/output/META-INF
39
+ - test/output/parent
40
+ - test/output/META-INF/MANIFEST.MF
41
+ - test/output/parent/FooBarParent.class
39
42
  - test/test-resources/build.xml
40
43
  - test/test-resources/foo.txt
41
44
  - test/test-resources/foo.zip