Skip to content

Commit

Permalink
Merge branch 'develop' and Update Readme
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
  • Loading branch information
netbe committed Oct 15, 2013
2 parents 72fe4a3 + 229ad57 commit b4264ec
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 82 deletions.
31 changes: 25 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
PATH
remote: .
specs:
csv2strings (0.2)
csv2strings (0.2.1)
google_drive (= 0.3.6)
nokogiri (= 1.5.10)
thor

GEM
remote: http://rubygems.org/
specs:
metaclass (0.0.1)
mocha (0.10.5)
metaclass (~> 0.0.1)
faraday (0.8.8)
multipart-post (~> 1.2.0)
google_drive (0.3.6)
nokogiri (>= 1.4.4, != 1.5.2, != 1.5.1)
oauth (>= 0.3.6)
oauth2 (>= 0.5.0)
httpauth (0.2.0)
jwt (0.1.6)
multi_json (>= 1.0)
multi_json (1.3.6)
multi_xml (0.5.5)
multipart-post (1.2.0)
nokogiri (1.5.10)
oauth (0.4.7)
oauth2 (0.9.2)
faraday (~> 0.8)
httpauth (~> 0.2)
jwt (~> 0.1.4)
multi_json (~> 1.0)
multi_xml (~> 0.5)
rack (~> 1.2)
rack (1.5.2)
rake (0.9.2.2)
simplecov (0.6.4)
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
test-unit (2.4.8)
thor (0.17.0)
thor (0.18.1)

PLATFORMS
ruby

DEPENDENCIES
csv2strings!
mocha
rake
simplecov
test-unit
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ Run `bundle install` to install all the dependencies. Tests are done with `Test:
# Todo

See GitHub isssues

# Known issues

None
10 changes: 2 additions & 8 deletions bin/csv2strings
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/usr/bin/env ruby
require File.expand_path('../../lib/command', __FILE__)
require File.expand_path('../../lib/csv2strings_command', __FILE__)


if ARGV.count == 1 && ARGV[0] == "help"
Command.start(["help", "csv2strings"],{})
else
args = ["csv2strings"]
args += ARGV
Command.start(args,{})
end
CSV2StringsCommand.start
11 changes: 3 additions & 8 deletions bin/strings2csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/usr/bin/env ruby
require File.expand_path('../../lib/command', __FILE__)
require File.expand_path('../../lib/strings2csv_command', __FILE__)

if ARGV.count == 1 && ARGV[0] == "help"
Command.start(["help", "strings2csv"],{})
else
args = ["strings2csv"]
args += ARGV
Command.start(args,{})
end

Strings2CSVCommand.start
17 changes: 12 additions & 5 deletions csv2strings.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
Gem::Specification.new do |s|
s.name = 'csv2strings'
s.version = '0.2.0'
s.date = '2013-09-02'
s.version = '0.2.1'
s.date = '2013-10-08'
s.summary = "CSV to iOS Localizable.strings converter"
s.description = "ruby script converts a CSV file of translations to Localizable.strings files and vice-versa"
s.authors = ["François Benaiteau"]
s.email = '[email protected]'
s.homepage = 'https://github.com/netbe/CSV-to-iOS-Localizable.strings-converter'
s.license = 'MIT'

s.add_dependency "thor"
s.add_dependency "fastercsv", :require => 'faster_csv' if RUBY_PLATFORM == 'ruby_19'
s.add_dependency "csv", :require => 'csv' if RUBY_PLATFORM == 'ruby_18'


if RUBY_VERSION < '1.9'
s.add_dependency "fastercsv"
end
s.add_dependency "nokogiri", "= 1.5.10"
s.add_dependency "google_drive", '0.3.6'
s.add_development_dependency "rake"
s.add_development_dependency "mocha"

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")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
Expand Down
66 changes: 21 additions & 45 deletions lib/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,31 @@
require 'yaml'
require 'thor'
require 'csvconverter'
require 'google_doc'

class Command < Thor
include Thor::Actions
class_option :verbose, :type => :boolean

desc "CSV_FILENAME", "convert CSV file to '.strings' file"
# required options but handled in method because of options read from yaml file
method_option :filename, :type => :string, :desc => "CSV file (CSV_FILENAME) to convert from"
method_option :langs, :type => :hash, :aliases => "-L", :desc => "Languages to convert"
# optional options
method_option :excluded_states, :type => :array, :aliases => "-x", :desc => "Exclude rows with given state"
method_option :state_column, :type => :numeric, :aliases => "-s", :desc => "Position of column for state if any"
method_option :keys_column, :type => :numeric, :aliases => "-k", :desc => "Position of column for keys"
method_option :default_lang, :type => :string, :aliases => "-l", :desc => "Default language to use for empty values if any"
method_option :default_path, :type => :string, :aliases => "-p", :desc => "Path of output files"
def csv2strings(filename = nil)
unless filename || options.has_key?('filename')
puts "No value provided for required options '--filename'"
help("csv2strings")
exit
desc "csv_download", "Download Google Spreadsheet containing translations"
method_option :gd_filename, :type => :string, :required => :true, :desc => "File to download from Google Drive"
method_option :output_filename, :type => :string, :desc => "Filepath of downloaded file"
def csv_download
filename = options['gd_filename']
gd = GoogleDoc.new
if options['output_filename']
file_path = gd.download filename.to_s, options['output_filename']
else
file_path = gd.download filename.to_s
end
unless options.has_key?('langs')
puts "No value provided for required options '--langs'"
help("csv2strings")
exit
if file_path
say "File '#{filename}' downloaded to '#{file_path}'"
else
say "Could not download the requested file: #{filename}"
end
filename ||= options['filename']
args = options.dup
args.delete(:langs)
args.delete(:filename)
converter = CSV2Strings::Converter.new(filename, options[:langs], args)
converter.csv_to_dotstrings
file_path
end

desc "FILENAMES", "convert '.strings' files to CSV file"
# required options but handled in method because of options read from yaml file
method_option :filenames, :type => :array, :aliases => "-i", :desc => "location of strings files (FILENAMES)"
# optional options
method_option :csv_filename, :type => :string, :aliases => "-o", :desc => "location of output file"
method_option :headers, :type => :array, :aliases => "-h", :desc => "override headers of columns, default is name of input files and 'Variables' for reference"
method_option :dryrun, :type => :boolean, :aliases => "-n", :desc => "prints out content of hash without writing file"
def strings2csv
unless options.has_key?('filenames')
puts "No value provided for required options '--filenames'"
help("strings2csv")
exit
end
converter = Strings2CSV::Converter.new(options)
debug_values = converter.dotstrings_to_csv(!options[:dryrun])
puts debug_values.inspect if options[:dryrun]
end


private
def options
original_options = super
Expand All @@ -60,7 +35,8 @@ def options
# add default values for options here
defaults["csv_filename"] = "translations.csv" unless defaults.has_key?("csv_filename")
defaults["dryrun"] = false unless defaults.has_key?("dryrun")
defaults["fetch"] = false unless defaults.has_key?("fetch")
defaults["keys_column"] = 0 unless defaults.has_key?("keys_column")
Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options))
end
end
end
9 changes: 6 additions & 3 deletions lib/csv2strings/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(filename, langs, args = {})
@langs = langs

if !@langs.is_a?(Hash) || @langs.size == 0
raise "wrong format or/and languages parameter"
raise "wrong format or/and languages parameter" + @langs.inspect
end
@output_file = (@langs.size == 1) ? args[:output_file] : nil

Expand Down Expand Up @@ -83,6 +83,7 @@ def csv_to_dotstrings(name = self.csv_filename)
excludedCols = []
defaultCol = 0
nb_translations = 0

CSVParserClass.foreach(name, :quote_char => '"', :col_sep =>',', :row_sep => :auto) do |row|

if rowIndex == 0
Expand Down Expand Up @@ -115,15 +116,17 @@ def csv_to_dotstrings(name = self.csv_filename)
end
rowIndex += 1
end
puts "\n>>>Created #{files.size} files. Content: #{nb_translations} translations\n"
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
end

info
end # end of method

end # end of class
Expand Down
44 changes: 44 additions & 0 deletions lib/csv2strings_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
$: << File.expand_path(File.join(File.dirname(__FILE__)))
require "command"
class CSV2StringsCommand < Command
default_task :csv2strings

desc "CSV_FILENAME", "convert CSV file to '.strings' file"
# required options but handled in method because of options read from yaml file
method_option :filename, :type => :string, :desc => "CSV file (CSV_FILENAME) to convert from or name of file in Google Drive"
method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive"
method_option :langs, :type => :hash, :aliases => "-L", :desc => "Languages to convert"
# optional options
method_option :excluded_states, :type => :array, :aliases => "-x", :desc => "Exclude rows with given state"
method_option :state_column, :type => :numeric, :aliases => "-s", :desc => "Position of column for state if any"
method_option :keys_column, :type => :numeric, :aliases => "-k", :desc => "Position of column for keys"
method_option :default_lang, :type => :string, :aliases => "-l", :desc => "Default language to use for empty values if any"
method_option :default_path, :type => :string, :aliases => "-p", :desc => "Path of output files"
def csv2strings(filename = nil)
unless filename || options.has_key?('filename')
say "No value provided for required options '--filename'"
help("csv2strings")
exit
end

filename ||= options['filename']
if options['fetch']
say "Downloading file from Google Drive"
filename = invoke :csv_download, nil, {"gd_filename" => filename}
exit unless filename
end

unless options.has_key?('langs')
say "No value provided for required options '--langs'"
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
end

end
25 changes: 25 additions & 0 deletions lib/google_doc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "google_drive"
class GoogleDoc
attr_accessor :session

def authenticate
# will try to get token from ~/.ruby_google_drive.token
@session = GoogleDrive.saved_session
end

def download(requested_filename, output_filename = "translations.csv")
unless @session
self.authenticate
end
result = @session.file_by_title(requested_filename)
if result.is_a? Array
file = result.first
else
file = result
end
return nil unless file
file.export_as_file(output_filename, "csv")
return output_filename
end

end
24 changes: 24 additions & 0 deletions lib/strings2csv_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$: << File.expand_path(File.join(File.dirname(__FILE__)))
require "command"

class Strings2CSVCommand < Command
default_task :strings2csv

desc "FILENAMES", "convert '.strings' files to CSV file"
# required options but handled in method because of options read from yaml file
method_option :filenames, :type => :array, :aliases => "-i", :desc => "location of strings files (FILENAMES)"
# optional options
method_option :csv_filename, :type => :string, :aliases => "-o", :desc => "location of output file"
method_option :headers, :type => :array, :aliases => "-h", :desc => "override headers of columns, default is name of input files and 'Variables' for reference"
method_option :dryrun, :type => :boolean, :aliases => "-n", :desc => "prints out content of hash without writing file"
def strings2csv
unless options.has_key?('filenames')
say "No value provided for required options '--filenames'"
help("strings2csv")
exit
end
converter = Strings2CSV::Converter.new(options)
debug_values = converter.dotstrings_to_csv(!options[:dryrun])
say debug_values.inspect if options[:dryrun]
end
end
12 changes: 6 additions & 6 deletions test/command_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'test_helper'
require File.expand_path('../../lib/command', __FILE__)
require File.expand_path('../test_helper', __FILE__)

require 'command'
class CommandTest < Test::Unit::TestCase
def test_csv2strings_with_multiple_2_languages
command = "./bin/csv2strings"
command += " test/data/test_data_multiple_langs.csv"
command += " --filename test/data/test_data_multiple_langs.csv"
command += " --langs=English:en French:fr"
system(command)

Expand All @@ -18,7 +18,7 @@ def test_csv2strings_with_multiple_2_languages

def test_csv2strings_with_default_path
command = "./bin/csv2strings"
command += " test/data/test_data_multiple_langs.csv"
command += " --filename test/data/test_data_multiple_langs.csv"
command += " --langs=English:en French:fr"
command += " --default_path=mynewlocation"
system(command)
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_strings2csv_with_output_file
end

def test_strings2csv_with_headers
command = "./bin/csv2strings strings2csv"
command = "./bin/strings2csv"
command += " -i=test/data/test_data.strings"
command += " -h=constants english"
system(command)
Expand All @@ -79,7 +79,7 @@ def test_strings2csv_with_headers
end

def test_strings2csv_with_two_files
command = "./bin/csv2strings strings2csv"
command = "./bin/strings2csv"
command += " --filenames=test/data/test_en.strings test/data/test_fr.strings"
command += " --headers=Constants English French"
command += " -o=enfr.csv"
Expand Down
6 changes: 6 additions & 0 deletions test/google_doc_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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
Loading

0 comments on commit b4264ec

Please sign in to comment.