novoda-buildr 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/CHANGELOG +1050 -0
  2. data/LICENSE +176 -0
  3. data/NOTICE +26 -0
  4. data/README.rdoc +134 -0
  5. data/Rakefile +45 -0
  6. data/_buildr +29 -0
  7. data/_jbuildr +29 -0
  8. data/addon/buildr/antlr.rb +65 -0
  9. data/addon/buildr/cobertura.rb +22 -0
  10. data/addon/buildr/drb.rb +281 -0
  11. data/addon/buildr/emma.rb +22 -0
  12. data/addon/buildr/hibernate.rb +142 -0
  13. data/addon/buildr/javacc.rb +85 -0
  14. data/addon/buildr/jdepend.rb +60 -0
  15. data/addon/buildr/jetty.rb +248 -0
  16. data/addon/buildr/jibx.rb +86 -0
  17. data/addon/buildr/nailgun.rb +221 -0
  18. data/addon/buildr/openjpa.rb +90 -0
  19. data/addon/buildr/org/apache/buildr/BuildrNail$Main.class +0 -0
  20. data/addon/buildr/org/apache/buildr/BuildrNail.class +0 -0
  21. data/addon/buildr/org/apache/buildr/BuildrNail.java +41 -0
  22. data/addon/buildr/org/apache/buildr/JettyWrapper$1.class +0 -0
  23. data/addon/buildr/org/apache/buildr/JettyWrapper$BuildrHandler.class +0 -0
  24. data/addon/buildr/org/apache/buildr/JettyWrapper.class +0 -0
  25. data/addon/buildr/org/apache/buildr/JettyWrapper.java +144 -0
  26. data/addon/buildr/xmlbeans.rb +93 -0
  27. data/bin/buildr +19 -0
  28. data/buildr.buildfile +58 -0
  29. data/buildr.gemspec +65 -0
  30. data/doc/_config.yml +1 -0
  31. data/doc/_layouts/default.html +88 -0
  32. data/doc/_layouts/preface.html +22 -0
  33. data/doc/artifacts.textile +211 -0
  34. data/doc/building.textile +245 -0
  35. data/doc/contributing.textile +252 -0
  36. data/doc/css/default.css +236 -0
  37. data/doc/css/print.css +101 -0
  38. data/doc/css/syntax.css +23 -0
  39. data/doc/download.textile +90 -0
  40. data/doc/extending.textile +205 -0
  41. data/doc/images/1442160941-frontcover.jpg +0 -0
  42. data/doc/images/asf-logo.gif +0 -0
  43. data/doc/images/asf-logo.png +0 -0
  44. data/doc/images/buildr-hires.png +0 -0
  45. data/doc/images/buildr.png +0 -0
  46. data/doc/images/favicon.png +0 -0
  47. data/doc/images/growl-icon.tiff +0 -0
  48. data/doc/images/note.png +0 -0
  49. data/doc/images/project-structure.png +0 -0
  50. data/doc/images/tip.png +0 -0
  51. data/doc/images/zbuildr.png +0 -0
  52. data/doc/images/zbuildr.tif +0 -0
  53. data/doc/index.textile +69 -0
  54. data/doc/installing.textile +265 -0
  55. data/doc/languages.textile +459 -0
  56. data/doc/mailing_lists.textile +25 -0
  57. data/doc/more_stuff.textile +457 -0
  58. data/doc/packaging.textile +429 -0
  59. data/doc/preface.textile +54 -0
  60. data/doc/projects.textile +276 -0
  61. data/doc/quick_start.textile +210 -0
  62. data/doc/scripts/buildr-git.rb +512 -0
  63. data/doc/scripts/gitflow.rb +296 -0
  64. data/doc/scripts/install-jruby.sh +44 -0
  65. data/doc/scripts/install-linux.sh +72 -0
  66. data/doc/scripts/install-osx.sh +52 -0
  67. data/doc/settings_profiles.textile +287 -0
  68. data/doc/testing.textile +233 -0
  69. data/etc/KEYS +151 -0
  70. data/lib/buildr.rb +36 -0
  71. data/lib/buildr/core.rb +35 -0
  72. data/lib/buildr/core/application.rb +662 -0
  73. data/lib/buildr/core/build.rb +463 -0
  74. data/lib/buildr/core/checks.rb +254 -0
  75. data/lib/buildr/core/common.rb +150 -0
  76. data/lib/buildr/core/compile.rb +608 -0
  77. data/lib/buildr/core/environment.rb +129 -0
  78. data/lib/buildr/core/filter.rb +363 -0
  79. data/lib/buildr/core/generate.rb +195 -0
  80. data/lib/buildr/core/help.rb +119 -0
  81. data/lib/buildr/core/osx.rb +46 -0
  82. data/lib/buildr/core/progressbar.rb +156 -0
  83. data/lib/buildr/core/project.rb +971 -0
  84. data/lib/buildr/core/shell.rb +198 -0
  85. data/lib/buildr/core/test.rb +774 -0
  86. data/lib/buildr/core/transports.rb +559 -0
  87. data/lib/buildr/core/util.rb +449 -0
  88. data/lib/buildr/groovy.rb +19 -0
  89. data/lib/buildr/groovy/bdd.rb +106 -0
  90. data/lib/buildr/groovy/compiler.rb +138 -0
  91. data/lib/buildr/groovy/shell.rb +48 -0
  92. data/lib/buildr/ide.rb +19 -0
  93. data/lib/buildr/ide/eclipse.rb +424 -0
  94. data/lib/buildr/ide/eclipse.rb~ +424 -0
  95. data/lib/buildr/ide/eclipse/java.rb +53 -0
  96. data/lib/buildr/ide/eclipse/plugin.rb +71 -0
  97. data/lib/buildr/ide/eclipse/scala.rb +68 -0
  98. data/lib/buildr/ide/idea.ipr.template +300 -0
  99. data/lib/buildr/ide/idea.rb +190 -0
  100. data/lib/buildr/ide/idea7x.ipr.template +290 -0
  101. data/lib/buildr/ide/idea7x.rb +212 -0
  102. data/lib/buildr/java.rb +23 -0
  103. data/lib/buildr/java/ant.rb +94 -0
  104. data/lib/buildr/java/bdd.rb +464 -0
  105. data/lib/buildr/java/cobertura.rb +274 -0
  106. data/lib/buildr/java/commands.rb +217 -0
  107. data/lib/buildr/java/compiler.rb +349 -0
  108. data/lib/buildr/java/deprecated.rb +141 -0
  109. data/lib/buildr/java/emma.rb +244 -0
  110. data/lib/buildr/java/jruby.rb +117 -0
  111. data/lib/buildr/java/jtestr_runner.rb.erb +118 -0
  112. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.class +0 -0
  113. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.java +142 -0
  114. data/lib/buildr/java/packaging.rb +717 -0
  115. data/lib/buildr/java/pom.rb +174 -0
  116. data/lib/buildr/java/rjb.rb +155 -0
  117. data/lib/buildr/java/test_result.rb +327 -0
  118. data/lib/buildr/java/tests.rb +341 -0
  119. data/lib/buildr/java/version_requirement.rb +172 -0
  120. data/lib/buildr/packaging.rb +24 -0
  121. data/lib/buildr/packaging/archive.rb +491 -0
  122. data/lib/buildr/packaging/artifact.rb +750 -0
  123. data/lib/buildr/packaging/artifact_namespace.rb +980 -0
  124. data/lib/buildr/packaging/artifact_search.rb +140 -0
  125. data/lib/buildr/packaging/gems.rb +106 -0
  126. data/lib/buildr/packaging/package.rb +241 -0
  127. data/lib/buildr/packaging/tar.rb +186 -0
  128. data/lib/buildr/packaging/version_requirement.rb +172 -0
  129. data/lib/buildr/packaging/zip.rb +73 -0
  130. data/lib/buildr/packaging/ziptask.rb +316 -0
  131. data/lib/buildr/resources/buildr.icns +0 -0
  132. data/lib/buildr/scala.rb +25 -0
  133. data/lib/buildr/scala/bdd.rb +114 -0
  134. data/lib/buildr/scala/compiler.rb +196 -0
  135. data/lib/buildr/scala/org/apache/buildr/SpecsSingletonRunner$.class +0 -0
  136. data/lib/buildr/scala/org/apache/buildr/SpecsSingletonRunner.class +0 -0
  137. data/lib/buildr/scala/org/apache/buildr/SpecsSingletonRunner.scala +35 -0
  138. data/lib/buildr/scala/shell.rb +60 -0
  139. data/lib/buildr/scala/tests.rb +157 -0
  140. data/lib/buildr/shell.rb +180 -0
  141. data/rakelib/all-in-one.rake +84 -0
  142. data/rakelib/checks.rake +57 -0
  143. data/rakelib/doc.rake +92 -0
  144. data/rakelib/jekylltask.rb +120 -0
  145. data/rakelib/package.rake +73 -0
  146. data/rakelib/release.rake +152 -0
  147. data/rakelib/rspec.rake +73 -0
  148. data/rakelib/setup.rake +54 -0
  149. data/rakelib/stage.rake +214 -0
  150. data/spec/addon/drb_spec.rb +328 -0
  151. data/spec/core/application_spec.rb +509 -0
  152. data/spec/core/build_spec.rb +678 -0
  153. data/spec/core/checks_spec.rb +519 -0
  154. data/spec/core/common_spec.rb +670 -0
  155. data/spec/core/compile_spec.rb +624 -0
  156. data/spec/core/extension_spec.rb +201 -0
  157. data/spec/core/generate_spec.rb +33 -0
  158. data/spec/core/project_spec.rb +762 -0
  159. data/spec/core/test_spec.rb +1175 -0
  160. data/spec/core/transport_spec.rb +537 -0
  161. data/spec/core/util_spec.rb +67 -0
  162. data/spec/groovy/bdd_spec.rb +80 -0
  163. data/spec/groovy/compiler_spec.rb +240 -0
  164. data/spec/ide/eclipse_spec.rb +650 -0
  165. data/spec/ide/idea7x_spec.rb +84 -0
  166. data/spec/java/ant_spec.rb +33 -0
  167. data/spec/java/bdd_spec.rb +379 -0
  168. data/spec/java/cobertura_spec.rb +85 -0
  169. data/spec/java/compiler_spec.rb +446 -0
  170. data/spec/java/emma_spec.rb +119 -0
  171. data/spec/java/java_spec.rb +124 -0
  172. data/spec/java/packaging_spec.rb +1140 -0
  173. data/spec/java/test_coverage_helper.rb +257 -0
  174. data/spec/java/tests_spec.rb +493 -0
  175. data/spec/packaging/archive_spec.rb +538 -0
  176. data/spec/packaging/artifact_namespace_spec.rb +697 -0
  177. data/spec/packaging/artifact_spec.rb +802 -0
  178. data/spec/packaging/packaging_helper.rb +63 -0
  179. data/spec/packaging/packaging_spec.rb +684 -0
  180. data/spec/sandbox.rb +148 -0
  181. data/spec/scala/bdd_spec.rb +119 -0
  182. data/spec/scala/compiler_spec.rb +284 -0
  183. data/spec/scala/scala.rb +38 -0
  184. data/spec/scala/tests_spec.rb +263 -0
  185. data/spec/spec_helpers.rb +340 -0
  186. data/spec/version_requirement_spec.rb +143 -0
  187. metadata +384 -0
@@ -0,0 +1,971 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+
17
+ require 'buildr/core/util'
18
+
19
+
20
+ module Buildr
21
+
22
+ # Symbolic mapping for directory layout. Used for both the default and custom layouts.
23
+ #
24
+ # For example, the default layout maps [:source, :main, :java] to 'src/main/java', and
25
+ # [:target, :main, :classes] to 'target/classes'. You can use this to change the layout
26
+ # of your projects.
27
+ #
28
+ # To map [:source, :main] into the 'sources' directory:
29
+ # my_layout = Layout.new
30
+ # my_layout[:source, :main] = 'sources'
31
+ #
32
+ # define 'foo', :layout=>my_layout do
33
+ # ...
34
+ # end
35
+ #
36
+ # To map [:source, :main, :java] to 'java/main':
37
+ # class MainLast < Layout
38
+ # def expand(*args)
39
+ # if args[0..1] == [:source, :main]
40
+ # super args[2], :main, *args[3,]
41
+ # else
42
+ # super
43
+ # end
44
+ # end
45
+ # end
46
+ #
47
+ # define 'foo', :layout=>MainLast do
48
+ # ...
49
+ # end
50
+ class Layout
51
+
52
+ class << self
53
+
54
+ # Default layout used by new projects.
55
+ attr_accessor :default
56
+
57
+ end
58
+
59
+ def initialize #:nodoc:
60
+ @mapping = {}
61
+ end
62
+
63
+ # Expands list of symbols and path names into a full path, for example:
64
+ # puts default.expand(:source, :main, :java)
65
+ # => "src/main/java"
66
+ def expand(*args)
67
+ args = args.compact.reject { |s| s.to_s.empty? }.map(&:to_sym)
68
+ return '' if args.empty?
69
+ @mapping[args] ||= File.join(*[expand(*args[0..-2]), args.last.to_s].reject(&:empty?)) if args.size > 1
70
+ return @mapping[args] || args.first.to_s
71
+ end
72
+
73
+ # Resolves a list of symbols into a path.
74
+ def [](*args)
75
+ @mapping[args.map(&:to_sym)]
76
+ end
77
+
78
+ # Specifies the path resolved from a list of symbols.
79
+ def []=(*args)
80
+ @mapping[args[0...-1].map(&:to_sym)] = args.last
81
+ end
82
+
83
+ def initialize_copy(copy)
84
+ copy.instance_variable_set :@mapping, @mapping.clone
85
+ end
86
+
87
+ # Default layout has the following properties:
88
+ # * :source maps to the 'src' directory.
89
+ # * Anything under :source maps verbatim (e.g. :source, :main becomes 'src/main')
90
+ # * :target maps to the 'target' directory.
91
+ # * :target, :main maps to the 'target' directory as well.
92
+ # * Anything under :target, :main maps verbatim (e.g. :target, :main, :classes becomes 'target/classes')
93
+ # * Anything else under :target also maps verbatim (e.g. :target, :test becomes 'target/test')
94
+ class Default < Layout
95
+
96
+ def initialize
97
+ super
98
+ self[:source] = 'src'
99
+ self[:target, :main] = 'target'
100
+ end
101
+
102
+ end
103
+
104
+ self.default = Default.new
105
+
106
+ end
107
+
108
+
109
+ # A project definition is where you define all the tasks associated with
110
+ # the project you're building.
111
+ #
112
+ # The project itself will define several life cycle tasks for you. For example,
113
+ # it automatically creates a compile task that will compile all the source files
114
+ # found in src/main/java into target/classes, a test task that will compile source
115
+ # files from src/test/java and run all the JUnit tests found there, and a build
116
+ # task to compile and then run the tests.
117
+ #
118
+ # You use the project definition to enhance these tasks, for example, telling the
119
+ # compile task which class path dependencies to use. Or telling the project how
120
+ # to package an artifact, e.g. creating a JAR using <tt>package :jar</tt>.
121
+ #
122
+ # You can also define additional tasks that are executed by project tasks,
123
+ # or invoked from rake.
124
+ #
125
+ # Tasks created by the project are all prefixed with the project name, e.g.
126
+ # the project foo creates the task foo:compile. If foo contains a sub-project bar,
127
+ # the later will define the task foo:bar:compile. Since the compile task is
128
+ # recursive, compiling foo will also compile foo:bar.
129
+ #
130
+ # If you run:
131
+ # buildr compile
132
+ # from the command line, it will execute the compile task of the current project.
133
+ #
134
+ # Projects and sub-projects follow a directory heirarchy. The Buildfile is assumed to
135
+ # reside in the same directory as the top-level project, and each sub-project is
136
+ # contained in a sub-directory in the same name. For example:
137
+ # /home/foo
138
+ # |__ Buildfile
139
+ # |__ src/main/java
140
+ # |__ foo
141
+ # |__ src/main/java
142
+ #
143
+ # The default structure of each project is assumed to be:
144
+ # src
145
+ # |__main
146
+ # | |__java <-- Source files to compile
147
+ # | |__resources <-- Resources to copy
148
+ # | |__webapp <-- For WARs
149
+ # |__test
150
+ # | |__java <-- Source files to compile (tests)
151
+ # | |__resources <-- Resources to copy (tests)
152
+ # |__target <-- Packages created here
153
+ # | |__classes <-- Generated when compiling
154
+ # | |__resources <-- Copied (and filtered) from resources
155
+ # | |__test/classes <-- Generated when compiling tests
156
+ # | |__test/resources <-- Copied (and filtered) from resources
157
+ # |__reports <-- Test, coverage and other reports
158
+ #
159
+ # You can change the project layout by passing a new Layout to the project definition.
160
+ #
161
+ # You can only define a project once using #define. Afterwards, you can obtain the project
162
+ # definition using #project. The order in which you define projects is not important,
163
+ # project definitions are evaluated when you ask for them. Circular dependencies will not
164
+ # work. Rake tasks are only created after the project is evaluated, so if you need to access
165
+ # a task (e.g. compile) use <code>project('foo').compile</code> instead of <code>task('foo:compile')</code>.
166
+ #
167
+ # For example:
168
+ # define 'myapp', :version=>'1.1' do
169
+ #
170
+ # define 'wepapp' do
171
+ # compile.with project('myapp:beans')
172
+ # package :war
173
+ # end
174
+ #
175
+ # define 'beans' do
176
+ # compile.with DEPENDS
177
+ # package :jar
178
+ # end
179
+ # end
180
+ #
181
+ # puts projects.map(&:name)
182
+ # => [ 'myapp', 'myapp:beans', 'myapp:webapp' ]
183
+ # puts project('myapp:webapp').parent.name
184
+ # => 'myapp'
185
+ # puts project('myapp:webapp').compile.classpath.map(&:to_spec)
186
+ # => 'myapp:myapp-beans:jar:1.1'
187
+ class Project < Rake::Task
188
+
189
+ class << self
190
+
191
+ # :call-seq:
192
+ # define(name, properties?) { |project| ... } => project
193
+ #
194
+ # See Buildr#define.
195
+ def define(name, properties, &block) #:nodoc:
196
+ # Make sure a sub-project is only defined within the parent project,
197
+ # to prevent silly mistakes that lead to inconsistencies (e.g.
198
+ # namespaces will be all out of whack).
199
+ Buildr.application.current_scope == name.split(':')[0...-1] or
200
+ raise "You can only define a sub project (#{name}) within the definition of its parent project"
201
+
202
+ @projects ||= {}
203
+ raise "You cannot define the same project (#{name}) more than once" if @projects[name]
204
+ # Projects with names like: compile, test, build are invalid, so we have
205
+ # to make sure the project has not the name of an already defined task
206
+ raise "Invalid project name: #{name.inspect} is already used for a task" if Buildr.application.lookup(name)
207
+
208
+ Project.define_task(name).tap do |project|
209
+ # Define the project to prevent duplicate definition.
210
+ @projects[name] = project
211
+ # Set the project properties first, actions may use them.
212
+ properties.each { |name, value| project.send "#{name}=", value } if properties
213
+ # Setup to call before/after define extension callbacks
214
+ # Don't cache list of extensions, since project may add new extensions.
215
+ project.enhance do |project|
216
+ project.send :call_callbacks, :before_define
217
+ project.enhance do |project|
218
+ project.send :call_callbacks, :after_define
219
+ end
220
+ end
221
+ project.enhance do |project|
222
+ @on_define.each { |extension| extension[project] }
223
+ end if @on_define
224
+ # Enhance the project using the definition block.
225
+ project.enhance { project.instance_exec project, &block } if block
226
+
227
+ # Top-level project? Invoke the project definition. Sub-project? We don't invoke
228
+ # the project definiton yet (allow project calls to establish order of evaluation),
229
+ # but must do so before the parent project's definition is done.
230
+ project.parent.enhance { project.invoke } if project.parent
231
+ end
232
+ end
233
+
234
+ # :call-seq:
235
+ # project(name) => project
236
+ #
237
+ # See Buildr#project.
238
+ def project(*args) #:nodoc:
239
+ options = args.pop if Hash === args.last
240
+ rake_check_options options, :scope if options
241
+ raise ArgumentError, 'Only one project name at a time' unless args.size == 1
242
+ @projects ||= {}
243
+ name = args.first.to_s
244
+ # Make sure parent project is evaluated (e.g. if looking for foo:bar, find foo first)
245
+ unless @projects[name]
246
+ parts = name.split(':')
247
+ project(parts.first, options || {}) if parts.size > 1
248
+ end
249
+ if options && options[:scope]
250
+ # We assume parent project is evaluated.
251
+ project = options[:scope].split(':').inject([[]]) { |scopes, scope| scopes << (scopes.last + [scope]) }.
252
+ map { |scope| @projects[(scope + [name]).join(':')] }.
253
+ select { |project| project }.last
254
+ end
255
+ project ||= @projects[name] # Not found in scope.
256
+ raise "No such project #{name}" unless project
257
+ project.invoke
258
+ project
259
+ end
260
+
261
+ # :call-seq:
262
+ # projects(*names) => projects
263
+ #
264
+ # See Buildr#projects.
265
+ def projects(*names) #:nodoc:
266
+ options = names.pop if Hash === names.last
267
+ rake_check_options options, :scope if options
268
+ @projects ||= {}
269
+ names = names.flatten
270
+ if options && options[:scope]
271
+ # We assume parent project is evaluated.
272
+ if names.empty?
273
+ parent = @projects[options[:scope].to_s] or raise "No such project #{options[:scope]}"
274
+ @projects.values.select { |project| project.parent == parent }.each { |project| project.invoke }.
275
+ map { |project| [project] + projects(:scope=>project) }.flatten.sort_by(&:name)
276
+ else
277
+ names.uniq.map { |name| project(name, :scope=>options[:scope]) }
278
+ end
279
+ elsif names.empty?
280
+ # Parent project(s) not evaluated so we don't know all the projects yet.
281
+ @projects.values.each(&:invoke)
282
+ @projects.keys.map { |name| project(name) or raise "No such project #{name}" }.sort_by(&:name)
283
+ else
284
+ # Parent project(s) not evaluated, for the sub-projects we may need to find.
285
+ names.map { |name| name.split(':') }.select { |name| name.size > 1 }.map(&:first).uniq.each { |name| project(name) }
286
+ names.uniq.map { |name| project(name) or raise "No such project #{name}" }.sort_by(&:name)
287
+ end
288
+ end
289
+
290
+ # :call-seq:
291
+ # clear
292
+ #
293
+ # Discard all project definitions.
294
+ def clear
295
+ @projects.clear if @projects
296
+ end
297
+
298
+ # :call-seq:
299
+ # local_task(name)
300
+ # local_task(name) { |name| ... }
301
+ #
302
+ # Defines a local task with an optional execution message.
303
+ #
304
+ # A local task is a task that executes a task with the same name, defined in the
305
+ # current project, the project's with a base directory that is the same as the
306
+ # current directory.
307
+ #
308
+ # Complicated? Try this:
309
+ # buildr build
310
+ # is the same as:
311
+ # buildr foo:build
312
+ # But:
313
+ # cd bar
314
+ # buildr build
315
+ # is the same as:
316
+ # buildr foo:bar:build
317
+ #
318
+ # The optional block is called with the project name when the task executes
319
+ # and returns a message that, for example "Building project #{name}".
320
+ def local_task(*args, &block)
321
+ task *args do |task, args|
322
+ args = task.arg_names.map {|n| args[n]}
323
+ local_projects do |project|
324
+ info block.call(project.name) if block
325
+ task("#{project.name}:#{task.name}").invoke *args
326
+ end
327
+ end
328
+ end
329
+
330
+ # *Deprecated* Check the Extension module to see how extensions are handled.
331
+ def on_define(&block)
332
+ Buildr.application.deprecated 'This method is deprecated, see Extension'
333
+ (@on_define ||= []) << block if block
334
+ end
335
+
336
+ def scope_name(scope, task_name) #:nodoc:
337
+ task_name
338
+ end
339
+
340
+ def local_projects(dir = nil, &block) #:nodoc:
341
+ dir = File.expand_path(dir || Buildr.application.original_dir)
342
+ projects = @projects ? @projects.values : []
343
+ projects = projects.select { |project| project.base_dir == dir }
344
+ if projects.empty? && dir != Dir.pwd && File.dirname(dir) != dir
345
+ local_projects(File.dirname(dir), &block)
346
+ elsif block
347
+ if projects.empty?
348
+ warn "No projects defined for directory #{Buildr.application.original_dir}"
349
+ else
350
+ projects.each { |project| block[project] }
351
+ end
352
+ else
353
+ projects
354
+ end
355
+ end
356
+
357
+ # :call-seq:
358
+ # parent_task(task_name) => task_name or nil
359
+ #
360
+ # Returns a parent task, basically a task in a higher namespace. For example, the parent
361
+ # of 'foo:test:compile' is 'foo:compile' and the parent of 'foo:compile' is 'compile'.
362
+ def parent_task(task_name) #:nodoc:
363
+ namespace = task_name.split(':')
364
+ last_name = namespace.pop
365
+ namespace.pop
366
+ Buildr.application.lookup((namespace + [last_name]).join(':'), []) unless namespace.empty?
367
+ end
368
+
369
+ # :call-seq:
370
+ # project_from_task(task) => project
371
+ #
372
+ # Figure out project associated to this task and return it.
373
+ def project_from_task(task) #:nodoc:
374
+ project = Buildr.application.lookup('rake:' + task.to_s.gsub(/:[^:]*$/, ''))
375
+ project if Project === project
376
+ end
377
+
378
+ # Loaded extension modules.
379
+ def extension_modules #:nodoc:
380
+ @extension_modules ||= []
381
+ end
382
+
383
+ # Extension callbacks that apply to all projects
384
+ def global_callbacks #:nodoc:
385
+ @global_callbacks ||= []
386
+ end
387
+ end
388
+
389
+
390
+ # Project has visibility to everything in the Buildr namespace.
391
+ include Buildr
392
+
393
+ # The project name. For example, 'foo' for the top-level project, and 'foo:bar'
394
+ # for its sub-project.
395
+ attr_reader :name
396
+
397
+ # The parent project if this is a sub-project.
398
+ attr_reader :parent
399
+
400
+ def initialize(*args) #:nodoc:
401
+ super
402
+ split = name.split(':')
403
+ if split.size > 1
404
+ # Get parent project, but do not invoke it's definition to prevent circular
405
+ # dependencies (it's being invoked right now, so calling project will fail).
406
+ @parent = task(split[0...-1].join(':'))
407
+ raise "No parent project #{split[0...-1].join(':')}" unless @parent && Project === parent
408
+ end
409
+ # Inherit all global callbacks
410
+ @callbacks = Project.global_callbacks.dup
411
+ end
412
+
413
+ # :call-seq:
414
+ # base_dir => path
415
+ #
416
+ # Returns the project's base directory.
417
+ #
418
+ # The Buildfile defines top-level project, so it's logical that the top-level project's
419
+ # base directory is the one in which we find the Buildfile. And each sub-project has
420
+ # a base directory that is one level down, with the same name as the sub-project.
421
+ #
422
+ # For example:
423
+ # /home/foo/ <-- base_directory of project 'foo'
424
+ # /home/foo/Buildfile <-- builds 'foo'
425
+ # /home/foo/bar <-- sub-project 'foo:bar'
426
+ def base_dir
427
+ if @base_dir.nil?
428
+ if parent
429
+ # For sub-project, a good default is a directory in the parent's base_dir,
430
+ # using the same name as the project.
431
+ @base_dir = File.expand_path(name.split(':').last, parent.base_dir)
432
+ else
433
+ # For top-level project, a good default is the directory where we found the Buildfile.
434
+ @base_dir = Dir.pwd
435
+ end
436
+ end
437
+ @base_dir
438
+ end
439
+
440
+ # Returns the layout associated with this project.
441
+ def layout
442
+ @layout ||= (parent ? parent.layout : Layout.default).clone
443
+ end
444
+
445
+ # :call-seq:
446
+ # path_to(*names) => path
447
+ #
448
+ # Returns a path from a combination of name, relative to the project's base directory.
449
+ # Essentially, joins all the supplied names and expands the path relative to #base_dir.
450
+ # Symbol arguments are converted to paths based on the layout, so whenever possible stick
451
+ # to these. For example:
452
+ # path_to(:source, :main, :java)
453
+ # => 'src/main/java'
454
+ #
455
+ # Keep in mind that all tasks are defined and executed relative to the Buildfile directory,
456
+ # so you want to use #path_to to get the actual path within the project as a matter of practice.
457
+ #
458
+ # For example:
459
+ # path_to('foo', 'bar')
460
+ # => foo/bar
461
+ # path_to('/tmp')
462
+ # => /tmp
463
+ # path_to(:base_dir, 'foo') # same as path_to('foo")
464
+ # => /home/project1/foo
465
+ def path_to(*names)
466
+ File.expand_path(layout.expand(*names), base_dir)
467
+ end
468
+ alias :_ :path_to
469
+
470
+ # :call-seq:
471
+ # file(path) => Task
472
+ # file(path=>prereqs) => Task
473
+ # file(path) { |task| ... } => Task
474
+ #
475
+ # Creates and returns a new file task in the project. Similar to calling Rake's
476
+ # file method, but the path is expanded relative to the project's base directory,
477
+ # and the task executes in the project's base directory.
478
+ #
479
+ # For example:
480
+ # define 'foo' do
481
+ # define 'bar' do
482
+ # file('src') { ... }
483
+ # end
484
+ # end
485
+ #
486
+ # puts project('foo:bar').file('src').to_s
487
+ # => '/home/foo/bar/src'
488
+ def file(*args, &block)
489
+ task_name, arg_names, deps = Buildr.application.resolve_args(args)
490
+ task = Rake::FileTask.define_task(path_to(task_name))
491
+ task.set_arg_names(arg_names) unless arg_names.empty?
492
+ task.enhance Array(deps), &block
493
+ end
494
+
495
+ # :call-seq:
496
+ # task(name) => Task
497
+ # task(name=>prereqs) => Task
498
+ # task(name) { |task| ... } => Task
499
+ #
500
+ # Creates and returns a new task in the project. Similar to calling Rake's task
501
+ # method, but prefixes the task name with the project name and executes the task
502
+ # in the project's base directory.
503
+ #
504
+ # For example:
505
+ # define 'foo' do
506
+ # task 'doda'
507
+ # end
508
+ #
509
+ # puts project('foo').task('doda').name
510
+ # => 'foo:doda'
511
+ #
512
+ # When called from within the project definition, creates a new task if the task
513
+ # does not already exist. If called from outside the project definition, returns
514
+ # the named task and raises an exception if the task is not defined.
515
+ #
516
+ # As with Rake's task method, calling this method enhances the task with the
517
+ # prerequisites and optional block.
518
+ def task(*args, &block)
519
+ task_name, arg_names, deps = Buildr.application.resolve_args(args)
520
+ if task_name =~ /^:/
521
+ task = Buildr.application.switch_to_namespace [] do
522
+ Rake::Task.define_task(task_name[1..-1])
523
+ end
524
+ elsif Buildr.application.current_scope == name.split(':')
525
+ task = Rake::Task.define_task(task_name)
526
+ else
527
+ unless task = Buildr.application.lookup(task_name, name.split(':'))
528
+ raise "You cannot define a project task outside the project definition, and no task #{name}:#{task_name} defined in the project"
529
+ end
530
+ end
531
+ task.set_arg_names(arg_names) unless arg_names.empty?
532
+ task.enhance Array(deps), &block
533
+ end
534
+
535
+ # :call-seq:
536
+ # recursive_task(name=>prereqs) { |task| ... }
537
+ #
538
+ # Define a recursive task. A recursive task executes itself and the same task
539
+ # in all the sub-projects.
540
+ def recursive_task(*args, &block)
541
+ task_name, arg_names, deps = Buildr.application.resolve_args(args)
542
+ task = Buildr.options.parallel ? multitask(task_name) : task(task_name)
543
+ parent.task(task_name).enhance [task] if parent
544
+ task.set_arg_names(arg_names) unless arg_names.empty?
545
+ task.enhance Array(deps), &block
546
+ end
547
+
548
+ # :call-seq:
549
+ # project(name) => project
550
+ # project => self
551
+ #
552
+ # Same as Buildr#project. This method is called on a project, so a relative name is
553
+ # sufficient to find a sub-project.
554
+ #
555
+ # When called on a project without a name, returns the project itself. You can use that when
556
+ # setting project properties, for example:
557
+ # define 'foo' do
558
+ # project.version = '1.0'
559
+ # end
560
+ def project(*args)
561
+ if Hash === args.last
562
+ options = args.pop
563
+ else
564
+ options = {}
565
+ end
566
+ if args.empty?
567
+ self
568
+ else
569
+ Project.project *(args + [{ :scope=>self.name }.merge(options)])
570
+ end
571
+ end
572
+
573
+ # :call-seq:
574
+ # projects(*names) => projects
575
+ #
576
+ # Same as Buildr#projects. This method is called on a project, so relative names are
577
+ # sufficient to find sub-projects.
578
+ def projects(*args)
579
+ if Hash === args.last
580
+ options = args.pop
581
+ else
582
+ options = {}
583
+ end
584
+ Project.projects *(args + [{ :scope=>self.name }.merge(options)])
585
+ end
586
+
587
+ def inspect #:nodoc:
588
+ %Q{project(#{name.inspect})}
589
+ end
590
+
591
+ def callbacks #:nodoc:
592
+ # global + project_local callbacks for this project
593
+ @callbacks ||= []
594
+ end
595
+
596
+ def calledback #:nodoc:
597
+ # project-local callbacks that have been called
598
+ @calledback ||= {}
599
+ end
600
+
601
+ protected
602
+
603
+ # :call-seq:
604
+ # base_dir = dir
605
+ #
606
+ # Sets the project's base directory. Allows you to specify a base directory by calling
607
+ # this accessor, or with the :base_dir property when calling #define.
608
+ #
609
+ # You can only set the base directory once for a given project, and only before accessing
610
+ # the base directory (for example, by calling #file or #path_to).
611
+ # Set the base directory. Note: you can only do this once for a project,
612
+ # and only before accessing the base directory. If you try reading the
613
+ # value with #base_dir, the base directory cannot be set again.
614
+ def base_dir=(dir)
615
+ raise 'Cannot set base directory twice, or after reading its value' if @base_dir
616
+ @base_dir = File.expand_path(dir)
617
+ end
618
+
619
+ # Sets the project layout. Accepts Layout object or class (or for that matter, anything
620
+ # that can expand).
621
+ def layout=(layout)
622
+ raise 'Cannot set directory layout twice, or after reading its value' if @layout
623
+ @layout = layout.is_a?(Class) ? layout.new : layout
624
+ end
625
+
626
+ # :call-seq:
627
+ # define(name, properties?) { |project| ... } => project
628
+ #
629
+ # Define a new sub-project within this project. See Buildr#define.
630
+ def define(name, properties = nil, &block)
631
+ Project.define "#{self.name}:#{name}", properties, &block
632
+ end
633
+
634
+ def execute(args) #:nodoc:
635
+ Buildr.application.switch_to_namespace name.split(':') do
636
+ super
637
+ end
638
+ end
639
+
640
+ # Call all extension callbacks for a particular phase, e.g. :before_define, :after_define.
641
+ def call_callbacks(phase) #:nodoc:
642
+ remaining = @callbacks.select { |cb| cb.phase == phase }
643
+ known_callbacks = remaining.map { |cb| cb.name }
644
+
645
+ # find first callback with satisfied dependencies
646
+ first_satisfied = lambda do
647
+ remaining_names = remaining.map { |cb| cb.name }
648
+ remaining.find do |cb|
649
+ cb.dependencies.each do |dep|
650
+ fail "Unknown #{phase.inspect} extension dependency: #{dep.inspect}" unless known_callbacks.index(dep)
651
+ end
652
+ satisfied = cb.dependencies.find { |dep| remaining_names.index(dep) } == nil
653
+ remaining.delete cb if satisfied
654
+ end
655
+ end
656
+
657
+ # call each extension in order
658
+ until remaining.empty?
659
+ callback = first_satisfied.call
660
+ if callback.nil?
661
+ hash = remaining.map { |cb| { cb.name => cb.dependencies} }
662
+ fail "Unsatisfied dependencies in extensions for #{phase}: #{hash.inspect}"
663
+ end
664
+ callback.blocks.each { |b| b.call(self) }
665
+ end
666
+ end
667
+
668
+ end
669
+
670
+
671
+ # The basic mechanism for extending projects in Buildr are Ruby modules. In fact,
672
+ # base features like compiling and testing are all developed in the form of modules,
673
+ # and then added to the core Project class.
674
+ #
675
+ # A module defines instance methods that are then mixed into the project and become
676
+ # instance methods of the project. There are two general ways for extending projects.
677
+ # You can extend all projects by including the module in Project:
678
+ # class Project
679
+ # include MyExtension
680
+ # end
681
+ # You can also extend a given project instance and only that instance by extending
682
+ # it with the module:
683
+ # define 'foo' do
684
+ # extend MyExtension
685
+ # end
686
+ #
687
+ # Some extensions require tighter integration with the project, specifically for
688
+ # setting up tasks and properties, or for configuring tasks based on the project
689
+ # definition. You can do that by adding callbacks to the process.
690
+ #
691
+ # The easiest way to add callbacks is by incorporating the Extension module in your
692
+ # own extension, and using the various class methods to define callback behavior:
693
+ # * first_time -- This block will be called once for any particular extension.
694
+ # You can use this to setup top-level and local tasks.
695
+ # * before_define -- This block is called once for the project with the project
696
+ # instance, right before running the project definition. You can use this
697
+ # to add tasks and set properties that will be used in the project definition.
698
+ # * after_define -- This block is called once for the project with the project
699
+ # instance, right after running the project definition. You can use this to
700
+ # do any post-processing that depends on the project definition.
701
+ #
702
+ # This example illustrates how to write a simple extension:
703
+ # module LinesOfCode
704
+ # include Extension
705
+ #
706
+ # first_time do
707
+ # # Define task not specific to any projet.
708
+ # desc 'Count lines of code in current project'
709
+ # Project.local_task('loc')
710
+ # end
711
+ #
712
+ # before_define do |project|
713
+ # # Define the loc task for this particular project.
714
+ # Rake::Task.define_task 'loc' do |task|
715
+ # lines = task.prerequisites.map { |path| Dir['#{path}/**/*'] }.flatten.uniq.
716
+ # inject(0) { |total, file| total + File.readlines(file).count }
717
+ # puts "Project #{project.name} has #{lines} lines of code"
718
+ # end
719
+ # end
720
+ #
721
+ # after_define do |project|
722
+ # # Now that we know all the source directories, add them.
723
+ # task('loc'=>compile.sources + compile.test.sources)
724
+ # end
725
+ #
726
+ # # To use this method in your project:
727
+ # # loc path_1, path_2
728
+ # def loc(*paths)
729
+ # task('loc'=>paths)
730
+ # end
731
+ #
732
+ # end
733
+ #
734
+ # class Buildr::Project
735
+ # include LinesOfCode
736
+ # end
737
+ module Extension
738
+
739
+ # Extension callback details
740
+ class Callback #:nodoc:
741
+ attr_accessor :phase, :name, :dependencies, :blocks
742
+
743
+ def initialize(phase, name, dependencies, blocks)
744
+ @phase = phase
745
+ @name = name
746
+ @dependencies = dependencies
747
+ @blocks = (blocks ? (Array === blocks ? blocks : [blocks]) : [])
748
+ end
749
+
750
+ def merge(callback)
751
+ Callback.new(phase, name, @dependencies + callback.dependencies, @blocks + callback.blocks)
752
+ end
753
+ end
754
+
755
+ def self.included(base) #:nodoc:
756
+ base.extend ClassMethods
757
+ end
758
+
759
+ # Methods added to the extension module when including Extension.
760
+ module ClassMethods
761
+
762
+ def included(base) #:nodoc:
763
+ # When included in Project, add module instance, merge callbacks and call first_time.
764
+ if Project == base && !base.extension_modules.include?(module_callbacks)
765
+ base.extension_modules << module_callbacks
766
+ merge_callbacks(base.global_callbacks, module_callbacks)
767
+ first_time = module_callbacks.select { |c| c.phase == :first_time }
768
+ first_time.each do |c|
769
+ c.blocks.each { |b| b.call }
770
+ end
771
+ end
772
+ end
773
+
774
+ def extended(base) #:nodoc:
775
+ # When extending project, merge after_define callbacks and call before_define callback(s)
776
+ # immediately
777
+ if Project === base
778
+ merge_callbacks(base.callbacks, module_callbacks.select { |cb| cb.phase == :after_define })
779
+ calls = module_callbacks.select { |cb| cb.phase == :before_define }
780
+ calls.each do |cb|
781
+ cb.blocks.each { |b| b.call(base) } unless base.calledback[cb]
782
+ base.calledback[cb] = cb
783
+ end
784
+ end
785
+ end
786
+
787
+ # This block will be called once for any particular extension included in Project.
788
+ # You can use this to setup top-level and local tasks.
789
+ def first_time(&block)
790
+ module_callbacks << Callback.new(:first_time, self.name, [], block)
791
+ end
792
+
793
+ # This block is called once for the project with the project instance,
794
+ # right before running the project definition. You can use this to add
795
+ # tasks and set properties that will be used in the project definition.
796
+ #
797
+ # The block may be named and dependencies may be declared similar to Rake
798
+ # task dependencies:
799
+ #
800
+ # before_define(:my_setup) do |project|
801
+ # # do stuff on project
802
+ # end
803
+ #
804
+ # # my_setup code must run before :compile
805
+ # before_define(:compile => :my_setup)
806
+ #
807
+ def before_define(*args, &block)
808
+ if args.empty?
809
+ name = self.name
810
+ deps = []
811
+ else
812
+ name, args, deps = Buildr.application.resolve_args(args)
813
+ end
814
+ module_callbacks << Callback.new(:before_define, name, deps, block)
815
+ end
816
+
817
+ # This block is called once for the project with the project instance,
818
+ # right after running the project definition. You can use this to do
819
+ # any post-processing that depends on the project definition.
820
+ #
821
+ # The block may be named and dependencies may be declared similar to Rake
822
+ # task dependencies:
823
+ #
824
+ # after_define(:my_setup) do |project|
825
+ # # do stuff on project
826
+ # end
827
+ #
828
+ # # my_setup code must run before :compile (but only after project is defined)
829
+ # after_define(:compile => :my_setup)
830
+ #
831
+ def after_define(*args, &block)
832
+ if args.empty?
833
+ name = self.name
834
+ deps = []
835
+ else
836
+ name, args, deps = Buildr.application.resolve_args(args)
837
+ end
838
+ module_callbacks << Callback.new(:after_define, name, deps, block)
839
+ end
840
+
841
+ private
842
+
843
+ def module_callbacks
844
+ begin
845
+ const_get('Callbacks')
846
+ rescue
847
+ callbacks = []
848
+ const_set('Callbacks', callbacks)
849
+ end
850
+ end
851
+
852
+ def merge_callbacks(base, merge)
853
+ # index by phase and name
854
+ index = base.inject({}) { |hash,cb| { [cb.phase, cb.name] => cb } }
855
+ merge.each do |cb|
856
+ existing = index[[cb.phase, cb.name]]
857
+ if existing
858
+ base[base.index(existing)] = existing.merge(cb)
859
+ else
860
+ base << cb
861
+ end
862
+ index[[cb.phase, cb.name]] = cb
863
+ end
864
+ base
865
+ end
866
+ end
867
+
868
+ end
869
+
870
+
871
+ # :call-seq:
872
+ # define(name, properties?) { |project| ... } => project
873
+ #
874
+ # Defines a new project.
875
+ #
876
+ # The first argument is the project name. Each project must have a unique name.
877
+ # For a sub-project, the actual project name is created by prefixing the parent
878
+ # project's name.
879
+ #
880
+ # The second argument is optional and contains a hash or properties that are set
881
+ # on the project. You can only use properties that are supported by the project
882
+ # definition, e.g. :group and :version. You can also set these properties from the
883
+ # project definition.
884
+ #
885
+ # You pass a block that is executed in the context of the project definition.
886
+ # This block is used to define the project and tasks that are part of the project.
887
+ # Do not perform any work inside the project itself, as it will execute each time
888
+ # the Buildfile is loaded. Instead, use it to create and extend tasks that are
889
+ # related to the project.
890
+ #
891
+ # For example:
892
+ # define 'foo', :version=>'1.0' do
893
+ #
894
+ # define 'bar' do
895
+ # compile.with 'org.apache.axis2:axis2:jar:1.1'
896
+ # end
897
+ # end
898
+ #
899
+ # puts project('foo').version
900
+ # => '1.0'
901
+ # puts project('foo:bar').compile.classpath.map(&:to_spec)
902
+ # => 'org.apache.axis2:axis2:jar:1.1'
903
+ # % buildr build
904
+ # => Compiling 14 source files in foo:bar
905
+ def define(name, properties = nil, &block) #:yields:project
906
+ Project.define(name, properties, &block)
907
+ end
908
+
909
+ # :call-seq:
910
+ # project(name) => project
911
+ #
912
+ # Returns a project definition.
913
+ #
914
+ # When called from outside a project definition, must reference the project by its
915
+ # full name, e.g. 'foo:bar' to access the sub-project 'bar' in 'foo'. When called
916
+ # from inside a project, relative names are sufficient, e.g. <code>project('foo').project('bar')</code>
917
+ # will find the sub-project 'bar' in 'foo'.
918
+ #
919
+ # You cannot reference a project before the project is defined. When working with
920
+ # sub-projects, the project definition is stored by calling #define, and evaluated
921
+ # before a call to the parent project's #define method returns.
922
+ #
923
+ # However, if you call #project with the name of another sub-project, its definition
924
+ # is evaluated immediately. So the returned project definition is always complete,
925
+ # and you can access its definition (e.g. to find files relative to the base directory,
926
+ # or packages created by that project).
927
+ #
928
+ # For example:
929
+ # define 'myapp' do
930
+ # self.version = '1.1'
931
+ #
932
+ # define 'webapp' do
933
+ # # webapp is defined first, but beans is evaluated first
934
+ # compile.with project('beans')
935
+ # package :war
936
+ # end
937
+ #
938
+ # define 'beans' do
939
+ # package :jar
940
+ # end
941
+ # end
942
+ #
943
+ # puts project('myapp:beans').version
944
+ def project(*args)
945
+ Project.project *args
946
+ end
947
+
948
+ # :call-seq:
949
+ # projects(*names) => projects
950
+ #
951
+ # With no arguments, returns a list of all projects defined so far. When called on a project,
952
+ # returns all its sub-projects (direct descendants).
953
+ #
954
+ # With arguments, returns a list of named projects, fails on any name that does not exist.
955
+ # As with #project, you can use relative names when calling this method on a project.
956
+ #
957
+ # Like #project, this method evaluates the definition of each project before returning it.
958
+ # Be advised of circular dependencies.
959
+ #
960
+ # For example:
961
+ # files = projects.map { |prj| FileList[prj.path_to('src/**/*.java') }.flatten
962
+ # puts "There are #{files.size} source files in #{projects.size} projects"
963
+ #
964
+ # puts projects('myapp:beans', 'myapp:webapp').map(&:name)
965
+ # Same as:
966
+ # puts project('myapp').projects.map(&:name)
967
+ def projects(*args)
968
+ Project.projects *args
969
+ end
970
+
971
+ end