How can I get a Ruby FileList to collect unnamed files, for example .htaccess on windows

I want to search any files with the extension in my file system .template.

Below everything works fine except .htaccess.template

FileList.new(File.join(root, '**', '*.template')).each do |file|
    # do stuff with file
end 

because windows don't like nameless files, grrrr

How to do it on Windows? This code works fine on Linux ....

+5
source share
2 answers

What about

Dir.glob([".*.template", "*.template"])
+6
source

Assuming FileListhere is a class FileListfrom rake, then the problem is in the underlying Ruby class Dir(which is being used FileList) that does not match the files starting with .for *wildcards. Relevant part of rake.rb

# Add matching glob patterns.
def add_matching(pattern)
  Dir[pattern].each do |fn|
    self << fn unless exclude?(fn)
  end
end

, add_matching, , .. , - .

class Rake::FileList
  def add_matching(pattern)
    files = Dir[pattern]
    # ugly hack to include files starting with . on Windows
    if RUBY_PLATFORM =~ /mswin/
      parts = File.split(pattern)
      # if filename portion of the pattern starts with * also
      # include the files matching '.' + the same pattern
      if parts.last[0] == ?*
        files += Dir[File.join(parts[0...-1] << '.' + parts.last)]
      end
    end    
    files.each do |fn|
      self << fn unless exclude?(fn)
    end
  end
end

: Linux , , ., . /home/mikej/root 2 a b, first.template .other.template,

Rake::FileList.new('home/mikej/root/**/*.template')
=> ["/home/mikej/root/a/first.template", "/home/mikej/root/b/first.template"]

Linux , , .

+1

All Articles