Class: PathList::GitignoreRuleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/path_list/gitignore_rule_builder.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

GitignoreIncludeRuleBuilder

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ GitignoreRuleBuilder

Returns a new instance of GitignoreRuleBuilder.



5
6
7
8
9
10
11
12
# File 'lib/path_list/gitignore_rule_builder.rb', line 5

def initialize(rule)
  @re = ::PathList::PathRegexpBuilder.new
  @s = ::PathList::GitignoreRuleScanner.new(rule)

  @negation = false
  @anchored = false
  @dir_only = false
end

Instance Method Details

#anchored!Object



30
31
32
# File 'lib/path_list/gitignore_rule_builder.rb', line 30

def anchored!
  @anchored ||= true
end

#blank!Object



18
19
20
# File 'lib/path_list/gitignore_rule_builder.rb', line 18

def blank!
  throw :abort_build, []
end

#break!Object



14
15
16
# File 'lib/path_list/gitignore_rule_builder.rb', line 14

def break!
  throw :break
end

#buildObject



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/path_list/gitignore_rule_builder.rb', line 180

def build
  catch :abort_build do
    blank! if @s.hash?
    negated! if @s.exclamation_mark?
    process_rule

    @anchored = false if @anchored == :never

    build_rule
  end
end

#build_ruleObject



171
172
173
174
175
176
177
178
# File 'lib/path_list/gitignore_rule_builder.rb', line 171

def build_rule
  @re.prepend(prefix)
  if @negation
    ::PathList::Matchers::AllowPathRegexp.new(@re.to_regexp, @anchored, @dir_only)
  else
    ::PathList::Matchers::IgnorePathRegexp.new(@re.to_regexp, @anchored, @dir_only)
  end
end

#dir_only!Object



38
39
40
# File 'lib/path_list/gitignore_rule_builder.rb', line 38

def dir_only!
  @dir_only = true
end

#emit_any_dirObject



51
52
53
54
# File 'lib/path_list/gitignore_rule_builder.rb', line 51

def emit_any_dir
  anchored!
  @re.append_any_dir
end

#emit_dirObject



46
47
48
49
# File 'lib/path_list/gitignore_rule_builder.rb', line 46

def emit_dir
  anchored!
  @re.append_dir
end

#emit_endObject



56
57
58
59
# File 'lib/path_list/gitignore_rule_builder.rb', line 56

def emit_end
  @re.append_end_anchor
  break!
end

#negated!Object



26
27
28
# File 'lib/path_list/gitignore_rule_builder.rb', line 26

def negated!
  @negation = true
end

#never_anchored!Object



34
35
36
# File 'lib/path_list/gitignore_rule_builder.rb', line 34

def never_anchored!
  @anchored = :never
end

#nothing_emitted?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/path_list/gitignore_rule_builder.rb', line 42

def nothing_emitted?
  @re.empty?
end

#prefixObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/path_list/gitignore_rule_builder.rb', line 160

def prefix
  out = ::PathList::PathRegexpBuilder.new

  if @anchored
    out.append_start_anchor
  else
    out.append_dir_or_start_anchor
  end
  out
end

#process_backslashObject



61
62
63
64
65
# File 'lib/path_list/gitignore_rule_builder.rb', line 61

def process_backslash
  return unless @s.backslash?

  @re.append_escaped(@s.next_character) || unmatchable_rule!
end

#process_character_classObject

rubocop:disable Metrics/MethodLength



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/path_list/gitignore_rule_builder.rb', line 117

def process_character_class # rubocop:disable Metrics/MethodLength
  return unless @s.character_class_start?

  @re.append_character_class_open
  @re.append_character_class_negation if @s.character_class_negation?
  unmatchable_rule! if @s.character_class_end?

  until @s.character_class_end?
    next if process_backslash
    next @re.append_character_class_dash if @s.dash?
    next if @re.append_escaped(@s.character_class_literal)

    unmatchable_rule!
  end

  @re.append_character_class_close
end

#process_endObject



135
136
137
138
139
# File 'lib/path_list/gitignore_rule_builder.rb', line 135

def process_end
  blank! if nothing_emitted?

  emit_end
end

#process_ruleObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/path_list/gitignore_rule_builder.rb', line 141

def process_rule # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  anchored! if @s.slash?

  catch :break do
    loop do
      next if process_backslash
      next if process_slash
      next if process_two_stars
      next @re.append_any_non_dir if @s.star?
      next @re.append_one_non_dir if @s.question_mark?
      next if process_character_class
      next if @re.append_escaped(@s.literal)
      next if @re.append_escaped(@s.significant_whitespace)

      process_end
    end
  end
end

#process_slashObject



85
86
87
88
89
90
91
92
# File 'lib/path_list/gitignore_rule_builder.rb', line 85

def process_slash
  return unless @s.slash?
  return dir_only! if @s.end?
  return unmatchable_rule! if @s.slash?

  emit_dir
  process_star_end_after_slash
end

#process_star_end_after_slashObject

rubocop:disable Metrics/MethodLength



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/path_list/gitignore_rule_builder.rb', line 67

def process_star_end_after_slash # rubocop:disable Metrics/MethodLength
  if @s.star_end?
    @re.append_many_non_dir
    emit_end
  elsif @s.two_star_end?
    break!
  elsif @s.star_slash_end?
    @re.append_many_non_dir
    dir_only!
    emit_end
  elsif @s.two_star_slash_end?
    dir_only!
    break!
  else
    true
  end
end

#process_two_starsObject

rubocop:disable Metrics/MethodLength



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/path_list/gitignore_rule_builder.rb', line 94

def process_two_stars # rubocop:disable Metrics/MethodLength
  return unless @s.two_stars?
  return break! if @s.end?

  if @s.slash?
    if @s.end?
      @re.append_any_non_dir
      dir_only!
    elsif @s.slash?
      unmatchable_rule!
    else
      if nothing_emitted?
        never_anchored!
      else
        emit_any_dir
      end
      process_star_end_after_slash
    end
  else
    @re.append_any_non_dir
  end
end

#unmatchable_rule!Object



22
23
24
# File 'lib/path_list/gitignore_rule_builder.rb', line 22

def unmatchable_rule!
  throw :abort_build, []
end