Skip to content

Commit

Permalink
Add import command
Browse files Browse the repository at this point in the history
  • Loading branch information
aschuch committed Mar 15, 2016
1 parent bfef031 commit e5724cf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
19 changes: 19 additions & 0 deletions bin/fame
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require 'commander'
require 'colorize'
require 'fame/version'
require 'fame/interface_builder'
require 'fame/xliff_import'
require 'fame/xliff_export'

#
Expand All @@ -21,6 +22,24 @@ class FameApplication
program :description, 'Delightful localization of .storyboard and .xib files, right within Interface Builder. The fame CLI exports .xliff files based on the settings provided in Interface Builder files.'
default_command :export

#
# Import
#
command :import do |c|
c.syntax = 'fame import --project /path/to/xcodeproj [options]'
c.description = 'Imports all given .xliff files into the given Xcode project.'
c.option '--project STRING', String, 'Path to an Xcode project that should be localized'
c.option '--xliff-path STRING', String, 'Path to an .xliff file or a folder that contains .xliff files to be imported.'

c.action do |args, options|
options.default :xliff_path => '.'

# Import .xliff files
xliff = Fame::XliffImport.new(options.project)
xliff.import(options.xliff_path)
end
end

#
# Export
#
Expand Down
83 changes: 83 additions & 0 deletions lib/fame/xliff_import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'colorize' # colorful console output
require_relative 'xcode_project'

module Fame
# Handles import and export of .xliff files
class XliffImport
# All accepted XLIFF file types
ACCEPTED_FILE_TYPES = [".xliff"].freeze

#
# Initializer
# @param xcode_proj_path A path to a .xcodeproj file whose contents should be localized.
#
def initialize(xcode_proj_path)
@xcode_proj = XcodeProject.new(xcode_proj_path)
end

#
# Imports all .xliff files at the given path into the current Xcode project
# @param path A folder of .xliff files that should be imported into the current Xcode project.
#
def import(path)
xliffs = determine_xliff_files!(path)
puts "Found #{xliffs.count} xliff file(s) -> #{xliffs.map { |x| File.basename(x, '.*') }}".light_black

errors = []
xliffs.each_with_index do |xliff, index|
language = File.basename(xliff, '.*')
puts "(#{index+1}/#{xliffs.count}) [#{language}] Importing #{xliff}".blue

# may result in the following error:
# xcodebuild: error: Importing localizations from en.xliff will make changes to Example. Import with xcodebuild can only modify existing strings files.
output = `xcodebuild -importLocalizations -localizationPath #{xliff} -project #{@xcode_proj.xcode_proj_path} 2>&1`
error = output.split("\n").grep(/^xcodebuild: error: Importing localizations/i)
errors << error
end

report_result(errors)
end

private

#
# Searches the given path for .xliff files and returns their paths.
# @param path The path that should be searched for .xliff files.
# @return [Array<String>] An array of paths to .xliff files.
#
def determine_xliff_files!(path)
raise "[XliffImport] The provided file or folder does not exist" unless File.exist? path

if File.directory?(path)
files = Dir.glob(path + "/**/*{#{ACCEPTED_FILE_TYPES.join(',')}}")
raise "[XliffImport] The provided folder did not contain any XLIFF files (#{ACCEPTED_FILE_TYPES.join(', ')})" unless files.count > 0
return files
else
raise "[XliffImport] The provided file is not an XLIFF (#{ACCEPTED_FILE_TYPES.join(', ')})" unless ACCEPTED_FILE_TYPES.include? File.extname(path)
return [path]
end
end

#
# Prints the result of the import
#
def report_result(errors)
# handle errors
if errors.count > 0
puts "\n✘ XLIFF import failed (#{errors.count} error(s))\n".red
puts errors
help = "\nOoops! xcodebuild cannot import one or more of the provided .xliff file(s) because the necessary .strings files do not exist yet.\n\n" +
"Here's how to fix it:\n" +
" 1. Open Xcode, select the project root (blue icon)\n" +
" 2. Choose Editor > Import Localizations...\n" +
" 3. Repeat steps 1 and 2 for every localization\n\n" +
"Don't worry, you only have to do this manually once.\n" +
"After the initial Xcode import, this command will be able to import your xliff files."
puts help.blue
else
puts "\n✔︎ Done importing XLIFFs\n".green
end
end

end
end

0 comments on commit e5724cf

Please sign in to comment.