class XcodeUtils::Carthage::XcodeConfig

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

add_embedded_binaries(build_phase) click to toggle source
# 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) click to toggle source
# 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_cleanup() click to toggle source
# 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_xcconfig() click to toggle source
# 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
clean() click to toggle source
# File lib/xcutils/carthage/xcconfig.rb, line 20
def clean
    open_project
    begin_cleanup
end
create_embed_frameworks_build_phase() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_project() click to toggle source
# 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
run() click to toggle source
# File lib/xcutils/carthage/xcconfig.rb, line 15
def run
    open_project
    begin_xcconfig
end