rake 0.4.11 → 13.4.2

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.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/History.rdoc +2454 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +155 -0
  5. data/doc/command_line_usage.rdoc +171 -0
  6. data/doc/glossary.rdoc +40 -49
  7. data/doc/jamis.rb +135 -107
  8. data/doc/proto_rake.rdoc +22 -22
  9. data/doc/rake.1 +156 -0
  10. data/doc/rakefile.rdoc +428 -27
  11. data/doc/rational.rdoc +6 -6
  12. data/exe/rake +27 -0
  13. data/lib/rake/application.rb +847 -0
  14. data/lib/rake/backtrace.rb +25 -0
  15. data/lib/rake/clean.rb +57 -10
  16. data/lib/rake/cloneable.rb +17 -0
  17. data/lib/rake/cpu_counter.rb +122 -0
  18. data/lib/rake/default_loader.rb +15 -0
  19. data/lib/rake/dsl_definition.rb +196 -0
  20. data/lib/rake/early_time.rb +22 -0
  21. data/lib/rake/ext/core.rb +26 -0
  22. data/lib/rake/ext/string.rb +176 -0
  23. data/lib/rake/file_creation_task.rb +25 -0
  24. data/lib/rake/file_list.rb +435 -0
  25. data/lib/rake/file_task.rb +58 -0
  26. data/lib/rake/file_utils.rb +137 -0
  27. data/lib/rake/file_utils_ext.rb +135 -0
  28. data/lib/rake/invocation_chain.rb +57 -0
  29. data/lib/rake/invocation_exception_mixin.rb +17 -0
  30. data/lib/rake/late_time.rb +18 -0
  31. data/lib/rake/linked_list.rb +112 -0
  32. data/lib/rake/loaders/makefile.rb +54 -0
  33. data/lib/rake/multi_task.rb +14 -0
  34. data/lib/rake/name_space.rb +38 -0
  35. data/lib/rake/options.rb +31 -0
  36. data/lib/rake/packagetask.rb +124 -54
  37. data/lib/rake/phony.rb +16 -0
  38. data/lib/rake/private_reader.rb +21 -0
  39. data/lib/rake/promise.rb +100 -0
  40. data/lib/rake/pseudo_status.rb +30 -0
  41. data/lib/rake/rake_module.rb +67 -0
  42. data/lib/rake/rake_test_loader.rb +27 -0
  43. data/lib/rake/rule_recursion_overflow_error.rb +20 -0
  44. data/lib/rake/scope.rb +43 -0
  45. data/lib/rake/task.rb +434 -0
  46. data/lib/rake/task_argument_error.rb +8 -0
  47. data/lib/rake/task_arguments.rb +113 -0
  48. data/lib/rake/task_manager.rb +333 -0
  49. data/lib/rake/tasklib.rb +4 -16
  50. data/lib/rake/testtask.rb +110 -36
  51. data/lib/rake/thread_history_display.rb +49 -0
  52. data/lib/rake/thread_pool.rb +157 -0
  53. data/lib/rake/trace_output.rb +23 -0
  54. data/lib/rake/version.rb +10 -0
  55. data/lib/rake/win32.rb +17 -0
  56. data/lib/rake.rb +64 -992
  57. data/rake.gemspec +102 -0
  58. metadata +117 -89
  59. data/CHANGES +0 -153
  60. data/README +0 -209
  61. data/Rakefile +0 -215
  62. data/TODO +0 -19
  63. data/bin/rake +0 -8
  64. data/install.rb +0 -88
  65. data/lib/rake/contrib/compositepublisher.rb +0 -24
  66. data/lib/rake/contrib/ftptools.rb +0 -139
  67. data/lib/rake/contrib/publisher.rb +0 -75
  68. data/lib/rake/contrib/rubyforgepublisher.rb +0 -18
  69. data/lib/rake/contrib/sshpublisher.rb +0 -47
  70. data/lib/rake/contrib/sys.rb +0 -207
  71. data/lib/rake/gempackagetask.rb +0 -98
  72. data/lib/rake/rdoctask.rb +0 -128
  73. data/lib/rake/runtest.rb +0 -23
  74. data/test/contrib/testsys.rb +0 -47
  75. data/test/data/rbext/rakefile.rb +0 -3
  76. data/test/filecreation.rb +0 -26
  77. data/test/functional.rb +0 -82
  78. data/test/shellcommand.rb +0 -3
  79. data/test/testclean.rb +0 -13
  80. data/test/testfilelist.rb +0 -255
  81. data/test/testfileutils.rb +0 -83
  82. data/test/testftp.rb +0 -55
  83. data/test/testpackagetask.rb +0 -81
  84. data/test/testtasks.rb +0 -371
  85. data/test/testtesttask.rb +0 -71
data/lib/rake.rb CHANGED
@@ -1,997 +1,69 @@
1
- #!/usr/bin/env ruby
2
-
1
+ # frozen_string_literal: true
3
2
  #--
4
- # Copyright (c) 2003, 2004 Jim Weirich
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # "Software"), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3
+ # Copyright 2003-2010 by Jim Weirich (jim.weirich@gmail.com)
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to
7
+ # deal in the Software without restriction, including without limitation the
8
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9
+ # sell copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ # IN THE SOFTWARE.
24
22
  #++
25
- #
26
- # = Rake -- Ruby Make
27
- #
28
- # This is the main file for the Rake application. Normally it is
29
- # referenced as a library via a require statement, but it can be
30
- # distributed independently as an application.
31
-
32
- RAKEVERSION = '0.4.11'
33
-
34
- require 'rbconfig'
35
- require 'ftools'
36
- require 'getoptlong'
37
- require 'fileutils'
38
-
39
- $last_comment = nil
40
- $show_tasks = nil
41
- $show_prereqs = nil
42
- $trace = nil
43
- $dryrun = nil
44
-
45
- ######################################################################
46
- # A Task is the basic unit of work in a Rakefile. Tasks have
47
- # associated actions (possibly more than one) and a list of
48
- # prerequisites. When invoked, a task will first ensure that all of
49
- # its prerequisites have an opportunity to run and then it will
50
- # execute its own actions.
51
- #
52
- # Tasks are not usually created directly using the new method, but
53
- # rather use the +file+ and +task+ convenience methods.
54
- #
55
- class Task
56
- TASKS = Hash.new
57
- RULES = Array.new
58
-
59
- # List of prerequisites for a task.
60
- attr_reader :prerequisites
61
-
62
- # Comment for this task.
63
- attr_accessor :comment
64
-
65
- # Source dependency for rule synthesized tasks. Nil if task was not
66
- # sythesized from a rule.
67
- attr_accessor :source
68
-
69
- # Create a task named +task_name+ with no actions or prerequisites..
70
- # use +enhance+ to add actions and prerequisites.
71
- def initialize(task_name)
72
- @name = task_name
73
- @prerequisites = []
74
- @actions = []
75
- @already_invoked = false
76
- @comment = nil
77
- end
78
-
79
- # Enhance a task with prerequisites or actions. Returns self.
80
- def enhance(deps=nil, &block)
81
- @prerequisites |= deps if deps
82
- @actions << block if block_given?
83
- self
84
- end
85
-
86
- # Name of the task.
87
- def name
88
- @name.to_s
89
- end
90
-
91
- # Invoke the task if it is needed. Prerequites are invoked first.
92
- def invoke
93
- if $trace
94
- puts "** Invoke #{name} #{format_trace_flags}"
95
- end
96
- return if @already_invoked
97
- @already_invoked = true
98
- @prerequisites.each { |n| Task[n].invoke }
99
- execute if needed?
100
- end
101
-
102
- # Format the trace flags for display.
103
- def format_trace_flags
104
- flags = []
105
- flags << "first_time" unless @already_invoked
106
- flags << "not_needed" unless needed?
107
- flags.empty? ? "" : "(" + flags.join(", ") + ")"
108
- end
109
- private :format_trace_flags
110
-
111
- # Execute the actions associated with this task.
112
- def execute
113
- puts "** Execute #{name}" if $trace
114
- self.class.enhance_with_matching_rule(name) if @actions.empty?
115
- @actions.each { |act| result = act.call(self) }
116
- end
117
-
118
- # Is this task needed?
119
- def needed?
120
- true
121
- end
122
-
123
- # Timestamp for this task. Basic tasks return the current time for
124
- # their time stamp. Other tasks can be more sophisticated.
125
- def timestamp
126
- @prerequisites.collect { |p| Task[p].timestamp }.max || Time.now
127
- end
128
-
129
- # Add a comment to the task. If a comment alread exists, separate
130
- # the new comment with " / ".
131
- def add_comment(comment)
132
- return if ! $last_comment
133
- if @comment
134
- @comment << " / "
135
- else
136
- @comment = ''
137
- end
138
- @comment << $last_comment
139
- $last_comment = nil
140
- end
141
-
142
- # Class Methods ----------------------------------------------------
143
-
144
- class << self
145
-
146
- # Clear the task list. This cause rake to immediately forget all
147
- # the tasks that have been assigned. (Normally used in the unit
148
- # tests.)
149
- def clear
150
- TASKS.clear
151
- RULES.clear
152
- end
153
-
154
- # List of all defined tasks.
155
- def tasks
156
- TASKS.keys.sort.collect { |tn| Task[tn] }
157
- end
158
23
 
159
- # Return a task with the given name. If the task is not currently
160
- # known, try to synthesize one from the defined rules. If no
161
- # rules are found, but an existing file matches the task name,
162
- # assume it is a file task with no dependencies or actions.
163
- def [](task_name)
164
- task_name = task_name.to_s
165
- if task = TASKS[task_name]
166
- return task
167
- end
168
- if task = enhance_with_matching_rule(task_name)
169
- return task
170
- end
171
- if File.exist?(task_name)
172
- return FileTask.define_task(task_name)
173
- end
174
- fail "Don't know how to build task '#{task_name}'"
175
- end
24
+ module Rake; end
25
+
26
+ require_relative "rake/version"
27
+
28
+ require "rbconfig"
29
+ require "fileutils"
30
+ require "singleton"
31
+ require "monitor"
32
+ require "optparse"
33
+
34
+ require_relative "rake/ext/string"
35
+
36
+ require_relative "rake/win32"
37
+
38
+ require_relative "rake/linked_list"
39
+ require_relative "rake/cpu_counter"
40
+ require_relative "rake/scope"
41
+ require_relative "rake/task_argument_error"
42
+ require_relative "rake/rule_recursion_overflow_error"
43
+ require_relative "rake/rake_module"
44
+ require_relative "rake/trace_output"
45
+ require_relative "rake/pseudo_status"
46
+ require_relative "rake/options"
47
+ require_relative "rake/task_arguments"
48
+ require_relative "rake/invocation_chain"
49
+ require_relative "rake/task"
50
+ require_relative "rake/file_task"
51
+ require_relative "rake/file_creation_task"
52
+ require_relative "rake/multi_task"
53
+ require_relative "rake/dsl_definition"
54
+ require_relative "rake/file_utils_ext"
55
+ require_relative "rake/file_list"
56
+ require_relative "rake/default_loader"
57
+ require_relative "rake/early_time"
58
+ require_relative "rake/late_time"
59
+ require_relative "rake/name_space"
60
+ require_relative "rake/task_manager"
61
+ require_relative "rake/application"
62
+ require_relative "rake/backtrace"
63
+
64
+ # :stopdoc:
65
+ #
66
+ # Some top level Constants.
176
67
 
177
- # TRUE if the task name is already defined.
178
- def task_defined?(task_name)
179
- task_name = task_name.to_s
180
- TASKS[task_name]
181
- end
182
-
183
- # Define a task given +args+ and an option block. If a rule with
184
- # the given name already exists, the prerequisites and actions are
185
- # added to the existing task. Returns the defined task.
186
- def define_task(args, &block)
187
- task_name, deps = resolve_args(args)
188
- deps = [deps] if (Symbol === deps) || (String === deps)
189
- deps = deps.collect {|d| d.to_s }
190
- t = lookup(task_name)
191
- t.add_comment($last_comment)
192
- t.enhance(deps, &block)
193
- end
194
-
195
- # Define a rule for synthesizing tasks.
196
- def create_rule(args, &block)
197
- pattern, deps = resolve_args(args)
198
- fail "Too many dependents specified in rule #{pattern}: #{deps.inspect}" if deps.size > 1
199
- pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
200
- RULES << [pattern, deps, block]
201
- end
202
-
203
-
204
- # Lookup a task. Return an existing task if found, otherwise
205
- # create a task of the current type.
206
- def lookup(task_name)
207
- name = task_name.to_s
208
- TASKS[name] ||= self.new(task_name)
209
- end
210
-
211
- # If a rule can be found that matches the task name, enhance the
212
- # task with the prerequisites and actions from the rule. Set the
213
- # source attribute of the task appropriately for the rule. Return
214
- # the enhanced task or nil of no rule was found.
215
- def enhance_with_matching_rule(task_name)
216
- RULES.each do |pattern, extensions, block|
217
- if md = pattern.match(task_name)
218
- ext = extensions.first
219
- case ext
220
- when String
221
- source = task_name.sub(/\.[^.]*$/, ext)
222
- when Proc
223
- source = ext.call(task_name)
224
- else
225
- fail "Don't know how to handle rule dependent: #{ext.inspect}"
226
- end
227
- if File.exist?(source)
228
- task = FileTask.define_task({task_name => [source]}, &block)
229
- task.source = source
230
- return task
231
- end
232
- end
233
- end
234
- nil
235
- end
236
-
237
- private
238
-
239
- # Resolve the arguments for a task/rule.
240
- def resolve_args(args)
241
- case args
242
- when Hash
243
- fail "Too Many Task Names: #{args.keys.join(' ')}" if args.size > 1
244
- fail "No Task Name Given" if args.size < 1
245
- task_name = args.keys[0]
246
- deps = args[task_name]
247
- deps = [deps] if (String===deps) || (Regexp===deps) || (Proc===deps)
248
- else
249
- task_name = args
250
- deps = []
251
- end
252
- [task_name, deps]
253
- end
254
- end
255
- end
256
-
257
-
258
- ######################################################################
259
- # A FileTask is a task that includes time based dependencies. If any
260
- # of a FileTask's prerequisites have a timestamp that is later than
261
- # the file represented by this task, then the file must be rebuilt
262
- # (using the supplied actions).
263
- #
264
- class FileTask < Task
265
-
266
- # Is this file task needed? Yes if it doesn't exist, or if its time
267
- # stamp is out of date.
268
- def needed?
269
- return true unless File.exist?(name)
270
- latest_prereq = @prerequisites.collect{|n| Task[n].timestamp}.max
271
- return false if latest_prereq.nil?
272
- timestamp < latest_prereq
273
- end
274
-
275
- # Time stamp for file task.
276
- def timestamp
277
- File.mtime(name.to_s)
278
- end
279
- end
280
-
281
- ######################################################################
282
- # Task Definition Functions ...
283
-
284
- # Declare a basic task.
285
- #
286
- # Example:
287
- # task :clobber => [:clean] do
288
- # rm_rf "html"
289
- # end
290
- #
291
- def task(args, &block)
292
- Task.define_task(args, &block)
293
- end
294
-
295
-
296
- # Declare a file task.
297
- #
298
- # Example:
299
- # file "config.cfg" => ["config.template"] do
300
- # open("config.cfg", "w") do |outfile|
301
- # open("config.template") do |infile|
302
- # while line = infile.gets
303
- # outfile.puts line
304
- # end
305
- # end
306
- # end
307
- # end
308
- #
309
- def file(args, &block)
310
- FileTask.define_task(args, &block)
311
- end
312
-
313
- # Declare a set of files tasks to create the given directories on
314
- # demand.
315
- #
316
- # Example:
317
- # directory "testdata/doc"
318
- #
319
- def directory(dir)
320
- while dir != '.' && dir != '/'
321
- file dir do |t|
322
- mkdir_p t.name if ! File.exist?(t.name)
323
- end
324
- dir = File.dirname(dir)
325
- end
326
- end
327
-
328
- # Declare a rule for auto-tasks.
329
- #
330
- # Example:
331
- # rule '.o' => '.c' do |t|
332
- # sh %{cc -o #{t.name} #{t.source}}
333
- # end
334
- #
335
- def rule(args, &block)
336
- Task.create_rule(args, &block)
337
- end
338
-
339
- # Describe the next rake task.
340
- #
341
- # Example:
342
- # desc "Run the Unit Tests"
343
- # task :test => [:build]
344
- # runtests
345
- # end
346
- #
347
- def desc(comment)
348
- $last_comment = comment
349
- end
350
-
351
-
352
- ######################################################################
353
- # This a FileUtils extension that defines several additional commands
354
- # to be added to the FileUtils utility functions.
355
- #
356
- module FileUtils
357
- RUBY = Config::CONFIG['ruby_install_name']
358
-
359
- OPT_TABLE['sh'] = %w(noop verbose)
360
- OPT_TABLE['ruby'] = %w(noop verbose)
361
-
362
- # Run the system command +cmd+.
363
- #
364
- # Example:
365
- # sh %{ls -ltr}
366
- #
367
- # # check exit status after command runs
368
- # sh %{grep pattern file} do |ok, res|
369
- # if ! ok
370
- # puts "pattern not found (status = #{res.exitstatus})"
371
- # end
372
- # end
373
- #
374
- def sh(cmd, options={}, &block)
375
- unless block_given?
376
- block = lambda { |ok, status|
377
- ok or fail "Command failed with status (#{status.exitstatus}): [#{cmd}]"
378
- }
379
- end
380
- fu_check_options options, :noop, :verbose
381
- fu_output_message cmd if options[:verbose]
382
- unless options[:noop]
383
- res = system(cmd)
384
- block.call(res, $?)
385
- end
386
- end
387
-
388
- # Run a Ruby interpreter with the given arguments.
389
- #
390
- # Example:
391
- # ruby %{-pe '$_.upcase!' <README}
392
- #
393
- def ruby(*args)
394
- if Hash === args.last
395
- options = args.pop
396
- else
397
- options = {}
398
- end
399
- sh "#{RUBY} #{args.join(' ')}", options
400
- end
401
-
402
- LN_SUPPORTED = [true]
403
-
404
- # Attempt to do a normal file link, but fall back to a copy if the
405
- # link fails.
406
- def safe_ln(*args)
407
- unless LN_SUPPORTED[0]
408
- cp(*args)
409
- else
410
- begin
411
- ln(*args)
412
- rescue Errno::EOPNOTSUPP
413
- LN_SUPPORTED[0] = false
414
- cp(*args)
415
- end
416
- end
417
- end
418
-
419
- # Split a file path into individual directory names.
420
- #
421
- # Example:
422
- # split_all("a/b/c") => ['a', 'b', 'c']
423
- #
424
- def split_all(path)
425
- head, tail = File.split(path)
426
- return [tail] if head == '.' || tail == '/'
427
- return [head, tail] if head == '/'
428
- return split_all(head) + [tail]
429
- end
430
- end
431
-
432
- ######################################################################
433
- # RakeFileUtils provides a custom version of the FileUtils methods
434
- # that respond to the <tt>verbose</tt> and <tt>nowrite</tt> commands.
435
- #
436
- module RakeFileUtils
437
- include FileUtils
438
-
439
- $fileutils_output = $stderr
440
- $fileutils_label = ''
441
- $fileutils_verbose = true
442
- $fileutils_nowrite = false
443
-
444
- FileUtils::OPT_TABLE.each do |name, opts|
445
- next unless opts.include?('verbose')
446
- module_eval(<<-EOS, __FILE__, __LINE__ + 1)
447
- def #{name}( *args, &block )
448
- super(*fu_merge_option(args,
449
- :verbose => $fileutils_verbose,
450
- :noop => $fileutils_nowrite),
451
- &block)
452
- end
453
- EOS
454
- end
455
-
456
- # Get/set the verbose flag controlling output from the FileUtils
457
- # utilities. If verbose is true, then the utility method is echoed
458
- # to standard output.
459
- #
460
- # Examples:
461
- # verbose # return the current value of the verbose flag
462
- # verbose(v) # set the verbose flag to _v_.
463
- # verbose(v) { code } # Execute code with the verbose flag set temporarily to _v_.
464
- # # Return to the original value when code is done.
465
- def verbose(value=nil)
466
- oldvalue = $fileutils_verbose
467
- $fileutils_verbose = value unless value.nil?
468
- if block_given?
469
- begin
470
- yield
471
- ensure
472
- $fileutils_verbose = oldvalue
473
- end
474
- end
475
- $fileutils_verbose
476
- end
477
-
478
- # Get/set the nowrite flag controlling output from the FileUtils
479
- # utilities. If verbose is true, then the utility method is echoed
480
- # to standard output.
481
- #
482
- # Examples:
483
- # nowrite # return the current value of the nowrite flag
484
- # nowrite(v) # set the nowrite flag to _v_.
485
- # nowrite(v) { code } # Execute code with the nowrite flag set temporarily to _v_.
486
- # # Return to the original value when code is done.
487
- def nowrite(value=nil)
488
- oldvalue = $fileutils_nowrite
489
- $fileutils_nowrite = value unless value.nil?
490
- if block_given?
491
- begin
492
- yield
493
- ensure
494
- $fileutils_nowrite = oldvalue
495
- end
496
- end
497
- oldvalue
498
- end
499
-
500
- # Use this function to prevent protentially destructive ruby code
501
- # from running when the :nowrite flag is set.
502
- #
503
- # Example:
504
- #
505
- # when_writing("Building Project") do
506
- # project.build
507
- # end
508
- #
509
- # The following code will build the project under normal conditions.
510
- # If the nowrite(true) flag is set, then the example will print:
511
- # DRYRUN: Building Project
512
- # instead of actually building the project.
513
- #
514
- def when_writing(msg=nil)
515
- if $fileutils_nowrite
516
- puts "DRYRUN: #{msg}" if msg
517
- else
518
- yield
519
- end
520
- end
521
-
522
- # Merge the given options with the default values.
523
- def fu_merge_option(args, defaults)
524
- if Hash === args.last
525
- defaults.update(args.last)
526
- args.pop
527
- end
528
- args.push defaults
529
- args
530
- end
531
- private :fu_merge_option
532
-
533
- extend self
534
-
535
- end
536
-
537
- include RakeFileUtils
538
-
539
- module Rake
540
-
541
- ####################################################################
542
- # A FileList is essentially an array with a few helper methods
543
- # defined to make file manipulation a bit easier.
544
- #
545
- class FileList < Array
546
-
547
- # Rewrite all array methods (and to_s/inspect) to resolve the list
548
- # before running.
549
- method_list = Array.instance_methods - Object.instance_methods
550
- %w[to_a to_s inspect].each do |meth|
551
- method_list << meth unless method_list.include? meth
552
- end
553
- method_list.each_with_index do |sym, i|
554
- if sym =~ /^[A-Za-z_]+$/
555
- name = sym
556
- else
557
- name = i
558
- end
559
- alias_method "array_#{name}", sym
560
- class_eval %{
561
- def #{sym}(*args, &block)
562
- resolve if @pending
563
- array_#{name}(*args, &block)
564
- end
565
- }
566
- end
567
-
568
- # Create a file list from the globbable patterns given. If you
569
- # wish to perform multiple includes or excludes at object build
570
- # time, use the "yield self" pattern.
571
- #
572
- # Example:
573
- # file_list = FileList.new['lib/**/*.rb', 'test/test*.rb']
574
- #
575
- # pkg_files = FileList.new['lib/**/*'] do |fl|
576
- # fl.exclude(/\bCVS\b/)
577
- # end
578
- #
579
- def initialize(*patterns)
580
- @pending_add = []
581
- @pending = false
582
- @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
583
- @exclude_re = nil
584
- patterns.each { |pattern| include(pattern) }
585
- yield self if block_given?
586
- end
587
-
588
- # Add file names defined by glob patterns to the file list. If an
589
- # array is given, add each element of the array.
590
- #
591
- # Example:
592
- # file_list.include("*.java", "*.cfg")
593
- # file_list.include %w( math.c lib.h *.o )
594
- #
595
- def include(*filenames)
596
- # TODO: check for pending
597
- filenames.each do |fn| @pending_add << fn end
598
- @pending = true
599
- self
600
- end
601
- alias :add :include
602
-
603
- # Register a list of file name patterns that should be excluded
604
- # from the list. Patterns may be regular expressions, glob
605
- # patterns or regular strings.
606
- #
607
- # Note that glob patterns are expanded against the file system.
608
- # If a file is explicitly added to a file list, but does not exist
609
- # in the file system, then an glob pattern in the exclude list
610
- # will not exclude the file.
611
- #
612
- # Examples:
613
- # FileList['a.c', 'b.c'].exclude("a.c") => ['b.c']
614
- # FileList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
615
- #
616
- # If "a.c" is a file, then ...
617
- # FileList['a.c', 'b.c'].exclude("a.*") => ['b.c']
618
- #
619
- # If "a.c" is not a file, then ...
620
- # FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
621
- #
622
- def exclude(*patterns)
623
- # TODO: check for pending
624
- patterns.each do |pat| @exclude_patterns << pat end
625
- if ! @pending
626
- calculate_exclude_regexp
627
- reject! { |fn| fn =~ @exclude_re }
628
- end
629
- self
630
- end
631
-
632
-
633
- def clear_exclude
634
- @exclude_patterns = []
635
- calculate_exclude_regexp if ! @pending
636
- end
637
-
638
- # Resolve all the pending adds now.
639
- def resolve
640
- @pending = false
641
- @pending_add.each do |fn| resolve_add(fn) end
642
- @pending_add = []
643
- resolve_exclude
644
- self
645
- end
646
-
647
- def calculate_exclude_regexp
648
- ignores = []
649
- @exclude_patterns.each do |pat|
650
- case pat
651
- when Regexp
652
- ignores << pat
653
- when /[*.]/
654
- Dir[pat].each do |p| ignores << p end
655
- else
656
- ignores << Regexp.quote(pat)
657
- end
658
- end
659
- if ignores.empty?
660
- @exclude_re = /^$/
661
- else
662
- re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
663
- @exclude_re = Regexp.new(re_str)
664
- end
665
- end
666
-
667
- def resolve_add(fn)
668
- case fn
669
- when Array
670
- fn.each { |f| self.resolve_add(f) }
671
- when %r{[*?]}
672
- add_matching(fn)
673
- else
674
- self << fn
675
- end
676
- end
677
-
678
- def resolve_exclude
679
- @exclude_patterns.each do |pat|
680
- case pat
681
- when Regexp
682
- reject! { |fn| fn =~ pat }
683
- when /[*.]/
684
- reject_list = Dir[pat]
685
- reject! { |fn| reject_list.include?(fn) }
686
- else
687
- reject! { |fn| fn == pat }
688
- end
689
- end
690
- self
691
- end
692
-
693
- # Return a new FileList with the results of running +sub+ against
694
- # each element of the oringal list.
695
- #
696
- # Example:
697
- # FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
698
- #
699
- def sub(pat, rep)
700
- inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
701
- end
702
-
703
- # Return a new FileList with the results of running +gsub+ against
704
- # each element of the original list.
705
- #
706
- # Example:
707
- # FileList['lib/test/file', 'x/y'].gsub(/\//, "\\")
708
- # => ['lib\\test\\file', 'x\\y']
709
- #
710
- def gsub(pat, rep)
711
- inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
712
- end
713
-
714
- # Same as +sub+ except that the oringal file list is modified.
715
- def sub!(pat, rep)
716
- each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
717
- self
718
- end
719
-
720
- # Same as +gsub+ except that the original file list is modified.
721
- def gsub!(pat, rep)
722
- each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
723
- self
724
- end
725
-
726
- # Convert a FileList to a string by joining all elements with a space.
727
- def to_s
728
- resolve if @pending
729
- self.join(' ')
730
- end
731
-
732
- # Add matching glob patterns.
733
- def add_matching(pattern)
734
- Dir[pattern].each do |fn|
735
- self << fn unless exclude?(fn)
736
- end
737
- end
738
- private :add_matching
739
-
740
- # Should the given file name be excluded?
741
- def exclude?(fn)
742
- calculate_exclude_regexp unless @exclude_re
743
- fn =~ @exclude_re
744
- end
745
-
746
- DEFAULT_IGNORE_PATTERNS = [
747
- /(^|[\/\\])CVS([\/\\]|$)/,
748
- /\.bak$/,
749
- /~$/,
750
- /(^|[\/\\])core$/
751
- ]
752
- @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
753
-
754
- class << self
755
- # Create a new file list including the files listed. Similar to:
756
- #
757
- # FileList.new(*args)
758
- def [](*args)
759
- new(*args)
760
- end
761
-
762
- # Set the ignore patterns back to the default value. The
763
- # default patterns will ignore files
764
- # * containing "CVS" in the file path
765
- # * ending with ".bak"
766
- # * ending with "~"
767
- # * named "core"
768
- #
769
- # Note that file names beginning with "." are automatically
770
- # ignored by Ruby's glob patterns and are not specifically
771
- # listed in the ignore patterns.
772
- def select_default_ignore_patterns
773
- @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
774
- end
775
-
776
- # Clear the ignore patterns.
777
- def clear_ignore_patterns
778
- @exclude_patterns = [ /^$/ ]
779
- end
780
- end
781
- end
782
- end
783
-
784
- # Alias FileList to be available at the top level.
785
68
  FileList = Rake::FileList
786
-
787
- ######################################################################
788
- # Rake main application object. When invoking +rake+ from the command
789
- # line, a RakeApp object is created and run.
790
- #
791
- class RakeApp
792
- RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb']
793
-
794
- OPTIONS = [
795
- ['--dry-run', '-n', GetoptLong::NO_ARGUMENT,
796
- "Do a dry run without executing actions."],
797
- ['--help', '-H', GetoptLong::NO_ARGUMENT,
798
- "Display this help message."],
799
- ['--libdir', '-I', GetoptLong::REQUIRED_ARGUMENT,
800
- "Include LIBDIR in the search path for required modules."],
801
- ['--nosearch', '-N', GetoptLong::NO_ARGUMENT,
802
- "Do not search parent directories for the Rakefile."],
803
- ['--prereqs', '-P', GetoptLong::NO_ARGUMENT,
804
- "Display the tasks and dependencies, then exit."],
805
- ['--quiet', '-q', GetoptLong::NO_ARGUMENT,
806
- "Do not log messages to standard output."],
807
- ['--rakefile', '-f', GetoptLong::REQUIRED_ARGUMENT,
808
- "Use FILE as the rakefile."],
809
- ['--require', '-r', GetoptLong::REQUIRED_ARGUMENT,
810
- "Require MODULE before executing rakefile."],
811
- ['--tasks', '-T', GetoptLong::NO_ARGUMENT,
812
- "Display the tasks and dependencies, then exit."],
813
- ['--trace', '-t', GetoptLong::NO_ARGUMENT,
814
- "Turn on invoke/execute tracing."],
815
- ['--usage', '-h', GetoptLong::NO_ARGUMENT,
816
- "Display usage."],
817
- ['--verbose', '-v', GetoptLong::NO_ARGUMENT,
818
- "Log message to standard output (default)."],
819
- ['--version', '-V', GetoptLong::NO_ARGUMENT,
820
- "Display the program version."],
821
- ]
822
-
823
- # Create a RakeApp object.
824
- def initialize
825
- @rakefile = nil
826
- @nosearch = false
827
- end
828
-
829
- # True if one of the files in RAKEFILES is in the current directory.
830
- # If a match is found, it is copied into @rakefile.
831
- def have_rakefile
832
- RAKEFILES.each do |fn|
833
- if File.exist?(fn)
834
- @rakefile = fn
835
- return true
836
- end
837
- end
838
- return false
839
- end
840
-
841
- # Display the program usage line.
842
- def usage
843
- puts "rake [-f rakefile] {options} targets..."
844
- end
845
-
846
- # Display the rake command line help.
847
- def help
848
- usage
849
- puts
850
- puts "Options are ..."
851
- puts
852
- OPTIONS.sort.each do |long, short, mode, desc|
853
- if mode == GetoptLong::REQUIRED_ARGUMENT
854
- if desc =~ /\b([A-Z]{2,})\b/
855
- long = long + "=#{$1}"
856
- end
857
- end
858
- printf " %-20s (%s)\n", long, short
859
- printf " %s\n", desc
860
- end
861
- end
862
-
863
- # Display the tasks and dependencies.
864
- def display_tasks_and_comments
865
- width = Task.tasks.select { |t|
866
- t.comment
867
- }.collect { |t|
868
- t.name.length
869
- }.max
870
- Task.tasks.each do |t|
871
- if t.comment
872
- printf "rake %-#{width}s # %s\n", t.name, t.comment
873
- end
874
- end
875
- end
876
-
877
- # Display the tasks and prerequisites
878
- def display_prerequisites
879
- Task.tasks.each do |t|
880
- puts "rake #{t.name}"
881
- t.prerequisites.each { |pre| puts " #{pre}" }
882
- end
883
- end
884
-
885
- # Return a list of the command line options supported by the
886
- # program.
887
- def command_line_options
888
- OPTIONS.collect { |lst| lst[0..-2] }
889
- end
890
-
891
- # Do the option defined by +opt+ and +value+.
892
- def do_option(opt, value)
893
- case opt
894
- when '--dry-run'
895
- verbose(true)
896
- nowrite(true)
897
- $dryrun = true
898
- $trace = true
899
- when '--help'
900
- help
901
- exit
902
- when '--libdir'
903
- $:.push(value)
904
- when '--nosearch'
905
- @nosearch = true
906
- when '--prereqs'
907
- $show_prereqs = true
908
- when '--quiet'
909
- verbose(false)
910
- when '--rakefile'
911
- RAKEFILES.clear
912
- RAKEFILES << value
913
- when '--require'
914
- require value
915
- when '--tasks'
916
- $show_tasks = true
917
- when '--trace'
918
- $trace = true
919
- verbose(true)
920
- when '--usage'
921
- usage
922
- exit
923
- when '--verbose'
924
- verbose(true)
925
- when '--version'
926
- puts "rake, version #{RAKEVERSION}"
927
- exit
928
- else
929
- fail "Unknown option: #{opt}"
930
- end
931
- end
932
-
933
- # Read and handle the command line options.
934
- def handle_options
935
- opts = GetoptLong.new(*command_line_options)
936
- opts.each { |opt, value| do_option(opt, value) }
937
- end
938
-
939
- def load_rakefile
940
- here = Dir.pwd
941
- while ! have_rakefile
942
- Dir.chdir("..")
943
- if Dir.pwd == here || @nosearch
944
- fail "No Rakefile found (looking for: #{RAKEFILES.join(', ')})"
945
- end
946
- here = Dir.pwd
947
- end
948
- puts "(in #{Dir.pwd})"
949
- $rakefile = @rakefile
950
- load @rakefile
951
- end
952
-
953
- # Collect the list of tasks on the command line. If no tasks are
954
- # give, return a list containing only the default task.
955
- # Environmental assignments are processed at this time as well.
956
- def collect_tasks
957
- tasks = []
958
- ARGV.each do |arg|
959
- if arg =~ /^(\w+)=(.*)$/
960
- ENV[$1] = $2
961
- else
962
- tasks << arg
963
- end
964
- end
965
- tasks.push("default") if tasks.size == 0
966
- tasks
967
- end
968
-
969
- # Run the +rake+ application.
970
- def run
971
- handle_options
972
- begin
973
- tasks = collect_tasks
974
- load_rakefile
975
- if $show_tasks
976
- display_tasks_and_comments
977
- elsif $show_prereqs
978
- display_prerequisites
979
- else
980
- tasks.each { |task_name| Task[task_name].invoke }
981
- end
982
- rescue Exception => ex
983
- puts "rake aborted!"
984
- puts ex.message
985
- if $trace
986
- puts ex.backtrace.join("\n")
987
- else
988
- puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
989
- end
990
- exit(1)
991
- end
992
- end
993
- end
994
-
995
- if __FILE__ == $0 then
996
- RakeApp.new.run
997
- end
69
+ RakeFileUtils = Rake::FileUtilsExt