Module: SmartlingXcode
- Defined in:
- lib/smartling_xcode.rb,
lib/smartling_xcode/xcode.rb,
lib/smartling_xcode/backend.rb,
lib/smartling_xcode/version.rb
Constant Summary collapse
- CREDFILE =
'./.smartling.json'
- VERSION =
"0.1.2"
Class Method Summary collapse
- .execute(command, project_path, version) ⇒ Object
- .extract(project) ⇒ Object
-
.fromBase(path) ⇒ Object
Returns true if the file is from Base localization or unlocalized.
- .getVersion(project) ⇒ Object
- .loadCredentials ⇒ Object
- .openProject(path) ⇒ Object
- .pushStringsFromFiles(files, version, creds) ⇒ Object
- .requestCredentials ⇒ Object
Class Method Details
.execute(command, project_path, version) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/smartling_xcode.rb', line 6 def self.execute(command, project_path, version) # Execute commands case command when 'init' requestCredentials return 0 when 'extract' files = extract(openProject(project_path)) puts "\nOutput:" puts files return 0 when 'push' creds = loadCredentials project = openProject(project_path) files = extract(project) if version.nil? version = getVersion(project) end pushStringsFromFiles(files, version, creds) puts "\nYour files are now available in the Files section of your Smartling project." return 0 else return 1 end end |
.extract(project) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/smartling_xcode/xcode.rb', line 34 def self.extract(project) strings_files = [] # Loop on IB files from Base localization project.files.select{|f| f.path.end_with?(".xib", ".storyboard") && fromBase(f.path) }.each do |f| output_name = f.path.gsub(/[\/\.]/, '_') output_path = "/tmp/#{output_name}.strings" puts "Extracting strings from #{f.path} into #{output_path}" Kernel.system("ibtool --generate-strings-file \"#{output_path}\" \"#{f.real_path}\"") strings_files.push(output_path) end # Loop on .strings files project.files.select{|f| f.path.end_with?(".strings") && fromBase(f.path) }.each do |f| strings_files.push(f.real_path) end return strings_files end |
.fromBase(path) ⇒ Object
Returns true if the file is from Base localization or unlocalized
26 27 28 29 30 31 32 |
# File 'lib/smartling_xcode/xcode.rb', line 26 def self.fromBase(path) if path.include?('.lproj') return path.include?('Base.lproj') else return true end end |
.getVersion(project) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/smartling_xcode/xcode.rb', line 58 def self.getVersion(project) versions = [] project.files.select{|f| f.path.end_with?(".plist")}.each do |f| contents = Xcodeproj::Plist.read_from_path(f.real_path) if contents['CFBundleShortVersionString'] versions.push({:version => contents['CFBundleShortVersionString'], :file => f.path}) end end case versions.count when 0 puts "No app version found in project.\nPlease enter a version number:" version = STDIN.gets.chomp return version when 1 return versions.first[:version] else puts "Multiple Info.plist files found." versions.each do |v, i| puts "#{i}. #{v[:version]} from #{:file}" end puts "Please select which version number to use:" choice = nil while choice.nil? choice = STDIN.gets.chomp.to_i if choice < versions.count return versions[choice][:version] end choice = nil end end end |
.loadCredentials ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/smartling_xcode/backend.rb', line 43 def self.loadCredentials if File.exist?(CREDFILE) file = File.read(CREDFILE) creds = JSON.parse(file) if creds['api_key'].nil? || creds['project_id'].nil? Kernel.abort("⚠️ Invalid credentials") else return creds end else Kernel.abort("⚠️ Credentials not found. Please run smartling_xcode init.") end end |
.openProject(path) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/smartling_xcode/xcode.rb', line 4 def self.openProject(path) # Get .xcodeproj path if path.nil? project_file = Dir.entries('./').select {|f| f.end_with?(".xcodeproj") }.first path = "./#{project_file}" end if !path.nil? && File.exist?(path) # Open project begin project = Xcodeproj::Project.open(path) rescue Kernel.abort("⚠️ Failed to open project file") end return project else Kernel.abort("⚠️ No .xcodeproj file found") end end |
.pushStringsFromFiles(files, version, creds) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/smartling_xcode/backend.rb', line 57 def self.pushStringsFromFiles(files, version, creds) sl = Smartling::File.new(:apiKey => creds['api_key'], :projectId => creds['project_id']) files.each do |f| # Check if file exists if !f || !File.exist?(f) puts "File not found #{f}" next end # Check if file contains at least one string file = File.open(f) line = file.read.scrub.tr("\000", '').gsub(/\s+/, "") if !line.include?('=') puts "No strings in #{f}" next end remote_path = "/#{version}/#{f.to_s.split('/').last}" # Upload begin res = sl.upload(f.to_s, remote_path, "ios", {:approved => true}) if res puts "Uploaded #{res['stringCount']} strings from #{f}" end rescue Exception => e puts "⚠️ Upload failed for #{f}" if e. puts e. end end end end |
.requestCredentials ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/smartling_xcode/backend.rb', line 8 def self.requestCredentials # Check for existing credentials if File.exist?(CREDFILE) puts "Credentials found in this directory. Overwrite? (y/N)" confirm = STDIN.gets.chomp case confirm when 'y', 'Y' # Continue else Kernel.abort("⚠️ Init cancelled") end end # Prompt for credentials puts "Smartling API Key:" api_key = STDIN.gets.chomp puts "Smartling Project ID:" project_id = STDIN.gets.chomp # Dummy request to validate creds puts "Validating credentials..." begin sl = Smartling::File.new(:apiKey => api_key, :projectId => project_id) res = sl.list rescue Kernel.abort("⚠️ Invalid credentials") end File.open(CREDFILE, "w") do |f| f.write({api_key: api_key, project_id: project_id}.to_json) puts "Credentials saved" end end |