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(options = {}, strings = StringsFile.new) ⇒ Runner

Returns a new instance of Runner.



33
34
35
36
# File 'lib/twine/runner.rb', line 33

def initialize(options = {}, strings = StringsFile.new)
  @options = options
  @strings = strings
end

Class Method Details

.run(args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/twine/runner.rb', line 8

def self.run(args)
  options = CLI.parse(args)
  
  strings = StringsFile.new
  strings.read options[:strings_file]
  runner = new(options, strings)

  case options[:command]
  when 'generate-string-file'
    runner.generate_string_file
  when 'generate-all-string-files'
    runner.generate_all_string_files
  when 'consume-string-file'
    runner.consume_string_file
  when 'consume-all-string-files'
    runner.consume_all_string_files
  when 'generate-loc-drop'
    runner.generate_loc_drop
  when 'consume-loc-drop'
    runner.consume_loc_drop
  when 'validate-strings-file'
    runner.validate_strings_file
  end
end

Instance Method Details

#consume_all_string_filesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/twine/runner.rb', line 82

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
        Twine::stderr.puts "#{e.message}"
      end
    end
  end

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

#consume_loc_dropObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/twine/runner.rb', line 154

def consume_loc_drop
  require_rubyzip

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

  Dir.mktmpdir do |dir|
    Zip::File.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
            Twine::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



71
72
73
74
75
76
77
78
79
80
# File 'lib/twine/runner.rb', line 71

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



235
236
237
238
# File 'lib/twine/runner.rb', line 235

def determine_format_given_directory(directory)
  formatter = Formatters.formatters.find { |f| f.can_handle_directory?(directory) }
  return formatter::FORMAT_NAME if formatter
end

#determine_format_given_path(path) ⇒ Object



230
231
232
233
# File 'lib/twine/runner.rb', line 230

def determine_format_given_path(path)
  formatter = Formatters.formatters.find { |f| f::EXTENSION == File.extname(path) }
  return formatter::FORMAT_NAME if formatter
end

#determine_language_given_path(path) ⇒ Object



225
226
227
228
# File 'lib/twine/runner.rb', line 225

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

#formatter_for_format(format) ⇒ Object



240
241
242
243
# File 'lib/twine/runner.rb', line 240

def formatter_for_format(format)
  formatter = Formatters.formatters.find { |f| f::FORMAT_NAME == format }
  return formatter.new(@strings, @options) if formatter
end

#generate_all_string_filesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/twine/runner.rb', line 52

def generate_all_string_files
  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

  format = @options[:format] || determine_format_given_directory(@options[:output_path])
  unless 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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/twine/runner.rb', line 129

def generate_loc_drop
  require_rubyzip

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

  Dir.mktmpdir do |dir|
    Zip::File.open(@options[:output_path], Zip::File::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_string_fileObject



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

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

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

#read_write_string_file(path, is_read, lang) ⇒ Object



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
# File 'lib/twine/runner.rb', line 101

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] || determine_format_given_path(path)
  unless format
    raise Twine::Error.new "Unable to determine format of #{path}"
  end

  formatter = formatter_for_format(format)

  lang = lang || determine_language_given_path(path) || formatter.determine_language_given_path(path)
  unless 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

#validate_strings_fileObject



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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/twine/runner.rb', line 182

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

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

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

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

      invalid_keys << row.key unless row.key =~ valid_key_regex
    end
  end

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

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

  if keys_without_tags.length == total_strings
    errors << "None of your strings have tags."
  elsif keys_without_tags.length > 0
    errors << "Found strings without tags:\n#{join_keys.call(keys_without_tags)}"
  end

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

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

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

#write_strings_data(path) ⇒ Object



38
39
40
41
42
43
# File 'lib/twine/runner.rb', line 38

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