Class: ShopifyCLI::Theme::IgnoreFilter

Inherits:
Object
  • Object
show all
Includes:
Filter::PathMatcher
Defined in:
lib/shopify_cli/theme/ignore_filter.rb

Constant Summary collapse

FILE =
".shopifyignore"
DEFAULT_REGEXES =
[
  /\.git/,
  /\.hg/,
  /\.bzr/,
  /\.svn/,
  /_darcs/,
  /CVS/,
  /\.sublime-(project|workspace)/,
  /\.DS_Store/,
  /\.sass-cache/,
  /Thumbs\.db/,
  /desktop\.ini/,
  /config.yml/,
  /node_modules/,
].freeze
DEFAULT_GLOBS =
[].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter::PathMatcher

#as_glob, #as_regex, #glob_match?, #regex?, #regex_match?

Constructor Details

#initialize(root, patterns: []) ⇒ IgnoreFilter

Returns a new instance of IgnoreFilter.



57
58
59
60
61
62
63
64
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 57

def initialize(root, patterns: [])
  @root = root

  regexes, globs = patterns_to_regexes_and_globs(patterns)

  @regexes = regexes
  @globs = globs
end

Instance Attribute Details

#globsObject (readonly)

Returns the value of attribute globs.



30
31
32
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 30

def globs
  @globs
end

#regexesObject (readonly)

Returns the value of attribute regexes.



30
31
32
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 30

def regexes
  @regexes
end

#rootObject (readonly)

Returns the value of attribute root.



30
31
32
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 30

def root
  @root
end

Class Method Details

.from_path(root) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 32

def self.from_path(root)
  root = Pathname.new(root)
  ignore_file = root.join(FILE)
  patterns = if ignore_file.file?
    parse_ignore_file(ignore_file)
  else
    []
  end
  new(root, patterns: patterns)
end

.parse_ignore_file(file) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 43

def self.parse_ignore_file(file)
  patterns = []

  file.each_line do |line|
    line.strip!

    next if line.empty? || line.start_with?("#")

    patterns << line
  end

  patterns
end

Instance Method Details

#add_patterns(patterns) ⇒ Object



66
67
68
69
70
71
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 66

def add_patterns(patterns)
  regexes, globs = patterns_to_regexes_and_globs(patterns)

  @regexes += regexes
  @globs += globs
end

#match?(path) ⇒ Boolean Also known as: ignore?

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shopify_cli/theme/ignore_filter.rb', line 73

def match?(path)
  path = path.to_s

  return true if path.empty?

  regexes.each do |regex|
    return true if regex_match?(regex, path)
  end

  globs.each do |glob|
    return true if glob_match?(glob, path)
  end

  false
end