Class: BulldozeRenamer::FileFinder

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

Class Method Summary collapse

Class Method Details

.extract_directories(files) ⇒ Object

Using git ls-files has many advantages but it does not return directories. This method extracts all directories as present for the files found.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bulldoze_renamer/file_finder.rb', line 28

def self.extract_directories(files)
  result = Set.new
  files.each do |f|
    dir = File.dirname(f)

    # File.dirname('foo') => "."
    # File.dirname('/foo') => "/"
    while(dir != '.' && dir != '/' && !result.include?(dir))
      result << dir
      dir = File.dirname(dir)
    end
  end
  result.to_a.sort
end

.file_considered?(file) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/bulldoze_renamer/file_finder.rb', line 17

def self.file_considered?(file)
  return true if FileTest.directory?(file)

  fm = file_magic.file(file)
  fm.include?('text') ||
  fm.include?('JSON')
end

.file_excluded?(file) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/bulldoze_renamer/file_finder.rb', line 10

def self.file_excluded?(file)
  file.include?(".git/") ||
  file.include?("node_modules") ||
  File.basename(file) == '.git' ||
  file == '.'
end

.file_magicObject



6
7
8
# File 'lib/bulldoze_renamer/file_finder.rb', line 6

def self.file_magic
  @@file_magic ||= FileMagic.new
end

.find(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bulldoze_renamer/file_finder.rb', line 43

def self.find(path)
  files = []
  Dir.chdir(path) do
    files = `git ls-files`.split
    files = (files + extract_directories(files)).sort

    if block_given?
      files.select { |f| yield(f) }
    else
      files.select do |p|
        !file_excluded?(p) &&
        file_considered?(p)
      end
    end
  end
end