Class: Dotsmack::Smacker

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

Overview

Smacker dotfile scanner/enumerator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dotignore = nil, additional_ignores = [], dotconfig = nil) ⇒ Smacker

Create a Smacker dotfile scanner/enumerator.



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
# File 'lib/dotsmack.rb', line 69

def initialize(dotignore = nil, additional_ignores = [], dotconfig = nil)
  @dotignore = dotignore
  @dotconfig = dotconfig
  @additional_ignores = additional_ignores

  @path2dotignore = {}
  @path2dotconfig = {}

  if !dotignore.nil?
    home_dotignore = "#{HOME}#{File::SEPARATOR}#{dotignore}"

    @path2dotignore[PARENT_OF_HOME] = []

    @path2dotignore[HOME] =
      if File.exist?(home_dotignore)
        open(home_dotignore).read.split("\n")
      else
        []
      end
  end

  if !dotconfig.nil?
    home_dotconfig = "#{HOME}#{File::SEPARATOR}#{dotconfig}"

    @path2dotconfig[PARENT_OF_HOME] = nil

    @path2dotconfig[HOME] =
      if File.exist?(home_dotconfig)
        open(home_dotconfig).read
      else
        nil
      end
  end
end

Instance Attribute Details

#additional_ignores ⇒ Object

Returns the value of attribute additional_ignores.



61
62
63
# File 'lib/dotsmack.rb', line 61

def additional_ignores
  @additional_ignores
end

#dotconfig ⇒ Object

Returns the value of attribute dotconfig.



61
62
63
# File 'lib/dotsmack.rb', line 61

def dotconfig
  @dotconfig
end

#dotignore ⇒ Object

Returns the value of attribute dotignore.



61
62
63
# File 'lib/dotsmack.rb', line 61

def dotignore
  @dotignore
end

#path2config ⇒ Object

Returns the value of attribute path2config.



61
62
63
# File 'lib/dotsmack.rb', line 61

def path2config
  @path2config
end

#path2ignore ⇒ Object

Returns the value of attribute path2ignore.



61
62
63
# File 'lib/dotsmack.rb', line 61

def path2ignore
  @path2ignore
end

Instance Method Details

#config(path) ⇒ Object

Returns dotconfig for path.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/dotsmack.rb', line 196

def config(path)
  path = File.absolute_path(path)

  dir =
    if File.directory?(path)
      path
    else
      File.expand_path('..', path)
    end

  relative_to_home = dir.start_with?(HOME)

  if !relative_to_home
    dir = HOME
  end

  scan_for_dotconfig!(dir)

  @path2dotconfig[dir]
end

#enumerate(paths) ⇒ Object

Recursively enumerate files named or nested in paths array.

Skips dotignored files.

Returns [file, dotconfig] pairs.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/dotsmack.rb', line 224

def enumerate(paths)
  files = []

  paths.each do |path|
    # STDIN idiom
    if path == '-'
      files << [
        path,
        if @dotconfig.nil?
          nil
        else
          config(Dir.pwd)
        end
      ]
    # Skip symlinks
    elsif File.symlink?(path)
      nil
    elsif File.directory?(path) && (@dotignore.nil? || !ignore?(path))
      files.concat(
        enumerate(
          Find.find(path).reject do |p| File.directory?(p) end
        )
      )
    elsif !ignore?(path)
      files << [
          path,
          if @dotconfig.nil?
            nil
          else
            config(path)
          end
      ]
    end
  end

  files
end

#ignore?(path) ⇒ Boolean

Determines whether path is dotignored.

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/dotsmack.rb', line 170

def ignore?(path)
  path = File.absolute_path(path)

  dir =
    if File.directory?(path)
      path
    else
      File.expand_path('..', path)
    end

  relative_to_home = dir.start_with?(HOME)

  if !relative_to_home
    dir = HOME
  end

  scan_for_dotignore!(dir)

  (@path2dotignore[dir] + @additional_ignores).any? do |pattern|
    Dotsmack::fnmatch?(pattern, path)
  end
end

#scan_for_dotconfig!(dir) ⇒ Object

Scan for dotconfig.

Assumes dir is an absolute path. Assumes dir is not a symlink. Assumes dir is $HOME-relative.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dotsmack.rb', line 145

def scan_for_dotconfig!(dir)
  if @path2dotconfig.has_key?(dir)
    @path2dotconfig[dir]
  else
    candidate = "#{dir}#{File::SEPARATOR}#{@dotconfig}"

    parent = File.expand_path('..', dir)

    # Use first dotconfig found.
    dotconfig =
      if File.exist?(candidate)
        open(candidate).read
      else
        scan_for_dotconfig!(parent)
      end

    @path2dotconfig[dir] = dotconfig

    dotconfig
  end
end

#scan_for_dotignore!(dir) ⇒ Object

Scan for dotignore.

Assumes dir is an absolute path. Assumes dir is not a symlink. Assumes dir is $HOME-relative.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dotsmack.rb', line 111

def scan_for_dotignore!(dir)
  if @path2dotignore.has_key?(dir)
    @path2dotignore[dir]
  else
    candidate = "#{dir}#{File::SEPARATOR}#{@dotignore}"

    dotignore =
      if File.exist?(candidate)
        open(candidate).read.split("\n")
      else
        []
      end

    parent = File.expand_path('..', dir)

    # Determine parent dotignore
    parents_dotignore = scan_for_dotignore!(parent)

    # Merge candidate and parent information
    dotignore.concat(parents_dotignore)

    @path2dotignore[dir] = dotignore

    dotignore
  end
end