diff --git a/.babelish.sample b/.babelish.sample index 221ea3b..ae3b3af 100644 --- a/.babelish.sample +++ b/.babelish.sample @@ -31,5 +31,6 @@ langs: # Languages to convert. i.e. English:en # ignore_lang_path: true # does not care about lang component path. i.e: en.lproj/ # sheet: 0 # Index of worksheet to download. First index is 0. # macros_filename: Babelish.h # File to output the defines of localized strings -# stripping: false # Strips values inside the spreadsheet -# csv_separator: ',' # CSV column separator character, uses ',' by default \ No newline at end of file +# stripping: false # Strips values inside the spreadsheet +# csv_separator: ',' # CSV column separator character, uses ',' by default +# pretty_json: false # Prettify your json output files diff --git a/lib/babelish/commandline.rb b/lib/babelish/commandline.rb index 29ce582..8d8890b 100644 --- a/lib/babelish/commandline.rb +++ b/lib/babelish/commandline.rb @@ -31,6 +31,7 @@ class Commandline < Thor method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs" method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive" method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0" + method_option :pretty_json, :type => :boolean, aliases: "-p", desc: "Prettify your json output files" if klass[:name] == "CSV2Strings" method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys" end diff --git a/lib/babelish/csv2json.rb b/lib/babelish/csv2json.rb index 4f10130..c2ff577 100644 --- a/lib/babelish/csv2json.rb +++ b/lib/babelish/csv2json.rb @@ -2,6 +2,11 @@ module Babelish require 'json' class CSV2JSON < Csv2Base + def initialize(filename, langs, args = {}) + super + @pretty_json = args[:pretty_json] + end + def language_filepaths(language) require 'pathname' filename = @output_basename || language.code @@ -11,7 +16,7 @@ def language_filepaths(language) end def hash_to_output(content = {}) - return content.to_json + @pretty_json ? JSON.pretty_generate(content) : content.to_json end def extension diff --git a/test/babelish/test_csv2json.rb b/test/babelish/test_csv2json.rb index 6038a4e..8cd965b 100644 --- a/test/babelish/test_csv2json.rb +++ b/test/babelish/test_csv2json.rb @@ -24,4 +24,40 @@ def test_converting_csv_to_dotstrings_one_output_option # clean up system("rm -rf ./" + single_file) end + + def test_converting_csv_to_json_with_unpretty_json + csv_file = "test/data/test_data.csv" + expected_json_filename = "test_unpretty_json.json" + given_json_filename = "output.json" + + expected_json = File.read("test/data/" + expected_json_filename) + converter = Babelish::CSV2JSON.new(csv_file, + { "English" => "en" }, + output_basename: "output", + pretty_json: false) + converter.convert + given_json = File.read(given_json_filename) + assert_equal(expected_json, given_json, "JSON file has incorrect format") + + # clean up + system("rm -rf ./" + given_json_filename) + end + + def test_converting_csv_to_json_with_pretty_json + csv_file = "test/data/test_data.csv" + expected_json_filename = "test_pretty_json.json" + given_json_filename = "output.json" + + expected_json = File.read("test/data/" + expected_json_filename) + converter = Babelish::CSV2JSON.new(csv_file, + { "English" => "en" }, + output_basename: "output", + pretty_json: true) + converter.convert + given_json = File.read(given_json_filename) + assert_equal(expected_json, given_json, "JSON file has incorrect format") + + # clean up + system("rm -rf ./" + given_json_filename) + end end diff --git a/test/data/test_pretty_json.json b/test/data/test_pretty_json.json new file mode 100644 index 0000000..81b4904 --- /dev/null +++ b/test/data/test_pretty_json.json @@ -0,0 +1,4 @@ +{ + "ERROR_HANDLER_WARNING_DISMISS": "OK", + "ANOTHER_STRING": "hello" +} \ No newline at end of file diff --git a/test/data/test_unpretty_json.json b/test/data/test_unpretty_json.json new file mode 100644 index 0000000..4bf8460 --- /dev/null +++ b/test/data/test_unpretty_json.json @@ -0,0 +1 @@ +{"ERROR_HANDLER_WARNING_DISMISS":"OK","ANOTHER_STRING":"hello"} \ No newline at end of file