Class: Twine::Runner

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

Defined Under Namespace

Classes: NullOutput

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, twine_file = TwineFile.new) ⇒ Runner

Returns a new instance of Runner.



43
44
45
46
47
48
49
# File 'lib/twine/runner.rb', line 43

def initialize(options = {}, twine_file = TwineFile.new)
  @options = options
  @twine_file = twine_file
  if @options[:quite]
    Twine::stdout = NullOutput.new
  end
end

Class Method Details

.run(args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twine/runner.rb', line 16

def self.run(args)
  options = CLI.parse(args)

  return unless options
  
  twine_file = TwineFile.new
  twine_file.read options[:twine_file]
  runner = new(options, twine_file)

  case options[:command]
  when 'generate-localization-file'
    runner.generate_localization_file
  when 'generate-all-localization-files'
    runner.generate_all_localization_files
  when 'consume-localization-file'
    runner.consume_localization_file
  when 'consume-all-localization-files'
    runner.consume_all_localization_files
  when 'generate-localization-archive'
    runner.generate_localization_archive
  when 'consume-localization-archive'
    runner.consume_localization_archive
  when 'validate-twine-file'
    runner.validate_twine_file
  end
end

Instance Method Details

#consume_all_localization_filesObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/twine/runner.rb', line 185

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

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

  output_path = @options[:output_path] || @options[:twine_file]
  write_twine_data(output_path)
end

#consume_localization_archiveObject



204
205
206
207
208
209
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
# File 'lib/twine/runner.rb', line 204

def consume_localization_archive
  require_rubyzip

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

  error_encountered = false
  Dir.mktmpdir do |temp_dir|
    Zip::File.open(@options[:input_path]) do |zipfile|
      zipfile.each do |entry|
        next if entry.name.end_with? '/' or File.basename(entry.name).start_with? '.'

        real_path = File.join(temp_dir, entry.name)
        FileUtils.mkdir_p(File.dirname(real_path))
        zipfile.extract(entry.name, real_path)
        begin
          read_localization_file(real_path)
        rescue Twine::Error => e
          Twine::stderr.puts "#{e.message}"
          error_encountered = true
        end
      end
    end
  end

  output_path = @options[:output_path] || @options[:twine_file]
  write_twine_data(output_path)

  if error_encountered
    raise Twine::Error.new("At least one file could not be consumed")
  end
end

#consume_localization_fileObject



174
175
176
177
178
179
180
181
182
183
# File 'lib/twine/runner.rb', line 174

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

  read_localization_file(@options[:input_path], lang)
  output_path = @options[:output_path] || @options[:twine_file]
  write_twine_data(output_path)
end

#generate_all_localization_filesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
# File 'lib/twine/runner.rb', line 72

def generate_all_localization_files
  validate_twine_file if @options[:validate]

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

  if @options[:format]
    formatter = formatter_for_format(@options[:format])
  else
    formatter = find_formatter { |f| f.can_handle_directory?(@options[:output_path]) }
  end
  
  unless formatter
    raise Twine::Error.new "Could not determine format given the contents of #{@options[:output_path]}. Try using `--format`."
  end

  file_name = @options[:file_name] || formatter.default_file_name
  if @options[:create_folders]
    @twine_file.language_codes.each do |lang|
      output_path = File.join(@options[:output_path], formatter.output_path_for_language(lang))

      FileUtils.mkdir_p(output_path)

      file_path = File.join(output_path, file_name)

      output = formatter.format_file(lang)
      unless output
        Twine::stdout.puts "Skipping file at path #{file_path} since it would not contain any translations."
        next
      end

      IO.write(file_path, output, encoding: output_encoding)
    end
  else
    language_found = false
    Dir.foreach(@options[:output_path]) do |item|
      next if item == "." or item == ".."

      output_path = File.join(@options[:output_path], item)
      next unless File.directory?(output_path)

      lang = formatter.determine_language_given_path(output_path)
      next unless lang

      language_found = true

      file_path = File.join(output_path, file_name)
      output = formatter.format_file(lang)
      unless output
        Twine::stdout.puts "Skipping file at path #{file_path} since it would not contain any translations."
        next
      end

      IO.write(file_path, output, encoding: output_encoding)
    end

    unless language_found
      raise Twine::Error.new("Failed to generate any files: No languages found at #{@options[:output_path]}")
    end
  end

end

#generate_localization_archiveObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/twine/runner.rb', line 140

def generate_localization_archive
  validate_twine_file if @options[:validate]
  
  require_rubyzip

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

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

      formatter = formatter_for_format(@options[:format])
      @twine_file.language_codes.each do |lang|
        if @options[:languages] == nil || @options[:languages].length == 0 || @options[:languages].include?(lang)
          file_name = lang + formatter.extension
          temp_path = File.join(temp_dir, file_name)
          zip_path = File.join('Locales', file_name)

          output = formatter.format_file(lang)
          unless output
            Twine::stdout.puts "Skipping file #{file_name} since it would not contain any translations."
            next
          end
          
          IO.write(temp_path, output, encoding: output_encoding)
          zipfile.add(zip_path, temp_path)
        end
      end
    end
  end
end

#generate_localization_fileObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/twine/runner.rb', line 58

def generate_localization_file
  validate_twine_file if @options[:validate]

  lang = nil
  lang = @options[:languages][0] if @options[:languages]

  formatter, lang = prepare_read_write(@options[:output_path], lang)
  output = formatter.format_file(lang)

  raise Twine::Error.new "Nothing to generate! The resulting file would not contain any translations." unless output

  IO.write(@options[:output_path], output, encoding: output_encoding)
end

#validate_twine_fileObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/twine/runner.rb', line 238

def validate_twine_file
  total_definitions = 0
  all_keys = Set.new
  duplicate_keys = Set.new
  keys_without_tags = Set.new
  invalid_keys = Set.new
  keys_with_python_only_placeholders = Set.new
  valid_key_regex = /^[A-Za-z0-9_]+$/

  @twine_file.sections.each do |section|
    section.definitions.each do |definition|
      total_definitions += 1

      duplicate_keys.add(definition.key) if all_keys.include? definition.key
      all_keys.add(definition.key)

      keys_without_tags.add(definition.key) if definition.tags == nil or definition.tags.length == 0

      invalid_keys << definition.key unless definition.key =~ valid_key_regex

      keys_with_python_only_placeholders << definition.key if definition.translations.values.any? { |v| Placeholders.contains_python_specific_placeholder(v) }
    end
  end

  errors = []
  join_keys = lambda { |set| set.map { |k| "  " + k }.join("\n") }

  unless duplicate_keys.empty?
    errors << "Found duplicate key(s):\n#{join_keys.call(duplicate_keys)}"
  end

  if @options[:pedantic]
    if keys_without_tags.length == total_definitions
      errors << "None of your definitions have tags."
    elsif keys_without_tags.length > 0
      errors << "Found definitions without tags:\n#{join_keys.call(keys_without_tags)}"
    end
  end

  unless invalid_keys.empty?
    errors << "Found key(s) with invalid characters:\n#{join_keys.call(invalid_keys)}"
  end

  unless keys_with_python_only_placeholders.empty?
    errors << "Found key(s) with placeholders that are only supported by Python:\n#{join_keys.call(keys_with_python_only_placeholders)}"
  end

  raise Twine::Error.new errors.join("\n\n") unless errors.empty?

  Twine::stdout.puts "#{@options[:twine_file]} is valid."
end

#write_twine_data(path) ⇒ Object



51
52
53
54
55
56
# File 'lib/twine/runner.rb', line 51

def write_twine_data(path)
  if @options[:developer_language]
    @twine_file.set_developer_language_code(@options[:developer_language])
  end
  @twine_file.write(path)
end