Class: Twine::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/twine/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
# File 'lib/twine/runner.rb', line 9

def initialize(args)
  @options = {}
  @args = args
end

Class Method Details

.run(args) ⇒ Object



14
15
16
# File 'lib/twine/runner.rb', line 14

def self.run(args)
  new(args).run
end

Instance Method Details

#consume_all_string_filesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/twine/runner.rb', line 94

def consume_all_string_files
  if !File.directory?(@options[:input_path])
    raise Twine::Error.new("Directory does not exist: #{@options[:output_path]}")
  end

  Dir.glob(File.join(@options[:input_path], "**/*")) do |item|
    if File.file?(item)
      begin
        read_write_string_file(item, true, nil)
      rescue Twine::Error => e
        STDERR.puts "#{e.message}"
      end
    end
  end

  output_path = @options[:output_path] || @options[:strings_file]
  write_strings_data(output_path)
end

#consume_loc_dropObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/twine/runner.rb', line 178

def consume_loc_drop
  if !File.file?(@options[:input_path])
    raise Twine::Error.new("File does not exist: #{@options[:input_path]}")
  end

  begin
    require 'zip/zip'
  rescue LoadError
    raise Twine::Error.new "You must run 'gem install rubyzip' in order to create or consume localization drops."
  end

  Dir.mktmpdir do |dir|
    Zip::ZipFile.open(@options[:input_path]) do |zipfile|
      zipfile.each do |entry|
        if !entry.name.end_with?'/' and !File.basename(entry.name).start_with?'.'
          real_path = File.join(dir, entry.name)
          FileUtils.mkdir_p(File.dirname(real_path))
          zipfile.extract(entry.name, real_path)
          begin
            read_write_string_file(real_path, true, nil)
          rescue Twine::Error => e
            STDERR.puts "#{e.message}"
          end
        end
      end
    end
  end

  output_path = @options[:output_path] || @options[:strings_file]
  write_strings_data(output_path)
end

#consume_string_fileObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/twine/runner.rb', line 83

def consume_string_file
  lang = nil
  if @options[:languages]
    lang = @options[:languages][0]
  end

  read_write_string_file(@options[:input_path], true, lang)
  output_path = @options[:output_path] || @options[:strings_file]
  write_strings_data(output_path)
end

#determine_format_given_directory(directory) ⇒ Object



283
284
285
286
287
288
289
290
291
# File 'lib/twine/runner.rb', line 283

def determine_format_given_directory(directory)
  Formatters.formatters.each do |formatter|
    if formatter.can_handle_directory?(directory)
      return formatter::FORMAT_NAME
    end
  end

  return
end

#determine_format_given_path(path) ⇒ Object



272
273
274
275
276
277
278
279
280
281
# File 'lib/twine/runner.rb', line 272

def determine_format_given_path(path)
  ext = File.extname(path)
  Formatters.formatters.each do |formatter|
    if formatter::EXTENSION == ext
      return formatter::FORMAT_NAME
    end
  end

  return
end

#determine_language_given_path(path) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/twine/runner.rb', line 263

def determine_language_given_path(path)
  code = File.basename(path, File.extname(path))
  if !@strings.language_codes.include? code
    code = nil
  end

  code
end

#execute_commandObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/twine/runner.rb', line 37

def execute_command
  case @options[:command]
  when 'generate-string-file'
    generate_string_file
  when 'generate-all-string-files'
    generate_all_string_files
  when 'consume-string-file'
    consume_string_file
  when 'consume-all-string-files'
    consume_all_string_files
  when 'generate-loc-drop'
    generate_loc_drop
  when 'consume-loc-drop'
    consume_loc_drop
  when 'generate-report'
    generate_report
  end
end

#formatter_for_format(format) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/twine/runner.rb', line 293

def formatter_for_format(format)
  Formatters.formatters.each do |formatter|
    if formatter::FORMAT_NAME == format
      return formatter.new(@strings, @options)
    end
  end

  return
end

#generate_all_string_filesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/twine/runner.rb', line 65

def generate_all_string_files
  if !File.directory?(@options[:output_path])
    raise Twine::Error.new("Directory does not exist: #{@options[:output_path]}")
  end

  format = @options[:format]
  if !format
    format = determine_format_given_directory(@options[:output_path])
  end
  if !format
    raise Twine::Error.new "Could not determine format given the contents of #{@options[:output_path]}"
  end

  formatter = formatter_for_format(format)

  formatter.write_all_files(@options[:output_path])
end

#generate_loc_dropObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/twine/runner.rb', line 149

def generate_loc_drop
  begin
    require 'zip/zip'
  rescue LoadError
    raise Twine::Error.new "You must run 'gem install rubyzip' in order to create or consume localization drops."
  end

  if File.file?(@options[:output_path])
    File.delete(@options[:output_path])
  end

  Dir.mktmpdir do |dir|
    Zip::ZipFile.open(@options[:output_path], Zip::ZipFile::CREATE) do |zipfile|
      zipfile.mkdir('Locales')

      formatter = formatter_for_format(@options[:format])
      @strings.language_codes.each do |lang|
        if @options[:languages] == nil || @options[:languages].length == 0 || @options[:languages].include?(lang)
          file_name = lang + formatter.class::EXTENSION
          real_path = File.join(dir, file_name)
          zip_path = File.join('Locales', file_name)
          formatter.write_file(real_path, lang)
          zipfile.add(zip_path, real_path)
        end
      end
    end
  end
end

#generate_reportObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/twine/runner.rb', line 210

def generate_report
  total_strings = 0
  strings_per_lang = {}
  all_keys = Set.new
  duplicate_keys = Set.new
  keys_without_tags = Set.new
  @strings.language_codes.each do |code|
    strings_per_lang[code] = 0
  end

  @strings.sections.each do |section|
    section.rows.each do |row|
      total_strings += 1

      if all_keys.include? row.key
        duplicate_keys.add(row.key)
      else
        all_keys.add(row.key)
      end

      row.translations.each_key do |code|
        strings_per_lang[code] += 1
      end

      if row.tags == nil || row.tags.length == 0
        keys_without_tags.add(row.key)
      end
    end
  end

  # Print the report.
  puts "Total number of strings = #{total_strings}"
  @strings.language_codes.each do |code|
    puts "#{code}: #{strings_per_lang[code]}"
  end

  if duplicate_keys.length > 0
    puts "\nDuplicate string keys:"
    duplicate_keys.each do |key|
      puts key
    end
  end

  if keys_without_tags.length == total_strings
    puts "\nNone of your strings have tags."
  elsif keys_without_tags.length > 0
    puts "\nStrings without tags:"
    keys_without_tags.each do |key|
      puts key
    end
  end
end

#generate_string_fileObject



56
57
58
59
60
61
62
63
# File 'lib/twine/runner.rb', line 56

def generate_string_file
  lang = nil
  if @options[:languages]
    lang = @options[:languages][0]
  end

  read_write_string_file(@options[:output_path], false, lang)
end

#read_strings_dataObject



25
26
27
28
# File 'lib/twine/runner.rb', line 25

def read_strings_data
  @strings = StringsFile.new
  @strings.read @options[:strings_file]
end

#read_write_string_file(path, is_read, lang) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/twine/runner.rb', line 113

def read_write_string_file(path, is_read, lang)
  if is_read && !File.file?(path)
    raise Twine::Error.new("File does not exist: #{path}")
  end

  format = @options[:format]
  if !format
    format = determine_format_given_path(path)
  end
  if !format
    raise Twine::Error.new "Unable to determine format of #{path}"
  end

  formatter = formatter_for_format(format)

  if !lang
    lang = determine_language_given_path(path)
  end
  if !lang
    lang = formatter.determine_language_given_path(path)
  end
  if !lang
    raise Twine::Error.new "Unable to determine language for #{path}"
  end

  if !@strings.language_codes.include? lang
    @strings.language_codes << lang
  end

  if is_read
    formatter.read_file(path, lang)
  else
    formatter.write_file(path, lang)
  end
end

#runObject



18
19
20
21
22
23
# File 'lib/twine/runner.rb', line 18

def run
  # Parse all CLI arguments.
  CLI::parse_args(@args, @options)
  read_strings_data
  execute_command
end

#write_strings_data(path) ⇒ Object



30
31
32
33
34
35
# File 'lib/twine/runner.rb', line 30

def write_strings_data(path)
  if @options[:developer_language]
    @strings.set_developer_language_code(@options[:developer_language])
  end
  @strings.write(path)
end