Carthage簡介
Carthage 是一款iOS項目依賴管理工具,與Cocoapods有著相似的功能,可以幫助你方便的管理三方依賴。它會把三方依賴編譯成
framework,以 framework 的形式將三方依賴加入到項目中進(jìn)行使用和管理。
與 Cocoapods 的異同
Cocoapods 是一個已經(jīng)長期存在的對 iOS 三方依賴進(jìn)行管理的工具,它已經(jīng)相當(dāng)?shù)某墒?,為什么要使?Carthage 呢?
CocoaPods 的方式使用更簡單,但 Carthage 更靈活,同時它對用戶的工程沒有侵入性。
在使用 CocoaPods 時,會為工程默認(rèn)自動創(chuàng)建一個 Xcode workspace 來管理主工程和三方依賴,Carthage 使用 xcodebuild 將依賴構(gòu)建成 framework 二進(jìn)制文件,交給用戶集成到自己的項目中。CocoaPods 集中式管理,Carthage 是分散式管理
CocoaPods 在官方 README 中指出它的一個目標(biāo):
… to improve discoverability of, and engagement in, third party open-source libraries, by creating a more centralized ecosystem.
CocoaPods 可支持的所有三方依賴信息都被存儲在 Specs 庫中,方便所有的開發(fā)者使用和完善它。
相比而言,Carthage 是一個分散式的三方依賴管理工具。Carthage 沒有所有三方依賴的信息列表中心,減少了網(wǎng)絡(luò)等因素對訪問類似這樣一個信息列表的失敗的可能性。但同時,這增加了用戶去發(fā)現(xiàn)查找想要使用的三方依賴的難度,你必須訪問 Github 的 Trending 或者類似的方式去查找想要使用的三方庫。
CocoaPods 的工程必須有一個 podspec 文件,它包含了這個工程的元數(shù)據(jù)信息和構(gòu)建信息。Carthage 使用 xcodebuild 構(gòu)建依賴,來代替將依賴都集成到一個 workspace ,它沒有類似像 podspec 的規(guī)格說明文件,不過你使用的依賴需要描述怎么構(gòu)建他們的產(chǎn)品。
Carthage 使用
1. 安裝 Carthage
- Carthage.pkg
到 Carthage官方下載 最新版本,直接使用
Carthage.pkg 安裝 - 使用 Homebrew
brew update
brew install carthage
2. 使用 Carthage 添加 frameworks 依賴到你的工程
- 創(chuàng)建并編輯Cartfile
到你的工程根目錄下創(chuàng)建一個 Cartfile,在文件中指明你想要添加的 frameworks 依賴
如:
github "SwiftyJSON/SwiftyJSON"
github "jdg/MBProgressHUD" ~> 1.0.0
- 更新并構(gòu)建
執(zhí)行
carthage update
執(zhí)行后會拉取在 Cartfile 中指明的依賴到 Carthage/Checkouts 文件夾,然后會構(gòu)建相應(yīng)的 framework。
設(shè)置 Linked Frameworks and Libraries
選中你的工程 target,到 'General' 設(shè)置的tab下,將 Carthage/Build 下的相應(yīng) framework 拖拽到 'Linked Frameworks and Libraries' 選項中。設(shè)置 Build Phases
選中你的工程 target,到 'Build Phases' tab下,點擊 '+' 選擇 'New Run Script Phase',創(chuàng)建一個Script,添加以下內(nèi)容:
/usr/local/bin/carthage copy-frameworks
然后添加相應(yīng)的內(nèi)容到下面的 'Input Files' (舉例):
$(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework
$(SRCROOT)/Carthage/Build/iOS/MBProgressHUD.framework
這個腳本是為了處理 App Store 提交的 bug,解決APP因為使用的frameworks包含二進(jìn)制圖像的iOS模擬器在提交APP Store時會被自動拒絕的問題。
3. 更新 frameworks
編輯你的 Cartfile,然后執(zhí)行:
carthage update
如果你想更新某個 framework:
carthage update MBProgressHUD
4.創(chuàng)建一個支持 Carthage 的庫
Carthage 僅支持 dynamic frameworks。 Dynamic frameworks只能在 OS X 或 iOS 8 以后的系統(tǒng)使用。
創(chuàng)建 framework target 到你的工程
在你的工程下創(chuàng)建一個新的 framework target。配置 framework target
選擇 framework target,選中Build Phases,將需要暴露的頭文件拖拽到 Public 里面,將相應(yīng)的.m文件拖拽到
Compile source 里面。
如果你的 framework target 名稱與你想要打包構(gòu)建的 framework 名稱不一致,選中Build Settings 選項卡,打開Packaging,把 'Produce Module Name' 和 'Produce Name' 改成你想要構(gòu)建的 framework 名稱。
注意:
- 如果你使用了類別,那么你需要在Build Settings的Linking的Other Linker Flags里加上-all_load
- 如果你想你的工程支持bitcode,需要在Other C Flags 里加上-fembed-bitcode
Manager Schemes
單擊頂部 target,在彈出選項中選中 'Manager Schemes' ,將 framework target 的 Shared 選項選中。
注意:
這是由于Carthage 在 build 時,會自動將設(shè)置為 Shared 的 framework target 構(gòu)建成 framework。構(gòu)建 framework
執(zhí)行
carthage build --no-skip-current
執(zhí)行結(jié)果會保存在工程的 Carthage/Build 文件夾下。
測試 framework
創(chuàng)建一個測試項目,將上面 4 中生成好的 framework 文件添加到測試工程進(jìn)行測試。(具體略)發(fā)布tag
在 5 沒有問題時,打一個 tag 并 push,tag名稱必須是 semantic version:
git tag 0.1.0
git push --tags
至此你的庫以及支持 Carthage 了,可以讓其他開發(fā)者使用 Carthage 來管理你的項目依賴了。
5. 相關(guān)
1. Cartfile 語法
Cartfile 描述工程所有使用 Carthage 依賴的 frameworks。Cartfile 遵循 Ordered Graph Data Language 語法。
- github 的庫
github "ReactiveCocoa/ReactiveCocoa" # GitHub.com
github "https://enterprise.local/ghe/desktop/git-error-translations" # GitHub Enterprise
- 其它 git 庫
git "https://enterprise.local/desktop/git-error-translations2.git"
- 只支持二進(jìn)制文件的 frameworks
binary "https://my.domain.com/release/MyFramework.json"
- 版本號
>= 1.0 //“大于等于 1.0 的版本”
~> 1.0 //“1.0及以上的兼容版本,<2.0”
== 1.0 //“1.0版本”
"some-branch-or-tag-or-commit" //特定的分支、tag、或者提交
- 示例
# Require version 2.3.1 or later
github "ReactiveCocoa/ReactiveCocoa" >= 2.3.1
# Require version 1.x
github "Mantle/Mantle" ~> 1.0 # (1.0 or later, but less than 2.0)
# Require exactly version 0.4.1
github "jspahrsummers/libextobjc" == 0.4.1
# Use the latest version
github "jspahrsummers/xcconfigs"
# Use the branch
github "jspahrsummers/xcconfigs" "branch"
# Use a project from GitHub Enterprise
github "https://enterprise.local/ghe/desktop/git-error-translations"
# Use a project from any arbitrary server, on the "development" branch
git "https://enterprise.local/desktop/git-error-translations2.git" "development"
# Use a local project
git "file:///directory/to/project" "branch"
# A binary only framework
binary "https://my.domain.com/release/MyFramework.json" ~> 2.3
2. 目錄結(jié)構(gòu)
在使用 Carthage 管理 frameworks 時,工程根目錄下與 Cartfile 同級的會存在 Cartfile.resolved 文件和
Carthage 文件夾, Carthage 文件夾又包含 Build 和 Checkouts兩個文件夾。
Cartfile.resolved文件
包含已經(jīng)添加的 frameworks 信息,包括依賴名稱和當(dāng)前使用的版本信息。Checkouts文件夾
包含所有 frameworks 源碼信息,在執(zhí)行carthage build時,會直接使用里面的project或者workspace相應(yīng)的scheme來構(gòu)建相應(yīng)的 framework。Build文件夾
包含所有的二進(jìn)制構(gòu)建結(jié)果,包括.framework二進(jìn)制文件和.dSYM等文件。
3.Carthage 語法
update或者build時指定平臺
carthage build --platform ios
carthage update --platform ios