Antwrap 0.5.0-java → 0.5.2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,14 @@
1
- <html><head><title>Antwrap - Invoke Ant tasks from your JRuby script!</title></head><body><ul>
1
+ <html><head><title>Antwrap - Invoke Ant tasks from your Ruby/JRuby scripts!</title></head><body><ul>
2
2
  <pre><li>Sample code of the Antwrap library looks like this;
3
3
 
4
4
  <code lang="ruby">
5
5
  #All options are optional. The defaults are as follows;
6
6
  #{:name=&gt'', :basedir=&gt'.', :declarative=&gt true, :logger=&gt Logger.new(STDOUT), :loglevel=&gt Logger::DEBUG}
7
- #so you can call AntProject.new() if you like.
7
+ #so you can call AntProject.new() if you like. If you specify an :ant_home property, Antwrap will load the jar files
8
+ for you dynamically. If not, it is assumed that they are in your Classpath.
8
9
 
9
- @ant = AntProject.new({:name=&gt"FooProject",
10
+ @ant = AntProject.new({:ant_home=&gt"/Users/fooman/tools/apache-ant-1.7.0",
11
+ :name=&gt"FooProject",
10
12
  :basedir=&gt current_dir,
11
13
  :declarative=&gt true,
12
14
  :logger=&gt Logger.new(STDOUT),
@@ -88,7 +90,9 @@ underscore (see above).
88
90
 
89
91
  </li>
90
92
  <li>Antwrap is a Beta release. I'd love any feedback you can provide on your experence with it.
91
- There are no 3rd party jars required other than the Ant jar files in your JRuby Classpath.
93
+ There are no 3rd party jars required other than the Ant jar files in your Classpath. Antwrap will run
94
+ on both the Ruby and the JRuby platforms. The Ruby gem requires the Ruby Java Bridge (RJG) gem
95
+ in order to instantiate Java objects.
92
96
  </li></pre>
93
97
 
94
98
  </ul>
@@ -1,48 +1,15 @@
1
- # antwrap
1
+ # antwrap.rb
2
2
  #
3
3
  # Copyright Caleb Powell 2007
4
4
  #
5
5
  # Licensed under the LGPL, see the file COPYING in the distribution
6
6
  #
7
-
8
- if(RUBY_PLATFORM == 'java')
9
- require 'jruby_modules.rb'
10
- else
11
- require 'rjb_modules.rb'
12
- end
7
+ require 'antwrap_utilities'
13
8
 
14
9
  class AntTask
15
- private
16
10
  @@task_stack = Array.new
17
11
  attr_accessor(:unknown_element, :project, :taskname, :logger, :executed)
18
12
 
19
- def create_unknown_element(project, taskname)
20
-
21
- element = ApacheAnt::UnknownElement.new(taskname)
22
- element.setProject(project)
23
- element.setOwningTarget(ApacheAnt::Target.new())
24
- element.setTaskName(taskname)
25
-
26
- if(@project_wrapper.ant_version >= 1.6)
27
- element.setTaskType(taskname)
28
- element.setNamespace('')
29
- element.setQName(taskname)
30
- end
31
-
32
- return element
33
-
34
- end
35
-
36
- def method_missing(sym, *args)
37
- begin
38
- @logger.debug("AntTask.method_missing sym[#{sym.to_s}]")
39
- task = AntTask.new(sym.to_s, @project_wrapper, args[0], block_given? ? Proc.new : nil)
40
- self.add(task)
41
- rescue StandardError
42
- @logger.error("AntTask.method_missing error:" + $!)
43
- end
44
- end
45
-
46
13
  public
47
14
  def initialize(taskname, antProject, attributes, proc)
48
15
  taskname = taskname[1, taskname.length-1] if taskname[0,1] == "_"
@@ -54,10 +21,10 @@ class AntTask
54
21
  @unknown_element = create_unknown_element(@project, taskname)
55
22
  @logger.debug(to_s)
56
23
 
57
- addAttributes(attributes)
24
+ add_attributes(attributes)
58
25
 
59
26
  if proc
60
- # @logger.debug("task_stack.push #{taskname} >> #{@@task_stack}")
27
+ @logger.debug("task_stack.push #{taskname} >> #{@@task_stack}")
61
28
  @@task_stack.push self
62
29
 
63
30
  singleton_class = class << proc; self; end
@@ -72,10 +39,44 @@ class AntTask
72
39
 
73
40
  end
74
41
 
42
+ def to_s
43
+ return self.class.name + "[#{@taskname}]"
44
+ end
45
+
46
+
47
+ def create_unknown_element(project, taskname)
48
+
49
+ element = ApacheAnt::UnknownElement.new(taskname)
50
+ element.setProject(project)
51
+ element.setOwningTarget(ApacheAnt::Target.new())
52
+ element.setTaskName(taskname)
53
+
54
+ #DNR. This initializes the Task's Wrapper object and prevents NullPointerExeption upon execution of the task
55
+ element.getRuntimeConfigurableWrapper()
56
+
57
+ if(@project_wrapper.ant_version >= 1.6)
58
+ element.setTaskType(taskname)
59
+ element.setNamespace('')
60
+ element.setQName(taskname)
61
+ end
62
+
63
+ return element
64
+
65
+ end
66
+
67
+ def method_missing(sym, *args)
68
+ begin
69
+ @logger.debug("AntTask.method_missing sym[#{sym.to_s}]")
70
+ task = AntTask.new(sym.to_s, @project_wrapper, args[0], block_given? ? Proc.new : nil)
71
+ self.add(task)
72
+ rescue StandardError
73
+ @logger.error("AntTask.method_missing error:" + $!)
74
+ end
75
+ end
75
76
 
76
77
  # Sets each attribute on the AntTask instance.
77
78
  # :attributes - is a Hash.
78
- def addAttributes(attributes)
79
+ def add_attributes(attributes)
79
80
 
80
81
  return if attributes == nil
81
82
 
@@ -99,14 +100,10 @@ class AntTask
99
100
 
100
101
  #Add <em>child</em> as a child of this task.
101
102
  def add(child)
102
- # @logger.debug("adding child[#{child.taskname()}] to [#{@taskname}]")
103
- @unknown_element.addChild(child.getUnknownElement())
104
- @unknown_element.getRuntimeConfigurableWrapper().addChild(child.getUnknownElement().getRuntimeConfigurableWrapper())
105
- end
106
-
107
- def getUnknownElement
108
- return @unknown_element
103
+ @unknown_element.addChild(child.unknown_element())
104
+ @unknown_element.getRuntimeConfigurableWrapper().addChild(child.unknown_element().getRuntimeConfigurableWrapper())
109
105
  end
106
+
110
107
  #Invokes the AntTask.
111
108
  def execute
112
109
  @unknown_element.maybeConfigure
@@ -114,24 +111,41 @@ class AntTask
114
111
  @executed = true
115
112
  end
116
113
 
117
- def to_s
118
- return self.class.name + "[#{@taskname}]"
119
- end
120
-
121
114
  end
122
115
 
123
116
  class AntProject
124
117
  require 'logger'
118
+
119
+ private
120
+ @@classes_loaded = false
121
+
122
+ def init_project(options)
123
+ @project= ApacheAnt::Project.new
124
+ @project.setName(options[:name] || '')
125
+ @project.setDefault('')
126
+ @project.setBasedir(options[:basedir] || '.')
127
+ @project.init
128
+ self.declarative= options[:declarative] || true
129
+ default_logger = ApacheAnt::DefaultLogger.new
130
+ default_logger.setMessageOutputLevel(2)
131
+ default_logger.setOutputPrintStream(options[:outputstr] || JavaLang::System.out)
132
+ default_logger.setErrorPrintStream(options[:errorstr] || JavaLang::System.err)
133
+ default_logger.setEmacsMode(false)
134
+ @project.addBuildListener(default_logger)
135
+ end
136
+
137
+ public
125
138
  attr :project, false
126
- attr :version, false
127
- attr :ant_version, true
128
- attr :declarative, true
129
- attr :logger, true
139
+ attr :ant_version, false
140
+ attr_accessor(:declarative, :logger)
130
141
 
131
142
  # Create an AntProject. Parameters are specified via a hash:
143
+ # :ant_home=><em>Ant basedir</em>
144
+ # -A String indicating the location of the ANT_HOME directory. If provided, Antwrap will
145
+ # load the classes from the ANT_HOME/lib dir. If ant_home is not provided, the Ant jar files
146
+ # must be available in the CLASSPATH.
132
147
  # :name=><em>project_name</em>
133
- # -A String indicating the name of this project. Corresponds to the
134
- # 'name' attrbute on an Ant project.
148
+ # -A String indicating the name of this project.
135
149
  # :basedir=><em>project_basedir</em>
136
150
  # -A String indicating the basedir of this project. Corresponds to the 'basedir' attribute
137
151
  # on an Ant project.
@@ -150,49 +164,41 @@ class AntProject
150
164
  # :loglevel=><em>The level to set the logger to</em>
151
165
  # -Defaults to Logger::ERROR
152
166
  def initialize(options=Hash.new)
153
- @project= ApacheAnt::Project.new
154
- @project.setName(options[:name] || '')
155
- @project.setDefault('')
156
- @project.setBasedir(options[:basedir] || '.')
157
- @project.init
158
- self.declarative= options[:declarative] || true
159
- default_logger = ApacheAnt::DefaultLogger.new
160
- default_logger.setMessageOutputLevel(2)
161
- default_logger.setOutputPrintStream(options[:outputstr] || JavaLang::System.out)
162
- default_logger.setErrorPrintStream(options[:errorstr] || JavaLang::System.err)
163
- default_logger.setEmacsMode(false)
164
- @project.addBuildListener(default_logger)
165
- @version = ApacheAnt::Main.getAntVersion
166
- @ant_version = @version[/\d\.\d\.\d/].to_f
167
+
167
168
  @logger = options[:logger] || Logger.new(STDOUT)
168
169
  @logger.level = options[:loglevel] || Logger::ERROR
169
- @logger.debug(@version)
170
- end
171
-
172
- def create_task(taskname, attributes, proc)
173
- @logger.debug("AntProject.create_task.taskname = " + taskname)
174
- @logger.debug("AntProject.create_task.attributes = " + attributes.to_s)
175
-
176
- task = AntTask.new(taskname, self, attributes, proc)
177
- task.execute if declarative
178
- return task
170
+
171
+ if(!@@classes_loaded && options[:ant_home])
172
+ @logger.debug("loading ant jar files. Ant_Home: #{options[:ant_home]}")
173
+ AntwrapClassLoader.load_ant_libs(options[:ant_home])
174
+ @@classes_loaded = true
175
+ end
176
+
177
+ @logger.debug(ApacheAnt::Main.getAntVersion())
178
+ @ant_version = ApacheAnt::Main.getAntVersion()[/\d\.\d\.\d/].to_f
179
+
180
+ init_project(options)
181
+
179
182
  end
180
183
 
181
184
  def method_missing(sym, *args)
182
185
  begin
183
186
  @logger.debug("AntProject.method_missing sym[#{sym.to_s}]")
184
- return create_task(sym.to_s, args[0], block_given? ? Proc.new : nil)
187
+ task = AntTask.new(sym.to_s, self, args[0], block_given? ? Proc.new : nil)
188
+ task.execute if declarative
189
+ return task
185
190
  rescue
186
191
  @logger.error("Error instantiating task[#{sym.to_s}]" + $!)
192
+ throw $!
187
193
  end
188
194
  end
189
195
 
190
- #The Ant Project's name. Default is ''.
196
+ #The Ant Project's name. Default is ''
191
197
  def name()
192
198
  return @project.getName
193
199
  end
194
200
 
195
- #The Ant Project's basedir. Default is '.'.
201
+ #The Ant Project's basedir. Default is '.'
196
202
  def basedir()
197
203
  return @project.getBaseDir().getAbsolutePath();
198
204
  end
@@ -200,11 +206,5 @@ class AntProject
200
206
  def to_s
201
207
  return self.class.name + "[#{@project.getName()}]"
202
208
  end
203
-
204
- #This method invokes create_task. It is here to prevent conflicts wth the JRuby library
205
- #over the 'java' symbol.
206
- def java(attributes=Hash.new)
207
- create_task('java', attributes, (block_given? ? Proc.new : nil))
208
- end
209
209
 
210
210
  end
@@ -0,0 +1,29 @@
1
+ if(RUBY_PLATFORM == 'java')
2
+ require 'java'
3
+ autoload :ApacheAnt, 'jruby_modules.rb'
4
+ autoload :JavaLang, 'jruby_modules.rb'
5
+ else
6
+ require 'rubygems'
7
+ require 'rjb'
8
+ autoload :ApacheAnt, 'rjb_modules.rb'
9
+ autoload :JavaLang, 'rjb_modules.rb'
10
+ end
11
+
12
+ module AntwrapClassLoader
13
+ require 'find'
14
+ def match(*paths)
15
+ matched=[]
16
+ Find.find(*paths){ |path| matched << path if yield path }
17
+ return matched
18
+ end
19
+
20
+ def load_ant_libs(ant_home)
21
+ jars = match(ant_home + '/lib') {|p| ext = p[-4...p.size]; ext && ext.downcase == '.jar'}
22
+ if(RUBY_PLATFORM == 'java')
23
+ jars.each {|jar| require jar }
24
+ else
25
+ Rjb::load(jars.join(":"), [])
26
+ end
27
+ end
28
+ module_function :match, :load_ant_libs
29
+ end
@@ -0,0 +1,104 @@
1
+ # antwrap
2
+ #
3
+ # Copyright Caleb Powell 2007
4
+ #
5
+ # Licensed under the LGPL, see the file COPYING in the distribution
6
+ #
7
+ require 'rexml/document'
8
+
9
+ if ARGV.empty?
10
+ puts "Usage: #{$0} [antfile] [rakefile]"
11
+ exit! 1
12
+ end
13
+
14
+ @antfile = File.open(ARGV[0])
15
+ @rakefile = File.new(ARGV[1], 'w+')
16
+ @@reserved_words = ['alias', 'and', 'BEGIN', 'begin', 'break', 'case', 'class',
17
+ 'def', 'defined', 'do', 'else', 'elsif', 'END', 'end', 'ensure',
18
+ 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', 'or',
19
+ 'redo', 'rescue', 'retry', 'return', 'self', 'super', 'then', 'true',
20
+ 'undef', 'unless', 'until', 'when', 'while', 'yield', 'java']
21
+
22
+ xml = REXML::Document.new(@antfile)
23
+
24
+ puts "Converting from Ant build script[#{@antfile.path}] == to ==> \n Rakefile[#{@rakefile.path}]"
25
+
26
+ def create_symbol(str)
27
+ str = rubyize(str)
28
+ return str.gsub(/(\w*[^,\s])/, ':\1')
29
+ end
30
+
31
+ def rubyize(str)
32
+ if (str == nil)
33
+ str = ''
34
+ elsif (@@reserved_words.index(str) != nil)
35
+ str = '_' + str
36
+ else
37
+ str = str.gsub(/(\w*)[\-|\.](\w*)/, '\1_\2')
38
+ end
39
+ return str
40
+ end
41
+
42
+
43
+ @rakefile.print "require_gem 'Antwrap'\n"
44
+ @rakefile.print "@ant = AntProject.new()\n"
45
+ @one_tab= ' '
46
+ def print_task(task, tab=@one_tab, prefix='')
47
+ task_name = rubyize(task.name)
48
+ @rakefile.print "#{tab}#{prefix}#{task_name}("
49
+
50
+ if(task_name == 'macrodef')
51
+ task.attributes['name'] = rubyize(task.attributes['name'])
52
+ elsif(task_name == 'java')
53
+ task_name = 'jvm'
54
+ end
55
+
56
+ isFirst = true;
57
+ task.attributes.each do |key, value|
58
+ if !isFirst
59
+ @rakefile.print(",\n#{tab+@one_tab}")
60
+ end
61
+ @rakefile.print ":#{key} => \"#{value}\""
62
+ isFirst = false;
63
+ end
64
+
65
+ if task.has_text?
66
+ pcdata = task.texts().join
67
+ if(pcdata.strip() != '')
68
+ @rakefile.print ":pcdata => \"#{pcdata}\""
69
+ end
70
+ end
71
+ @rakefile.print ")"
72
+
73
+
74
+ if task.elements.size > 0
75
+ @rakefile.print "{"
76
+ task.elements.each do |child|
77
+ @rakefile.print "\n"
78
+ print_task(child, (tab+@one_tab), '')
79
+ end
80
+ @rakefile.print "\n#{tab}}"
81
+ end
82
+ end
83
+
84
+ xml.elements.each("/project/*") do |node|
85
+ if node.name != 'target'
86
+ print_task(node, '', '@ant.')
87
+ @rakefile.print "\n\n"
88
+ end
89
+ end
90
+
91
+ xml.elements.each("/project/target") do |node|
92
+
93
+ task = "\ntask " + create_symbol(node.attributes['name']) +
94
+ " => [" + create_symbol(node.attributes['depends']) + "] do\n"
95
+
96
+ @rakefile.print task
97
+
98
+ node.elements.each do |child|
99
+ print_task(child, @one_tab, '@ant.')
100
+ @rakefile.print "\n"
101
+ end
102
+ @rakefile.print "end\n"
103
+ end
104
+
@@ -1,5 +1,4 @@
1
1
  module ApacheAnt
2
- require 'java'
3
2
  include_class "org.apache.tools.ant.DefaultLogger"
4
3
  include_class "org.apache.tools.ant.Main"
5
4
  include_class "org.apache.tools.ant.Project"
@@ -9,6 +8,5 @@ module ApacheAnt
9
8
  end
10
9
 
11
10
  module JavaLang
12
- require 'java'
13
11
  include_class "java.lang.System"
14
12
  end
@@ -1,6 +1,4 @@
1
1
  module ApacheAnt
2
- require 'rubygems'
3
- require 'rjb'
4
2
  DefaultLogger = Rjb::import("org.apache.tools.ant.DefaultLogger")
5
3
  Main = Rjb::import("org.apache.tools.ant.Main")
6
4
  Project = Rjb::import("org.apache.tools.ant.Project")
@@ -10,7 +8,5 @@ module ApacheAnt
10
8
  end
11
9
 
12
10
  module JavaLang
13
- require 'rubygems'
14
- require 'rjb'
15
11
  System = Rjb::import("java.lang.System")
16
12
  end
@@ -0,0 +1,15 @@
1
+ <project name="antwrap" default="run-java" basedir=".">
2
+
3
+
4
+
5
+ <target name="run-java" description="Antwrap: run a trivial Java class">
6
+ <java classpath="output/classes:test-resources/parent.jar" classname="foo.bar.FooBar" fork="true">
7
+ <jvmarg value="-Dantwrap=coolio"/>
8
+ <arg value="argOne"/>
9
+ <arg value="argTwo"/>
10
+ </java>
11
+ </target>
12
+
13
+
14
+
15
+ </project>
@@ -32,16 +32,19 @@ class TestAntwrap < Test::Unit::TestCase
32
32
 
33
33
  def setup
34
34
  @current_dir = ENV['PWD']
35
+ @output_dir = @current_dir + '/test/output'
36
+ @resource_dir = @current_dir + '/test/test-resources'
37
+ @ant_home = @resource_dir + "/apache-ant-1.7.0"
38
+ # @ant_home = "/Users/caleb/tools/apache-ant-1.6.5"
39
+ # @ant_home = "/Users/caleb/tools/apache-ant-1.5.4"
35
40
  @ant_proj_props = {:name=>"testProject", :basedir=>@current_dir, :declarative=>true,
36
- :logger=>Logger.new(STDOUT), :loglevel=>Logger::DEBUG}
41
+ :logger=>Logger.new(STDOUT), :loglevel=>Logger::DEBUG, :ant_home => @ant_home}
37
42
  @ant = AntProject.new(@ant_proj_props)
38
43
  assert(@ant_proj_props[:name] == @ant.name())
39
44
 
40
45
  assert(@ant_proj_props[:basedir] == @ant.basedir())
41
46
  assert(@ant_proj_props[:declarative] == @ant.declarative())
42
47
 
43
- @output_dir = @current_dir + '/test/output'
44
- @resource_dir = @current_dir + '/test/test-resources'
45
48
 
46
49
  if File.exists?(@output_dir)
47
50
  FileUtils.remove_dir(@output_dir)
@@ -221,7 +224,7 @@ class TestAntwrap < Test::Unit::TestCase
221
224
 
222
225
  def test_ant_contrib
223
226
 
224
- return if @ant.ant_version < 1.6
227
+ return if @ant.ant_version() < 1.6
225
228
 
226
229
  @ant.taskdef(:resource => "net/sf/antcontrib/antlib.xml")
227
230
 
@@ -249,6 +252,9 @@ class TestAntwrap < Test::Unit::TestCase
249
252
 
250
253
  end
251
254
 
255
+ def test_tstamp
256
+ @ant.tstamp
257
+ end
252
258
  private
253
259
  def assert_exists(file_path)
254
260
  assert(File.exists?(file_path), "Does not exist[#{file_path}]")
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.5.0
7
- date: 2007-03-15
6
+ version: 0.5.2
7
+ date: 2007-04-17
8
8
  summary: "A Ruby module that wraps the Apache Ant build tool, enabling Ant Tasks to be
9
9
  invoked from a Ruby/JRuby scripts."
10
10
  require_paths:
@@ -29,8 +29,11 @@ authors:
29
29
  - Caleb Powell
30
30
  files:
31
31
  - lib/antwrap.rb
32
+ - lib/antwrap_utilities.rb
33
+ - lib/convert.rb
32
34
  - lib/jruby_modules.rb
33
35
  - lib/rjb_modules.rb
36
+ - test/build.xml
34
37
  - test/output
35
38
  - test/tc_antwrap.rb
36
39
  - test/tc_convert.rb