Sha256: 3f957a89e1991f6330bf31a685c8c8ab125c27256cb14aa709593a7b24603014

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module VMC::Cli
  module FileHelper
    
    
    def match(pattern,filename)
      return false if pattern =~ /^\s*$/ # ignore blank lines
      
      return false if pattern =~ /^#/ # lines starting with # are comments
      
      if pattern =~ /\/$/ 
        # pattern ending in a slash should ignore directory and all its children
        dirname = pattern.sub(/\/$/,'')
        return filename == dirname || filename =~ /#{dirname}\/.*$/
      end
      
      if pattern =~ /^!/
        return !match(pattern.sub(/^!/,''),filename)
      end
      
      if pattern =~ /^\//
        parts = filename.split('/')
        return File.fnmatch(pattern.sub(/^\//,''),parts[0])
      end
      
      if pattern.include? '/'
        return File.fnmatch(pattern,filename)
      end
      
      File.fnmatch(pattern,filename,File::FNM_PATHNAME)
    end
    
    def reject_patterns(patterns,filenames)
      filenames.reject do |filename|
        patterns.detect { |pattern| match(pattern,filename)} != nil
      end
    end

    def afignore(ignore_path,files)
      if File.exists?(ignore_path) 
        patterns = File.read(ignore_path).split("\n")
        reject_patterns(patterns,files)
      else
        files
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
af-0.3.18.7 lib/cli/file_helper.rb