@@ -269,18 +269,11 @@ def has_chain?(exception) # :nodoc:
269
269
end
270
270
private :has_chain?
271
271
272
- # True if one of the files in RAKEFILES is in the current directory.
273
- # If a match is found, it is copied into @rakefile.
274
- def have_rakefile # :nodoc:
275
- @rakefiles . each do |fn |
276
- if File . exist? ( fn )
277
- others = FileList . glob ( fn , File ::FNM_CASEFOLD )
278
- return others . size == 1 ? others . first : fn
279
- elsif fn == ""
280
- return fn
281
- end
272
+ # Returns first filename from @rakefiles that exists in the specified dir.
273
+ def have_rakefile ( dir = Dir . pwd ) # :nodoc:
274
+ Dir . chdir ( dir ) do
275
+ Dir . glob ( @rakefiles . map { |name | escape_for_glob ( name ) } ) . first || @rakefiles . find ( &:empty? )
282
276
end
283
- return nil
284
277
end
285
278
286
279
# True if we are outputting to TTY, false otherwise
@@ -676,45 +669,37 @@ def rake_require(file_name, paths=$LOAD_PATH, loaded=$") # :nodoc:
676
669
end
677
670
678
671
def find_rakefile_location # :nodoc:
679
- here = Dir . pwd
680
- until ( fn = have_rakefile )
681
- Dir . chdir ( ".." )
682
- return nil if Dir . pwd == here || options . nosearch
683
- here = Dir . pwd
672
+ previous_dir , current_dir = nil , original_dir
673
+ until ( rakefile = have_rakefile ( current_dir ) ) || current_dir == previous_dir
674
+ break if options . nosearch
675
+ previous_dir , current_dir = current_dir , File . expand_path ( ".." , current_dir )
684
676
end
685
- [ fn , here ]
686
- ensure
687
- Dir . chdir ( Rake . original_dir )
677
+ [ rakefile , current_dir ] if rakefile
688
678
end
689
679
690
- def print_rakefile_directory ( location ) # :nodoc:
691
- $stderr. puts "(in #{ Dir . pwd } )" unless
692
- options . silent or original_dir == location
680
+ def print_rakefile_directory # :nodoc:
681
+ $stderr. puts "(in #{ @rakefile_dir } )" unless
682
+ options . silent || original_dir == @rakefile_dir
693
683
end
694
684
695
685
def raw_load_rakefile # :nodoc:
696
- rakefile , location = find_rakefile_location
686
+ @rakefile , @rakefile_dir = find_rakefile_location
687
+
697
688
if ( !options . ignore_system ) &&
698
- ( options . load_system || rakefile . nil? ) &&
689
+ ( options . load_system || @ rakefile. nil? ) &&
699
690
system_dir && File . directory? ( system_dir )
700
- print_rakefile_directory ( location )
701
- glob ( "#{ system_dir } /*.rake" ) do |name |
702
- add_import name
703
- end
691
+ print_rakefile_directory
692
+ add_globbed_imports ( system_dir , "*.rake" )
704
693
else
705
- fail "No Rakefile found (looking for: #{ @rakefiles . join ( ', ' ) } )" if
706
- rakefile . nil?
707
- @rakefile = rakefile
708
- Dir . chdir ( location )
709
- print_rakefile_directory ( location )
710
- Rake . load_rakefile ( File . expand_path ( @rakefile ) ) if
711
- @rakefile && @rakefile != ""
712
- options . rakelib . each do |rlib |
713
- glob ( "#{ rlib } /*.rake" ) do |name |
714
- add_import name
715
- end
694
+ fail "No Rakefile found (looking for: #{ @rakefiles . join ( ", " ) } )" if @rakefile . nil?
695
+ Dir . chdir ( @rakefile_dir ) unless @rakefile_dir == Dir . pwd
696
+ print_rakefile_directory
697
+ Rake . load_rakefile ( File . expand_path ( @rakefile , @rakefile_dir ) ) unless @rakefile . empty?
698
+ options . rakelib . each do |rakelib |
699
+ add_globbed_imports ( File . expand_path ( rakelib , @rakefile_dir ) , "*.rake" )
716
700
end
717
701
end
702
+
718
703
load_imports
719
704
end
720
705
@@ -723,6 +708,11 @@ def glob(path, &block) # :nodoc:
723
708
end
724
709
private :glob
725
710
711
+ def escape_for_glob ( pattern )
712
+ pattern . tr ( "\\ " , "/" ) . gsub ( /[*?\[ \] {}]/ , "\\ \\ " + '\0' )
713
+ end
714
+ private :escape_for_glob
715
+
726
716
# The directory path containing the system wide rakefiles.
727
717
def system_dir # :nodoc:
728
718
@system_dir ||=
@@ -778,6 +768,14 @@ def add_import(fn) # :nodoc:
778
768
@pending_imports << fn
779
769
end
780
770
771
+ # Globs "#{directory}/#{pattern}", and adds the results to the list
772
+ # of files to be imported.
773
+ def add_globbed_imports ( directory , pattern ) # :nodoc:
774
+ Dir . glob ( "#{ escape_for_glob ( directory ) . chomp ( "/" ) } /#{ pattern } " ) do |path |
775
+ add_import path
776
+ end
777
+ end
778
+
781
779
# Load the pending list of imported files.
782
780
def load_imports # :nodoc:
783
781
while fn = @pending_imports . shift
0 commit comments