class Tmsync::FileSearch

Public Class Methods

new(base_path:, matching_regex:, exclude_regex:) click to toggle source

Initializes a file search object.

@param [String] base_path the path of the directory to search within @param [String] matching_regex a regex that all localizable files match, optionally including a catch group for the language @param [String] exclude_regex a regex to exclude some matches from matching_regex

# File lib/tmsync.rb, line 19
def initialize(base_path:, matching_regex:, exclude_regex:)
  @base_path = base_path
  @matching_regex = /#{matching_regex}/
  @exclude_regex = /#{exclude_regex}/
end

Public Instance Methods

find_all_grouped_by_language() click to toggle source

Finds all files with corresponding language within a given directory matching the specified regexes.

@return [Hash] a hash containing language codes as keys and all found files paths as values (so values are of type Array)

# File lib/tmsync.rb, line 29
def find_all_grouped_by_language
  all_files = Dir.glob(File.join(@base_path, '**/*'))

  # apply exclude regex
  found_files = all_files.select { |file_path|
    file_path !~ @exclude_regex && !File.directory?(file_path)
  }

  # exclude empty files
  found_files = found_files.select { |file_path|
    content = File.open(file_path, 'r') { |f| f.read }
    !content.to_s.scrub("<?>").strip.empty?
  }

  # apply matching regex
  found_files = found_files.select { |file_path|
    file_path =~ @matching_regex
  }.map { |file_path|
    [file_path.match(@matching_regex).captures.last, file_path]
  }

  result = found_files.group_by(&:first).map { |k,v| [k, v.each(&:shift).flatten] }.to_h

  # replace nil key with fallback language
  if !(nil_values = result[nil]).nil?
    result[Tmsync::Constants::FALLBACK_LANGUAGE] = nil_values
    result.delete(nil)
  end

  result
end