CocoPods之.podspec語法參考


一,概述  

spec文件描述了Pod庫的版本。它包括有關從哪里獲取source、要使用哪些文件、應用程序構建設置以及其他通用元數據(如名稱、版本和描述)的詳細信息。

二、spec文件(Specification) 規范說明

  • 1.創建spec文件

    pod spec create DemoSpec
  • 2.podspec示例

    Pod::Spec.new do |spec|
      spec.name         = 'Reachability'
      spec.version      = '3.1.0'
      spec.license      = { :type => 'BSD' }
      spec.homepage     = 'https://github.com/tonymillion/Reachability'
      spec.authors      = { 'Tony Million' => 'tonymillion@gmail.com' }
      spec.summary      = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
      spec.source       = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0' }
      spec.module_name  = 'Rich'
    
      spec.ios.deployment_target  = '9.0'
      spec.osx.deployment_target  = '10.10'
    
      spec.source_files       = 'Reachability/common/*.swift'
      spec.ios.source_files   = 'Reachability/ios/*.swift', 'Reachability/extensions/*.swift'
      spec.osx.source_files   = 'Reachability/osx/*.swift'
    
      spec.framework      = 'SystemConfiguration'
      spec.ios.framework  = 'UIKit'
      spec.osx.framework  = 'AppKit'
    
      spec.dependency 'SomeOtherPod'
    end

三、語法

  • 1、Root specification

    根規范“根”規范存儲有關庫的特定版本的信息。此組中的屬性只能寫入“根”規范,而不是“子規范”。在這個組中列出的屬性是podspec需要的唯一屬性。其他組的屬性被用來改進podspec並遵循一個關於配置方法的約定。根規范可以直接通過“sub-specifications”來描述這些屬性。

    • name 必須

      #跟podspec文件名相同
      spec.name = 'AFNetworking'
    • version 必須

      spec.version = '0.0.1'
    • cocoapods_version

      規范支持的CocoaPods版本。

      spec.cocoapods_version = '>= 0.36'
    • authors 必須

      庫維護人員的名稱和電子郵件地址,不是Podspec維護者。

      spec.author = '作者名字'

      #多個作者

      spec.authors = 'Darth Vader', 'Wookiee'

      #多個作者,以及其郵箱

      spec.authors = { 'Darth Vader' => 'darthvader@darkside.com', 'Wookiee' => 'wookiee@aggrrttaaggrrt.com' }
    • license 必須

      pod庫的許可證。除非源文件包含一個名為LICENSE.* or LICENCE.*的文件,否則許可證文件的路徑或通常用於許可證類型的公告完整文本,必須指定。許可證文件可以沒有文件擴展名也可以是txt, md, 或是markdown,

      #許可證默認與spec文件在同一目錄下
      #MIT是一個比較寬泛的開源許可協議

      spec.license = 'MIT'

      #指定許可文件

      spec.license = { :type => 'MIT', :file => 'MIT-LICENSE.txt' }

      #指定了許可證文件的內容

      spec.license = { :type => 'MIT', :text => <<-LICENSE Copyright 2012 Permission is granted to... LICENSE }
    • homepage 必須

      pod庫主頁地址

      spec.homepage = 'http://www.example.com'
    • source 必須

      pod庫的位置

      #通過tag指定Git源 用的最多的方式!!
      #tag值與spec.version一致。

      spec.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => spec.version.to_s }

      以下的方式,項目中沒有用到,不能過多的解釋,更多參照source

      #使用v前綴的tag值和子模塊

      spec.source = { :git => 'https://github.com/typhoon-framework/Typhoon.git', :tag => "v#{spec.version}", :submodules => true }

      #使用svn源

      spec.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => '4.8.8' }

      #使用HTTP下載代碼的壓縮文件。它支持zip、tgz、bz2、txz和tar。

      spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip' }

      #使用HTTP下載文件,使用hash來驗證下載。它支持sha1和sha256。

      spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip',:sha1 => '7e21857fe11a511f472cfd7cfa2d979bd7ab7d96' }

      支持的key:

      :git => :tag, :branch, :commit, :submodules
      :svn => :folder, :tag, :revision
      :hg => :revision
      :http => :flatten, :type, :sha256, :sha1
      :path
    • summary 必須

      pod庫簡介,最多140個字符。

      spec.summary = 'Computes the meaning of life.'
    • description

      pod庫的描述,比summary詳細

      spec.description = <<-DESC
                           Computes the meaning of life.
                           Features:
                           1. Is self aware
                           ...
                           42. Likes candies.
                         DESC
    • screenshots

      用圖片展示pod庫,UI庫可以使用這種方式,其他的建議使用git地址

      # 單個截圖

      spec.screenshot = 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png'

      #多個截圖

      spec.screenshots = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png', 'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]
    • documentation_url

      可選的Pod庫文檔的URL。
      spec.documentation_url = 'http://www.example.com/docs.html'
    • prepare_command

      在Pod下載完之后將執行的bash腳本。該命令可用於創建、刪除和修改任何下載的文件。

      這個命令是在Pod被清理之前且創建Pod項目之前執行的。工作目錄是Pod的根目錄。

      如果是使用:path安裝的pod,這個命令將不會被執行。

      # 指定腳本文件,ruby build_files.rb是腳本文件名

      spec.prepare_command = 'ruby build_files.rb'

      這里用到了ruby命令

      #sed命令是利用script來處理文本文件
      #i :插入
      #s :取代
      #sed 's/要被取代的字串/新的字串/g'
      #第一個sed 語句表示將當前目錄下的所有.h文件中的MyNameSpacedHeader替換成Header

      spec.prepare_command = <<-CMD
      sed -i 's/MyNameSpacedHeader/Header/g' ./**/*.h
      sed -i 's/MyNameOtherSpacedHeader/OtherHeader/g' ./**/*.h
      CMD
    • deprecated

      是否棄用

      spec.deprecated = true
    • deprecated_in_favor_of

      設置棄用的pod庫的新名字,告訴使用者可以搜索新的庫名。

      spec.deprecated_in_favor_of = 'NewMoreAwesomePod'
  • 2、Platform

    規范應該指出使用pod支持的平台和相應的部署目標。如果沒有在子規范中定義,該分組的屬性將繼承父類的值。
    • platform

      不設置表示支持全部平台,如果支持多個平台使用deployment_target命令

      #只能在OS系統使用,要求系統版本至少10.8

      spec.platform = :osx, '10.8'

      #也可以只指定平台,不指定版本

      spec.platform = :ios
    • deployment_target

      平台支持的最低部署版本,可以分別為每個平台指定不同的版本。

      spec.ios.deployment_target = '6.
      spec.osx.deployment_target = '10.8'
  • 3、Build settings

    接下來列出了與構建庫所用的構建環境相關的屬性。

    • dependency

      設置對其他pod或“sub-specification”的依賴。

      依賴關系如果需要指定版本,推薦使用~>

      spec.dependency 'AFNetworking', '~> 1.0'

      #依賴RestKit庫中的子模塊

      spec.dependency 'RestKit/CoreData', '~> 0.20.0'

      #指定某個平台的依賴

      spec.ios.dependency 'MBProgressHUD', '~> 0.5'
    • requires_arc

      支持多平台

      requires_arc允許指定哪個source_files使用ARC。不使用ARC的文件將添加- fno-objc- ARC編譯器標記。

      此屬性的默認值為true。

      #不支持ARC

      spec.requires_arc = false

      #指定某個文件夾支持ARC的

      spec.requires_arc = 'Classes/Arc'

      #指定某些文件支持ARC

      spec.requires_arc = ['Classes/*ARC.m', 'Classes/ARC.mm']
    • frameworks 支持多平台

      需要鏈接的系統frameworks

      spec.ios.framework = 'CFNetwork'

      #多個庫

      spec.frameworks = 'QuartzCore', 'CoreData'
    • libraries 支持多平台

      spec.ios.library = 'xml2'
      #多個庫,其中xml2對應ibxml2.tbd       z對應libz.tbd
      #所以lib庫 省略lib與后綴。
      spec.libraries = 'xml2', 'z'
    • compiler_flags

      支持多平台

      編譯參數 #-D是前綴,-Wno-format這個是gcc編譯警告的參數

      spec.compiler_flags = '-DOS_OBJECT_USE_OBJC=0', '-Wno-format'
    • pod_target_xcconfig

      支持多平台

      設置的參數都會添加到最終的私有pod的target xcconfig文件中

      注意】這是設置pod庫的target 命令

      #OTHER_LDFLAGS  對應buildsetting的other linker flags
      spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }
    • user_target_xcconfig

      支持多平台

      這個會添加到最終target 的xcconfig中,不建議使用,不應該污染用戶項目的構建設置,因為這可能導致沖突。

      如果導入pod時,需要使用clang編譯器標志或預編譯器宏定義,最好使用pod_target_xcconfig,這個只會影響你自己的pod target。

      spec.user_target_xcconfig = { 'MY_SUBSPEC' => 'YES' }

      spec.user_target_xcconfig = { 'MY_SUBSPEC' => 'YES' }
    • prefix_header_contents

      支持多平台

      在pod項目的前綴頭文件中注入的內容,前綴頭文件是模塊的pch文件。

      spec.prefix_header_contents = '#import <UIKit/UIKit.h>'
      spec.prefix_header_contents = '#import <UIKit/UIKit.h>', '#import <Foundation/Foundation.h>'
      //可以將多行內容放到兩個EOS中間
      s.prefix_header_contents = <<-EOS
      #ifdef __OBJC__
      #import <UIKit/UIKit.h>
      #else
      EOS
      s.prefix_header_contents = <<-EOS
      #define HHHH @"測試代碼"
      EOS















      prefix_header_contents的內容會寫到這個文件中














      prefix_header_contents 注入的內容

    • prefix_header_file

      支持多平台

      指定pch文件

      spec.prefix_header_file = 'iphone/include/prefix.pch'
    • module_name

      模塊對外的名稱,如果設置了,主工程鏈接的時候用的是指定的名稱。

      spec.module_name = 'Three20'
  • 4、File patterns 文件格式

    Podspecs應該放在pod庫的根目錄,文件模式不支持遍歷父目錄(. .),要相對於pod庫的根來指定文件的路徑。

    • source_files

      pod庫的源文件

      #包含Classes目錄下的所有.h .m文件

      spec.source_files = 'Classes/**/*.{h,m}'

      #包含Classes和More_Classes目錄下的所有.h .m文件

      spec.source_files = 'Classes/**/*.{h,m}', 'More_Classes/**/*.{h,m}'
      通配符*:

      *    匹配所有文件
      c*  匹配名字以c開頭的文件。
      *c  匹配名字以c結束的文件。
      c   匹配名字含有c的,包含c在開頭和結尾的情況。

      通配符**:

      目錄遞歸地匹配。也就是包含子目錄

      通配符?:

      匹配任何一個字符
      與正則中 /.{1}/ 一致

      通配符[set]:

      匹配多個字符。匹配在字符集中的任何一個字符。
      跟正則中的字符集一樣,也可以取反 [^a-z]

      通配符{p,q}:

      匹配文件名包含p或q的,可以寫兩個或多個字

      通配符\:跳過下一個元字符
      #以JSONKit為例
      "JSONKit.?"    #=> ["JSONKit.h", "JSONKit.m"] "*.[a-z][a-z]" #=> ["CHANGELOG.md", "README.md"] "*.[^m]*" #=> ["JSONKit.h"] "*.{h,m}" #=> ["JSONKit.h", "JSONKit.m"] "*" #=> ["CHANGELOG.md", "JSONKit.h", "JSONKit.m", "README.md"]
    • public_header_files

      pod庫暴露給用戶工程的頭文件。如果不指定那么source_files中的所有header都被認為是公共的。

      spec.public_header_files = 'Headers/Public/*.h'
    • private_header_files

      設置私有頭文件

      spec.private_header_files = 'Headers/Private/*.h'
    • vendored_frameworks

      pod庫中framework的路徑

      spec.ios.vendored_frameworks = 'Frameworks/MyFramework.framework'
      spec.vendored_frameworks = 'MyFramework.framework', 'TheirFramework.framework'
    • vendored_libraries

      pod庫中的靜態庫

      spec.ios.vendored_library = 'Libraries/libProj4.a'
      spec.vendored_libraries = 'libProj4.a', 'libJavaScriptCore.a'
    • resource_bundles

      pod中的資源會以bundle的形式添加到項目中。鍵表示bundles的名稱,值表示文件格式。

      推薦使用,bundle的名稱應該包括Pod的名稱,以盡量減少名稱沖突的幾率。

      為了提供不同的資源,每個平台都必須使用帶有名稱空間的包。

      spec.ios.resource_bundle = { 'MapBox' => 'MapView/Map/Resources/*.png' }
      #多個路徑
      spec.resource_bundles = {
          'MapBox' => ['MapView/Map/Resources/*.png'],
          'OtherResources' => ['MapView/Map/OtherResources/*.png']
      }
    • resources

      使用此屬性指定的資源直接復制到客戶端目標,因此不會被Xcode優化,推薦使用resource_bundles

      spec.resource = 'Resources/HockeySDK.bundle'
      #多個,注意是resources復數形式,也可以不加[ ]
      spec.resources = ['Images/*.png', 'Sounds/*']
    • exclude_files

      排除在外的文件,與source_files相對

      spec.ios.exclude_files = 'Classes/osx'

      spec.exclude_files = 'Classes/**/unused.{h,m}'

    • preserve_paths

      下載后不會被刪除的文件。默認刪除不匹配任何file pattern的文件。

      spec.preserve_path = 'IMPORTANT.txt'
      spec.preserve_paths = 'Frameworks/*.framework'
    • module_map

      pod被組裝成framework的時候module map文件可能用的上,默認的cocoapod會基於public headers 創建一個module map。

      spec.module_map = 'source/module.modulemap'
  • 5、Subspecs

    可以理解為pod庫中的子模塊。一個庫可以指定對另一個庫的依賴,另一個庫的子規范,或者是它自身的子規范。

    • subspec

      用來表示pod庫模塊的規范

      一方面,spec自動將subspecs作為依賴項(除非指定了默認的子規范)。

      另一方面,“子規范”繼承了父屬性的值,因此可以在父類中指定屬性的共同值。

      #安裝ShareKit,會包括ShareKit / Evernote,ShareKit / Facebook等,因為它們被定義為subspecs。

      pod 'ShareKit', '2.0'

      #只安裝ShareKit中的某個子庫,這種情況下subspec需要源文件,依賴和其他在根spec中定義的屬性,不過cocoapods能幫我們處理這些問題。

      pod 'ShareKit/Twitter', '2.0' 
      pod 'ShareKit/Pinboard', '2.0'
      #有不同源文件的
      subspec subspec 'Twitter' do |sp| 
      sp.source_files = 'Classes/Twitter'
      end
      subspec 'Pinboard' do |sp| 
      sp.source_files = 'Classes/Pinboard'
      end
      #子庫的spec依賴其他subspec·
      Pod::Spec.new do |s|
        s.name = 'RestKit'
        s.subspec 'Core' do |cs|
          cs.dependency 'RestKit/ObjectMapping'
          cs.dependency 'RestKit/Network'
          cs.dependency 'RestKit/CoreData'
        end
      
        s.subspec 'ObjectMapping' do |os|
        end
      end 
      #嵌套的Subspec
      Pod::Spec.new do |s|
        s.name = 'Root'
        s.subspec 'Level_1' do |sp|
          sp.subspec 'Level_2' do |ssp|
          end
        end
      end
    • default_subspecs

      一組用來作為依賴項的subspec名稱。如果沒有指定則要求其所有的subspec作為依賴項。

      默認情況下pod庫應該提供完整的庫。用戶可以根據需求微調他們的依賴項,排除不需要的子模塊(subspec)。

      這個屬性很少用。

      spec.default_subspec = 'Core'
      spec.default_subspecs = 'Core', 'UI'
  • 6、Multi-Platform support

    所有標識支持多平台的,都可以針對平台設置參數

    spec.ios.resources = 'Resources_ios/**/*.png'
    spec.osx.source_files = 'Classes/osx/**/*.{h,m}'
    spec.tvos.source_files = 'Classes/tvos/**/*.{h,m}'
    spec.watchos.source_files = 'Classes/watchos/**/*.{h,m}'

    至此,只是對官方文檔的簡單翻譯,更多的可以參照ruby語法。

四,參考      

  CocoaPods官網


免責聲明!

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



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