lib/i18n/tasks/scanners/base_scanner.rb in i18n-tasks-0.2.22 vs lib/i18n/tasks/scanners/base_scanner.rb in i18n-tasks-0.3.0.rc1

- old
+ new

@@ -3,38 +3,44 @@ class BaseScanner include ::I18n::Tasks::RelativeKeys include ::I18n::Tasks::KeyPatternMatching attr_reader :config, :key_filter, :record_usages - def initialize(config) + def initialize(config = {}) @config = config.dup.with_indifferent_access.tap do |conf| conf[:paths] = %w(app/) if conf[:paths].blank? conf[:include] = Array(conf[:include]) if conf[:include].present? - conf[:exclude] = Array(conf[:exclude]) + if conf.key?(:exclude) + conf[:exclude] = Array(conf[:exclude]) + else + # exclude common binary extensions by default (images and fonts) + conf[:exclude] = %w(*.jpg *.png *.gif *.svg *.ico *.eot *.ttf *.woff) + end end @record_usages = false end def key_filter=(value) - @key_filter = value + @key_filter = value @key_filter_pattern = compile_key_pattern(value) if @key_filter end # @return [Array] found key usages, absolutized and unique def keys if @record_usages keys_with_usages else - @keys ||= traverse_files { |path| scan_file(path, read_file(path)).map(&:key) }.reduce(:+).uniq + @keys ||= (traverse_files { |path| scan_file(path, read_file(path)).map(&:key) }.reduce(:+) || []).uniq end end def keys_with_usages with_usages do - traverse_files { |path| + keys = traverse_files { |path| ::I18n::Tasks::KeyGroup.new(scan_file(path, read_file(path)), src_path: path) - }.map(&:keys).reduce(:+).group_by(&:key).map { |key, key_usages| + }.map(&:keys).reduce(:+) || [] + keys.group_by(&:key).map { |key, key_usages| {key: key, usages: key_usages.map { |usage| usage[:src].merge(path: usage[:src_path]) }} } end end @@ -51,11 +57,16 @@ # Run given block for every relevant file, according to config # @return [Array] Results of block calls def traverse_files result = [] - Find.find(*config[:paths]) do |path| + paths = config[:paths].select { |p| File.exists?(p) } + if paths.empty? + STDERR.puts Term::ANSIColor.yellow("i18n-tasks: [WARN] search.paths (#{config[:paths]}) do not exist") + return result + end + Find.find(*paths) do |path| next if File.directory?(path) || config[:include] && !path_fnmatch_any?(path, config[:include]) || path_fnmatch_any?(path, config[:exclude]) result << yield(path) end @@ -63,10 +74,11 @@ end def path_fnmatch_any?(path, globs) globs.any? { |glob| File.fnmatch(glob, path) } end + protected :path_fnmatch_any? def with_key_filter(key_filter = nil) filter_was = @key_filter self.key_filter = key_filter @@ -95,19 +107,13 @@ line_pos: src_pos - line_begin + 1, line: text[line_begin..line_end] }} end - def extract_key_from_match(match, path) - key = strip_literal(match[0]) - key = absolutize_key(key, path) if path && key.start_with?('.') - key - end - # remove the leading colon and unwrap quotes from the key match def strip_literal(literal) key = literal - key.slice!(0) if ':' == key[0] + key = key[1..-1] if ':' == key[0] key = key[1..-2] if %w(' ").include?(key[0]) key end VALID_KEY_RE = /^[\w.\#{}]+$/