Class: Xcodeproj::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/smartling_xcode/xcode.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(path) ⇒ Object



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/xcode.rb', line 7

def self.find(path)
    # Get .xcodeproj path
    if path.nil?
        path = './'
    end

    if File.exist?(path)
        if !path.end_with?(".xcodeproj")
            project_file = Dir.entries(path).select {|f| f.end_with?(".xcodeproj") }.first
            path = "#{path}/#{project_file}".gsub("//", "/")
        end

        puts "Using project #{path}"

        # Open project
        begin
            return Xcodeproj::Project.open(path)
        rescue
            Kernel.abort("⚠️  Failed to open project file")
        end    
    else
        Kernel.abort("⚠️  No .xcodeproj file found")
    end
end

.fromBase(path) ⇒ Object

Returns true if the file is from Base localization or unlocalized



33
34
35
36
37
38
39
# File 'lib/smartling_xcode/xcode.rb', line 33

def self.fromBase(path)
    if path.include?('.lproj')
        return path.include?('Base.lproj')
    else 
        return true
    end
end

Instance Method Details

#extractObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/smartling_xcode/xcode.rb', line 41

def extract
    strings_files = []

    # Loop on IB files from Base localization
    self.files.select{|f| 
        f.path.end_with?(".xib", ".storyboard") && Project.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 and .stringsdict files
    self.files.select{|f| 
        Project.fromBase(f.path) && f.path.end_with?(".strings", ".stringsdict")
    }.each do |f|
        strings_files.push(f.real_path)
    end

    return strings_files
end

#getVersionObject



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
91
92
93
94
95
96
97
98
99
100
# File 'lib/smartling_xcode/xcode.rb', line 65

def getVersion
    puts "Looking for an app version number to use for the strings files upload..."
    versions = []
    self.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.hierarchy_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
        file = versions.first
        version = file[:version]
        puts "Using #{version} found in #{file[:file]}"
        return version
    else
        puts "Multiple Info.plist files found. Please select which version number to use:"
        versions.each_with_index do |v, i|
            puts "#{i}. #{v[:version]} from #{v[:file]}"
        end
        puts "Selection: "
        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