XcodeProj,使用Ruby更改工程文件


利用xcodeproj修改xcode工程文件

一,Ruby基礎

Ruby迭代器each、map、collect、inject  

each——連續訪問集合的所有元素
collect—-從集合中獲得各個元素傳遞給block,block返回的結果生成新的集合。
map——-同collect。
inject——遍歷集合中的各個元素,將各個元素累積成返回一個值。 

二,Ruby腳本

此腳本的作用是,待CoreLib工程中的文件copy到SDK工程中后,給這些文件添加依賴。目的是為了減少人工干預。

靈感來自於Cocapods。需要使用到Xcodeproj這個工具。

此腳本的缺陷是:如果是在編譯過程中去給文件添加依賴,修改到了Xcode的工程文件,工程會停止運行,必須要跑兩遍,才會有結果,所以后來這個方法被否決了。

require 'xcodeproj'
project_path = './EcoRobotCoreLib/EcoRobotCoreLib.xcodeproj'    # 工程的全路徑
sdk_project_path = './EcoRobotSDK/EcoRobotSDK.xcodeproj'
project = Xcodeproj::Project.open(project_path)

# 1、顯示所有的target
project.targets.each do |target|
    puts target.name
end

# 增加新的文件到工程中
target = project.targets.first
group = project.main_group.find_subpath(File.join('testXcodeproj','newGroup'), true)
group.set_source_tree('SOURCE_ROOT')

# 獲取全部的文件引用
file_ref_list = target.source_build_phase.files_references

# 設置文件引用是否存在標識
file_ref_mark = false

# 檢測需要添加的文件是否存在
for file_ref_temp in file_ref_list
    puts file_ref_temp.path.to_s
    if file_ref_temp.path.to_s.end_with?('ViewController1.m') then
        file_ref_mark = true
    end
end

if !file_ref_mark then
    file_ref = group.new_reference('ViewController1.h文件路徑')
    target.add_file_references([file_ref])
    else
    puts '文件引用已存在'
end

if !file_ref_mark then
    file_ref = group.new_reference('ViewController1.m文件路徑')
    target.add_file_references([file_ref])
    else
    puts '文件引用已存在'
end
project.save
puts '文件添加完成'

 

 

require 'xcodeproj'
project_path = './EcoRobotSDK/EcoRobotSDK.xcodeproj'    # 工程的全路徑
project = Xcodeproj::Project.open(project_path)

#獲取target
target = project.targets.first
puts target.name  
#根據路徑名尋找group,def find_subpath(path, should_create = false) ,不重新創建
group = project.main_group.find_subpath(File.join('CommonClass(公共類)','StaticLibaryHeaders', 'iotclient'), false)
#如果找不到路徑,就退出
if !group
    exit
end
group.set_source_tree('<group>')   #SOURCE_ROOT "<group>"
puts "group.real_path:#{group.real_path}"

header_path=group.real_path.to_s; #+"/Class/StaticLibaryHeaders/iotclient";
puts "#{header_path}"

# 獲取全部的文件引用  
file_ref_list = target.headers_build_phase.files_references  
file_ref_mark = false;
if File::directory?(header_path)
    Dir::foreach(header_path)  do |file|
        if file !="." and file !=".."
            # puts file.to_s
          for file_ref_temp in file_ref_list  
                    if file_ref_temp  && file_ref_temp.path.to_s.end_with?(file.to_s) 
                        then
                        # if file_ref_temp.path.to_s.end_with?(file.to_s) then  
                            file_ref_mark = true  
                            puts  "文件依賴存在,頭文件:#{file}"
                            break;
                      end
            end
            if file_ref_temp && !file_ref_mark
                puts "文件依賴不存在,file:#{file}"
                puts  File.basename(file)
                puts  "路徑地址:#{header_path}/#{file}"
                file_ref = group.new_reference("#{header_path}/#{file}")
                target.add_file_references([file_ref]);
                
            end
            file_ref_mark = false  

        end

    end
end


project.save;

# def traverse_dir(file_path)
#     if File.directory? file_path
#         Dir.foreach(file_path) do |file|
#             if file !="." and file !=".."
#                 traverse_dir(file_path+"/"+file)
#             end
#         end
#     else
#         puts "File:#{File.basename(file_path)}"
#         # 檢測需要添加的文件是否存在  
#             # for file_ref_temp in file_ref_list  
#             #     puts file_ref_temp.path.to_s  
#             #     if file_ref_temp.path.to_s.end_with?('#{File.basename(file_path)}') then  
#             #         # file_ref_mark = true  
#             #         file_ref = group.new_reference(file_path)
#             #         target.add_file_references([file_ref]);
#             #     end  
#             # end  

#     end
# end

# traverse_dir(header_path);



#對的
# if File::directory?(header_path)
#     Dir::foreach(header_path)  do |file|
#         puts file.to_s
#     end
# end

# file_ref_list = target.headers_build_phase.files_references  
#       for file_ref_temp in file_ref_list  
#         if file_ref_temp
#              puts file_ref_temp.path.to_s 
#         end
#     end
  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM