assaf-buildr 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/CHANGELOG +887 -0
  2. data/DISCLAIMER +7 -0
  3. data/LICENSE +176 -0
  4. data/NOTICE +26 -0
  5. data/README.rdoc +146 -0
  6. data/Rakefile +62 -0
  7. data/_buildr +38 -0
  8. data/addon/buildr/antlr.rb +65 -0
  9. data/addon/buildr/cobertura.rb +236 -0
  10. data/addon/buildr/emma.rb +238 -0
  11. data/addon/buildr/hibernate.rb +142 -0
  12. data/addon/buildr/javacc.rb +85 -0
  13. data/addon/buildr/jdepend.rb +60 -0
  14. data/addon/buildr/jetty.rb +248 -0
  15. data/addon/buildr/jibx.rb +86 -0
  16. data/addon/buildr/nailgun.rb +817 -0
  17. data/addon/buildr/openjpa.rb +90 -0
  18. data/addon/buildr/org/apache/buildr/BuildrNail$Main.class +0 -0
  19. data/addon/buildr/org/apache/buildr/BuildrNail.class +0 -0
  20. data/addon/buildr/org/apache/buildr/BuildrNail.java +41 -0
  21. data/addon/buildr/org/apache/buildr/JettyWrapper$1.class +0 -0
  22. data/addon/buildr/org/apache/buildr/JettyWrapper$BuildrHandler.class +0 -0
  23. data/addon/buildr/org/apache/buildr/JettyWrapper.class +0 -0
  24. data/addon/buildr/org/apache/buildr/JettyWrapper.java +144 -0
  25. data/addon/buildr/xmlbeans.rb +93 -0
  26. data/bin/buildr +28 -0
  27. data/buildr.buildfile +53 -0
  28. data/buildr.gemspec +58 -0
  29. data/doc/css/default.css +228 -0
  30. data/doc/css/print.css +100 -0
  31. data/doc/css/syntax.css +52 -0
  32. data/doc/images/apache-incubator-logo.png +0 -0
  33. data/doc/images/buildr-hires.png +0 -0
  34. data/doc/images/buildr.png +0 -0
  35. data/doc/images/favicon.png +0 -0
  36. data/doc/images/growl-icon.tiff +0 -0
  37. data/doc/images/note.png +0 -0
  38. data/doc/images/project-structure.png +0 -0
  39. data/doc/images/tip.png +0 -0
  40. data/doc/images/zbuildr.tif +0 -0
  41. data/doc/pages/artifacts.textile +207 -0
  42. data/doc/pages/building.textile +240 -0
  43. data/doc/pages/contributing.textile +208 -0
  44. data/doc/pages/download.textile +62 -0
  45. data/doc/pages/extending.textile +175 -0
  46. data/doc/pages/getting_started.textile +273 -0
  47. data/doc/pages/index.textile +42 -0
  48. data/doc/pages/languages.textile +407 -0
  49. data/doc/pages/mailing_lists.textile +17 -0
  50. data/doc/pages/more_stuff.textile +286 -0
  51. data/doc/pages/packaging.textile +427 -0
  52. data/doc/pages/projects.textile +274 -0
  53. data/doc/pages/recipes.textile +103 -0
  54. data/doc/pages/settings_profiles.textile +274 -0
  55. data/doc/pages/testing.textile +212 -0
  56. data/doc/pages/troubleshooting.textile +103 -0
  57. data/doc/pages/whats_new.textile +323 -0
  58. data/doc/print.haml +51 -0
  59. data/doc/print.toc.yaml +29 -0
  60. data/doc/scripts/buildr-git.rb +412 -0
  61. data/doc/scripts/install-jruby.sh +44 -0
  62. data/doc/scripts/install-linux.sh +64 -0
  63. data/doc/scripts/install-osx.sh +52 -0
  64. data/doc/site.haml +56 -0
  65. data/doc/site.toc.yaml +47 -0
  66. data/etc/KEYS +151 -0
  67. data/etc/git-svn-authors +16 -0
  68. data/lib/buildr.rb +35 -0
  69. data/lib/buildr/core.rb +27 -0
  70. data/lib/buildr/core/application.rb +489 -0
  71. data/lib/buildr/core/application_cli.rb +139 -0
  72. data/lib/buildr/core/build.rb +311 -0
  73. data/lib/buildr/core/checks.rb +382 -0
  74. data/lib/buildr/core/common.rb +154 -0
  75. data/lib/buildr/core/compile.rb +596 -0
  76. data/lib/buildr/core/environment.rb +120 -0
  77. data/lib/buildr/core/filter.rb +362 -0
  78. data/lib/buildr/core/generate.rb +195 -0
  79. data/lib/buildr/core/help.rb +118 -0
  80. data/lib/buildr/core/progressbar.rb +156 -0
  81. data/lib/buildr/core/project.rb +892 -0
  82. data/lib/buildr/core/test.rb +715 -0
  83. data/lib/buildr/core/transports.rb +558 -0
  84. data/lib/buildr/core/util.rb +289 -0
  85. data/lib/buildr/groovy.rb +18 -0
  86. data/lib/buildr/groovy/bdd.rb +105 -0
  87. data/lib/buildr/groovy/compiler.rb +138 -0
  88. data/lib/buildr/ide.rb +19 -0
  89. data/lib/buildr/ide/eclipse.rb +212 -0
  90. data/lib/buildr/ide/idea.ipr.template +300 -0
  91. data/lib/buildr/ide/idea.rb +189 -0
  92. data/lib/buildr/ide/idea7x.ipr.template +290 -0
  93. data/lib/buildr/ide/idea7x.rb +210 -0
  94. data/lib/buildr/java.rb +23 -0
  95. data/lib/buildr/java/ant.rb +92 -0
  96. data/lib/buildr/java/bdd.rb +449 -0
  97. data/lib/buildr/java/commands.rb +211 -0
  98. data/lib/buildr/java/compiler.rb +348 -0
  99. data/lib/buildr/java/deprecated.rb +141 -0
  100. data/lib/buildr/java/jruby.rb +117 -0
  101. data/lib/buildr/java/jtestr_runner.rb.erb +116 -0
  102. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.class +0 -0
  103. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.java +119 -0
  104. data/lib/buildr/java/packaging.rb +713 -0
  105. data/lib/buildr/java/pom.rb +178 -0
  106. data/lib/buildr/java/rjb.rb +155 -0
  107. data/lib/buildr/java/test_result.rb +308 -0
  108. data/lib/buildr/java/tests.rb +324 -0
  109. data/lib/buildr/java/version_requirement.rb +172 -0
  110. data/lib/buildr/packaging.rb +21 -0
  111. data/lib/buildr/packaging/artifact.rb +730 -0
  112. data/lib/buildr/packaging/artifact_namespace.rb +972 -0
  113. data/lib/buildr/packaging/artifact_search.rb +140 -0
  114. data/lib/buildr/packaging/gems.rb +102 -0
  115. data/lib/buildr/packaging/package.rb +233 -0
  116. data/lib/buildr/packaging/tar.rb +104 -0
  117. data/lib/buildr/packaging/zip.rb +722 -0
  118. data/lib/buildr/resources/buildr.icns +0 -0
  119. data/lib/buildr/scala.rb +19 -0
  120. data/lib/buildr/scala/compiler.rb +109 -0
  121. data/lib/buildr/scala/tests.rb +203 -0
  122. data/rakelib/apache.rake +191 -0
  123. data/rakelib/changelog.rake +57 -0
  124. data/rakelib/doc.rake +103 -0
  125. data/rakelib/package.rake +73 -0
  126. data/rakelib/release.rake +65 -0
  127. data/rakelib/rspec.rake +83 -0
  128. data/rakelib/rubyforge.rake +53 -0
  129. data/rakelib/scm.rake +49 -0
  130. data/rakelib/setup.rake +86 -0
  131. data/rakelib/stage.rake +48 -0
  132. data/spec/addon/cobertura_spec.rb +77 -0
  133. data/spec/addon/emma_spec.rb +120 -0
  134. data/spec/addon/test_coverage_spec.rb +255 -0
  135. data/spec/core/application_spec.rb +412 -0
  136. data/spec/core/artifact_namespace_spec.rb +646 -0
  137. data/spec/core/build_spec.rb +415 -0
  138. data/spec/core/checks_spec.rb +537 -0
  139. data/spec/core/common_spec.rb +664 -0
  140. data/spec/core/compile_spec.rb +566 -0
  141. data/spec/core/generate_spec.rb +33 -0
  142. data/spec/core/project_spec.rb +754 -0
  143. data/spec/core/test_spec.rb +1091 -0
  144. data/spec/core/transport_spec.rb +500 -0
  145. data/spec/groovy/bdd_spec.rb +80 -0
  146. data/spec/groovy/compiler_spec.rb +239 -0
  147. data/spec/ide/eclipse_spec.rb +243 -0
  148. data/spec/java/ant.rb +28 -0
  149. data/spec/java/bdd_spec.rb +358 -0
  150. data/spec/java/compiler_spec.rb +446 -0
  151. data/spec/java/java_spec.rb +88 -0
  152. data/spec/java/packaging_spec.rb +1103 -0
  153. data/spec/java/tests_spec.rb +466 -0
  154. data/spec/packaging/archive_spec.rb +503 -0
  155. data/spec/packaging/artifact_spec.rb +754 -0
  156. data/spec/packaging/packaging_helper.rb +63 -0
  157. data/spec/packaging/packaging_spec.rb +589 -0
  158. data/spec/sandbox.rb +137 -0
  159. data/spec/scala/compiler_spec.rb +228 -0
  160. data/spec/scala/tests_spec.rb +215 -0
  161. data/spec/spec_helpers.rb +304 -0
  162. data/spec/version_requirement_spec.rb +123 -0
  163. metadata +369 -0
@@ -0,0 +1,892 @@
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
+ # Instantiate callbacks for this project, and setup to call before/after define.
214
+ # Don't cache list of callbacks, since project may add new callbacks.
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 { |callback| callback[project] }
223
+ end if @on_define
224
+ # Enhance the project using the definition block.
225
+ project.enhance { project.instance_eval &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
244
+ if options && options[:scope]
245
+ # We assume parent project is evaluated.
246
+ project = options[:scope].split(':').inject([[]]) { |scopes, scope| scopes << (scopes.last + [scope]) }.
247
+ map { |scope| @projects[(scope + [name]).join(':')] }.
248
+ select { |project| project }.last
249
+ end
250
+ unless project
251
+ # Parent project not evaluated.
252
+ name.split(':').tap { |parts| @projects[parts.first].invoke if parts.size > 1 }
253
+ project = @projects[name]
254
+ end
255
+ raise "No such project #{name}" unless project
256
+ project.invoke
257
+ project
258
+ end
259
+
260
+ # :call-seq:
261
+ # projects(*names) => projects
262
+ #
263
+ # See Buildr#projects.
264
+ def projects(*names) #:nodoc:
265
+ options = names.pop if Hash === names.last
266
+ rake_check_options options, :scope if options
267
+ @projects ||= {}
268
+ names = names.flatten
269
+ if options && options[:scope]
270
+ # We assume parent project is evaluated.
271
+ if names.empty?
272
+ parent = @projects[options[:scope].to_s] or raise "No such project #{options[:scope]}"
273
+ @projects.values.select { |project| project.parent == parent }.each { |project| project.invoke }.
274
+ map { |project| [project] + projects(:scope=>project) }.flatten.sort_by(&:name)
275
+ else
276
+ names.uniq.map { |name| project(name, :scope=>options[:scope]) }
277
+ end
278
+ elsif names.empty?
279
+ # Parent project(s) not evaluated so we don't know all the projects yet.
280
+ @projects.values.each(&:invoke)
281
+ @projects.keys.map { |name| project(name) or raise "No such project #{name}" }.sort_by(&:name)
282
+ else
283
+ # Parent project(s) not evaluated, for the sub-projects we may need to find.
284
+ names.map { |name| name.split(':') }.select { |name| name.size > 1 }.map(&:first).uniq.each { |name| project(name) }
285
+ names.uniq.map { |name| project(name) or raise "No such project #{name}" }.sort_by(&:name)
286
+ end
287
+ end
288
+
289
+ # :call-seq:
290
+ # clear
291
+ #
292
+ # Discard all project definitions.
293
+ def clear
294
+ @projects.clear if @projects
295
+ end
296
+
297
+ # :call-seq:
298
+ # local_task(name)
299
+ # local_task(name) { |name| ... }
300
+ #
301
+ # Defines a local task with an optional execution message.
302
+ #
303
+ # A local task is a task that executes a task with the same name, defined in the
304
+ # current project, the project's with a base directory that is the same as the
305
+ # current directory.
306
+ #
307
+ # Complicated? Try this:
308
+ # buildr build
309
+ # is the same as:
310
+ # buildr foo:build
311
+ # But:
312
+ # cd bar
313
+ # buildr build
314
+ # is the same as:
315
+ # buildr foo:bar:build
316
+ #
317
+ # The optional block is called with the project name when the task executes
318
+ # and returns a message that, for example "Building project #{name}".
319
+ def local_task(args, &block)
320
+ task args do |task|
321
+ local_projects do |project|
322
+ info block.call(project.name) if block
323
+ task("#{project.name}:#{task.name}").invoke
324
+ end
325
+ end
326
+ end
327
+
328
+ # *Deprecated* Check the Extension module to see how extensions are handled.
329
+ def on_define(&block)
330
+ Buildr.application.deprecated 'This method is deprecated, see Extension'
331
+ (@on_define ||= []) << block if block
332
+ end
333
+
334
+ def scope_name(scope, task_name) #:nodoc:
335
+ task_name
336
+ end
337
+
338
+ def local_projects(dir = nil, &block) #:nodoc:
339
+ dir = File.expand_path(dir || Buildr.application.original_dir)
340
+ projects = @projects ? @projects.values : []
341
+ projects = projects.select { |project| project.base_dir == dir }
342
+ if projects.empty? && dir != Dir.pwd && File.dirname(dir) != dir
343
+ local_projects(File.dirname(dir), &block)
344
+ elsif block
345
+ if projects.empty?
346
+ warn "No projects defined for directory #{Buildr.application.original_dir}"
347
+ else
348
+ projects.each { |project| block[project] }
349
+ end
350
+ else
351
+ projects
352
+ end
353
+ end
354
+
355
+ # :call-seq:
356
+ # parent_task(task_name) => task_name or nil
357
+ #
358
+ # Returns a parent task, basically a task in a higher namespace. For example, the parent
359
+ # of 'foo:test:compile' is 'foo:compile' and the parent of 'foo:compile' is 'compile'.
360
+ def parent_task(task_name) #:nodoc:
361
+ namespace = task_name.split(':')
362
+ last_name = namespace.pop
363
+ namespace.pop
364
+ Buildr.application.lookup((namespace + [last_name]).join(':'), []) unless namespace.empty?
365
+ end
366
+
367
+ # :call-seq:
368
+ # project_from_task(task) => project
369
+ #
370
+ # Figure out project associated to this task and return it.
371
+ def project_from_task(task) #:nodoc:
372
+ project = Buildr.application.lookup('rake:' + task.to_s.gsub(/:[^:]*$/, ''))
373
+ project if Project === project
374
+ end
375
+
376
+ # Callback classes.
377
+ def callbacks #:nodoc:
378
+ @callbacks ||= []
379
+ end
380
+
381
+ end
382
+
383
+
384
+ # Project has visibility to everything in the Buildr namespace.
385
+ include Buildr
386
+
387
+ # The project name. For example, 'foo' for the top-level project, and 'foo:bar'
388
+ # for its sub-project.
389
+ attr_reader :name
390
+
391
+ # The parent project if this is a sub-project.
392
+ attr_reader :parent
393
+
394
+ def initialize(*args) #:nodoc:
395
+ super
396
+ split = name.split(':')
397
+ if split.size > 1
398
+ # Get parent project, but do not invoke it's definition to prevent circular
399
+ # dependencies (it's being invoked right now, so calling project will fail).
400
+ @parent = task(split[0...-1].join(':'))
401
+ raise "No parent project #{split[0...-1].join(':')}" unless @parent && Project === parent
402
+ end
403
+ callbacks = Project.callbacks.uniq.map(&:new)
404
+ @callbacks = [:before_define, :after_define].inject({}) do |hash, state|
405
+ methods = callbacks.select { |callback| callback.respond_to?(state) }.map { |callback| callback.method(state) }
406
+ hash.update(state=>methods)
407
+ end
408
+ end
409
+
410
+ # :call-seq:
411
+ # base_dir => path
412
+ #
413
+ # Returns the project's base directory.
414
+ #
415
+ # The Buildfile defines top-level project, so it's logical that the top-level project's
416
+ # base directory is the one in which we find the Buildfile. And each sub-project has
417
+ # a base directory that is one level down, with the same name as the sub-project.
418
+ #
419
+ # For example:
420
+ # /home/foo/ <-- base_directory of project 'foo'
421
+ # /home/foo/Buildfile <-- builds 'foo'
422
+ # /home/foo/bar <-- sub-project 'foo:bar'
423
+ def base_dir
424
+ if @base_dir.nil?
425
+ if parent
426
+ # For sub-project, a good default is a directory in the parent's base_dir,
427
+ # using the same name as the project.
428
+ @base_dir = File.expand_path(name.split(':').last, parent.base_dir)
429
+ else
430
+ # For top-level project, a good default is the directory where we found the Buildfile.
431
+ @base_dir = Dir.pwd
432
+ end
433
+ end
434
+ @base_dir
435
+ end
436
+
437
+ # Returns the layout associated with this project.
438
+ def layout
439
+ @layout ||= (parent ? parent.layout : Layout.default).clone
440
+ end
441
+
442
+ # :call-seq:
443
+ # path_to(*names) => path
444
+ #
445
+ # Returns a path from a combination of name, relative to the project's base directory.
446
+ # Essentially, joins all the supplied names and expands the path relative to #base_dir.
447
+ # Symbol arguments are converted to paths based on the layout, so whenever possible stick
448
+ # to these. For example:
449
+ # path_to(:source, :main, :java)
450
+ # => 'src/main/java'
451
+ #
452
+ # Keep in mind that all tasks are defined and executed relative to the Buildfile directory,
453
+ # so you want to use #path_to to get the actual path within the project as a matter of practice.
454
+ #
455
+ # For example:
456
+ # path_to('foo', 'bar')
457
+ # => foo/bar
458
+ # path_to('/tmp')
459
+ # => /tmp
460
+ # path_to(:base_dir, 'foo') # same as path_to('foo")
461
+ # => /home/project1/foo
462
+ def path_to(*names)
463
+ File.expand_path(layout.expand(*names), base_dir)
464
+ end
465
+ alias :_ :path_to
466
+
467
+ # :call-seq:
468
+ # file(path) => Task
469
+ # file(path=>prereqs) => Task
470
+ # file(path) { |task| ... } => Task
471
+ #
472
+ # Creates and returns a new file task in the project. Similar to calling Rake's
473
+ # file method, but the path is expanded relative to the project's base directory,
474
+ # and the task executes in the project's base directory.
475
+ #
476
+ # For example:
477
+ # define 'foo' do
478
+ # define 'bar' do
479
+ # file('src') { ... }
480
+ # end
481
+ # end
482
+ #
483
+ # puts project('foo:bar').file('src').to_s
484
+ # => '/home/foo/bar/src'
485
+ def file(*args, &block)
486
+ task_name, arg_names, deps = Buildr.application.resolve_args(args)
487
+ task = Rake::FileTask.define_task(path_to(task_name))
488
+ task.set_arg_names(arg_names) unless arg_names.empty?
489
+ task.enhance Array(deps), &block
490
+ end
491
+
492
+ # :call-seq:
493
+ # task(name) => Task
494
+ # task(name=>prereqs) => Task
495
+ # task(name) { |task| ... } => Task
496
+ #
497
+ # Creates and returns a new task in the project. Similar to calling Rake's task
498
+ # method, but prefixes the task name with the project name and executes the task
499
+ # in the project's base directory.
500
+ #
501
+ # For example:
502
+ # define 'foo' do
503
+ # task 'doda'
504
+ # end
505
+ #
506
+ # puts project('foo').task('doda').name
507
+ # => 'foo:doda'
508
+ #
509
+ # When called from within the project definition, creates a new task if the task
510
+ # does not already exist. If called from outside the project definition, returns
511
+ # the named task and raises an exception if the task is not defined.
512
+ #
513
+ # As with Rake's task method, calling this method enhances the task with the
514
+ # prerequisites and optional block.
515
+ def task(*args, &block)
516
+ task_name, arg_names, deps = Buildr.application.resolve_args(args)
517
+ if task_name =~ /^:/
518
+ task = Buildr.application.switch_to_namespace [] do
519
+ Rake::Task.define_task(task_name[1..-1])
520
+ end
521
+ elsif Buildr.application.current_scope == name.split(':')
522
+ task = Rake::Task.define_task(task_name)
523
+ else
524
+ unless task = Buildr.application.lookup(task_name, name.split(':'))
525
+ raise "You cannot define a project task outside the project definition, and no task #{name}:#{task_name} defined in the project"
526
+ end
527
+ end
528
+ task.set_arg_names(arg_names) unless arg_names.empty?
529
+ task.enhance Array(deps), &block
530
+ end
531
+
532
+ # :call-seq:
533
+ # recursive_task(name=>prereqs) { |task| ... }
534
+ #
535
+ # Define a recursive task. A recursive task executes itself and the same task
536
+ # in all the sub-projects.
537
+ def recursive_task(*args, &block)
538
+ task_name, arg_names, deps = Buildr.application.resolve_args(args)
539
+ task = Buildr.options.parallel ? multitask(task_name) : task(task_name)
540
+ parent.task(task_name).enhance [task] if parent
541
+ task.set_arg_names(arg_names) unless arg_names.empty?
542
+ task.enhance Array(deps), &block
543
+ end
544
+
545
+ # :call-seq:
546
+ # project(name) => project
547
+ # project => self
548
+ #
549
+ # Same as Buildr#project. This method is called on a project, so a relative name is
550
+ # sufficient to find a sub-project.
551
+ #
552
+ # When called on a project without a name, returns the project itself. You can use that when
553
+ # setting project properties, for example:
554
+ # define 'foo' do
555
+ # project.version = '1.0'
556
+ # end
557
+ def project(*args)
558
+ if Hash === args.last
559
+ options = args.pop
560
+ else
561
+ options = {}
562
+ end
563
+ if args.empty?
564
+ self
565
+ else
566
+ Project.project *(args + [{ :scope=>self.name }.merge(options)])
567
+ end
568
+ end
569
+
570
+ # :call-seq:
571
+ # projects(*names) => projects
572
+ #
573
+ # Same as Buildr#projects. This method is called on a project, so relative names are
574
+ # sufficient to find sub-projects.
575
+ def projects(*args)
576
+ if Hash === args.last
577
+ options = args.pop
578
+ else
579
+ options = {}
580
+ end
581
+ Project.projects *(args + [{ :scope=>self.name }.merge(options)])
582
+ end
583
+
584
+ def inspect #:nodoc:
585
+ %Q{project(#{name.inspect})}
586
+ end
587
+
588
+ protected
589
+
590
+ # :call-seq:
591
+ # base_dir = dir
592
+ #
593
+ # Sets the project's base directory. Allows you to specify a base directory by calling
594
+ # this accessor, or with the :base_dir property when calling #define.
595
+ #
596
+ # You can only set the base directory once for a given project, and only before accessing
597
+ # the base directory (for example, by calling #file or #path_to).
598
+ # Set the base directory. Note: you can only do this once for a project,
599
+ # and only before accessing the base directory. If you try reading the
600
+ # value with #base_dir, the base directory cannot be set again.
601
+ def base_dir=(dir)
602
+ raise 'Cannot set base directory twice, or after reading its value' if @base_dir
603
+ @base_dir = File.expand_path(dir)
604
+ end
605
+
606
+ # Sets the project layout. Accepts Layout object or class (or for that matter, anything
607
+ # that can expand).
608
+ def layout=(layout)
609
+ raise 'Cannot set directory layout twice, or after reading its value' if @layout
610
+ @layout = layout.is_a?(Class) ? layout.new : layout
611
+ end
612
+
613
+ # :call-seq:
614
+ # define(name, properties?) { |project| ... } => project
615
+ #
616
+ # Define a new sub-project within this project. See Buildr#define.
617
+ def define(name, properties = nil, &block)
618
+ Project.define "#{self.name}:#{name}", properties, &block
619
+ end
620
+
621
+ def execute(args) #:nodoc:
622
+ Buildr.application.switch_to_namespace name.split(':') do
623
+ super
624
+ end
625
+ end
626
+
627
+ # Call all callbacks for a particular state, e.g. :before_define, :after_define.
628
+ def call_callbacks(state) #:nodoc:
629
+ methods = @callbacks.delete(state) || []
630
+ methods.each { |method| method.call(project) }
631
+ end
632
+
633
+ def add_callback(callback)
634
+ @callbacks[:after_define] << callback.method(:after_define) if callback.respond_to?(:after_define)
635
+ end
636
+
637
+ end
638
+
639
+
640
+ # The basic mechanism for extending projects in Buildr are Ruby modules. In fact,
641
+ # base features like compiling and testing are all developed in the form of modules,
642
+ # and then added to the core Project class.
643
+ #
644
+ # A module defines instance methods that are then mixed into the project and become
645
+ # instance methods of the project. There are two general ways for extending projects.
646
+ # You can extend all projects by including the module in Project:
647
+ # class Project
648
+ # include MyExtension
649
+ # end
650
+ # You can also extend a given project instance and only that instance by extending
651
+ # it with the module:
652
+ # define 'foo' do
653
+ # extend MyExtension
654
+ # end
655
+ #
656
+ # Some extensions require tighter integration with the project, specifically for
657
+ # setting up tasks and properties, or for configuring tasks based on the project
658
+ # definition. You can do that by adding callbacks to the process.
659
+ #
660
+ # The easiest way to add callbacks is by incorporating the Extension module in your
661
+ # own extension, and using the various class methods to define callback behavior:
662
+ # * first_time -- This block will be called once for any particular extension.
663
+ # You can use this to setup top-level and local tasks.
664
+ # * before_define -- This block is called once for the project with the project
665
+ # instance, right before running the project definition. You can use this
666
+ # to add tasks and set properties that will be used in the project definition.
667
+ # * after_define -- This block is called once for the project with the project
668
+ # instance, right after running the project definition. You can use this to
669
+ # do any post-processing that depends on the project definition.
670
+ #
671
+ # This example illustrates how to write a simple extension:
672
+ # module LinesOfCode
673
+ # include Extension
674
+ #
675
+ # first_time do
676
+ # # Define task not specific to any projet.
677
+ # desc 'Count lines of code in current project'
678
+ # Project.local_task('loc')
679
+ # end
680
+ #
681
+ # before_define do |project|
682
+ # # Define the loc task for this particular project.
683
+ # Rake::Task.define_task 'loc' do |task|
684
+ # lines = task.prerequisites.map { |path| Dir['#{path}/**/*'] }.flatten.uniq.
685
+ # inject(0) { |total, file| total + File.readlines(file).count }
686
+ # puts "Project #{project.name} has #{lines} lines of code"
687
+ # end
688
+ # end
689
+ #
690
+ # after_define do |project|
691
+ # # Now that we know all the source directories, add them.
692
+ # task('loc'=>compile.sources + compile.test.sources)
693
+ # end
694
+ #
695
+ # # To use this method in your project:
696
+ # # loc path_1, path_2
697
+ # def loc(*paths)
698
+ # task('loc'=>paths)
699
+ # end
700
+ #
701
+ # end
702
+ #
703
+ # class Buildr::Project
704
+ # include LinesOfCode
705
+ # end
706
+ module Extension
707
+
708
+ def self.included(base) #:nodoc:
709
+ base.extend ClassMethods
710
+ end
711
+
712
+ # Methods added to the extension module when including Extension.
713
+ module ClassMethods
714
+
715
+ def included(base) #:nodoc:
716
+ # When included in Project, add callback and call first_time.
717
+ if Project == base && !base.callbacks.include?(callbacks)
718
+ base.callbacks << callbacks
719
+ callbacks.first_time if callbacks.respond_to?(:first_time)
720
+ end
721
+ end
722
+
723
+ def extended(base) #:nodoc:
724
+ # When extending project, add instance and call before_define.
725
+ if Project === base
726
+ callbacks = self.send(:callbacks).new
727
+ callbacks.before_define(base) if callbacks.respond_to?(:before_define)
728
+ base.send :add_callback, callbacks
729
+ end
730
+ end
731
+
732
+ # This block will be called once for any particular extension.
733
+ # You can use this to setup top-level and local tasks.
734
+ def first_time(&block)
735
+ meta = class << callbacks ; self ; end
736
+ meta.send :define_method, :first_time, &block
737
+ end
738
+
739
+ # This block is called once for the project with the project instance,
740
+ # right before running the project definition. You can use this to add
741
+ # tasks and set properties that will be used in the project definition.
742
+ def before_define(&block)
743
+ callbacks.send :define_method, :before_define, &block
744
+ end
745
+
746
+ # This block is called once for the project with the project instance,
747
+ # right after running the project definition. You can use this to do
748
+ # any post-processing that depends on the project definition.
749
+ def after_define(&block)
750
+ callbacks.send :define_method, :after_define, &block
751
+ end
752
+
753
+ private
754
+
755
+ def callbacks
756
+ const_get('Callbacks') rescue const_set('Callbacks', Class.new)
757
+ end
758
+
759
+ end
760
+
761
+ end
762
+
763
+
764
+ # :call-seq:
765
+ # define(name, properties?) { |project| ... } => project
766
+ #
767
+ # Defines a new project.
768
+ #
769
+ # The first argument is the project name. Each project must have a unique name.
770
+ # For a sub-project, the actual project name is created by prefixing the parent
771
+ # project's name.
772
+ #
773
+ # The second argument is optional and contains a hash or properties that are set
774
+ # on the project. You can only use properties that are supported by the project
775
+ # definition, e.g. :group and :version. You can also set these properties from the
776
+ # project definition.
777
+ #
778
+ # You pass a block that is executed in the context of the project definition.
779
+ # This block is used to define the project and tasks that are part of the project.
780
+ # Do not perform any work inside the project itself, as it will execute each time
781
+ # the Buildfile is loaded. Instead, use it to create and extend tasks that are
782
+ # related to the project.
783
+ #
784
+ # For example:
785
+ # define 'foo', :version=>'1.0' do
786
+ #
787
+ # define 'bar' do
788
+ # compile.with 'org.apache.axis2:axis2:jar:1.1'
789
+ # end
790
+ # end
791
+ #
792
+ # puts project('foo').version
793
+ # => '1.0'
794
+ # puts project('foo:bar').compile.classpath.map(&:to_spec)
795
+ # => 'org.apache.axis2:axis2:jar:1.1'
796
+ # % buildr build
797
+ # => Compiling 14 source files in foo:bar
798
+ def define(name, properties = nil, &block) #:yields:project
799
+ Project.define(name, properties, &block)
800
+ end
801
+
802
+ # :call-seq:
803
+ # project(name) => project
804
+ #
805
+ # Returns a project definition.
806
+ #
807
+ # When called from outside a project definition, must reference the project by its
808
+ # full name, e.g. 'foo:bar' to access the sub-project 'bar' in 'foo'. When called
809
+ # from inside a project, relative names are sufficient, e.g. <code>project('foo').project('bar')</code>
810
+ # will find the sub-project 'bar' in 'foo'.
811
+ #
812
+ # You cannot reference a project before the project is defined. When working with
813
+ # sub-projects, the project definition is stored by calling #define, and evaluated
814
+ # before a call to the parent project's #define method returns.
815
+ #
816
+ # However, if you call #project with the name of another sub-project, its definition
817
+ # is evaluated immediately. So the returned project definition is always complete,
818
+ # and you can access its definition (e.g. to find files relative to the base directory,
819
+ # or packages created by that project).
820
+ #
821
+ # For example:
822
+ # define 'myapp' do
823
+ # self.version = '1.1'
824
+ #
825
+ # define 'webapp' do
826
+ # # webapp is defined first, but beans is evaluated first
827
+ # compile.with project('beans')
828
+ # package :war
829
+ # end
830
+ #
831
+ # define 'beans' do
832
+ # package :jar
833
+ # end
834
+ # end
835
+ #
836
+ # puts project('myapp:beans').version
837
+ def project(*args)
838
+ Project.project *args
839
+ end
840
+
841
+ # :call-seq:
842
+ # projects(*names) => projects
843
+ #
844
+ # With no arguments, returns a list of all projects defined so far. When called on a project,
845
+ # returns all its sub-projects (direct descendants).
846
+ #
847
+ # With arguments, returns a list of named projects, fails on any name that does not exist.
848
+ # As with #project, you can use relative names when calling this method on a project.
849
+ #
850
+ # Like #project, this method evaluates the definition of each project before returning it.
851
+ # Be advised of circular dependencies.
852
+ #
853
+ # For example:
854
+ # files = projects.map { |prj| FileList[prj.path_to('src/**/*.java') }.flatten
855
+ # puts "There are #{files.size} source files in #{projects.size} projects"
856
+ #
857
+ # puts projects('myapp:beans', 'myapp:webapp').map(&:name)
858
+ # Same as:
859
+ # puts project('myapp').projects.map(&:name)
860
+ def projects(*args)
861
+ Project.projects *args
862
+ end
863
+
864
+ # Forces all the projects to be evaluated before executing any other task.
865
+ # If we don't do that, we don't get to have tasks available when running Rake.
866
+ namespace 'buildr' do
867
+ task 'initialize' do
868
+ projects
869
+ end
870
+
871
+ desc "Freeze the Buildfile so it always uses Buildr version #{Buildr::VERSION}"
872
+ task 'freeze' do
873
+ puts "Freezing the Buildfile so it always uses Buildr version #{Buildr::VERSION}"
874
+ original = File.read(Buildr.application.buildfile)
875
+ if original =~ /gem\s*(["'])buildr\1/
876
+ modified = original.sub(/gem\s*(["'])buildr\1\s*,\s*(["']).*\2/, %{gem "buildr", "#{Buildr::VERSION}"})
877
+ else
878
+ modified = %{gem "buildr", "#{Buildr::VERSION}"\n} + original
879
+ end
880
+ File.open(Buildr.application.buildfile, "w") { |file| file.write modified }
881
+ end
882
+
883
+ desc 'Unfreeze the Buildfile to use the latest version of Buildr'
884
+ task 'unfreeze' do
885
+ puts 'Unfreezing the Buildfile to use the latest version of Buildr from your Gems repository.'
886
+ modified = File.read(Buildr.application.buildfile).sub(/^\s*gem\s*(["'])buildr\1.*\n/, "")
887
+ File.open(Buildr.application.buildfile, "w") { |file| file.write modified }
888
+ end
889
+ end
890
+
891
+
892
+ end