Class: RuboCop::TargetFinder Private
- Inherits:
-
Object
- Object
- RuboCop::TargetFinder
- Defined in:
- lib/rubocop/target_finder.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class finds target files to inspect by scanning the directory tree and picking ruby files.
Constant Summary collapse
- HIDDEN_PATH_SUBSTRING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"#{File::SEPARATOR}."
Instance Method Summary collapse
- #all_cops_include ⇒ Object private
- #combined_exclude_glob_patterns(base_dir) ⇒ Object private
- #configured_include?(file) ⇒ Boolean private
- #debug? ⇒ Boolean private
- #fail_fast? ⇒ Boolean private
-
#find(args, mode) ⇒ Array
private
Generate a list of target files by expanding globbing patterns (if any).
-
#find_files(base_dir, flags) ⇒ Object
private
Search for files recursively starting at the given base directory using the given flags that determine how the match is made.
- #force_exclusion? ⇒ Boolean private
- #included_file?(file) ⇒ Boolean private
-
#initialize(config_store, options = {}) ⇒ TargetFinder
constructor
private
A new instance of TargetFinder.
- #process_explicit_path(path, mode) ⇒ Object private
- #ruby_executable?(file) ⇒ Boolean private
- #ruby_extension?(file) ⇒ Boolean private
- #ruby_extensions ⇒ Object private
- #ruby_file?(file) ⇒ Boolean private
- #ruby_filename?(file) ⇒ Boolean private
- #ruby_filenames ⇒ Object private
- #ruby_interpreters(file) ⇒ Object private
- #stdin? ⇒ Boolean private
- #symlink_excluded_or_infinite_loop?(base_dir, current_dir, exclude_pattern, flags) ⇒ Boolean private
-
#target_files_in_dir(base_dir = Dir.pwd) ⇒ Array
private
Finds all Ruby source files under the current or other supplied directory.
- #to_inspect?(file, hidden_files, base_dir_config) ⇒ Boolean private
- #wanted_dir_patterns(base_dir, exclude_pattern, flags) ⇒ Object private
Constructor Details
#initialize(config_store, options = {}) ⇒ TargetFinder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of TargetFinder.
10 11 12 13 |
# File 'lib/rubocop/target_finder.rb', line 10 def initialize(config_store, = {}) @config_store = config_store @options = end |
Instance Method Details
#all_cops_include ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
149 150 151 |
# File 'lib/rubocop/target_finder.rb', line 149 def all_cops_include @all_cops_include ||= @config_store.for_pwd.for_all_cops['Include'].map(&:to_s) end |
#combined_exclude_glob_patterns(base_dir) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
120 121 122 123 124 125 |
# File 'lib/rubocop/target_finder.rb', line 120 def combined_exclude_glob_patterns(base_dir) exclude = @config_store.for(base_dir).for_all_cops['Exclude'] || [] patterns = exclude.select { |pattern| pattern.is_a?(String) && pattern.end_with?('/**/*') } .map { |pattern| pattern.sub("#{base_dir}/", '') } "#{base_dir}/{#{patterns.join(',')}}" end |
#configured_include?(file) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
176 177 178 |
# File 'lib/rubocop/target_finder.rb', line 176 def configured_include?(file) @config_store.for_pwd.file_to_include?(file) end |
#debug? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
19 20 21 |
# File 'lib/rubocop/target_finder.rb', line 19 def debug? @options[:debug] end |
#fail_fast? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 |
# File 'lib/rubocop/target_finder.rb', line 23 def fail_fast? @options[:fail_fast] end |
#find(args, mode) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rubocop/target_finder.rb', line 31 def find(args, mode) 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, mode) end end files.map { |f| File.(f) }.uniq end |
#find_files(base_dir, flags) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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/*/).
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rubocop/target_finder.rb', line 83 def find_files(base_dir, flags) # get all wanted directories first to improve speed of finding all files exclude_pattern = combined_exclude_glob_patterns(base_dir) dir_flags = flags | File::FNM_PATHNAME | File::FNM_EXTGLOB patterns = wanted_dir_patterns(base_dir, exclude_pattern, dir_flags) patterns.map! { |dir| File.join(dir, '*') } # We need this special case to avoid creating the pattern # /**/* which searches the entire file system. patterns = [File.join(base_dir, '**/*')] if patterns.empty? Dir.glob(patterns, flags).select { |path| FileTest.file?(path) } end |
#force_exclusion? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
15 16 17 |
# File 'lib/rubocop/target_finder.rb', line 15 def force_exclusion? @options[:force_exclusion] end |
#included_file?(file) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
180 181 182 |
# File 'lib/rubocop/target_finder.rb', line 180 def included_file?(file) ruby_file?(file) || configured_include?(file) end |
#process_explicit_path(path, mode) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rubocop/target_finder.rb', line 184 def process_explicit_path(path, mode) files = path.include?('*') ? Dir[path] : [path] if mode == :only_recognized_file_types || force_exclusion? files.select! { |file| included_file?(file) } 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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rubocop/target_finder.rb', line 153 def ruby_executable?(file) return false unless File.extname(file).empty? && File.exist?(file) first_line = File.open(file, &:readline) /#!.*(#{ruby_interpreters(file).join('|')})/.match?(first_line) rescue EOFError, ArgumentError => e warn("Unprocessable file #{file}: #{e.class}, #{e.}") if debug? false end |
#ruby_extension?(file) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
127 128 129 |
# File 'lib/rubocop/target_finder.rb', line 127 def ruby_extension?(file) ruby_extensions.include?(File.extname(file)) end |
#ruby_extensions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
131 132 133 134 135 136 |
# File 'lib/rubocop/target_finder.rb', line 131 def ruby_extensions @ruby_extensions ||= begin ext_patterns = all_cops_include.select { |pattern| pattern.start_with?('**/*.') } ext_patterns.map { |pattern| pattern.sub('**/*', '') } end end |
#ruby_file?(file) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
172 173 174 |
# File 'lib/rubocop/target_finder.rb', line 172 def ruby_file?(file) stdin? || ruby_extension?(file) || ruby_filename?(file) || ruby_executable?(file) end |
#ruby_filename?(file) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
138 139 140 |
# File 'lib/rubocop/target_finder.rb', line 138 def ruby_filename?(file) ruby_filenames.include?(File.basename(file)) end |
#ruby_filenames ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
142 143 144 145 146 147 |
# File 'lib/rubocop/target_finder.rb', line 142 def ruby_filenames @ruby_filenames ||= begin file_patterns = all_cops_include.reject { |pattern| pattern.start_with?('**/*.') } file_patterns.map { |pattern| pattern.sub('**/', '') } end end |
#ruby_interpreters(file) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
164 165 166 |
# File 'lib/rubocop/target_finder.rb', line 164 def ruby_interpreters(file) @config_store.for(file).for_all_cops['RubyInterpreters'] end |
#stdin? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
168 169 170 |
# File 'lib/rubocop/target_finder.rb', line 168 def stdin? @options.key?(:stdin) end |
#symlink_excluded_or_infinite_loop?(base_dir, current_dir, exclude_pattern, flags) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
112 113 114 115 116 117 118 |
# File 'lib/rubocop/target_finder.rb', line 112 def symlink_excluded_or_infinite_loop?(base_dir, current_dir, exclude_pattern, flags) dir_realpath = File.realpath(current_dir) File.symlink?(current_dir.chomp('/')) && ( File.fnmatch?(exclude_pattern, "#{dir_realpath}/", flags) || File.realpath(base_dir).start_with?(dir_realpath) ) end |
#target_files_in_dir(base_dir = Dir.pwd) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/target_finder.rb', line 56 def target_files_in_dir(base_dir = Dir.pwd) # Support Windows: Backslashes from command-line -> forward slashes base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR all_files = find_files(base_dir, File::FNM_DOTMATCH) # use file.include? for performance optimization hidden_files = all_files.select { |file| file.include?(HIDDEN_PATH_SUBSTRING) }.sort base_dir_config = @config_store.for(base_dir) target_files = all_files.select { |file| to_inspect?(file, hidden_files, base_dir_config) } target_files.sort_by!(&order) end |
#to_inspect?(file, hidden_files, base_dir_config) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
69 70 71 72 73 74 75 76 |
# File 'lib/rubocop/target_finder.rb', line 69 def to_inspect?(file, hidden_files, base_dir_config) return false if base_dir_config.file_to_exclude?(file) return true if !hidden_files.bsearch do |hidden_file| file <=> hidden_file end && ruby_file?(file) base_dir_config.file_to_include?(file) end |
#wanted_dir_patterns(base_dir, exclude_pattern, flags) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubocop/target_finder.rb', line 96 def wanted_dir_patterns(base_dir, exclude_pattern, flags) # Escape glob characters in base_dir to avoid unwanted behavior. base_dir = base_dir.gsub(/[\\\{\}\[\]\*\?]/) do |reserved_glob_character| "\\#{reserved_glob_character}" end dirs = Dir.glob(File.join(base_dir, '*/'), flags) .reject do |dir| next true if dir.end_with?('/./', '/../') next true if File.fnmatch?(exclude_pattern, dir, flags) symlink_excluded_or_infinite_loop?(base_dir, dir, exclude_pattern, flags) end dirs.flat_map { |dir| wanted_dir_patterns(dir, exclude_pattern, flags) }.unshift(base_dir) end |