cocoapods在podfile中如何修改build phase

問題

升級(jí)Xcode12之后叉存,單測(cè)的target (ReadInJoyTests) 出現(xiàn)了報(bào)錯(cuò):

error: Cycle inside ReadInJoyTests; building could produce unreliable results.
Cycle details:
→ Target 'ReadInJoyTests': CodeSign /Users/yoferzhang/Library/Developer/Xcode/DerivedData/QQMSFContact-gpvqexdcrtvrsdczqfqownxrezqo/Build/Products/Debug-iphonesimulator/QQ.app/EarlGrey.framework
○ That command depends on command in Target 'ReadInJoyTests': script phase “[CP] Copy Pods Resources”
○ Target 'ReadInJoyTests': CodeSign /Users/yoferzhang/Library/Developer/Xcode/DerivedData/QQMSFContact-gpvqexdcrtvrsdczqfqownxrezqo/Build/Products/Debug-iphonesimulator/QQ.app/EarlGrey.framework

解決方案,是要?jiǎng)h除 ReadInJoyTests target 中 build phase 的 [CP] Copy Pods Resources 的 Output file lists

手動(dòng)刪除

image

手動(dòng)清空后蝶押,pod install 后還是會(huì)生成润讥。

腳本刪除

考慮是否可以通過腳本刪除呛梆。

刪除方法

腳本刪除可以分為以下幾步:

  1. 獲取主工程的xcodeproj
  2. 找到target ReadInJoyTests target
  3. 找到build phase [CP] Copy Pods Resources
  4. 保存project

刪除的代碼

require 'xcodeproj'

def delete_rij_tests_output_file_lists
    puts "===================> Start delete ReadInJoyTests Target [CP] Copy Pods Resources output file lists"

    project_path = './QQMSFContact.xcodeproj'
    project = Xcodeproj::Project.open(project_path)
    project.targets.each do |target|
        if target.name == "ReadInJoyTests"
            puts "===================> Find ReadInJoyTests Target"
            build_phase = target.build_phases.find { |bp| bp.display_name == '[CP] Copy Pods Resources' }

            puts "===================> Before delete, Output file lists:"
            puts build_phase.output_file_list_paths
            
            # 執(zhí)行刪除
            while build_phase.output_file_list_paths.length > 0
                build_phase.output_file_list_paths.pop()
            end
        end
    end

    # 保存工程文件
    project.save
    puts "===================> Finish delete ReadInJoyTests Target [CP] Copy Pods Resources output file lists"
end

刪除時(shí)機(jī)

首先來看一下cocoapods的hook執(zhí)行順序

  1. 終端執(zhí)行命令pod install
  2. 運(yùn)行podfile 中的 ruby method
  3. 執(zhí)行hook pre_install
  4. 執(zhí)行 pod install 的行為吟税,但此時(shí)修改內(nèi)容還在內(nèi)存的 installer 中,還未寫入磁盤
  5. 執(zhí)行hook post_install 稀轨,同上還是未寫入磁盤
  6. 將installer修改部分寫入磁盤

如果在 post_install 中執(zhí)行 delete_rij_tests_output_file_lists 扼脐,那因?yàn)?post_install 內(nèi)pod的修改還沒寫入磁盤,delete_rij_tests_output_file_lists 的修改靶端,會(huì)被 post_install 之后pod寫入磁盤而覆蓋掉谎势。導(dǎo)致刪除失敗凛膏。

調(diào)試發(fā)現(xiàn)杨名,如果在第3步之前, output file paths 內(nèi)容存在猖毫,則在post_install 中執(zhí)行刪除台谍,就不會(huì)發(fā)生覆蓋寫入時(shí)重新生成 output file paths

也就是說可以在第3步之前吁断,先通過腳本寫入 path趁蕊,欺騙cocoapods,path存在仔役,這次 install 就不要生成path 做添加動(dòng)作掷伙。然后在post_install 中執(zhí)行刪除邏輯,這樣就解決了問題又兵。

寫入path的代碼為:

def add_rij_tests_temp_output_file_list
    project_path = './QQMSFContact.xcodeproj'
    project = Xcodeproj::Project.open(project_path)
    project.targets.each do |target|
        if target.name == "ReadInJoyTests"
            puts "===================> Find ReadInJoyTests Target"
            build_phase = target.build_phases.find { |bp| bp.display_name == '[CP] Copy Pods Resources' }

            puts "===================> Before add, temp output file lists:"
            puts build_phase.output_file_list_paths
            
            list_path = '${PODS_ROOT}/Target Support Files/Pods-ReadInJoyTests/Pods-ReadInJoyTests-resources-${CONFIGURATION}-output-files.xcfilelist'
            # 添加路徑
            if build_phase.output_file_list_paths.include?(list_path) == false
                build_phase.output_file_list_paths.push(list_path)
            end
        end
    end

    # 保存工程文件
    project.save
end

最后修改完成的podfile 為

# 省略上述兩個(gè)method定義
post_install do |installer|
    # 省略...
  
  # 刪除 ReadInJoyTests Target [CP] Copy Pods Resources output file lists
  delete_rij_tests_output_file_lists
end

# pod install之前添加一次任柜,讓 pod 認(rèn)為已經(jīng)有了path,pod install過程中就不會(huì)再新增了
add_rij_tests_temp_output_file_list

完整流程

最后來看完整的流程

  1. 臨時(shí)添加output file path沛厨,讓cocoapods認(rèn)為本次 install 無需做添加path的動(dòng)作宙地。
  2. install 生成工程修改(不會(huì)添加path)
  3. post_install 中再刪除 output file path
  4. pod將install 寫入磁盤。

See Also

最后提供一個(gè)不用 xcodeproj 工具的刪除方法

post_install do |installer|

  installer.aggregate_targets.each do |target|
    if target.name == "Pods-ReadInJoyTests"
      puts "==========> Find Pods-ReadInJoyTests:"
      project = target.user_project
      puts project
      project.targets.each do |sub_target|
        if sub_target.name =="ReadInJoyTests"
          puts "==========> Find ReadInJoyTests Target"
          build_phase = sub_target.build_ phases. find { |bp| bp.display_name == '[CP] Copy Pods Resources' }
          puts "==========> Before delete, Output file lists:"
          puts build_phase.output_file_list_paths
          
          #執(zhí)行刪除
          build_phase.output_file_list_paths.pop( )
          puts"==========> After delete, Output file lists:"
          puts build_phase.output_file_list_paths
        end
      end
      project. save
    end
  end
end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末逆皮,一起剝皮案震驚了整個(gè)濱河市宅粥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌电谣,老刑警劉巖秽梅,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異剿牺,居然都是意外死亡风纠,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門牢贸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來竹观,“玉大人,你說我怎么就攤上這事〕粼觯” “怎么了懂酱?”我有些...
    開封第一講書人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長誊抛。 經(jīng)常有香客問我列牺,道長,這世上最難降的妖魔是什么拗窃? 我笑而不...
    開封第一講書人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任瞎领,我火速辦了婚禮,結(jié)果婚禮上随夸,老公的妹妹穿的比我還像新娘九默。我一直安慰自己,他們只是感情好宾毒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開白布驼修。 她就那樣靜靜地躺著,像睡著了一般诈铛。 火紅的嫁衣襯著肌膚如雪乙各。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評(píng)論 1 299
  • 那天幢竹,我揣著相機(jī)與錄音耳峦,去河邊找鬼。 笑死焕毫,一個(gè)胖子當(dāng)著我的面吹牛蹲坷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咬荷,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼冠句,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了幸乒?” 一聲冷哼從身側(cè)響起懦底,我...
    開封第一講書人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎罕扎,沒想到半個(gè)月后聚唐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡腔召,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年杆查,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片臀蛛。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡亲桦,死狀恐怖崖蜜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情客峭,我是刑警寧澤豫领,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站舔琅,受9級(jí)特大地震影響等恐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜备蚓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一课蔬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧郊尝,春花似錦二跋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽样傍。三九已至横缔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間衫哥,已是汗流浹背茎刚。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留撤逢,地道東北人膛锭。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像蚊荣,于是被迫代替她去往敵國和親初狰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354