File: silencer.rb

package info (click to toggle)
ruby-listen 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: ruby: 5,033; makefile: 9
file content (106 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

module Listen
  class Silencer
    # The default list of directories that get ignored.
    DEFAULT_IGNORED_FILES = %r{\A(?:
    \.git
      | \.svn
      | \.hg
      | \.rbx
      | \.bundle
      | bundle
      | vendor/bundle
      | log
      | tmp
      | vendor/ruby

      # emacs temp files
      | \#.+\#
      | \.\#.+
      )(/|\z)}x.freeze

    # The default list of files that get ignored.
    DEFAULT_IGNORED_EXTENSIONS = %r{(?:
      # Kate's tmp\/swp files
      \..*\d+\.new
      | \.kate-swp

      # Gedit tmp files
      | \.goutputstream-.{6}

      # Intellij files
      | ___jb_bak___
      | ___jb_old___

      # Vim swap files and write test
      | \.sw[px]
      | \.swpx
      | ^4913

      # Sed temporary files - but without actual words, like 'sedatives'
      | (?:\A
         sed

         (?:
          [a-zA-Z0-9]{0}[A-Z]{1}[a-zA-Z0-9]{5} |
          [a-zA-Z0-9]{1}[A-Z]{1}[a-zA-Z0-9]{4} |
          [a-zA-Z0-9]{2}[A-Z]{1}[a-zA-Z0-9]{3} |
          [a-zA-Z0-9]{3}[A-Z]{1}[a-zA-Z0-9]{2} |
          [a-zA-Z0-9]{4}[A-Z]{1}[a-zA-Z0-9]{1} |
          [a-zA-Z0-9]{5}[A-Z]{1}[a-zA-Z0-9]{0}
         )
        )

      # Mutagen sync temporary files
      | \.mutagen-temporary.*

      # other files
      | \.DS_Store
      | \.tmp
      | ~
    )\z}x.freeze

    # TODO: deprecate these mutators; use attr_reader instead
    attr_accessor :only_patterns, :ignore_patterns

    def initialize(**options)
      configure(options)
    end

    # TODO: deprecate this mutator
    def configure(options)
      @only_patterns = options[:only] ? Array(options[:only]) : nil
      @ignore_patterns = _init_ignores(options[:ignore], options[:ignore!])
    end

    def silenced?(relative_path, type)
      path = relative_path.to_s   # in case it is a Pathname

      _ignore?(path) || (only_patterns && type == :file && !_only?(path))
    end

    private

    def _ignore?(path)
      ignore_patterns.any? { |pattern| path =~ pattern }
    end

    def _only?(path)
      only_patterns.any? { |pattern| path =~ pattern }
    end

    def _init_ignores(ignores, overrides)
      patterns = []
      unless overrides
        patterns << DEFAULT_IGNORED_FILES
        patterns << DEFAULT_IGNORED_EXTENSIONS
      end

      patterns << ignores
      patterns << overrides

      patterns.compact.flatten
    end
  end
end