Class: RuboCop::TargetFinder

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

Overview

This class finds target files to inspect by scanning the directory tree and picking ruby files.

Instance Method Summary collapse

Constructor Details

#initialize(config_store, options = {}) ⇒ TargetFinder

Returns a new instance of TargetFinder.



9
10
11
12
# File 'lib/rubocop/target_finder.rb', line 9

def initialize(config_store, options = {})
  @config_store = config_store
  @options = options
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/rubocop/target_finder.rb', line 18

def debug?
  @options[:debug]
end

#fail_fast?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rubocop/target_finder.rb', line 22

def fail_fast?
  @options[:fail_fast]
end

#find(args) ⇒ Array

Generate a list of target files by expanding globbing patterns (if any). If args is empty, recursively find all Ruby source files under the current directory

Returns:

  • (Array)

    array of file paths



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/target_finder.rb', line 30

def find(args)
  return target_files_in_dir if args.empty?

  files = []

  args.uniq.each do |arg|
    files += if File.directory?(arg)
               target_files_in_dir(arg.chomp(File::SEPARATOR))
             else
               process_explicit_path(arg)
             end
  end

  files.map { |f| File.expand_path(f) }.uniq
end

#find_files(base_dir, flags) ⇒ Object



83
84
85
# File 'lib/rubocop/target_finder.rb', line 83

def find_files(base_dir, flags)
  Dir.glob("#{base_dir}/**/*", flags).select { |path| FileTest.file?(path) }
end

#force_exclusion?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rubocop/target_finder.rb', line 14

def force_exclusion?
  @options[:force_exclusion]
end

#process_explicit_path(path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubocop/target_finder.rb', line 96

def process_explicit_path(path)
  files = if path.include?('*')
            Dir[path]
          else
            [path]
          end

  return files unless force_exclusion?

  files.reject do |file|
    config = @config_store.for(file)
    config.file_to_exclude?(file)
  end
end

#ruby_executable?(file) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
# File 'lib/rubocop/target_finder.rb', line 87

def ruby_executable?(file)
  return false unless File.extname(file).empty?
  first_line = File.open(file) { |f| f.readline }
  first_line =~ /#!.*ruby/
rescue EOFError, ArgumentError => e
  warn "Unprocessable file #{file}: #{e.class}, #{e.message}" if debug?
  false
end

#target_files_in_dir(base_dir = Dir.pwd) ⇒ Array

Finds all Ruby source files under the current or other supplied directory. A Ruby source file is defined as a file with the .rb extension or a file with no extension that has a ruby shebang line as its first line. It is possible to specify includes and excludes using the config file, so you can include other Ruby files like Rakefiles and gemspecs.

Parameters:

  • base_dir (defaults to: Dir.pwd)

    Root directory under which to search for ruby source files

Returns:

  • (Array)

    Array of filenames



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/target_finder.rb', line 55

def target_files_in_dir(base_dir = Dir.pwd)
  # Support Windows: Backslashes from command-line -> forward slashes
  if File::ALT_SEPARATOR
    base_dir.gsub!(File::ALT_SEPARATOR, File::SEPARATOR)
  end
  all_files = find_files(base_dir, File::FNM_DOTMATCH)
  hidden_files = Set.new(all_files - find_files(base_dir, 0))
  base_dir_config = @config_store.for(base_dir)

  target_files = all_files.select do |file|
    to_inspect?(file, hidden_files, base_dir_config)
  end

  # Most recently modified file first.
  target_files.sort_by! { |path| -File.mtime(path).to_i } if fail_fast?

  target_files
end

#to_inspect?(file, hidden_files, base_dir_config) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# File 'lib/rubocop/target_finder.rb', line 74

def to_inspect?(file, hidden_files, base_dir_config)
  return false if base_dir_config.file_to_exclude?(file)
  unless hidden_files.include?(file)
    return true if File.extname(file) == '.rb'
    return true if ruby_executable?(file)
  end
  base_dir_config.file_to_include?(file)
end