Class: VMC::Cli::FileHelper::OLYMPEIgnore

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/file_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns, project_root = "") ⇒ OLYMPEIgnore

Returns a new instance of OLYMPEIgnore.



6
7
8
9
# File 'lib/cli/file_helper.rb', line 6

def initialize(patterns,project_root = "")
  @patterns = patterns + [ ".git/" ]
  @project_root = project_root
end

Class Method Details

.from_file(project_root) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/cli/file_helper.rb', line 38

def self.from_file(project_root)
  f = "#{project_root}/.olympeignore"
  if File.exists?(f) 
    contents = File.read(f).split("\n")
    OLYMPEIgnore.new(contents,project_root)
  else
    OLYMPEIgnore.new([],project_root)
  end
end

Instance Method Details

#exclude_dots_only(filenames) ⇒ Object



25
26
27
28
29
30
# File 'lib/cli/file_helper.rb', line 25

def exclude_dots_only(filenames)
  filenames.reject do |filename|
    base = File.basename(filename)
    base == "." || base == ".."
  end
end

#excluded_files(filenames) ⇒ Object



34
35
36
# File 'lib/cli/file_helper.rb', line 34

def excluded_files(filenames)
  filenames - included_files(filenames)
end

#included_files(filenames) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cli/file_helper.rb', line 11

def included_files(filenames)
  exclude_dots_only(filenames).reject do |filename|
    exclude = false
    @patterns.each do |pattern|
      if is_negative_pattern?(pattern)
        exclude = false if negative_match(pattern,filename)
      else
        exclude ||= match(pattern,filename)
      end
    end
    exclude
  end
end

#is_negative_pattern?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/cli/file_helper.rb', line 76

def is_negative_pattern?(pattern)
  pattern =~ /^!/
end

#match(pattern, filename) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cli/file_helper.rb', line 48

def match(pattern,filename)

  filename = filename.sub(/^#{@project_root}\//,'') # remove any project directory prefix

  return false if pattern =~ /^\s*$/ # ignore blank lines

  return false if pattern =~ /^#/ # lines starting with # are comments

  return false if pattern =~ /^!/ # lines starting with ! are negated

  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 =~ /^\//
    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

#negative_match(pattern, filename) ⇒ Object



80
81
82
83
# File 'lib/cli/file_helper.rb', line 80

def negative_match(pattern,filename)
  return false unless pattern =~ /^!/
  match(pattern.sub(/^!/,''),filename)
end