From 3c07e90c1f621b73d147023189c5ecdbec688845 Mon Sep 17 00:00:00 2001 From: Ben Cochran Date: Mon, 16 Mar 2015 03:28:44 -0700 Subject: [PATCH 1/4] Add `validate-strings-file` command --- lib/twine/cli.rb | 3 +++ lib/twine/runner.rb | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) 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 From 8e0fccf853ffda17ba779e6b618cf0e006fd9209 Mon Sep 17 00:00:00 2001 From: Ben Cochran Date: Mon, 16 Mar 2015 10:46:36 -0700 Subject: [PATCH 2/4] Add `validate-strings-file` to readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 69b03fa..8b54f81 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,12 @@ This command gives you useful information about your strings. It will tell you h $ twine generate-report /path/to/strings.txt +#### `validate-strings-file` + +This command validates that the strings file can be parsed, contains no duplicate keys, and that all strings have at least one tag. It will exit with a status code other than zero if any of those criteria are not met. + + $ twine validate-strings-file /path/to/strings.txt + ## Creating Your First strings.txt File The easiest way to create your first strings.txt file is to run the `consume-all-string-files` command. The one caveat is to first create a blank strings.txt file to use as your starting point. Then, just point the `consume-all-string-files` command at a directory in your project containing all of your iOS, OS X, or Android strings files. From ee2e679c4a78bcbcfa2f9ad80046d9f0f9e6f0fa Mon Sep 17 00:00:00 2001 From: Ben Cochran Date: Tue, 17 Mar 2015 02:21:07 -0700 Subject: [PATCH 3/4] Remove validity checks from `generate-report` --- README.md | 4 ++-- lib/twine/cli.rb | 2 +- lib/twine/runner.rb | 29 ----------------------------- 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 8b54f81..8884b4d 100644 --- a/README.md +++ b/README.md @@ -130,13 +130,13 @@ This command is a convenient way of taking a zip file and executing the `consume #### `generate-report` -This command gives you useful information about your strings. It will tell you how many strings you have, how many have been translated into each language, and whether your master strings data file has any duplicate string keys. +This command gives you useful information about your strings. It will tell you how many strings you have and how many have been translated into each language. $ twine generate-report /path/to/strings.txt #### `validate-strings-file` -This command validates that the strings file can be parsed, contains no duplicate keys, and that all strings have at least one tag. It will exit with a status code other than zero if any of those criteria are not met. +This command validates that the strings file can be parsed, contains no duplicate keys, and that all strings have at least one tag. It will exit with a non-zero status code if any of those criteria are not met. $ twine validate-strings-file /path/to/strings.txt diff --git a/lib/twine/cli.rb b/lib/twine/cli.rb index 19b7b6a..6a80ae7 100644 --- a/lib/twine/cli.rb +++ b/lib/twine/cli.rb @@ -31,7 +31,7 @@ module Twine opts.separator '' opts.separator 'consume-loc-drop -- Consumes an archive of translated files. This archive should be in the same format as the one created by the generate-loc-drop command.' 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 'generate-report -- Generates a report containing data about your strings. 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 '' diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 6c90fc9..9315a41 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -212,9 +212,6 @@ module Twine 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 @@ -223,19 +220,9 @@ module Twine 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 @@ -244,22 +231,6 @@ module Twine @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 def validate_strings_file From 2bdf42f399afcd4f05f8e63cd79b3822d4fec562 Mon Sep 17 00:00:00 2001 From: Ben Cochran Date: Tue, 17 Mar 2015 02:21:43 -0700 Subject: [PATCH 4/4] Report all errors, not just the first --- lib/twine/runner.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 9315a41..9946cbc 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -238,6 +238,7 @@ module Twine all_keys = Set.new duplicate_keys = Set.new keys_without_tags = Set.new + errors = [] @strings.sections.each do |section| section.rows.each do |row| @@ -257,14 +258,18 @@ module Twine 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}" + errors << "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." + errors << "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}" + errors << "Found strings(s) without tags:\n #{error_body}" + end + + if errors.length > 0 + raise Twine::Error.new errors.join("\n\n") end puts "#{@options[:strings_file]} is valid."