atoulme-Antwrap 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,141 @@
1
+ # Copyright 2008 Caleb Powell
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and limitations
12
+ # under the License.
13
+
14
+ module Antwrap
15
+ class AntProject
16
+ require 'logger'
17
+ require 'ant_task'
18
+
19
+ private
20
+ @@classes_loaded = false
21
+
22
+ def init_project(options)
23
+
24
+ @project= Antwrap::ApacheAnt::Project.new
25
+ @project.setName(options[:name] || '')
26
+ @project.setDefault('')
27
+ @project.setBasedir(options[:basedir] || FileUtils::pwd)
28
+ @project.init
29
+ if options[:declarative] == nil
30
+ @logger.debug("declarative is nil")
31
+ self.declarative= true
32
+ else
33
+ @logger.debug("declarative is #{options[:declarative]}")
34
+ self.declarative= options[:declarative]
35
+ end
36
+ default_logger = ApacheAnt::DefaultLogger.new
37
+ default_logger.setMessageOutputLevel(2)
38
+ default_logger.setOutputPrintStream(options[:outputstr] || JavaLang::System.out)
39
+ default_logger.setErrorPrintStream(options[:errorstr] || JavaLang::System.err)
40
+ default_logger.setEmacsMode(false)
41
+ @project.addBuildListener(default_logger)
42
+
43
+ end
44
+
45
+ public
46
+ attr :project, false
47
+ attr :ant_version, false
48
+ attr_accessor(:declarative, :logger)
49
+
50
+ # Create an AntProject. Parameters are specified via a hash:
51
+ # :ant_home=><em>Ant basedir</em>
52
+ # -A String indicating the location of the ANT_HOME directory. If provided, Antwrap will
53
+ # load the classes from the ANT_HOME/lib dir. If ant_home is not provided, the Ant jar files
54
+ # must be available in the CLASSPATH.
55
+ # :name=><em>project_name</em>
56
+ # -A String indicating the name of this project.
57
+ # :basedir=><em>project_basedir</em>
58
+ # -A String indicating the basedir of this project. Corresponds to the 'basedir' attribute
59
+ # on an Ant project.
60
+ # :declarative=><em>declarative_mode</em>
61
+ # -A boolean value indicating wether Ant tasks created by this project instance should
62
+ # have their execute() method invoked during their creation. For example, with
63
+ # the option :declarative=>true the following task would execute;
64
+ # @antProject.echo(:message => "An Echo Task")
65
+ # However, with the option :declarative=>false, the programmer is required to execute the
66
+ # task explicitly;
67
+ # echoTask = @antProject.echo(:message => "An Echo Task")
68
+ # echoTask.execute()
69
+ # Default value is <em>true</em>.
70
+ # :logger=><em>Logger</em>
71
+ # -A Logger instance. Defaults to Logger.new(STDOUT)
72
+ # :loglevel=><em>The level to set the logger to</em>
73
+ # -Defaults to Logger::ERROR
74
+ def initialize(options=Hash.new)
75
+
76
+ @logger = options[:logger] || Logger.new(STDOUT)
77
+ @logger.level = options[:loglevel] || Logger::ERROR
78
+
79
+ if(!@@classes_loaded && options[:ant_home])
80
+ @logger.debug("loading ant jar files. Ant_Home: #{options[:ant_home]}")
81
+ AntwrapClassLoader.load_ant_libs(options[:ant_home])
82
+ @@classes_loaded = true
83
+ end
84
+
85
+ @logger.debug(Antwrap::ApacheAnt::Main.getAntVersion())
86
+ @ant_version = Antwrap::ApacheAnt::Main.getAntVersion()[/\d\.\d\.\d/].to_f
87
+ init_project(options)
88
+
89
+ @task_stack = Array.new
90
+
91
+ end
92
+
93
+ def method_missing(sym, *args)
94
+
95
+ begin
96
+ task = AntTask.new(sym.to_s, self, args[0])
97
+
98
+ parent_task = @task_stack.last
99
+ @task_stack << task
100
+
101
+ yield self if block_given?
102
+
103
+ parent_task.add(task) if parent_task
104
+
105
+ if @task_stack.size == 1
106
+ if declarative == true
107
+ @logger.debug("Executing #{task}")
108
+ task.execute
109
+ else
110
+ @logger.debug("Returning #{task}")
111
+ return task
112
+ end
113
+ end
114
+
115
+ rescue
116
+ @logger.error("Error instantiating '#{sym.to_s}' task: " + $!)
117
+ raise
118
+ ensure
119
+ @task_stack.pop
120
+ end
121
+
122
+ end
123
+
124
+ #The Ant Project's name. Default is ''
125
+ def name
126
+ return @project.getName
127
+ end
128
+
129
+ #The Ant Project's basedir. Default is '.'
130
+ def basedir
131
+ return @project.getBaseDir().getAbsolutePath();
132
+ end
133
+
134
+ #Displays the Class name followed by the AntProject name
135
+ # -e.g. AntProject[BigCoProject]
136
+ def to_s
137
+ return self.class.name + "[#{name}]"
138
+ end
139
+
140
+ end
141
+ end
data/lib/ant_task.rb ADDED
@@ -0,0 +1,126 @@
1
+ # Copyright 2008 Caleb Powell
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and limitations
12
+ # under the License.
13
+
14
+ module Antwrap
15
+ class AntTask
16
+ attr_accessor :unknown_element, :project, :taskname, :logger, :executed
17
+
18
+ public
19
+ # Creates an AntTask
20
+ # taskname
21
+ # -A String representing the name of an Ant Task. This name should correspond to
22
+ # the element name of the Ant xml task (e.g. javac, jar, war, etc).
23
+ # antProject
24
+ # -An instance of an AntProject
25
+ # attributes
26
+ # -A Hash of task name/values to be applied to the task.
27
+ #
28
+ # For example:
29
+ # antProject = AntProject.new()
30
+ # antTask = AntTask.new('javac', antProject, {:debug => 'on', :verbose => 'no', :fork => 'no'})
31
+ def initialize(taskname, antProject, attributes)
32
+
33
+ taskname = taskname[1, taskname.length-1] if taskname[0,1] == "_"
34
+ @logger = antProject.logger
35
+ @taskname = taskname
36
+ @project_wrapper = antProject
37
+ @project = antProject.project()
38
+ @logger.debug(antProject.to_s)
39
+ @unknown_element = create_unknown_element(@project, taskname)
40
+ @logger.debug(to_s)
41
+
42
+ add_attributes(attributes)
43
+
44
+ end
45
+
46
+ # Displays the Class name followed by the Task name
47
+ # -e.g. AntTask[javac]
48
+ def to_s
49
+ return self.class.name + "[#{@taskname}]"
50
+ end
51
+
52
+ def create_unknown_element(project, taskname)
53
+
54
+ element = ApacheAnt::UnknownElement.new(taskname)
55
+ element.setProject(project)
56
+ element.setOwningTarget(ApacheAnt::Target.new())
57
+ element.setTaskName(taskname)
58
+
59
+ #DNR. This initializes the Task's Wrapper object and prevents NullPointerExeption upon execution of the task
60
+ element.getRuntimeConfigurableWrapper()
61
+
62
+ if(@project_wrapper.ant_version >= 1.6)
63
+ element.setTaskType(taskname)
64
+ element.setNamespace('')
65
+ element.setQName(taskname)
66
+ end
67
+
68
+ return element
69
+
70
+ end
71
+
72
+ # Sets each attribute on the AntTask instance.
73
+ # :attributes - is a Hash.
74
+ def add_attributes(attributes)
75
+
76
+ return if attributes == nil
77
+
78
+ wrapper = ApacheAnt::RuntimeConfigurable.new(@unknown_element, @unknown_element.getTaskName());
79
+
80
+ if(@project_wrapper.ant_version >= 1.6)
81
+ attributes.each do |key, val|
82
+ apply_to_wrapper(wrapper, key.to_s, val){ |k,v| wrapper.setAttribute(k, v)}
83
+ end
84
+ else
85
+ @unknown_element.setRuntimeConfigurableWrapper(wrapper)
86
+ attribute_list = XmlSax::AttributeListImpl.new()
87
+ attributes.each do |key, val|
88
+ apply_to_wrapper(wrapper, key.to_s, val){ |k,v| attribute_list.addAttribute(k, 'CDATA', v)}
89
+ end
90
+ wrapper.setAttributes(attribute_list)
91
+ end
92
+
93
+ end
94
+
95
+ def apply_to_wrapper(wrapper, key, value)
96
+
97
+ 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)
98
+
99
+ begin
100
+ if(key == 'pcdata')
101
+ wrapper.addText(value.to_s)
102
+ else
103
+ yield key, value.to_s
104
+ end
105
+ rescue StandardError
106
+ raise ArgumentError, "ArgumentError: Unable to set :#{key} attribute with value => '#{value}'"
107
+ end
108
+
109
+ end
110
+
111
+ # Add <em>child</em> as a child of this task.
112
+ def add(child)
113
+ @unknown_element.addChild(child.unknown_element())
114
+ @unknown_element.getRuntimeConfigurableWrapper().addChild(child.unknown_element().getRuntimeConfigurableWrapper())
115
+ end
116
+
117
+ # Invokes the AntTask.
118
+ def execute
119
+ @unknown_element.maybeConfigure
120
+ @unknown_element.execute
121
+ @executed = true
122
+ return nil
123
+ end
124
+
125
+ end
126
+ end
data/lib/antwrap.rb ADDED
@@ -0,0 +1,22 @@
1
+ # Copyright 2008 Caleb Powell
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and limitations
12
+ # under the License.
13
+
14
+ require 'antwrap_utilities'
15
+ require 'ant_project'
16
+ require 'ant_task'
17
+ module Antwrap
18
+ autoload :ApacheAnt, 'ant_libraries.rb'
19
+ autoload :JavaLang, 'ant_libraries.rb'
20
+ autoload :XmlOrg, 'ant_libraries.rb'
21
+ VERSION = "0.7.1"
22
+ end
@@ -0,0 +1,32 @@
1
+ # Copyright 2008 Caleb Powell
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and limitations
12
+ # under the License.
13
+ require 'java_adapter'
14
+ module Antwrap
15
+ module AntwrapClassLoader
16
+ require 'find'
17
+
18
+ def match(*paths)
19
+ matched = Array.new
20
+ Find.find(*paths){ |path| matched << path if yield path }
21
+ return matched
22
+ end
23
+
24
+ def load_ant_libs(ant_home)
25
+ jars = match(ant_home + File::SEPARATOR + 'lib') {|p| ext = p[-4...p.size]; ext && ext.downcase == '.jar'}
26
+ Antwrap::JavaAdapter.load(jars)
27
+ end
28
+
29
+ module_function :match, :load_ant_libs
30
+
31
+ end
32
+ end
@@ -0,0 +1,52 @@
1
+ # Copyright 2008 Caleb Powell
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and limitations
12
+ # under the License.
13
+
14
+ module Antwrap
15
+ module JavaAdapter
16
+
17
+ def is_jruby_interpreter?
18
+ return RUBY_PLATFORM =~ /java/
19
+ end
20
+
21
+ def import_class(name)
22
+ if is_jruby_interpreter?
23
+ return import_using_jruby(name)
24
+ else
25
+ return Rjb::import(name)
26
+ end
27
+ end
28
+
29
+ def load(files=[], args=[])
30
+ if is_jruby_interpreter?
31
+ files.each {|jar| require jar }
32
+ else
33
+ Rjb::load(files.join(File::PATH_SEPARATOR), [])
34
+ end
35
+ end
36
+
37
+ module_function :import_class, :load, :is_jruby_interpreter?
38
+
39
+ if is_jruby_interpreter?
40
+ require 'java'
41
+ else
42
+ require 'rubygems'
43
+ require 'rjb'
44
+ end
45
+
46
+ private
47
+ def JavaAdapter.import_using_jruby(name)
48
+ include_class(name)
49
+ return remove_const(name.scan(/[_a-zA-Z0-9$]+/).last)
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atoulme-Antwrap
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 1
10
+ version: 0.7.1
11
+ platform: ruby
12
+ authors:
13
+ - Caleb Powell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-20 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rjb
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 17
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 3
34
+ version: 1.0.3
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: hoe
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 5
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 3
50
+ version: 2.3.3
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: "\tA Ruby module that wraps the Apache Ant build tool. Antwrap can be used to invoke Ant Tasks from a Ruby or a JRuby script.\n\n\
54
+ == FEATURES/PROBLEMS:\n\n\
55
+ \tAntwrap runs on the native Ruby interpreter via the RJB (Ruby Java Bridge gem) and on the JRuby interpreter. Antwrap is compatible with Ant versions 1.5.4, \n\
56
+ \t1.6.5 and 1.7.0. For more information, \tsee the Project Info (http://rubyforge.org/projects/antwrap/) page. \n\
57
+ \t \n\
58
+ == SYNOPSIS:\n\n\
59
+ \tAntwrap is a Ruby library that can be used to invoke Ant tasks. It is being used in the Buildr (http://incubator.apache.org/buildr/) project to execute \n\
60
+ \tAnt (http://ant.apache.org/) tasks in a Java project. If you are tired of fighting with Ant or Maven XML files in your Java project, take some time to \n\
61
+ \tcheck out Buildr!"
62
+ email: caleb.powell@gmail.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files:
68
+ - History.txt
69
+ - Manifest.txt
70
+ - README.txt
71
+ files:
72
+ - History.txt
73
+ - LICENSE
74
+ - Manifest.txt
75
+ - README.txt
76
+ - Rakefile
77
+ - docs/index.html
78
+ - docs/index_files/Brander.css
79
+ - docs/index_files/blankdot.gif
80
+ - docs/index_files/blankdot.html
81
+ - docs/index_files/urchin.js
82
+ - lib/ant_project.rb
83
+ - lib/ant_task.rb
84
+ - lib/antwrap.rb
85
+ - lib/antwrap_utilities.rb
86
+ - lib/ant_libraries.rb
87
+ - lib/java_adapter.rb
88
+ has_rdoc: true
89
+ homepage: http://rubyforge.org/projects/antwrap/
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --main
95
+ - README.txt
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project: antwrap
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A Ruby module that wraps the Apache Ant build tool. Antwrap can be used to invoke Ant Tasks from a Ruby or a JRuby script.
123
+ test_files: []
124
+