(四)Podfile文件(原文翻譯)

學習關於Podfile的一切。Podfile用於在工程中聲明依賴。

1 Podfile是什麼?

Podfile是描述一個或多個Xcode工程的目標的依賴的明確說明。這個文件被簡單命名爲Podfile。本指南中的所有例子都是基於CocoaPods1.0及以前版本的。

Podfile文件可以非常簡單,這裏就是添加Alamofire到一個單獨目標的代碼:

target 'MyApp' do
    use_frameworks!
    pod 'Alamofire', '~> 3.0'
end

更復雜的Podfile的例子是鏈接app和它的測試包:

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'

platform :ios, '9.0'
inhibit_all_warnings!

target 'MyApp' do
    pod 'GoogleAnalytics', '~> 3.1'

    # Has its own copy of OCMock
    # and has access to GoogleAnalytics via the app
    # that hosts the test target

    target 'MyAppTests' do
        inherit! :search_paths
        pod 'OCMock', '~> 2.0.1'
    end
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        puts target.name
    end
end

如果你想要多個目標共享相同的pods,使用抽象目標(abstract_target)。

# There are no targets called "Shows" in any Xcode projects
abstract_target 'Shows' do
    pod 'ShowsKit'
    pod 'Fabric'

    # Has its own copy of ShowsKit + ShowWebAuth
    target 'ShowsiOS' do
        pod 'ShowWebAuth'
    end

    # Has its own copy of ShowsKit + ShowTVAuth
    target 'ShowsTV' do
        pod 'ShowTVAuth'
    end
end

也可以在Podfile的根節點隱含抽象目標,因此上面的例子可以寫成:

pod 'ShowsKit'
pod 'Fabric'

# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
    pod 'ShowWebAuth'
end

# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
    pod 'ShowTVAuth'
end

1.1 從0.x前移到1.0

我們有一個博客帖子深入的解釋了這個變化。

1.2 指定pod版本

當開始一個工程時,可能你想要使用某個Pod庫的最新版本。在這種情況下,簡單的刪除版本要求即可。

pod 'SSZipArchive'

之後在這個工程裏你可能想要凍結這個Pod的指定版本,在這種情況下你可以指定版本號。

pod 'Objection', '0.9'

除了不指定版本,或者指定一個版本,也可以使用邏輯運算符:

  • ‘> 0.1’ Any version higher than 0.1 任何比0.1大的版本
  • ‘>= 0.1’ Version 0.1 and any higher version 任何大於等於0.1的版本
  • ‘< 0.1’ Any version lower than 0.1 任何比0.1小的版本
  • ‘<= 0.1’ Version 0.1 and any lower version 任何小於等於0.1的版本

In addition to the logic operators CocoaPods has an optimistic operator ~>:

在邏輯運算符之外,CocoaPods還有樂觀運算符~>:

  • ‘~> 0.1.2’ Version 0.1.2 and the versions up to 0.2, not including 0.2 and higher 大於等於0.1.2且小於0.2的版本,不包含0.2及更高版本
  • ‘~> 0.1’ Version 0.1 and the versions up to 1.0, not including 1.0 and higher 大於等於0.1且小於1.0的版本,不包含1.0及更高版本
  • ‘~> 0’ Version 0 and higher, this is basically the same as not having it. 版本0及以上版本,這與不寫基本沒區別

For more information, regarding versioning policy, see:

關於版本策略的更多信息,請參考:

2 從本地設備的文件夾路徑使用文件

如果你想要聯合開發一個Pod庫及它的客戶端工程,你可以使用:path。

pod 'Alamofire', :path => '~/Documents/Alamofire'

使用這個選項時,CocoaPods會假定給出的文件夾是Pod庫的根節點,並且直接從那兒連接文件到Pods工程。這意味着你的編輯會在CocoaPods的安裝中保持。引用的文件夾可以是從你最喜歡的SCM檢出,或者甚至是當前repo(倉庫)的git子模塊。

注意Pod文件的podspec應該在指定的文件夾裏。

2.1 從庫repo的根節點裏的podspec開始

有時你可能想使用Pod庫的最前沿版本、指定的修正版或者你自己的分支。在這種情況下,你可以指定你的pod聲明。

要使用repo的主幹分支:

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'

要使用repo的不同分支:

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'

要使用repo的tag標籤:

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'

或者指定一次提交:

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'

非常重要的提醒是,這意味着這個Pod的版本要滿足來自於其他Pods的任何其他依賴。

podspec文件應該在repo的根節點,如果這個庫在它的repo中沒有podspec文件,你將不得不使用後面章節中介紹的方法之一。

3 擴展資源


原文鏈接:《The Podfile

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章