Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i get Ruby FileList to pick up files without a name, like .htaccess on windows

Tags:

ruby

I want to search my filesystem for any files with the extension .template.

The below works fine for everything except .htaccess.template

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

because windows doesn't like nameless files, grrrr

How do I make this work on Windows? This code works fine on Linux....

like image 411
Andrew Bullock Avatar asked Mar 29 '10 10:03

Andrew Bullock


People also ask

What does __ file __ mean in Ruby?

__FILE__ is the filename with extension of the file containing the code being executed. In foo. rb , __FILE__ would be "foo. rb".

How do I put the file path in Ruby?

Ruby has a method for this case. It is File::expand_path . Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point.

How do I read a file in Ruby?

Opening a File in Ruby There are two methods which are widely used − the sysread(n) and the read() method. The open method is used to open the file, while the sysread(n) is used to read the first "n" characters from a file, and the read() method is used to read the entire file content.


2 Answers

How about

Dir.glob([".*.template", "*.template"])
like image 182
fl00r Avatar answered Oct 07 '22 00:10

fl00r


Assuming that FileList here is the FileList class from rake then the problem is in Ruby's underlying Dir class (which is used by FileList) not matching files starting with . for the * wildcard. The relevant portion of rake.rb is

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

Below is an ugly hack that overrides add_matching to also include files starting with . Hopefully someone else will be along to suggest a more elegant solution.

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

Update: I have just tested this on Linux here and the files starting with . are not included either. e.g. If I have a directory /home/mikej/root with 2 subdirectories a and b where each contains first.template and .other.template then

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

so I would double check the behaviour on Linux and verify that there isn't something else causing the difference in behaviour.

like image 26
mikej Avatar answered Oct 07 '22 02:10

mikej