Class: RuboCop::TargetFinder
- Inherits:
-
Object
- Object
- RuboCop::TargetFinder
- 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
- #all_cops_include ⇒ Object
- #configured_include?(file) ⇒ Boolean
- #debug? ⇒ Boolean
- #excluded_dirs(base_dir) ⇒ Object
- #fail_fast? ⇒ Boolean
-
#find(args) ⇒ Array
Generate a list of target files by expanding globbing patterns (if any).
-
#find_files(base_dir, flags) ⇒ Object
Search for files recursively starting at the given base directory using the given flags that determine how the match is made.
- #force_exclusion? ⇒ Boolean
- #included_file?(file) ⇒ Boolean
-
#initialize(config_store, options = {}) ⇒ TargetFinder
constructor
A new instance of TargetFinder.
- #process_explicit_path(path) ⇒ Object
- #ruby_executable?(file) ⇒ Boolean
- #ruby_extension?(file) ⇒ Boolean
- #ruby_extensions ⇒ Object
- #ruby_file?(file) ⇒ Boolean
- #ruby_filename?(file) ⇒ Boolean
- #ruby_filenames ⇒ Object
- #ruby_interpreters(file) ⇒ Object
- #stdin? ⇒ Boolean
-
#target_files_in_dir(base_dir = Dir.pwd) ⇒ Array
Finds all Ruby source files under the current or other supplied directory.
- #to_inspect?(file, hidden_files, base_dir_config) ⇒ Boolean
- #toplevel_dirs(base_dir, flags) ⇒ Object
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, = {}) @config_store = config_store @options = end |
Instance Method Details
#all_cops_include ⇒ Object
141 142 143 |
# File 'lib/rubocop/target_finder.rb', line 141 def all_cops_include @config_store.for('.').for_all_cops['Include'].map(&:to_s) end |
#configured_include?(file) ⇒ Boolean
169 170 171 |
# File 'lib/rubocop/target_finder.rb', line 169 def configured_include?(file) @config_store.for('.').file_to_include?(file) end |
#debug? ⇒ Boolean
18 19 20 |
# File 'lib/rubocop/target_finder.rb', line 18 def debug? @options[:debug] end |
#excluded_dirs(base_dir) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/rubocop/target_finder.rb', line 111 def excluded_dirs(base_dir) all_cops_config = @config_store.for(base_dir).for_all_cops dir_tree_excludes = all_cops_config['Exclude'].select do |pattern| pattern.is_a?(String) && pattern.end_with?('/**/*') end dir_tree_excludes.map { |pattern| pattern.sub(%r{/\*\*/\*$}, '') } end |
#fail_fast? ⇒ 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
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.(f) }.uniq end |
#find_files(base_dir, flags) ⇒ Object
Search for files recursively starting at the given base directory using the given flags that determine how the match is made. Excluded files will be removed later by the caller, but as an optimization find_files removes the top level directories that are excluded in configuration in the normal way (dir/*/).
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rubocop/target_finder.rb', line 88 def find_files(base_dir, flags) wanted_toplevel_dirs = toplevel_dirs(base_dir, flags) - excluded_dirs(base_dir) wanted_toplevel_dirs.map! { |dir| dir << '/**/*' } pattern = if wanted_toplevel_dirs.empty? # We need this special case to avoid creating the pattern # /**/* which searches the entire file system. ["#{base_dir}/**/*"] else # Search the non-excluded top directories, but also add files # on the top level, which would otherwise not be found. wanted_toplevel_dirs.unshift("#{base_dir}/*") end Dir.glob(pattern, flags).select { |path| FileTest.file?(path) } end |
#force_exclusion? ⇒ Boolean
14 15 16 |
# File 'lib/rubocop/target_finder.rb', line 14 def force_exclusion? @options[:force_exclusion] end |
#included_file?(file) ⇒ Boolean
173 174 175 |
# File 'lib/rubocop/target_finder.rb', line 173 def included_file?(file) ruby_file?(file) || configured_include?(file) end |
#process_explicit_path(path) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rubocop/target_finder.rb', line 177 def process_explicit_path(path) files = path.include?('*') ? Dir[path] : [path] files.select! { |file| included_file?(file) } 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
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rubocop/target_finder.rb', line 145 def ruby_executable?(file) return false unless File.extname(file).empty? && File.exist?(file) first_line = File.open(file, &:readline) !(first_line =~ /#!.*(#{ruby_interpreters(file).join('|')})/).nil? rescue EOFError, ArgumentError => e warn("Unprocessable file #{file}: #{e.class}, #{e.}") if debug? false end |
#ruby_extension?(file) ⇒ Boolean
119 120 121 |
# File 'lib/rubocop/target_finder.rb', line 119 def ruby_extension?(file) ruby_extensions.include?(File.extname(file)) end |
#ruby_extensions ⇒ Object
123 124 125 126 127 128 |
# File 'lib/rubocop/target_finder.rb', line 123 def ruby_extensions ext_patterns = all_cops_include.select do |pattern| pattern.start_with?('**/*.') end ext_patterns.map { |pattern| pattern.sub('**/*', '') } end |
#ruby_file?(file) ⇒ Boolean
164 165 166 167 |
# File 'lib/rubocop/target_finder.rb', line 164 def ruby_file?(file) stdin? || ruby_extension?(file) || ruby_filename?(file) || ruby_executable?(file) end |
#ruby_filename?(file) ⇒ Boolean
130 131 132 |
# File 'lib/rubocop/target_finder.rb', line 130 def ruby_filename?(file) ruby_filenames.include?(File.basename(file)) end |
#ruby_filenames ⇒ Object
134 135 136 137 138 139 |
# File 'lib/rubocop/target_finder.rb', line 134 def ruby_filenames file_patterns = all_cops_include.reject do |pattern| pattern.start_with?('**/*.') end file_patterns.map { |pattern| pattern.sub('**/', '') } end |
#ruby_interpreters(file) ⇒ Object
156 157 158 |
# File 'lib/rubocop/target_finder.rb', line 156 def ruby_interpreters(file) @config_store.for(file).for_all_cops['RubyInterpreters'] end |
#stdin? ⇒ Boolean
160 161 162 |
# File 'lib/rubocop/target_finder.rb', line 160 def stdin? @options.key?(:stdin) 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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 = 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 if fail_fast? # Most recently modified file first. target_files.sort_by! { |path| -Integer(File.mtime(path)) } else target_files.sort! end end |
#to_inspect?(file, hidden_files, base_dir_config) ⇒ Boolean
76 77 78 79 80 81 |
# File 'lib/rubocop/target_finder.rb', line 76 def to_inspect?(file, hidden_files, base_dir_config) return false if base_dir_config.file_to_exclude?(file) return true if !hidden_files.include?(file) && ruby_file?(file) base_dir_config.file_to_include?(file) end |
#toplevel_dirs(base_dir, flags) ⇒ Object
105 106 107 108 109 |
# File 'lib/rubocop/target_finder.rb', line 105 def toplevel_dirs(base_dir, flags) Dir.glob(File.join(base_dir, '*'), flags).select do |dir| File.directory?(dir) && !dir.end_with?('/.', '/..') end end |