class Lokale::Agent

Attributes

lfiles[R]
macros[R]
proj_path[R]
project_lfiles[R]
sfiles_proceeded[R]

Public Class Methods

new(proj_path, macros) click to toggle source
# File lib/lokale/agent.rb, line 89
def initialize(proj_path, macros)
  @proj_path = proj_path
  @macros = macros
  @project_lfiles = []

  @writer = Writer.new
  @exporter = Exporter.new
  @importer = Importer.new

  get_localization_files
  find_all_localization_calls
end
source_file?(path) click to toggle source
# File lib/lokale/agent.rb, line 112
def self.source_file?(path)
  (File.directory?(path) == false) && (path =~ /\/Pods\//).nil? && ((path =~ /\.(swift|h|m)$/) != nil)
end

Public Instance Methods

append_new_macro_calls() click to toggle source
# File lib/lokale/agent.rb, line 163
def append_new_macro_calls
  @macros.each do |macro|
    next if macro.localization_file.nil?
    file = @lfiles.select { |lf| lf.full_name == macro.localization_file && lf.lang == Config.get.main_lang }.sample
    next if file.nil?

    found_strings = macro.found_strings.keys
    proj_keys = found_strings.map { |ls| ls.key }.to_set
    current_keys = file.keys.to_set
    new_keys = proj_keys - current_keys

    next if new_keys.empty?

    new_lstrings = found_strings.select { |ls| new_keys.include? ls.key }
    @writer.append_new_strings(new_lstrings, file)
  end
end
copy_base() click to toggle source
# File lib/lokale/agent.rb, line 150
def copy_base
  return if Config.get.base_lang.nil? || Config.get.main_lang.nil?

  main_lfiles = @lfiles.group_by { |f| f.lang }[Config.get.main_lang].select { |f| f.strings_file? }
  base_lfiles = @lfiles.group_by { |f| f.lang }[Config.get.base_lang].select { |f| f.strings_file? }

  main_lfiles.each do |en|
    base = base_lfiles.select { |f| f.full_name == en.full_name }.sample
    next if base.nil?
    IO.copy_stream(en.path, base.path)
  end
end
export_xliffs() click to toggle source
# File lib/lokale/agent.rb, line 181
def export_xliffs
  files = Config.get.base_lang.nil? ? @lfiles : @lfiles.select { |lf| lf.lang != Config.get.base_lang }
  main_lang = Config.get.main_lang
  diffs = Exporter::Diff.find(files, main_lang)
  @exporter.export(diffs) unless diffs.empty?
end
find_all_localization_calls() click to toggle source
# File lib/lokale/agent.rb, line 121
def find_all_localization_calls
  @macros.each { |m| m.clear_calls }

  @sfiles_proceeded = 0
  proj_files do |file| 
    next unless Agent.source_file? file

    file_content = File.read(file)
    @macros.each { |macro| macro.read_from file_content }
    @sfiles_proceeded += 1
  end
end
find_project_lfiles() click to toggle source
# File lib/lokale/agent.rb, line 134
def find_project_lfiles
  macro_proj_files = @macros.map { |m| m.project_file }.compact
  return if macro_proj_files.empty?

  h = Hash.new
  proj_files do |f|
    macro_proj_files.each do |pf|
      h[pf] = f if f.chomp(pf) != f
    end
  end

  return h
end
get_localization_files() click to toggle source
# File lib/lokale/agent.rb, line 116
def get_localization_files
  return @lfiles unless @lfiles.nil?
  @lfiles = proj_files.map { |file| LFile.try_to_read(file) }.compact
end
proj_files() { |f| ... } click to toggle source
# File lib/lokale/agent.rb, line 104
def proj_files 
  if block_given?
    Dir.glob("#{@proj_path}/**/**") { |f| yield f }
  else
    Dir.glob("#{@proj_path}/**/**")
  end
end
try_to_import() click to toggle source
# File lib/lokale/agent.rb, line 188
def try_to_import
  @importer.import_strings(self, @writer)
end
write_to_project_file() click to toggle source
# File lib/lokale/agent.rb, line 192
def write_to_project_file
  strings_to_write = Hash.new { |h, k| h[k] = [] }
  @macros.each do |m|
    next if m.project_file.nil?
    next if m.found_strings.nil?
    strings_to_write[m.project_file] += m.found_strings.keys
  end

  pfiles_pathes = find_project_lfiles
  strings_to_write.each do |file, lstrings|
    @writer.write_to_project_file(lstrings, pfiles_pathes[file])
  end
end