iOS自动打包之xcodeproj(三)

Demo地址:https://github.com/ayangcool/LeoLearnXcodeproj
iOS自动打包之xcodeproj(一)
iOS自动打包之xcodeproj(二)
iOS自动打包之xcodeproj(三)
iOS自动打包之xcodeproj(四)
iOS自动打包之xcodeproj(五)

1.添加.a文件
#!/usr/bin/ruby -w
#created by leo

# 添加 libTest1.a Test1.h 静态库 对应的移除脚本:remove_sdk_a.rb

require 'xcodeproj'

# 打开工程
project_path = './LeoXcodeProjDemo.xcodeproj'
project = Xcodeproj::Project.open(project_path)

# 查询有多少个target
project.targets.each do |target|
    puts target.name
end

# 遍历配置
project.targets[0].build_configurations.each do |config|
    puts config.name
    build_settings = config.build_settings
    build_settings.each do |key, value|
        print key, " == ", value, "\n"
    end
end

# 找到需要操作的target,我这里只有一个target
target_index = 0
project.targets.each_with_index do |target, index|
    if target.name == "LeoXcodeProjDemo"
        target_index = index
        puts target_index
    end
end
target = project.targets[target_index]

# 执行shell命令复制sdk_a文件夹到 LeoXcodeProjDemo文件夹内
system 'cp -rf ../testFile/sdk_a ./LeoXcodeProjDemo/sdk_a'

# 找到要操作的文件夹(此文件夹已存在且添加到项目中)
sdk_a_file = project.main_group.find_subpath(File.join('LeoXcodeProjDemo', 'sdk_a'), true)
sdk_a_file.set_source_tree('')
sdk_a_file.set_path('sdk_a')

# 添加 libTest1.a 索引到 sdk_a 目录
file_ref = sdk_a_file.new_reference("libTest1.a")
# add to target
target.frameworks_build_phases.add_file_reference(file_ref)

# 添加 Test1.h 索引到 sdk_a 目录
sdk_a_file.new_reference("Test1.h")

project.save
2.移除.a文件
#!/usr/bin/ruby -w
#created by leo

# 移除 libTest1.a Test1.h 静态库 对应的添加脚本为:add_sdk_a.rb

require 'xcodeproj'

# 打开工程
project_path = './LeoXcodeProjDemo.xcodeproj'
project = Xcodeproj::Project.open(project_path)

# 查询有多少个target
project.targets.each do |target|
    puts target.name
end

# 遍历配置
project.targets[0].build_configurations.each do |config|
    puts config.name
    build_settings = config.build_settings
    build_settings.each do |key, value|
        print key, " == ", value, "\n"
    end
end

# 找到需要操作的target,我这里只有一个target
target_index = 0
project.targets.each_with_index do |target, index|
    if target.name == "LeoXcodeProjDemo"
        target_index = index
        puts target_index
    end
end
target = project.targets[target_index]

# 找到要操作的文件夹(此文件夹已存在且添加到项目中)
sdk_a_file = project.main_group.find_subpath(File.join('LeoXcodeProjDemo', 'sdk_a'), false)

# 找到索引
file_a_ref = sdk_a_file.find_file_by_path("libTest1.a")
# 移除索引
sdk_a_file.remove_reference(file_a_ref)# 或者使用这个
# file_ref.remove_from_project

# 从 Link Binary With Libraries 中移除
target.frameworks_build_phases.remove_file_reference(file_a_ref)

# 移除 Test1.h
file_h_ref = sdk_a_file.find_file_by_path("Test1.h")
sdk_a_file.remove_reference(file_h_ref)

# 移除 sdk_a 文件夹
father_file = project.main_group.find_subpath(File.join('LeoXcodeProjDemo'), false)
father_file.remove_reference(sdk_a_file)

# 执行shell命令移除文件
system 'rm -rf ./LeoXcodeProjDemo/sdk_a'

project.save
3.添加.framework文件
#!/usr/bin/ruby -w
#created by leo

# 添加 Test3.framework 对应的移除脚本为:remove_sdk_framework.rb

require 'xcodeproj'

# 打开工程
project_path = './LeoXcodeProjDemo.xcodeproj'
project = Xcodeproj::Project.open(project_path)

# 查询有多少个target
project.targets.each do |target|
    puts target.name
end

# 遍历配置
project.targets[0].build_configurations.each do |config|
    puts config.name
    build_settings = config.build_settings
    build_settings.each do |key, value|
        print key, " == ", value, "\n"
    end
end

# 找到需要操作的target,我这里只有一个target
target_index = 0
project.targets.each_with_index do |target, index|
    if target.name == "LeoXcodeProjDemo"
        target_index = index
        puts target_index
    end
end
target = project.targets[target_index]

# 执行shell命令复制sdk_framework文件夹到 LeoXcodeProjDemo文件夹内
system 'cp -rf ../testFile/sdk_framework ./LeoXcodeProjDemo/sdk_framework'

# 找到要操作的文件夹(此文件夹已存在且添加到项目中)
sdk_framework_file = project.main_group.find_subpath(File.join('LeoXcodeProjDemo', 'sdk_framework'), true)
sdk_framework_file.set_source_tree('')
sdk_framework_file.set_path('sdk_framework')

# 添加 Test3.framework 索引到 sdk_framework 目录
sdk_framework_ref = sdk_framework_file.new_reference("Test3.framework")
# add to target
target.frameworks_build_phases.add_file_reference(sdk_framework_ref)

# 如果framework是动态库,还需要把它添加到 Embed Frameworks 中:
# 先找到 Embed Frameworks 对应的group(新创建的项目可能没有这一项,需要先手动添加 Embed Frameworks 这个选项)
embed_frameworks_group = nil
target.copy_files_build_phases.each do |e|
    puts e.to_s
    if e.display_name.end_with?("Embed Frameworks")
        embed_frameworks_group = e
        break
    end
end

# 如果找到 Embed Frameworks ,添加索引
if embed_frameworks_group
    embed_frameworks_group.add_file_reference(sdk_framework_ref)
end

# 添加的项目
if embed_frameworks_group
    embed_frameworks_group.files.each do |e|
        puts e.display_name
        if e.display_name.end_with?("Test3.framework")
            e.settings = Hash.new
            e.settings["ATTRIBUTES"] = ["CodeSignOnCopy", "RemoveHeadersOnCopy"]
        end
    end
end

project.save
4.移除.framework文件
#!/usr/bin/ruby -w
#created by leo

# 移除 Test3.framework 对应的添加脚本为:add_sdk_framework.rb

require 'xcodeproj'

# 打开工程
project_path = './LeoXcodeProjDemo.xcodeproj'
project = Xcodeproj::Project.open(project_path)

# 查询有多少个target
project.targets.each do |target|
    puts target.name
end

# 遍历配置
project.targets[0].build_configurations.each do |config|
    puts config.name
    build_settings = config.build_settings
    build_settings.each do |key, value|
        print key, " == ", value, "\n"
    end
end

# 找到需要操作的target,我这里只有一个target
target_index = 0
project.targets.each_with_index do |target, index|
    if target.name == "LeoXcodeProjDemo"
        target_index = index
        puts target_index
    end
end
target = project.targets[target_index]

# 找到要操作的文件夹(此文件夹已存在且添加到项目中)
sdk_framework_file = project.main_group.find_subpath(File.join('LeoXcodeProjDemo', 'sdk_framework'), false)

# 找到 Test3.framework 索引
sdk_framework_ref = sdk_framework_file.find_file_by_path("Test3.framework")
# 移除索引
sdk_framework_file.remove_reference(sdk_framework_ref)
# 从 Link Binary With Libraries 中移除
target.frameworks_build_phases.remove_file_reference(sdk_framework_ref)

# 如果framework是动态库,且添加到了 Embed Frameworks 中,还需要把它从 Embed Frameworks 中移除:
embed_frameworks_group = nil
target.copy_files_build_phases.each do |e|
    puts e.to_s
    if e.display_name.end_with?("Embed Frameworks")
        embed_frameworks_group = e
        break
    end
end

# 如果找到 Embed Frameworks ,移除索引
if embed_frameworks_group
    embed_frameworks_group.remove_file_reference(sdk_framework_ref)
end

# 移除 sdk_framework 文件夹
father_file = project.main_group.find_subpath(File.join('LeoXcodeProjDemo'), false)
father_file.remove_reference(sdk_framework_file)

# 执行shell命令移除文件
system 'rm -rf ./LeoXcodeProjDemo/sdk_framework'

project.save

你可能感兴趣的:(iOS自动打包之xcodeproj(三))