Class: XcodeUtils::Carthage::XcodeConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/xcutils/carthage/xcconfig.rb

Instance Method Summary collapse

Constructor Details

#initializeXcodeConfig

Returns a new instance of XcodeConfig.



7
8
9
10
11
12
13
# File 'lib/xcutils/carthage/xcconfig.rb', line 7

def initialize
    @build_path = './Carthage/Build/iOS'
    @embed_framework_name = 'Embed Carthage Frameworks'
    @framework_search_paths = '$(PROJECT_DIR)/Carthage/Build/iOS'
    @remove_unwanted_framework_architectures = 'Remove Unwanted Framework Architectures'
    @remove_unwanted_framework_architectures_script = get_file_as_string(File.expand_path('../../../../sh/remove_unwanted_architectures.sh', __FILE__))    
end

Instance Method Details

#add_embedded_binaries(build_phase) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/xcutils/carthage/xcconfig.rb', line 166

def add_embedded_binaries(build_phase)
    input_paths = []
    Dir.entries(@build_path).each do |entry|
        matched = /^(.*)\.framework$/.match(entry)
    
        if !matched.nil?
            frameworks_group = @project.groups.find { |group| group.display_name == 'Frameworks' }
            framework_ref = frameworks_group.new_file("Carthage/Build/iOS/#{matched.string}")
            build_file = build_phase.add_file_reference(framework_ref)
            build_file.settings = { 'ATTRIBUTES' => ['CodeSignOnCopy', 'RemoveHeadersOnCopy'] }
            input_paths.push("${SRCROOT}/Carthage/Build/iOS/#{matched.string}")
            puts "framework_ref -> #{framework_ref}".gray
        end
    end
end

#add_input_files(build_phase) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/xcutils/carthage/xcconfig.rb', line 182

def add_input_files(build_phase)
    input_paths = []
    
    Dir.entries(@build_path).each do |entry|
        matched = /^(.*)\.framework$/.match(entry)
        if !matched.nil?
            input_paths.push("${SRCROOT}/Carthage/Build/iOS/#{matched.string}")
        end
    end
    
    build_phase.input_paths = input_paths
    
    puts "Add input files to run script -> #{input_paths}".gray
end

#begin_cleanupObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xcutils/carthage/xcconfig.rb', line 41

def begin_cleanup
    puts "🙏  Start cleanup..".green
    
    @project.targets.each do |target|
        puts "👻 Clean target -> #{target.name}"
    
        exist_build_phase = target.build_phases.find { |build_phase| build_phase.class == Xcodeproj::Project::Object::PBXCopyFilesBuildPhase && build_phase.name == @embed_framework_name }
        exist_arch_build_phase = target.build_phases.find { |build_phase| build_phase.class == Xcodeproj::Project::Object::PBXShellScriptBuildPhase && build_phase.name == @remove_unwanted_framework_architectures }
    
        if !exist_build_phase.nil?
            puts "Delete exist embed carthage framework in build phases".gray
            exist_build_phase.files_references.each do |reference|
                reference.remove_from_project
            end
            exist_build_phase.clear
            target.build_phases.delete(exist_build_phase)
        end
    
        if !exist_arch_build_phase.nil?
            puts "Delete exist remove unwanted framework architectures in build phases".gray
            exist_arch_build_phase.clear
            target.build_phases.delete(exist_arch_build_phase)
        end

        target.build_configurations.each do |config|
            paths = config.build_settings['FRAMEWORK_SEARCH_PATHS']

            if paths.include?(@framework_search_paths)
                paths.delete(@framework_search_paths)
                config.build_settings['FRAMEWORK_SEARCH_PATHS'] = paths
                puts "#{config}: Delete #{@framework_search_paths} in FRAMEWORK_SEARCH_PATHS".gray
            end
        end
        puts "\n"
    end
        
    @project.save()

    puts "👌 Finish cleanup".green
end

#begin_xcconfigObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/xcutils/carthage/xcconfig.rb', line 82

def begin_xcconfig
    puts "🙏 Start xcconfig..".green
    
    new_build_phase = nil
    new_arch_build_phase = nil
    
    @project.targets.each do |target|
        puts "👻 Build target -> #{target.name}"
    
        exist_build_phase = target.build_phases.find { |build_phase| build_phase.class == Xcodeproj::Project::Object::PBXCopyFilesBuildPhase && build_phase.name == @embed_framework_name }
        exist_arch_build_phase = target.build_phases.find { |build_phase| build_phase.class == Xcodeproj::Project::Object::PBXShellScriptBuildPhase && build_phase.name == @remove_unwanted_framework_architectures }
    
        if !exist_build_phase.nil?
            puts "Delete exist embed carthage framework in build phases".gray
            exist_build_phase.files_references.each do |reference|
                reference.remove_from_project
            end
            exist_build_phase.clear
            target.build_phases.delete(exist_build_phase)
        end
    
        if new_build_phase.nil?
            new_build_phase = create_embed_frameworks_build_phase
            add_embedded_binaries(new_build_phase)
            puts "Insert new embed carthage framework #{new_build_phase} into build phases".gray
        end
    
        if !exist_arch_build_phase.nil?
            puts "Delete exist remove unwanted framework architectures in build phases".gray
            exist_arch_build_phase.clear
            target.build_phases.delete(exist_arch_build_phase)
        end
    
        if new_arch_build_phase.nil?
            new_arch_build_phase = create_shell_script_build_phase(@remove_unwanted_framework_architectures)
            new_arch_build_phase.shell_script =  @remove_unwanted_framework_architectures_script
            add_input_files(new_arch_build_phase)
            puts "Insert new remove unwanted framework architectures #{new_arch_build_phase} into build phases".gray
        end
    
        target.build_phases << new_build_phase
        target.build_phases << new_arch_build_phase
    
        puts "#{target.build_configurations}"
    
        target.build_configurations.each do |config|
            paths = config.build_settings['FRAMEWORK_SEARCH_PATHS']

            if !paths.include?(@framework_search_paths)
                paths << @framework_search_paths
                config.build_settings['FRAMEWORK_SEARCH_PATHS'] = paths
                puts "#{config}: Insert #{@framework_search_paths} into FRAMEWORK_SEARCH_PATHS".gray
            end
        end
        puts "\n"
    end
    
    @project.save()
    
    puts "👌 Finish xcconfig".green
end

#cleanObject



20
21
22
23
# File 'lib/xcutils/carthage/xcconfig.rb', line 20

def clean
    open_project
    begin_cleanup
end

#create_embed_frameworks_build_phaseObject



153
154
155
156
157
158
# File 'lib/xcutils/carthage/xcconfig.rb', line 153

def create_embed_frameworks_build_phase
    build_phase = @project.new(Xcodeproj::Project::Object::PBXCopyFilesBuildPhase)
    build_phase.name = @embed_framework_name
    build_phase.symbol_dst_subfolder_spec = :frameworks
    return build_phase
end

#create_shell_script_build_phase(name) ⇒ Object



160
161
162
163
164
# File 'lib/xcutils/carthage/xcconfig.rb', line 160

def create_shell_script_build_phase(name)
    build_phase = @project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
    build_phase.name = name
    return build_phase
end

#get_file_as_string(filename) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/xcutils/carthage/xcconfig.rb', line 144

def get_file_as_string(filename)
    data = ''
    f = File.open(filename, "r") 
    f.each_line do |line|
        data += line
    end
    return data
end

#open_projectObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xcutils/carthage/xcconfig.rb', line 25

def open_project
    if !Dir.exist?(@build_path)
        abort "😭 The Carthage build has not yet been successful. Please try again after successful arthage build.".red
    end

    project_names = Dir["./*.xcodeproj"]
    
    if project_names.first.nil?
        abort "😭 Does not exist project to configure.".red
    end
        
    puts "💎💎 Project Found -> #{project_names.first} 💎💎"
    
    @project = Xcodeproj::Project.open(project_names.first)
end

#runObject



15
16
17
18
# File 'lib/xcutils/carthage/xcconfig.rb', line 15

def run
    open_project
    begin_xcconfig
end