diff --git a/lib/twine/cli.rb b/lib/twine/cli.rb index dd5e086..19b7b6a 100644 --- a/lib/twine/cli.rb +++ b/lib/twine/cli.rb @@ -33,6 +33,8 @@ module Twine opts.separator '' opts.separator 'generate-report -- Generates a report containing data about your strings. For example, it will tell you if you have any duplicate strings or if any of your strings are missing tags. In addition, it will tell you how many strings you have and how many of those strings have been translated into each language.' opts.separator '' + opts.separator 'validate-strings-file -- Validates that the given strings file is parseable, contains no duplicates, and that every string has a tag. Exits with a non-zero exit code if those criteria are not met.' + opts.separator '' opts.separator 'General Options:' opts.separator '' opts.on('-l', '--lang LANGUAGES', Array, 'The language code(s) to use for the specified action.') do |langs| @@ -97,6 +99,7 @@ module Twine opts.separator '> twine generate-loc-drop strings.txt LocDrop5.zip --tags FT,FB --format android --lang de,en,en-GB,ja,ko' opts.separator '> twine consume-loc-drop strings.txt LocDrop5.zip' opts.separator '> twine generate-report strings.txt' + opts.separator '> twine validate-strings-file strings.txt' end parser.parse! @args diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index f811b7a..6c90fc9 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -3,7 +3,7 @@ require 'tmpdir' Twine::Plugin.new # Initialize plugins first in Runner. module Twine - VALID_COMMANDS = ['generate-string-file', 'generate-all-string-files', 'consume-string-file', 'consume-all-string-files', 'generate-loc-drop', 'consume-loc-drop', 'generate-report'] + VALID_COMMANDS = ['generate-string-file', 'generate-all-string-files', 'consume-string-file', 'consume-all-string-files', 'generate-loc-drop', 'consume-loc-drop', 'generate-report', 'validate-strings-file'] class Runner def initialize(args) @@ -50,6 +50,8 @@ module Twine consume_loc_drop when 'generate-report' generate_report + when 'validate-strings-file' + validate_strings_file end end @@ -260,6 +262,43 @@ module Twine end end + def validate_strings_file + total_strings = 0 + all_keys = Set.new + duplicate_keys = Set.new + keys_without_tags = Set.new + + @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 + + if row.tags == nil || row.tags.length == 0 + keys_without_tags.add(row.key) + end + end + end + + if duplicate_keys.length > 0 + error_body = duplicate_keys.to_a.join("\n ") + raise Twine::Error.new "Found duplicate string key(s):\n #{error_body}" + end + + if keys_without_tags.length == total_strings + raise Twine::Error.new "None of your strings have tags." + elsif keys_without_tags.length > 0 + error_body = keys_without_tags.to_a.join("\n ") + raise Twine::Error.new "Found strings(s) without tags:\n #{error_body}" + end + + puts "#{@options[:strings_file]} is valid." + end + def determine_language_given_path(path) code = File.basename(path, File.extname(path)) if !@strings.language_codes.include? code