From e42097de446b9015c65fc75e3b9fb17c32a7af07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 01:30:00 +0200 Subject: [PATCH 01/18] Config travis to publish to rubygems on tags --- .travis.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index c6f80ea..6a05389 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,14 @@ language: ruby rvm: - - 1.9.3 - - 1.9.2 - - 1.8.7 - - 2.0.0 \ No newline at end of file +- 1.9.3 +- 1.9.2 +- 1.8.7 +- 2.0.0 +deploy: + provider: rubygems + api_key: + secure: Xjq+v+jEU6wK4BtyfnV1elegNcxK6Ah/O99Sn9c2IlkCmJ1wxLBouqzEiSorSJ4IOMa5H2y3gwo5GXOr6Y7d8huyGrPuBeCSGqAmH77wNCIv7G+jnLiYb1sRZbtKcPW2QaN6JF81qDIelwyspMfo6/ug1qN1x323UaxZl7f7nUE= + gem: csv2strings + on: + tags: true + repo: netbe/CSV-to-iOS-Localizable.strings-converter From 02e2a04753884d27917ba831fe8bc4082c23e3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 01:35:46 +0200 Subject: [PATCH 02/18] Revert "Move coveralls to top of test_helper" This reverts commit 4851fb6c02cdd40d062a5a7ece06e5f0b4a782ef. --- csv2strings.gemspec | 5 +++-- test/test_helper.rb | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/csv2strings.gemspec b/csv2strings.gemspec index 3fac226..6d54e3e 100644 --- a/csv2strings.gemspec +++ b/csv2strings.gemspec @@ -16,11 +16,12 @@ Gem::Specification.new do |s| s.add_dependency "fastercsv" s.add_dependency "nokogiri", "= 1.5.10" end - s.add_dependency "orderedhash", "~> 0.0.6" - s.add_dependency "google_drive", '0.3.6' + s.add_dependency "google_drive", '0.3.6' s.add_development_dependency "rake" + s.add_development_dependency "test-unit" + s.add_development_dependency "simplecov" s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/test/test_helper.rb b/test/test_helper.rb index aefe4ab..dc71b52 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,12 @@ +begin + require 'simplecov' + SimpleCov.start do + add_filter "test" + end +rescue LoadError + puts 'Coverage disabled, enable by installing simplecov' +end + require 'coveralls' Coveralls.wear! From f617a563ae3ebe4125b843bacb4902b4f00fc050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 01:55:01 +0200 Subject: [PATCH 03/18] Moving files to correct gem architecture --- lib/{ => csv2converter}/command.rb | 0 .../commands}/csv2strings_command.rb | 8 +++--- .../commands}/strings2csv_command.rb | 0 lib/{ => csv2converter}/google_doc.rb | 0 lib/csvconverter.rb | 5 ++-- test/{ => csvconverter}/command_test.rb | 3 --- .../csv2strings_test.rb} | 11 +++----- test/{ => csvconverter}/google_doc_test.rb | 0 .../strings2csv_test.rb} | 26 +++++++++---------- 9 files changed, 22 insertions(+), 31 deletions(-) rename lib/{ => csv2converter}/command.rb (100%) rename lib/{ => csv2converter/commands}/csv2strings_command.rb (93%) rename lib/{ => csv2converter/commands}/strings2csv_command.rb (100%) rename lib/{ => csv2converter}/google_doc.rb (100%) rename test/{ => csvconverter}/command_test.rb (95%) rename test/{csv2strings/converter_test.rb => csvconverter/csv2strings_test.rb} (61%) rename test/{ => csvconverter}/google_doc_test.rb (100%) rename test/{strings2csv/converter_test.rb => csvconverter/strings2csv_test.rb} (70%) diff --git a/lib/command.rb b/lib/csv2converter/command.rb similarity index 100% rename from lib/command.rb rename to lib/csv2converter/command.rb diff --git a/lib/csv2strings_command.rb b/lib/csv2converter/commands/csv2strings_command.rb similarity index 93% rename from lib/csv2strings_command.rb rename to lib/csv2converter/commands/csv2strings_command.rb index d1a25f8..d50e002 100644 --- a/lib/csv2strings_command.rb +++ b/lib/csv2converter/commands/csv2strings_command.rb @@ -20,7 +20,7 @@ def csv2strings(filename = nil) help("csv2strings") exit end - + filename ||= options['filename'] if options['fetch'] say "Downloading file from Google Drive" @@ -33,12 +33,12 @@ def csv2strings(filename = nil) help("csv2strings") exit end - + args = options.dup args.delete(:langs) args.delete(:filename) - converter = CSV2Strings::Converter.new(filename, options[:langs], args) - say converter.csv_to_dotstrings + converter = CSV2Strings.new(filename, options[:langs], args) + say converter.csv_to_dotstrings end end diff --git a/lib/strings2csv_command.rb b/lib/csv2converter/commands/strings2csv_command.rb similarity index 100% rename from lib/strings2csv_command.rb rename to lib/csv2converter/commands/strings2csv_command.rb diff --git a/lib/google_doc.rb b/lib/csv2converter/google_doc.rb similarity index 100% rename from lib/google_doc.rb rename to lib/csv2converter/google_doc.rb diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index 4322f11..3295eb9 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -1,5 +1,4 @@ $: << File.expand_path(File.join(File.dirname(__FILE__))) -require 'rubygems' CSVGEM = RUBY_VERSION.match(/^[0-1]\.[0-8]\./) ? 'faster_csv' : 'csv' @@ -12,5 +11,5 @@ end CSVParserClass = CSVGEM == 'csv' ? CSV : FasterCSV -require "csv2strings/converter" -require "strings2csv/converter" +require "csvconverter/csv2strings" +require "csvconverter/strings2csv" diff --git a/test/command_test.rb b/test/csvconverter/command_test.rb similarity index 95% rename from test/command_test.rb rename to test/csvconverter/command_test.rb index 3d1969e..2b9d805 100644 --- a/test/command_test.rb +++ b/test/csvconverter/command_test.rb @@ -1,6 +1,3 @@ -require File.expand_path('../../lib/command', __FILE__) -require File.expand_path('../test_helper', __FILE__) - class CommandTest < Test::Unit::TestCase def test_csv2strings_with_multiple_2_languages command = "./bin/csv2strings" diff --git a/test/csv2strings/converter_test.rb b/test/csvconverter/csv2strings_test.rb similarity index 61% rename from test/csv2strings/converter_test.rb rename to test/csvconverter/csv2strings_test.rb index 46b3600..c1d19d9 100644 --- a/test/csv2strings/converter_test.rb +++ b/test/csvconverter/csv2strings_test.rb @@ -1,11 +1,8 @@ -require File.expand_path('../../../lib/csv2strings/converter', __FILE__) -require File.expand_path('../../test_helper', __FILE__) +class CSV2StringsTest < Test::Unit::TestCase -class CSV2Strings::ConverterTest < Test::Unit::TestCase - def test_converting_csv_to_dotstrings csv_file = "test/data/test_data.csv" - converter = CSV2Strings::Converter.new(csv_file, 'English' => [:en]) + converter = CSV2Strings.new(csv_file, 'English' => [:en]) converter.csv_to_dotstrings assert File.exists?("en.lproj/Localizable.strings"), "the ouptut file does not exist" end @@ -13,10 +10,10 @@ def test_converting_csv_to_dotstrings def test_converting_csv_to_dotstrings_one_output_option csv_file = "test/data/test_data.csv" single_file = 'myApp.strings' - converter = CSV2Strings::Converter.new(csv_file, + converter = CSV2Strings.new(csv_file, {'English' => [:en]}, :output_file => single_file) converter.csv_to_dotstrings assert File.exists?(single_file), "the ouptut file does not exist" end -end \ No newline at end of file +end diff --git a/test/google_doc_test.rb b/test/csvconverter/google_doc_test.rb similarity index 100% rename from test/google_doc_test.rb rename to test/csvconverter/google_doc_test.rb diff --git a/test/strings2csv/converter_test.rb b/test/csvconverter/strings2csv_test.rb similarity index 70% rename from test/strings2csv/converter_test.rb rename to test/csvconverter/strings2csv_test.rb index 6a3612f..b70ce38 100644 --- a/test/strings2csv/converter_test.rb +++ b/test/csvconverter/strings2csv_test.rb @@ -1,7 +1,5 @@ -require File.expand_path('../../../lib/strings2csv/converter', __FILE__) -require File.expand_path('../../test_helper', __FILE__) -class Strings2CSV::ConverterTest < Test::Unit::TestCase +class Strings2CSVTest < Test::Unit::TestCase def test_parse_dotstrings_line_with_good_string input = String.new(<<-EOS) @@ -9,7 +7,7 @@ def test_parse_dotstrings_line_with_good_string EOS expected_output = {"MY_CONSTANT"=>"This is ok"} - output = Strings2CSV::Converter.new.parse_dotstrings_line input + output = Strings2CSV.new.parse_dotstrings_line input assert_equal output, expected_output end @@ -19,7 +17,7 @@ def test_parse_dotstrings_line_with_single_quote EOS expected_output = {"MY_CONSTANT"=>"This 'is' ok"} - output = Strings2CSV::Converter.new.parse_dotstrings_line input + output = Strings2CSV.new.parse_dotstrings_line input assert_equal output, expected_output end @@ -29,7 +27,7 @@ def test_parse_dotstrings_line_with_double_quotes EOS expected_output = {"MY_CONSTANT"=>"This \"is\" ok"} - output = Strings2CSV::Converter.new.parse_dotstrings_line input + output = Strings2CSV.new.parse_dotstrings_line input assert_equal output, expected_output end @@ -38,30 +36,30 @@ def test_parse_dotstrings_line_with_wrong_string "MY_CONSTANT" = "wrong syntax" EOS - output = Strings2CSV::Converter.new.parse_dotstrings_line input + output = Strings2CSV.new.parse_dotstrings_line input assert_nil output, "output should be nil with wrong syntax" end def test_load_strings_with_wrong_file assert_raise(Errno::ENOENT) do - output = Strings2CSV::Converter.new.load_strings "file that does not exist.strings" + output = Strings2CSV.new.load_strings "file that does not exist.strings" end end def test_load_strings expected_output = {"ERROR_HANDLER_WARNING_DISMISS" => "OK", "ANOTHER_STRING" => "hello"} - output = Strings2CSV::Converter.new.load_strings "test/data/test_data.strings" + output = Strings2CSV.new.load_strings "test/data/test_data.strings" assert_equal expected_output, output end def test_load_strings_with_empty_lines assert_nothing_raised do - output = Strings2CSV::Converter.new.load_strings "test/data/test_with_nil.strings" + output = Strings2CSV.new.load_strings "test/data/test_with_nil.strings" end end def test_dotstrings_to_csv - converter = Strings2CSV::Converter.new(:filenames => ["test/data/test_data.strings"]) + converter = Strings2CSV.new(:filenames => ["test/data/test_data.strings"]) keys, strings = converter.dotstrings_to_csv(false) assert_equal ["ERROR_HANDLER_WARNING_DISMISS", "ANOTHER_STRING"], keys expected_strings = {"test/data/test_data.strings" => {"ERROR_HANDLER_WARNING_DISMISS" => "OK", "ANOTHER_STRING" => "hello"}} @@ -73,7 +71,7 @@ def test_create_csv_file filename = "test_data" strings = {filename => {"ERROR_HANDLER_WARNING_DISMISS" => "OK", "ANOTHER_STRING" => "hello"}} - converter = Strings2CSV::Converter.new(:headers => %w{variables english}, :filenames => [filename]) + converter = Strings2CSV.new(:headers => %w{variables english}, :filenames => [filename]) converter.create_csv_file(keys, strings) assert File.exist?(converter.csv_filename) @@ -83,7 +81,7 @@ def test_initialize csv_filename = "file.csv" filenames = %w{"french.strings english.strings"} headers = %w{"constants french english"} - converter = Strings2CSV::Converter.new({ + converter = Strings2CSV.new({ :csv_filename => csv_filename, :headers => headers, :filenames => filenames @@ -95,7 +93,7 @@ def test_initialize end def test_initialize_with_default_values - converter = Strings2CSV::Converter.new + converter = Strings2CSV.new assert_not_nil converter.csv_filename end From 1008de4de4490328099a075db0531ebf4303044f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 01:57:46 +0200 Subject: [PATCH 04/18] Move classes --- lib/{csv2strings/converter.rb => csv2strings.rb} | 0 lib/{strings2csv/converter.rb => strings2csv.rb} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename lib/{csv2strings/converter.rb => csv2strings.rb} (100%) rename lib/{strings2csv/converter.rb => strings2csv.rb} (100%) diff --git a/lib/csv2strings/converter.rb b/lib/csv2strings.rb similarity index 100% rename from lib/csv2strings/converter.rb rename to lib/csv2strings.rb diff --git a/lib/strings2csv/converter.rb b/lib/strings2csv.rb similarity index 100% rename from lib/strings2csv/converter.rb rename to lib/strings2csv.rb From cbbc19a92bfac761e2295ab434ffe1abf8aceb5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 01:58:08 +0200 Subject: [PATCH 05/18] Rename modules to classes --- lib/csv2strings.rb | 219 ++++++++++++++++++++++----------------------- lib/strings2csv.rb | 153 ++++++++++++++++--------------- 2 files changed, 184 insertions(+), 188 deletions(-) diff --git a/lib/csv2strings.rb b/lib/csv2strings.rb index 687d8f6..7836e26 100644 --- a/lib/csv2strings.rb +++ b/lib/csv2strings.rb @@ -1,133 +1,132 @@ -module CSV2Strings - class Converter - attr_accessor :csv_filename, :output_file - attr_accessor :langs, :default_lang - attr_accessor :default_path - attr_accessor :excluded_states, :state_column, :keys_column +class CSV2Strings + attr_accessor :csv_filename, :output_file + attr_accessor :langs, :default_lang + attr_accessor :default_path + attr_accessor :excluded_states, :state_column, :keys_column - def initialize(filename, langs, args = {}) - args.merge!({ - :excluded_states => [], - :state_column => nil, - :keys_column => 0}) + def initialize(filename, langs, args = {}) + args.merge!({ + :excluded_states => [], + :state_column => nil, + :keys_column => 0}) - @csv_filename = filename - @langs = langs + @csv_filename = filename + @langs = langs - if !@langs.is_a?(Hash) || @langs.size == 0 - raise "wrong format or/and languages parameter" + @langs.inspect - end - @output_file = (@langs.size == 1) ? args[:output_file] : nil - - @default_path = args[:default_path].to_s - @excluded_states = args[:excluded_states] - @state_column = args[:state_column] - @keys_column = args[:keys_column] - @default_lang = args[:default_lang] + if !@langs.is_a?(Hash) || @langs.size == 0 + raise "wrong format or/and languages parameter" + @langs.inspect end + @output_file = (@langs.size == 1) ? args[:output_file] : nil - def create_file_from_path(file_path) - path = File.dirname(file_path) - FileUtils.mkdir_p path - return File.new(file_path,"w") - end + @default_path = args[:default_path].to_s + @excluded_states = args[:excluded_states] + @state_column = args[:state_column] + @keys_column = args[:keys_column] + @default_lang = args[:default_lang] + end - def process_header(excludedCols, files, row, index) - files[index] = [] - lang_index = row[index] - - # create output files here - if @output_file - # one single file - files[index] << self.create_file_from_path(@output_file) - else - # create one file for each languages - if self.langs[lang_index].is_a?(Array) + def create_file_from_path(file_path) + path = File.dirname(file_path) + FileUtils.mkdir_p path + return File.new(file_path,"w") + end - self.langs[lang_index].each do |locale| - filename = self.file_path_for_locale(locale) - files[index] << self.create_file_from_path(filename) - end - elsif self.langs[lang_index].is_a?(String) - locale = self.langs[lang_index] + def process_header(excludedCols, files, row, index) + files[index] = [] + lang_index = row[index] + + # create output files here + if @output_file + # one single file + files[index] << self.create_file_from_path(@output_file) + else + # create one file for each languages + if self.langs[lang_index].is_a?(Array) + + self.langs[lang_index].each do |locale| filename = self.file_path_for_locale(locale) files[index] << self.create_file_from_path(filename) - else - raise "wrong format or/and languages parameter" end - + elsif self.langs[lang_index].is_a?(String) + locale = self.langs[lang_index] + filename = self.file_path_for_locale(locale) + files[index] << self.create_file_from_path(filename) + else + raise "wrong format or/and languages parameter" end + end + end + + def file_path_for_locale(locale) + require 'pathname' + Pathname.new(self.default_path) + "#{locale}.lproj" + "Localizable.strings" + end - def file_path_for_locale(locale) - require 'pathname' - Pathname.new(self.default_path) + "#{locale}.lproj" + "Localizable.strings" + def process_value(row_value, default_value) + value = row_value.nil? ? default_value : row_value + value = "" if value.nil? + value.gsub!(/\\*\"/, "\\\"") #escape double quotes + value.gsub!(/\s*(\n|\\\s*n)\s*/, "\\n") #replace new lines with \n + strip + value.gsub!(/%\s+([a-zA-Z@])([^a-zA-Z@]|$)/, "%\\1\\2") #repair string formats ("% d points" etc) + value.gsub!(/([^0-9\s\(\{\[^])%/, "\\1 %") + value.strip! + return value end - - def process_value(row_value, default_value) - value = row_value.nil? ? default_value : row_value - value = "" if value.nil? - value.gsub!(/\\*\"/, "\\\"") #escape double quotes - value.gsub!(/\s*(\n|\\\s*n)\s*/, "\\n") #replace new lines with \n + strip - value.gsub!(/%\s+([a-zA-Z@])([^a-zA-Z@]|$)/, "%\\1\\2") #repair string formats ("% d points" etc) - value.gsub!(/([^0-9\s\(\{\[^])%/, "\\1 %") - value.strip! - return value - end - # Convert csv file to multiple Localizable.strings files for each column - def csv_to_dotstrings(name = self.csv_filename) - files = {} - rowIndex = 0 - excludedCols = [] - defaultCol = 0 - nb_translations = 0 - - CSVParserClass.foreach(name, :quote_char => '"', :col_sep =>',', :row_sep => :auto) do |row| - - if rowIndex == 0 - return unless row.count > 1 #check there's at least two columns - else - next if row == nil or row[self.keys_column].nil? #skip empty lines (or sections) - end + # Convert csv file to multiple Localizable.strings files for each column + def csv_to_dotstrings(name = self.csv_filename) + files = {} + rowIndex = 0 + excludedCols = [] + defaultCol = 0 + nb_translations = 0 + + CSVParserClass.foreach(name, :quote_char => '"', :col_sep =>',', :row_sep => :auto) do |row| + + if rowIndex == 0 + return unless row.count > 1 #check there's at least two columns + else + next if row == nil or row[self.keys_column].nil? #skip empty lines (or sections) + end - row.size.times do |i| - next if excludedCols.include? i - if rowIndex == 0 #header - # ignore all headers not listed in langs to create files - (excludedCols << i and next) unless self.langs.has_key?(row[i]) - self.process_header(excludedCols, files, row, i) - # define defaultCol - defaultCol = i if self.default_lang == row[i] - elsif !self.state_column || (row[self.state_column].nil? or row[self.state_column] == '' or !self.excluded_states.include? row[self.state_column]) - # TODO: add option to strip the constant or referenced language - key = row[self.keys_column].strip - value = self.process_value(row[i], row[defaultCol]) - # files for a given language, i.e could group english US with english UK. - localized_files = files[i] - if localized_files - localized_files.each do |file| - nb_translations += 1 - file.write "\"#{key}\" = \"#{value}\";\n" - end + row.size.times do |i| + next if excludedCols.include? i + if rowIndex == 0 #header + # ignore all headers not listed in langs to create files + (excludedCols << i and next) unless self.langs.has_key?(row[i]) + self.process_header(excludedCols, files, row, i) + # define defaultCol + defaultCol = i if self.default_lang == row[i] + elsif !self.state_column || (row[self.state_column].nil? or row[self.state_column] == '' or !self.excluded_states.include? row[self.state_column]) + # TODO: add option to strip the constant or referenced language + key = row[self.keys_column].strip + value = self.process_value(row[i], row[defaultCol]) + # files for a given language, i.e could group english US with english UK. + localized_files = files[i] + if localized_files + localized_files.each do |file| + nb_translations += 1 + file.write "\"#{key}\" = \"#{value}\";\n" end end end - rowIndex += 1 end - info = "Created #{files.size} files. Content: #{nb_translations} translations\n" - info += "List of created files:\n" - - # closing I/O - files.each do |key,locale_files| - locale_files.each do |file| - info += "#{file.path.to_s}\n" - file.close - end + rowIndex += 1 + end + info = "Created #{files.size} files. Content: #{nb_translations} translations\n" + info += "List of created files:\n" + + # closing I/O + files.each do |key,locale_files| + locale_files.each do |file| + info += "#{file.path.to_s}\n" + file.close end - info - end # end of method + end + info + end # end of method + +end # end of class - end # end of class -end diff --git a/lib/strings2csv.rb b/lib/strings2csv.rb index 1a9b3ba..a0097cd 100644 --- a/lib/strings2csv.rb +++ b/lib/strings2csv.rb @@ -1,102 +1,99 @@ -module Strings2CSV - class Converter - # default_lang is the the column to refer to if a value is missing - # actually default_lang = default_filename - attr_accessor :csv_filename, :headers, :filenames, :default_lang +class Strings2CSV + # default_lang is the the column to refer to if a value is missing + # actually default_lang = default_filename + attr_accessor :csv_filename, :headers, :filenames, :default_lang - def initialize(args = {:filenames => []}) - raise ArgumentError.new("No filenames given") unless args[:filenames] - if args[:headers] - raise ArgumentError.new("number of headers and files don't match, don't forget the constant column") unless args[:headers].size == (args[:filenames].size + 1) - end + def initialize(args = {:filenames => []}) + raise ArgumentError.new("No filenames given") unless args[:filenames] + if args[:headers] + raise ArgumentError.new("number of headers and files don't match, don't forget the constant column") unless args[:headers].size == (args[:filenames].size + 1) + end - @filenames = args[:filenames] + @filenames = args[:filenames] - @csv_filename = args[:csv_filename] || "translations.csv" - @default_lang = args[:default_lang] - @headers = args[:headers] || self.default_headers - end + @csv_filename = args[:csv_filename] || "translations.csv" + @default_lang = args[:default_lang] + @headers = args[:headers] || self.default_headers + end - def default_headers - headers = ["Variables"] - @filenames.each do |fname| - headers << fname - end - headers + def default_headers + headers = ["Variables"] + @filenames.each do |fname| + headers << fname end + headers + end - # Load all strings of a given file - def load_strings(strings_filename) - strings = {} - File.open(strings_filename, 'r') do |strings_file| - strings_file.read.each_line do |line| - hash = self.parse_dotstrings_line(line) - strings.merge!(hash) if hash - end + # Load all strings of a given file + def load_strings(strings_filename) + strings = {} + File.open(strings_filename, 'r') do |strings_file| + strings_file.read.each_line do |line| + hash = self.parse_dotstrings_line(line) + strings.merge!(hash) if hash end - strings end + strings + end - def parse_dotstrings_line(line) - line.strip! - if (line[0] != ?# and line[0] != ?=) - m = line.match(/^[^\"]*\"(.+)\"[^=]+=[^\"]*\"(.*)\";/) - return {m[1] => m[2]} unless m.nil? - end + def parse_dotstrings_line(line) + line.strip! + if (line[0] != ?# and line[0] != ?=) + m = line.match(/^[^\"]*\"(.+)\"[^=]+=[^\"]*\"(.*)\";/) + return {m[1] => m[2]} unless m.nil? end + end - # Convert Localizable.strings files to one CSV file - # output: strings hash has filename for keys and the content of csv - def dotstrings_to_csv(write_to_file = true) - # Parse .strings files - strings = {} - keys = nil - lang_order = [] - - @filenames.each do |fname| - header = fname - strings[header] = load_strings(fname) - keys ||= [] - strings[header].each do |key, value| - keys << key - end - end + # Convert Localizable.strings files to one CSV file + # output: strings hash has filename for keys and the content of csv + def dotstrings_to_csv(write_to_file = true) + # Parse .strings files + strings = {} + keys = nil + lang_order = [] - if(write_to_file) - # Create csv file - puts "Creating #{@csv_filename}" - create_csv_file(keys, strings) - else - return keys, strings + @filenames.each do |fname| + header = fname + strings[header] = load_strings(fname) + keys ||= [] + strings[header].each do |key, value| + keys << key end end - def basename(file_path) - filename = File.basename(file_path) - return filename.split('.')[0].to_sym if file_path + if(write_to_file) + # Create csv file + puts "Creating #{@csv_filename}" + create_csv_file(keys, strings) + else + return keys, strings end + end - # Create the resulting file - def create_csv_file(keys, strings) - raise "csv_filename must not be nil" unless self.csv_filename - CSVParserClass.open(self.csv_filename, "wb") do |csv| - csv << @headers - keys.each do |key| - line = [key] - default_val = strings[self.default_lang][key] if strings[self.default_lang] - @filenames.each do |fname| - lang = fname - current_val = strings[lang][key] - line << ((lang != self.default_lang and current_val == default_val) ? '' : current_val) - end - csv << line + def basename(file_path) + filename = File.basename(file_path) + return filename.split('.')[0].to_sym if file_path + end + + # Create the resulting file + def create_csv_file(keys, strings) + raise "csv_filename must not be nil" unless self.csv_filename + CSVParserClass.open(self.csv_filename, "wb") do |csv| + csv << @headers + keys.each do |key| + line = [key] + default_val = strings[self.default_lang][key] if strings[self.default_lang] + @filenames.each do |fname| + lang = fname + current_val = strings[lang][key] + line << ((lang != self.default_lang and current_val == default_val) ? '' : current_val) end - puts "Done" + csv << line end + puts "Done" end - end -end +end From f3a7affcc8e47a40301da2f1f91bc1f04c5a64c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 01:59:21 +0200 Subject: [PATCH 06/18] Move classes --- lib/{ => csv2converter}/csv2strings.rb | 0 lib/{ => csv2converter}/strings2csv.rb | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename lib/{ => csv2converter}/csv2strings.rb (100%) rename lib/{ => csv2converter}/strings2csv.rb (100%) diff --git a/lib/csv2strings.rb b/lib/csv2converter/csv2strings.rb similarity index 100% rename from lib/csv2strings.rb rename to lib/csv2converter/csv2strings.rb diff --git a/lib/strings2csv.rb b/lib/csv2converter/strings2csv.rb similarity index 100% rename from lib/strings2csv.rb rename to lib/csv2converter/strings2csv.rb From 55dc660604ccb96f029f12366cc107fe6690edb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 02:51:05 +0200 Subject: [PATCH 07/18] FIX lib filder --- lib/{csv2converter => csvconverter}/command.rb | 0 .../commands/csv2strings_command.rb | 0 .../commands/strings2csv_command.rb | 0 lib/{csv2converter => csvconverter}/csv2strings.rb | 0 lib/{csv2converter => csvconverter}/google_doc.rb | 0 lib/{csv2converter => csvconverter}/strings2csv.rb | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename lib/{csv2converter => csvconverter}/command.rb (100%) rename lib/{csv2converter => csvconverter}/commands/csv2strings_command.rb (100%) rename lib/{csv2converter => csvconverter}/commands/strings2csv_command.rb (100%) rename lib/{csv2converter => csvconverter}/csv2strings.rb (100%) rename lib/{csv2converter => csvconverter}/google_doc.rb (100%) rename lib/{csv2converter => csvconverter}/strings2csv.rb (100%) diff --git a/lib/csv2converter/command.rb b/lib/csvconverter/command.rb similarity index 100% rename from lib/csv2converter/command.rb rename to lib/csvconverter/command.rb diff --git a/lib/csv2converter/commands/csv2strings_command.rb b/lib/csvconverter/commands/csv2strings_command.rb similarity index 100% rename from lib/csv2converter/commands/csv2strings_command.rb rename to lib/csvconverter/commands/csv2strings_command.rb diff --git a/lib/csv2converter/commands/strings2csv_command.rb b/lib/csvconverter/commands/strings2csv_command.rb similarity index 100% rename from lib/csv2converter/commands/strings2csv_command.rb rename to lib/csvconverter/commands/strings2csv_command.rb diff --git a/lib/csv2converter/csv2strings.rb b/lib/csvconverter/csv2strings.rb similarity index 100% rename from lib/csv2converter/csv2strings.rb rename to lib/csvconverter/csv2strings.rb diff --git a/lib/csv2converter/google_doc.rb b/lib/csvconverter/google_doc.rb similarity index 100% rename from lib/csv2converter/google_doc.rb rename to lib/csvconverter/google_doc.rb diff --git a/lib/csv2converter/strings2csv.rb b/lib/csvconverter/strings2csv.rb similarity index 100% rename from lib/csv2converter/strings2csv.rb rename to lib/csvconverter/strings2csv.rb From bf74b9768d57520e5225e47dcc997f0d5325bf0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 02:52:19 +0200 Subject: [PATCH 08/18] Revert to default test files convention FIX require directives --- Rakefile | 6 ++++-- lib/csvconverter.rb | 2 -- .../csv2strings_test.rb => test_csv2strings.rb} | 3 ++- test/test_helper.rb | 6 ++---- 4 files changed, 8 insertions(+), 9 deletions(-) rename test/{csvconverter/csv2strings_test.rb => test_csv2strings.rb} (90%) diff --git a/Rakefile b/Rakefile index 7d8ec04..b62924e 100644 --- a/Rakefile +++ b/Rakefile @@ -2,8 +2,10 @@ require 'rake/testtask' Rake::TestTask.new do |t| t.libs << "test" - t.test_files = FileList['test/**/*_test.rb'] + t.test_files = FileList['test/test_*.rb'] + # t.warning = true + t.verbose = true end desc "Run tests" -task :default => :test \ No newline at end of file +task :default => :test diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index 3295eb9..e2497a7 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -1,5 +1,3 @@ -$: << File.expand_path(File.join(File.dirname(__FILE__))) - CSVGEM = RUBY_VERSION.match(/^[0-1]\.[0-8]\./) ? 'faster_csv' : 'csv' begin diff --git a/test/csvconverter/csv2strings_test.rb b/test/test_csv2strings.rb similarity index 90% rename from test/csvconverter/csv2strings_test.rb rename to test/test_csv2strings.rb index c1d19d9..a422872 100644 --- a/test/csvconverter/csv2strings_test.rb +++ b/test/test_csv2strings.rb @@ -1,4 +1,5 @@ -class CSV2StringsTest < Test::Unit::TestCase +require 'test_helper' +class TestCSV2Strings < Test::Unit::TestCase def test_converting_csv_to_dotstrings csv_file = "test/data/test_data.csv" diff --git a/test/test_helper.rb b/test/test_helper.rb index dc71b52..dbbcbf2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,14 +1,12 @@ begin require 'simplecov' SimpleCov.start do - add_filter "test" + add_filter '/test/' end rescue LoadError puts 'Coverage disabled, enable by installing simplecov' end -require 'coveralls' -Coveralls.wear! require 'test/unit' - +require 'csvconverter' From 88fd7ca69b4d1d112ac0756a9d0870d0c0ef1029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 02:55:54 +0200 Subject: [PATCH 09/18] Move strings2csv test --- .../{csvconverter/strings2csv_test.rb => test_strings2csv.rb} | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) rename test/{csvconverter/strings2csv_test.rb => test_strings2csv.rb} (98%) diff --git a/test/csvconverter/strings2csv_test.rb b/test/test_strings2csv.rb similarity index 98% rename from test/csvconverter/strings2csv_test.rb rename to test/test_strings2csv.rb index b70ce38..7f6abae 100644 --- a/test/csvconverter/strings2csv_test.rb +++ b/test/test_strings2csv.rb @@ -1,5 +1,4 @@ - -class Strings2CSVTest < Test::Unit::TestCase +class TestStrings2CSV < Test::Unit::TestCase def test_parse_dotstrings_line_with_good_string input = String.new(<<-EOS) @@ -97,5 +96,4 @@ def test_initialize_with_default_values assert_not_nil converter.csv_filename end - end From c1612df4284e822a8f0c52f485ac194241b4c02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 02:56:31 +0200 Subject: [PATCH 10/18] Remove empty test class for GoogleDoc class --- test/csvconverter/google_doc_test.rb | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 test/csvconverter/google_doc_test.rb diff --git a/test/csvconverter/google_doc_test.rb b/test/csvconverter/google_doc_test.rb deleted file mode 100644 index 44d7ee7..0000000 --- a/test/csvconverter/google_doc_test.rb +++ /dev/null @@ -1,6 +0,0 @@ -require File.expand_path('../../lib/google_doc', __FILE__) -require File.expand_path('../test_helper', __FILE__) - -class GoogleDocTest < Test::Unit::TestCase - # TODO implement test with mocks -end \ No newline at end of file From 05d7a12f56a6e289216fb9837550282b99308a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Fri, 25 Oct 2013 03:01:36 +0200 Subject: [PATCH 11/18] Move tests cases to separate from test_helper --- Rakefile | 2 +- lib/csvconverter.rb | 7 +++++++ test/{ => csvconverter}/test_csv2strings.rb | 0 test/{ => csvconverter}/test_strings2csv.rb | 0 4 files changed, 8 insertions(+), 1 deletion(-) rename test/{ => csvconverter}/test_csv2strings.rb (100%) rename test/{ => csvconverter}/test_strings2csv.rb (100%) diff --git a/Rakefile b/Rakefile index b62924e..773c73a 100644 --- a/Rakefile +++ b/Rakefile @@ -2,7 +2,7 @@ require 'rake/testtask' Rake::TestTask.new do |t| t.libs << "test" - t.test_files = FileList['test/test_*.rb'] + t.test_files = FileList['test/**/test_*.rb'] # t.warning = true t.verbose = true end diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index e2497a7..94e358f 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -1,5 +1,12 @@ CSVGEM = RUBY_VERSION.match(/^[0-1]\.[0-8]\./) ? 'faster_csv' : 'csv' +# if RUBY_VERSION.match(/^[0-1]\.[0-8]\./) +# require "orderedhash" +# ORDERED_HASH_CLASS = OrderedHash +# else +# ORDERED_HASH_CLASS = Hash +# end + begin require CSVGEM rescue LoadError diff --git a/test/test_csv2strings.rb b/test/csvconverter/test_csv2strings.rb similarity index 100% rename from test/test_csv2strings.rb rename to test/csvconverter/test_csv2strings.rb diff --git a/test/test_strings2csv.rb b/test/csvconverter/test_strings2csv.rb similarity index 100% rename from test/test_strings2csv.rb rename to test/csvconverter/test_strings2csv.rb From 9e81e16fa6517ee4a5425d677c73f94dffd0710b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 22:50:22 +0100 Subject: [PATCH 12/18] Modify strings2csv command tests to test class instead of binary --- Rakefile | 2 +- lib/csvconverter.rb | 3 + lib/csvconverter/command.rb | 3 - .../commands/strings2csv_command.rb | 12 ++- .../commands/test_command_strings2csv.rb | 76 +++++++++++++++++++ test/csvconverter/test_strings2csv.rb | 1 + 6 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 test/csvconverter/commands/test_command_strings2csv.rb diff --git a/Rakefile b/Rakefile index 773c73a..316a502 100644 --- a/Rakefile +++ b/Rakefile @@ -2,7 +2,7 @@ require 'rake/testtask' Rake::TestTask.new do |t| t.libs << "test" - t.test_files = FileList['test/**/test_*.rb'] + t.test_files = FileList['test/csvconverter/**/test_*.rb'] # t.warning = true t.verbose = true end diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index 94e358f..0e2b6c1 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -18,3 +18,6 @@ CSVParserClass = CSVGEM == 'csv' ? CSV : FasterCSV require "csvconverter/csv2strings" require "csvconverter/strings2csv" +require "csvconverter/google_doc" + +require "csvconverter/commands/strings2csv_command" diff --git a/lib/csvconverter/command.rb b/lib/csvconverter/command.rb index ac8d62d..40b14ec 100644 --- a/lib/csvconverter/command.rb +++ b/lib/csvconverter/command.rb @@ -1,8 +1,5 @@ -$: << File.expand_path(File.join(File.dirname(__FILE__))) require 'yaml' require 'thor' -require 'csvconverter' -require 'google_doc' class Command < Thor include Thor::Actions diff --git a/lib/csvconverter/commands/strings2csv_command.rb b/lib/csvconverter/commands/strings2csv_command.rb index df372d5..e97a466 100644 --- a/lib/csvconverter/commands/strings2csv_command.rb +++ b/lib/csvconverter/commands/strings2csv_command.rb @@ -1,6 +1,4 @@ -$: << File.expand_path(File.join(File.dirname(__FILE__))) -require "command" - +require "csvconverter/command" class Strings2CSVCommand < Command default_task :strings2csv @@ -15,10 +13,10 @@ def strings2csv unless options.has_key?('filenames') say "No value provided for required options '--filenames'" help("strings2csv") - exit + return end - converter = Strings2CSV::Converter.new(options) + converter = Strings2CSV.new(options) debug_values = converter.dotstrings_to_csv(!options[:dryrun]) - say debug_values.inspect if options[:dryrun] + say debug_values.inspect if options[:dryrun] end -end \ No newline at end of file +end diff --git a/test/csvconverter/commands/test_command_strings2csv.rb b/test/csvconverter/commands/test_command_strings2csv.rb new file mode 100644 index 0000000..a8a79a4 --- /dev/null +++ b/test/csvconverter/commands/test_command_strings2csv.rb @@ -0,0 +1,76 @@ +require 'test_helper' +class TestCommand < Test::Unit::TestCase + def test_strings2csv + options = {:filenames => ["test/data/test_data.strings"]} + Strings2CSVCommand.new([], options).strings2csv + + assert File.exist?("translations.csv") + + #clean up + system("rm -f translations.csv") + end + + def test_strings2csv_with_dryrun_option + options = {:filenames => ["test/data/test_data.strings"], :dryrun => true} + Strings2CSVCommand.new([], options).strings2csv + + assert !File.exist?("translations.csv") + + #clean up + system("rm -f translations.csv") + end + + def test_strings2csv_with_output_file + options = {:filenames => ["test/data/test_data.strings"], :csv_filename => "myfile.csv"} + # -i, -o + Strings2CSVCommand.new([], options).strings2csv + + assert File.exist?("myfile.csv") + + #clean up + system("rm -f myfile.csv") + end + + def test_strings2csv_with_headers + options = {:filenames => ["test/data/test_data.strings"], :headers => ["constants", "english"]} + # -i, -h + Strings2CSVCommand.new([], options).strings2csv + + #TODO assertion or move test on at lib level + + #clean up + system("rm -f translations.csv") + end + + def test_strings2csv_with_two_files + options = { + :filenames => ["test/data/test_en.strings", "test/data/test_fr.strings"], + :headers => %w{Constants English French}, + :csv_filename => "enfr.csv" + } + # --filenames, --headers, -o + Strings2CSVCommand.new([], options).strings2csv + + #TODO assertion + + #clean up + system("rm -f enfr.csv") + end + + def test_strings2csv_with_empty_lines + options = { + :filenames => ["test/data/test_with_nil.strings"], + :csv_filename => "test_with_nil.csv" + } + # -i, -o + + + assert_nothing_raised do + Strings2CSVCommand.new([], options).strings2csv + end + assert system("diff test_with_nil.csv test/data/test_with_nil.csv"), "no difference on output" + + #clean up + system("rm -f test_with_nil.csv") + end +end diff --git a/test/csvconverter/test_strings2csv.rb b/test/csvconverter/test_strings2csv.rb index 7f6abae..f9f55f4 100644 --- a/test/csvconverter/test_strings2csv.rb +++ b/test/csvconverter/test_strings2csv.rb @@ -1,3 +1,4 @@ +require 'test_helper' class TestStrings2CSV < Test::Unit::TestCase def test_parse_dotstrings_line_with_good_string From 2e99c40e12cfd85e5bcd5f3eb985fe8e7ea7401a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 23:02:16 +0100 Subject: [PATCH 13/18] Modify csv2strings command tests to test class CSV2StringsCommand --- lib/csvconverter.rb | 2 + .../commands/csv2strings_command.rb | 3 +- test/csvconverter/command_test.rb | 100 ------------------ .../commands/test_command_csv2strings.rb | 36 +++++++ .../commands/test_command_strings2csv.rb | 1 + 5 files changed, 40 insertions(+), 102 deletions(-) delete mode 100644 test/csvconverter/command_test.rb create mode 100644 test/csvconverter/commands/test_command_csv2strings.rb diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index 0e2b6c1..8fca59a 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -20,4 +20,6 @@ require "csvconverter/strings2csv" require "csvconverter/google_doc" +# commands require "csvconverter/commands/strings2csv_command" +require "csvconverter/commands/csv2strings_command" diff --git a/lib/csvconverter/commands/csv2strings_command.rb b/lib/csvconverter/commands/csv2strings_command.rb index d50e002..74d6c18 100644 --- a/lib/csvconverter/commands/csv2strings_command.rb +++ b/lib/csvconverter/commands/csv2strings_command.rb @@ -1,5 +1,4 @@ -$: << File.expand_path(File.join(File.dirname(__FILE__))) -require "command" +require "csvconverter/command" class CSV2StringsCommand < Command default_task :csv2strings diff --git a/test/csvconverter/command_test.rb b/test/csvconverter/command_test.rb deleted file mode 100644 index 2b9d805..0000000 --- a/test/csvconverter/command_test.rb +++ /dev/null @@ -1,100 +0,0 @@ -class CommandTest < Test::Unit::TestCase - def test_csv2strings_with_multiple_2_languages - command = "./bin/csv2strings" - command += " --filename test/data/test_data_multiple_langs.csv" - command += " --langs=English:en French:fr" - system(command) - - assert File.exist?("./en.lproj/Localizable.strings") - assert File.exist?("./fr.lproj/Localizable.strings") - - #clean up - system("rm -rf ./en.lproj/") - system("rm -rf ./fr.lproj/") - end - - def test_csv2strings_with_default_path - command = "./bin/csv2strings" - command += " --filename test/data/test_data_multiple_langs.csv" - command += " --langs=English:en French:fr" - command += " --default_path=mynewlocation" - system(command) - - # testing - assert File.exist?("./mynewlocation/en.lproj/Localizable.strings"), "can't find output file for English" - assert File.exist?("./mynewlocation/fr.lproj/Localizable.strings"), "can't find output file for French" - - #clean up - system("rm -rf ./mynewlocation") - end - - def test_strings2csv - command = "./bin/strings2csv" - command += " --filenames test/data/test_data.strings" - system(command) - - assert File.exist?("translations.csv") - - #clean up - system("rm -f translations.csv") - end - - def test_strings2csv_with_dryrun_option - command = "./bin/strings2csv" - command += " --filenames test/data/test_data.strings" - command += " --dryrun" - system(command) - - assert !File.exist?("translations.csv") - - #clean up - system("rm -f translations.csv") - end - - def test_strings2csv_with_output_file - command = "./bin/strings2csv" - command += " -i=test/data/test_data.strings" - command += " -o=myfile.csv" - system(command) - - assert File.exist?("myfile.csv") - - #clean up - system("rm -f myfile.csv") - end - - def test_strings2csv_with_headers - command = "./bin/strings2csv" - command += " -i=test/data/test_data.strings" - command += " -h=constants english" - system(command) - - #TODO assertion or move test on at lib level - - #clean up - system("rm -f translations.csv") - end - - def test_strings2csv_with_two_files - command = "./bin/strings2csv" - command += " --filenames=test/data/test_en.strings test/data/test_fr.strings" - command += " --headers=Constants English French" - command += " -o=enfr.csv" - system(command) - end - - def test_strings2csv_with_empty_lines - command = "./bin/strings2csv" - command += " -i=test/data/test_with_nil.strings" - command += " -o=test_with_nil.csv" - assert_nothing_raised do - system(command) - end - assert system("diff test_with_nil.csv test/data/test_with_nil.csv"), "no difference on output" - - #clean up - system("rm -f test_with_nil.csv") - end - -end - diff --git a/test/csvconverter/commands/test_command_csv2strings.rb b/test/csvconverter/commands/test_command_csv2strings.rb new file mode 100644 index 0000000..98ba446 --- /dev/null +++ b/test/csvconverter/commands/test_command_csv2strings.rb @@ -0,0 +1,36 @@ +require 'test_helper' +class TestCommand < Test::Unit::TestCase + + def test_csv2strings_with_multiple_2_languages + options = { + :filename => "test/data/test_data_multiple_langs.csv", + :langs => {"English" => "en", "French" => "fr"} + } + CSV2StringsCommand.new([], options).csv2strings + + assert File.exist?("./en.lproj/Localizable.strings") + assert File.exist?("./fr.lproj/Localizable.strings") + + #clean up + system("rm -rf ./en.lproj/") + system("rm -rf ./fr.lproj/") + end + + def test_csv2strings_with_default_path + options = { + :filename => "test/data/test_data_multiple_langs.csv", + :langs => {"English" => "en", "French" => "fr"}, + :default_path => "mynewlocation" + } + CSV2StringsCommand.new([], options).csv2strings + + # testing + assert File.exist?("./mynewlocation/en.lproj/Localizable.strings"), "can't find output file for English" + assert File.exist?("./mynewlocation/fr.lproj/Localizable.strings"), "can't find output file for French" + + #clean up + system("rm -rf ./mynewlocation") + end + +end + diff --git a/test/csvconverter/commands/test_command_strings2csv.rb b/test/csvconverter/commands/test_command_strings2csv.rb index a8a79a4..a12652e 100644 --- a/test/csvconverter/commands/test_command_strings2csv.rb +++ b/test/csvconverter/commands/test_command_strings2csv.rb @@ -1,5 +1,6 @@ require 'test_helper' class TestCommand < Test::Unit::TestCase + def test_strings2csv options = {:filenames => ["test/data/test_data.strings"]} Strings2CSVCommand.new([], options).strings2csv From 52fe7838b375021e9bbc6accecbf516658bd8edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 23:20:21 +0100 Subject: [PATCH 14/18] Fix load paths, remove commands from being required in lib --- bin/csv2strings | 4 ++-- bin/strings2csv | 4 ++-- lib/csvconverter.rb | 4 ---- test/test_helper.rb | 3 +++ 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/bin/csv2strings b/bin/csv2strings index 7c3d403..38b7f28 100755 --- a/bin/csv2strings +++ b/bin/csv2strings @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -require File.expand_path('../../lib/csv2strings_command', __FILE__) - +$: << File.expand_path("../lib/", __FILE__) +require 'csvconverter/commands/csv2strings_command' CSV2StringsCommand.start diff --git a/bin/strings2csv b/bin/strings2csv index a195a8d..806291c 100755 --- a/bin/strings2csv +++ b/bin/strings2csv @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -require File.expand_path('../../lib/strings2csv_command', __FILE__) - +$: << File.expand_path("../lib/", __FILE__) +require 'csvconverter/commands/strings2csv_command' Strings2CSVCommand.start diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index 8fca59a..55022ae 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -19,7 +19,3 @@ require "csvconverter/csv2strings" require "csvconverter/strings2csv" require "csvconverter/google_doc" - -# commands -require "csvconverter/commands/strings2csv_command" -require "csvconverter/commands/csv2strings_command" diff --git a/test/test_helper.rb b/test/test_helper.rb index dbbcbf2..c024954 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,4 +9,7 @@ require 'test/unit' + require 'csvconverter' +require "csvconverter/commands/strings2csv_command" +require "csvconverter/commands/csv2strings_command" From 20fe51e5957f949735f7dbb42b8ed1216e67363d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 23:23:15 +0100 Subject: [PATCH 15/18] Update ignore files --- .gitignore | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 614a023..916742b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,16 @@ +# Generated files *.csv *.strings *.lproj +*.gem *~ *# -*.gem -*.sublime-project #*# + +# Config file .csvconverter + +# SimpleCov coverage # ignore Gemfile.lock as http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/ @@ -17,3 +21,7 @@ Gemfile.lock .ruby-version .rbenv-version .rvmrc + +# Sublime Text +*.sublime-workspace +*.sublime-project From df55bc5088d38fb8eb6cc3dfae93b328a24a884a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 23:31:35 +0100 Subject: [PATCH 16/18] ADD coveralls --- test/test_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index c024954..9c6100b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,8 @@ begin require 'simplecov' + require 'coveralls' + + SimpleCov.formatter = Coveralls::SimpleCov::Formatter SimpleCov.start do add_filter '/test/' end @@ -7,7 +10,6 @@ puts 'Coverage disabled, enable by installing simplecov' end - require 'test/unit' require 'csvconverter' From c49853babe9c8c31978704d9cd830c78ffe1fecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 23:50:00 +0100 Subject: [PATCH 17/18] Using OrderedHash in ruby 1.8.7 to keep right order for strings2csv conversion --- csv2strings.gemspec | 1 + lib/csvconverter.rb | 12 ++++++------ lib/csvconverter/strings2csv.rb | 7 ++----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/csv2strings.gemspec b/csv2strings.gemspec index 6d54e3e..20c7233 100644 --- a/csv2strings.gemspec +++ b/csv2strings.gemspec @@ -15,6 +15,7 @@ Gem::Specification.new do |s| if RUBY_VERSION < '1.9' s.add_dependency "fastercsv" s.add_dependency "nokogiri", "= 1.5.10" + s.add_dependency "orderedhash" end s.add_dependency "google_drive", '0.3.6' diff --git a/lib/csvconverter.rb b/lib/csvconverter.rb index 55022ae..1843e04 100644 --- a/lib/csvconverter.rb +++ b/lib/csvconverter.rb @@ -1,11 +1,11 @@ CSVGEM = RUBY_VERSION.match(/^[0-1]\.[0-8]\./) ? 'faster_csv' : 'csv' -# if RUBY_VERSION.match(/^[0-1]\.[0-8]\./) -# require "orderedhash" -# ORDERED_HASH_CLASS = OrderedHash -# else -# ORDERED_HASH_CLASS = Hash -# end +if RUBY_VERSION.match(/^[0-1]\.[0-8]\./) + require "orderedhash" + ORDERED_HASH_CLASS = OrderedHash +else + ORDERED_HASH_CLASS = Hash +end begin require CSVGEM diff --git a/lib/csvconverter/strings2csv.rb b/lib/csvconverter/strings2csv.rb index a0097cd..c1c1f1f 100644 --- a/lib/csvconverter/strings2csv.rb +++ b/lib/csvconverter/strings2csv.rb @@ -27,7 +27,7 @@ def default_headers # Load all strings of a given file def load_strings(strings_filename) - strings = {} + strings = ORDERED_HASH_CLASS.new File.open(strings_filename, 'r') do |strings_file| strings_file.read.each_line do |line| hash = self.parse_dotstrings_line(line) @@ -57,10 +57,7 @@ def dotstrings_to_csv(write_to_file = true) @filenames.each do |fname| header = fname strings[header] = load_strings(fname) - keys ||= [] - strings[header].each do |key, value| - keys << key - end + keys ||= strings[header].keys end if(write_to_file) From e057675a2ddb48f5060016f07cebc556ffd2f7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Benaiteau?= Date: Tue, 29 Oct 2013 23:57:27 +0100 Subject: [PATCH 18/18] Set Coveralls not to be installed on 1.8 --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index dfc4c69..ecbb687 100644 --- a/Gemfile +++ b/Gemfile @@ -4,5 +4,5 @@ source "http://rubygems.org" gemspec group :test do - gem 'coveralls', :require => false + gem 'coveralls', :require => false, :platforms => [:ruby_19, :ruby_20] end