atoulme-Antwrap 0.7.1-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.
@@ -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,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atoulme-Antwrap
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 7
8
+ - 1
9
+ version: 0.7.1
10
+ platform: java
11
+ authors:
12
+ - Caleb Powell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-20 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hoe
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 3
31
+ version: 2.3.3
32
+ type: :development
33
+ version_requirements: *id001
34
+ 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\
35
+ == FEATURES/PROBLEMS:\n\n\
36
+ \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\
37
+ \t1.6.5 and 1.7.0. For more information, \tsee the Project Info (http://rubyforge.org/projects/antwrap/) page. \n\
38
+ \t \n\
39
+ == SYNOPSIS:\n\n\
40
+ \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\
41
+ \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\
42
+ \tcheck out Buildr!"
43
+ email: caleb.powell@gmail.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - History.txt
50
+ - Manifest.txt
51
+ - README.txt
52
+ files:
53
+ - History.txt
54
+ - LICENSE
55
+ - Manifest.txt
56
+ - README.txt
57
+ - Rakefile
58
+ - docs/index.html
59
+ - docs/index_files/Brander.css
60
+ - docs/index_files/blankdot.gif
61
+ - docs/index_files/blankdot.html
62
+ - docs/index_files/urchin.js
63
+ - lib/ant_project.rb
64
+ - lib/ant_task.rb
65
+ - lib/antwrap.rb
66
+ - lib/antwrap_utilities.rb
67
+ - lib/ant_libraries.rb
68
+ - lib/java_adapter.rb
69
+ has_rdoc: true
70
+ homepage: http://rubyforge.org/projects/antwrap/
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --main
76
+ - README.txt
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: antwrap
96
+ rubygems_version: 1.3.6
97
+ signing_key:
98
+ specification_version: 3
99
+ 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.
100
+ test_files: []
101
+