From 598cba5b45806a7c35ceb01b2edd11d018ad5256 Mon Sep 17 00:00:00 2001 From: Zach Margolis Date: Thu, 1 Jun 2017 15:41:51 -0400 Subject: [PATCH] Check in YML-XML conversion scripts **Why**: Share the scripts we use to share our files with vendors --- lib/i18n_converter.rb | 39 ++++++++++++++++ scripts/i18n-xml-to-yml | 5 ++ scripts/i18n-yml-to-xml | 5 ++ spec/lib/i18n_converter_spec.rb | 81 +++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 lib/i18n_converter.rb create mode 100755 scripts/i18n-xml-to-yml create mode 100755 scripts/i18n-yml-to-xml create mode 100644 spec/lib/i18n_converter_spec.rb diff --git a/lib/i18n_converter.rb b/lib/i18n_converter.rb new file mode 100644 index 00000000000..9c5514598fd --- /dev/null +++ b/lib/i18n_converter.rb @@ -0,0 +1,39 @@ +require 'yaml' +require 'active_support/core_ext/hash/conversions' + +class I18nConverter + def initialize(stdin:, stdout:) + @stdin = stdin + @stdout = stdout + end + + def xml_to_yml + return if bad_usage?(in_format: :xml, out_format: :yml) + + data = Hash.from_xml(stdin.read) + data_hash = data['hash'] + data = data_hash if data_hash + stdout.puts YAML.dump(data) + end + + def yml_to_xml + return if bad_usage?(in_format: :yml, out_format: :xml) + + data = YAML.safe_load(stdin.read) + stdout.puts data.to_xml + end + + private + + attr_reader :stdin, :stdout + + def bad_usage?(in_format:, out_format:) + return false unless stdin.tty? + + stdout.puts "Usage: cat en.#{in_format} | #{$PROGRAM_NAME} > output.#{out_format}" + # rubocop:disable Rails/Exit + exit 1 + # rubocop:enable Rails/Exit + true + end +end diff --git a/scripts/i18n-xml-to-yml b/scripts/i18n-xml-to-yml new file mode 100755 index 00000000000..81ee85559f7 --- /dev/null +++ b/scripts/i18n-xml-to-yml @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__))) +require 'i18n_converter' + +I18nConverter.new(stdin: STDIN, stdout: STDOUT).xml_to_yml diff --git a/scripts/i18n-yml-to-xml b/scripts/i18n-yml-to-xml new file mode 100755 index 00000000000..1b78bcb06ac --- /dev/null +++ b/scripts/i18n-yml-to-xml @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__))) +require 'i18n_converter' + +I18nConverter.new(stdin: STDIN, stdout: STDOUT).yml_to_xml diff --git a/spec/lib/i18n_converter_spec.rb b/spec/lib/i18n_converter_spec.rb new file mode 100644 index 00000000000..5448449019b --- /dev/null +++ b/spec/lib/i18n_converter_spec.rb @@ -0,0 +1,81 @@ +require 'spec_helper' +require 'i18n_converter' + +RSpec.describe I18nConverter do + let(:translation_yml) do + <<~YAML + --- + en: + test: + key: Some string + other_test: + other_key: Other key + some_key: Some key + YAML + end + + let(:translation_xml) do + <<~XML + + + + + Some string + + + Other key + Some key + + + + XML + end + + subject(:converter) { I18nConverter.new(stdin: stdin, stdout: stdout) } + + describe '.yml_to_xml' do + let(:stdout) { StringIO.new } + let(:stdin) { StringIO.new(translation_yml) } + + context 'with a TTY on STDIN' do + let(:stdin) { instance_double('IO', tty?: true) } + + it 'prints an error and exits' do + expect(converter).to receive(:exit).with(1) + + converter.yml_to_xml + + expect(stdout.string.chomp).to eq("Usage: cat en.yml | #{$PROGRAM_NAME} > output.xml") + end + end + + it 'outputs XML' do + converter.yml_to_xml + + expect(stdout.string).to eq(translation_xml) + end + end + + describe '.xml_to_yml' do + let(:stdout) { StringIO.new } + let(:stdin) { StringIO.new(translation_xml) } + + context 'with a TTY on STDIN' do + let(:stdin) { instance_double('IO', tty?: true) } + + it 'prints an error and exits' do + expect(converter).to receive(:exit).with(1) + + converter.xml_to_yml + + expect(stdout.string.chomp).to eq("Usage: cat en.xml | #{$PROGRAM_NAME} > output.yml") + end + end + + it 'outputs YML' do + converter.xml_to_yml + + expect(stdout.string).to eq(translation_yml) + end + end +end