iOS開源二進(jìn)制使用插件 - 使用教程

iOS編譯速度如何穩(wěn)定提高10倍以上

GitHub開源

一、概要

cocoapods-imy-bin功能點(diǎn):

  1. 組件二進(jìn)制化,無(wú)入侵式支持組件二進(jìn)制化,致力于解決Ci打包速度慢、研發(fā)編譯慢等編譯問(wèn)題。
  2. 本地配置文件 - Podfile_local
  3. 二進(jìn)制源碼調(diào)試pod bin code,類似美團(tuán) iOS 工程 zsource 命令背后的那些事兒的效果。
  4. 命令快捷鍵pod bin imy,如游戲快捷鍵,根據(jù)配置會(huì)在特定目錄執(zhí)行特定命令(如任意終端目錄下,執(zhí)行某個(gè)特定目錄的pod update --no-repo-update命令),減少其他繁瑣操作。支持任意個(gè)快捷鍵。

cocoapods-imy-bin插件所關(guān)聯(lián)的組件二進(jìn)制化策略:

預(yù)先將打包成 .a 的組件保存到靜態(tài)服務(wù)器上,并在 install 時(shí),去下載組件對(duì)應(yīng)的二進(jìn)制版本,以減少組件編譯時(shí)間,達(dá)到加快 App 打包、組件發(fā)布等操作的目的。

關(guān)于 插件具體的架構(gòu)部署實(shí)踐和更詳細(xì)的資源,可以參考

Demo

二、準(zhǔn)備工作

1、安裝插件

sudo gem install cocoapods-imy-bin

三、使用二進(jìn)制組件

1、環(huán)境搭建

環(huán)境搭建詳細(xì)教程

使用二進(jìn)制時(shí),本插件需要提供以下資源:

  • 靜態(tài)資源服務(wù)器( binary-server,附詳細(xì)使用教程)
  • 二進(jìn)制私有源倉(cāng)庫(kù)(保存組件二進(jìn)制版本 podspec)

2、初始化插件

xx:Demo slj$ pod bin init

======  dev 環(huán)境 ========

開始設(shè)置二進(jìn)制化初始信息.
所有的信息都會(huì)保存在 /Users/slj/.cocoapods/bin_dev.yml 文件中.
%w[bin_dev.yml bin_debug_iphoneos.yml bin_release_iphoneos.yml] 
你可以在對(duì)應(yīng)目錄下手動(dòng)添加編輯該文件. 文件包含的配置信息樣式如下:

---
configuration_env: dev
code_repo_url: git@github.com:su350380433/example_spec_source.git
binary_repo_url: git@github.com:su350380433/example_spec_bin_dev.git
binary_download_url: http://localhost:8080/frameworks/%s/%s.zip
download_file_type: zip


編譯環(huán)境
可選值:[ dev / debug_iphoneos / release_iphoneos ]
舊值:dev

按提示輸入所屬環(huán)境、源碼私有源、二進(jìn)制私有源、二進(jìn)制下載地址、下載文件類型后,插件就配置完成了。其中 binary_download_url 需要預(yù)留組件名稱與組件版本占位符,插件內(nèi)部會(huì)依次替換 %s 為相應(yīng)組件的值。

cococapod-bin 也支持從 url 下載配置文件,方便對(duì)多臺(tái)機(jī)器進(jìn)行配置:

?  ~ pod bin init --bin-url=https://github.com/su350380433/cocoapods-imy-bin-configs/raw/master/bin_dev.yml

配置文件模版內(nèi)容如下,根據(jù)不同團(tuán)隊(duì)的需求定制即可:

---
configuration_env: dev
code_repo_url: git@github.com:su350380433/example_spec_source.git
binary_repo_url: git@github.com:su350380433/example_spec_bin_dev.git
binary_download_url: http://localhost:8080/frameworks/%s/%s/zip
download_file_type: zip

配置時(shí),不需要手動(dòng)添加源碼和二進(jìn)制私有源的 repo,插件在找不到對(duì)應(yīng) repo 時(shí)會(huì)主動(dòng) clone。

記得啟動(dòng) sudo mongod服務(wù),靜態(tài)資源服務(wù)。

四、制作二進(jìn)制組件

視頻演示

1、制作命令

可以直接使用插件的 pod bin auto命令,在插件初始化配置完成后,目錄下只要有包含podspec文件,根據(jù)podspec文件的version版本號(hào)會(huì)自動(dòng)化執(zhí)行build、組裝二進(jìn)制組件、制作二進(jìn)制podspec、上傳二進(jìn)制文件、上傳二進(jìn)制podspec到私有源倉(cāng)庫(kù)。

pod bin auto

帶上—all-make參數(shù)會(huì)把當(dāng)前組件所依賴的組件都自動(dòng)化制作成二進(jìn)制組件。

pod bin local

pod bin local 是配合其他三方編譯產(chǎn)物的命令,需要配置編譯產(chǎn)物的目錄。

BinArchive.json是制作二進(jìn)制的一些配置項(xiàng),放在項(xiàng)目跟目錄下:

{
    "http://": "archive-white-pod-list 不制作二進(jìn)制白名單,",
    "archive-white-pod-list" : [
        "YYTargetDemo",
        "YYModel"
    ],
    "http://": "ignore-git-list 不制作二進(jìn)制 所屬git白名單,",
    "ignore-git-list": [
        "git@gitlab.xxx.com:Github-iOS"
    ],
     "http://": "ignore-http-list 不制作二進(jìn)制 所屬https白名單,",
    "ignore-http-list": [
        "https://gitlab.xxx.com/Github-iOS"
    ],
    "http://": "xcode_build_path 設(shè)置編譯緩存完整路徑, 默認(rèn)地址如下",
    "xcode_build_path" : "xcode-build/Build/Intermediates.noindex/ArchiveIntermediates/#{target_name}/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/",
}

2. 二進(jìn)制Podspec

通過(guò)pod bin autopod bin local二進(jìn)制Podspec 會(huì)自動(dòng)生成、上傳,無(wú)需關(guān)心。

3、查看結(jié)果

二進(jìn)制存儲(chǔ)服務(wù):http://localhost:8080/frameworks/(默認(rèn)本地8080端口)

二進(jìn)制私有源參考:https://github.com/su350380433/example_spec_bin_dev.git(自定義)

4、使用二進(jìn)制

視頻演示

在Podfile文件中,加入這兩行代碼,對(duì)已經(jīng)制作二進(jìn)制的就會(huì)生效,自動(dòng)轉(zhuǎn)換二進(jìn)制組件依賴。

plugin 'cocoapods-imy-bin'
use_binaries!

五、擴(kuò)展功能

1、本地配置文件 - Podfile_local

本地組件配置文件 Podfile_local,目前已支持Podfile下的大部分功能,可以把一些本地配置的語(yǔ)句放到Podfile_local。

Podfile_local.png

場(chǎng)景:

  1. 不希望把本地采用的源碼/二進(jìn)制配置、本地庫(kù)傳到遠(yuǎn)程倉(cāng)庫(kù)。
  2. 避免直接修改Podfile文件,引起更新代碼時(shí)沖突、或者誤提交。

如Podfile本地庫(kù)的寫法:

pod YYModel :path => '../' #提交的時(shí)候往往要修改回來(lái)才提交,操作繁瑣

用法:

在與Podfile同級(jí)目錄下,新增一個(gè)Podfile_local文件,模板可到這里下載Podfile_local

#target 'Seeyou' do 不同的項(xiàng)目注意修改下Seeyou的值
#:path => '../IMYYQHome',根據(jù)實(shí)際情況自行修改,與之前在podfile寫法一致


 plugin 'cocoapods-imy-bin'
#是否啟用二進(jìn)制插件,想開啟把下面注釋去掉
# use_binaries! 

#設(shè)置使用【源碼】版本的組件。
#set_use_source_pods ['YYKit','SDWebImaage']

#需要替換Podfile里面的組件才寫到這里
#在這里面的所寫的組件庫(kù)依賴,默認(rèn)切換為【源碼】依賴
target 'Seeyou' do
  #本地庫(kù)引用
    #pod 'YYModel', :path => '../YYModel'

  #覆蓋、自定義組件
    #pod 'YYCache', :podspec => 'http://覆蓋、自定義/'
end
以前的 pod update --no-repo-update 命令加個(gè)前綴 `bin` 變成
pod bin update --no-repo-update 

or

pod bin install

支持 pod install/update 命令參數(shù)

并將其加入 .gitignore ,再也不用擔(dān)心我誤提交或者沖突了,Podfile_local 中的配置選項(xiàng)優(yōu)先級(jí)比 Podfile 高,支持和 Podfile 相同的配置語(yǔ)句,同時(shí)支持pre_install or post_install。

如果您不習(xí)慣Podfile_local的使用方式,可以把命令寫在Podfile里面,pod時(shí)不需要加bin,依舊是 pod update/install。

2、二進(jìn)制源碼調(diào)試

視頻演示

在項(xiàng)目根目錄下,輸入命令:

pod bin code YYModel

YYModel為需要源碼調(diào)試的組件庫(kù)名稱。成功之后像平時(shí)一樣單步調(diào)試,控制臺(tái)打印變量。讓我們同時(shí)擁有使用二進(jìn)制的便利和源碼調(diào)試的能力。

 $ pod bin code --help                                                                   [11:37:50]
Usage:

    $ pod bin code [NAME]

      通過(guò)將二進(jìn)制對(duì)應(yīng)源碼放置在臨時(shí)目錄中,讓二進(jìn)制出現(xiàn)斷點(diǎn)時(shí)可以跳到對(duì)應(yīng)的源碼,方便調(diào)試。 在不刪除二進(jìn)制的情況下為某個(gè)組件添加源碼調(diào)試能力,多個(gè)組件名稱用空格分隔

Options:

    --all-clean   刪除所有已經(jīng)下載的源碼
    --clean       刪除所有指定下載的源碼
    --list        展示所有一級(jí)下載的源碼以及其大小
    --source      源碼路徑,本地路徑,會(huì)去自動(dòng)鏈接本地源碼

效果與演示參考鏈接1視頻

3、快捷鍵命令

在任意的終端執(zhí)行命令,都能執(zhí)行特定目錄下特定命令

使用命令:

pod bin imy

or

pod bin imy 2    #2 是自定義的快捷鍵

使用場(chǎng)景:

1. 在任意目錄下,執(zhí)行項(xiàng)目A的pod update --no-repo-update命令

命令快捷鍵配置

 $ pod bin inithk                                                                        [11:37:58]

開始設(shè)置快捷鍵 pod bin imy.
所有的信息都會(huì)保存在 /Users/ci/.cocoapods/hot_key_1.yml 文件中.
%w[hot_key.yaml] 
你可以在對(duì)應(yīng)目錄下手動(dòng)添加編輯該文件. 文件包含的配置信息樣式如下:

---
hot_key_index: '1'
hot_key_dir: '/User/ci/自定義目錄'
hot_key_cmd: pod bin update --no-repo-update


快捷鍵
可選值:[ 1 / 2 / 3... ]
舊值:1 

六、 DSL參數(shù)解釋

首先,開發(fā)者需要在 Podfile 中需要使用 plugin 'cocoapods-imy-bin' 語(yǔ)句引入插件

plugin 'cocoapods-imy-bin'

順帶可以刪除 Podfile 中的 source ,因?yàn)椴寮?nèi)部會(huì)自動(dòng)幫你添加兩個(gè)私有源。

cocoapods-bin插件提供二進(jìn)制相關(guān)的配置語(yǔ)句有 use_binaries!use_binaries_with_spec_selector! 以及 set_use_source_pods,下面會(huì)分別介紹。

use_binaries!

全部組件使用二進(jìn)制版本。

支持傳入布爾值控制是否使用二進(jìn)制版本,比如 DEBUG 包使用二進(jìn)制版本,正式包使用源碼版本,Podfile 關(guān)聯(lián)語(yǔ)句可以這樣寫:

use_binaries! (ENV['DEBUG'].nil? || ENV['DEBUG'] == 'true')
set_use_source_pods

設(shè)置使用源碼版本的組件。

實(shí)際開發(fā)中,可能需要查看 YYModel 組件的源碼,這時(shí)候可以這么設(shè)置:

set_use_source_pods ['YYModel']

如果 CocoaPods 版本為 1.5.3 ,終端會(huì)輸出以下內(nèi)容,表示 YYModel 的參照源從二進(jìn)制私有源切換到了源碼私有源:

Analyzing dependencies
Fetching podspec for `A` from `../`
Downloading dependencies
Using A (0.1.0)
Installing YYModel 1.0.4.2 (source changed to `git@git.xxxxxx.net:ios/cocoapods-spec.git` from `git@git.xxxxxx.net:ios/cocoapods-spec-binary.git`)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 2 total pods installed.
use_binaries_with_spec_selector!

過(guò)濾出需要使用二進(jìn)制版本組件。

假如開發(fā)者只需要 YYModel 的二進(jìn)制版本,那么他可以在 Podfile 中添加以下代碼:

use_binaries_with_spec_selector! do |spec|
  spec.name == 'YYModel'
end

需要注意的是,如果組件有 subspec ,使用組件名作為判斷條件應(yīng)如下

use_binaries_with_spec_selector! do |spec|
  spec.name.start_with? == '組件名'
end

如果像上個(gè)代碼塊一樣,直接對(duì)比組件名,則插件會(huì)忽略此組件的所有 subspec,導(dǎo)致資源拉取錯(cuò)誤,這種場(chǎng)景下,最好通過(guò) set_use_source_pods 語(yǔ)句配置依賴。

一個(gè)實(shí)際應(yīng)用是,三方組件采用二進(jìn)制版本,團(tuán)隊(duì)編寫的組件依舊采用源碼版本。如果三方組件都在 cocoapods-repo 組下,就可以使用以下代碼過(guò)濾出三方組件:

use_binaries_with_spec_selector! do |spec|
 git = spec.source && spec.source['git']
 git && git.include?('cocoapods-repo')
end
切換Dev/Debug_iPhoneos/Release_iPhoneos環(huán)境初始化設(shè)置
#dev 初始化插件配置 默認(rèn)dev環(huán)境
pod bin init --bin-url=https://gitlab.xxx.com/cocoapods-imy-bin-config/raw/master/bin_dev.yml

#Debug_iPhoneos 初始化插件配置
pod bin init --bin-url=https://gitlab.xxx.com/cocoapods-imy-bin-config/raw/master/bin_debug_iphoneos.yml


#release_iPhoneos 初始化插件配置
pod bin init --bin-url=https://gitlab.xxx.com/cocoapods-imy-bin-config/raw/master/bin_release_iphoneos.yml

使用時(shí)在podfile 或者 podfile_local指定設(shè)置

#在podfile 或者 podfile_local 文件下加這句話
set_configuration_env('debug_iphoneos') 
其他設(shè)置

插件默認(rèn)開啟多線程下載組件資源,如果要禁用這個(gè)功能,Podfile 添加以下代碼即可:

install! 'cocoapods', { install_with_multi_threads: false }

七、感謝

cocoapods-bin

美團(tuán) iOS 工程 zsource 命令背后的那些事兒

您有什么更好的想法,可以提出來(lái),我們一起來(lái)實(shí)現(xiàn),共創(chuàng)一個(gè)強(qiáng)大的平臺(tái),同時(shí)也歡迎給我們提PR。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容