Class: Lokale::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/lokale/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proj_path, macros) ⇒ Agent

Returns a new instance of Agent.



89
90
91
92
93
94
95
96
97
98
99
100
# 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

Instance Attribute Details

#lfilesObject (readonly)

Returns the value of attribute lfiles.



87
88
89
# File 'lib/lokale/agent.rb', line 87

def lfiles
  @lfiles
end

#macrosObject (readonly)

Returns the value of attribute macros.



87
88
89
# File 'lib/lokale/agent.rb', line 87

def macros
  @macros
end

#proj_pathObject (readonly)

Returns the value of attribute proj_path.



87
88
89
# File 'lib/lokale/agent.rb', line 87

def proj_path
  @proj_path
end

#project_lfilesObject (readonly)

Returns the value of attribute project_lfiles.



87
88
89
# File 'lib/lokale/agent.rb', line 87

def project_lfiles
  @project_lfiles
end

#sfiles_proceededObject (readonly)

Returns the value of attribute sfiles_proceeded.



87
88
89
# File 'lib/lokale/agent.rb', line 87

def sfiles_proceeded
  @sfiles_proceeded
end

Class Method Details

.source_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# 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

Instance Method Details

#append_new_macro_callsObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# 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_baseObject



150
151
152
153
154
155
156
157
158
159
160
161
# 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_xliffsObject



181
182
183
184
185
186
# 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_callsObject



121
122
123
124
125
126
127
128
129
130
131
132
# 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_lfilesObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# 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_filesObject



116
117
118
119
# 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_filesObject



104
105
106
107
108
109
110
# 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_importObject



188
189
190
# File 'lib/lokale/agent.rb', line 188

def try_to_import
  @importer.import_strings(self, @writer)
end

#write_to_project_fileObject



192
193
194
195
196
197
198
199
200
201
202
203
204
# 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