CocoaPods 創建私有倉庫,使用和維護(2019更新)

前方

隨着項目的業務發展擴大,公司決定另外開一個APP,但是會用到現在的一些公共組件,照搬過去用當然沒問題,但是有一個問題,如果以後維護公共組件那還不得幾個APP都去修改,所以想到了封裝成私有庫便於維護。

準備工作

1、CocoaPods,Cocoapods是出色的一個iOS依賴管理工具,使用它可以方便的管理和更新項目中所使用到的第三方庫,以及將自己的項目中的公共組件交由它去管理。
2、Git版本控制管理,項目中使用的是gitlab,註冊或使用現有的賬號。

一、創建

1)、創建私有索引庫

1、首頁創建一個存放版本描述文件的git倉庫,去https://gitlab.com/創建一個遠程私有倉庫(免費)

這裏寫圖片描述

這裏寫圖片描述
這樣遠程的索引庫就創建完成了

2、在終端把遠程的私有索引庫添加到本地索引

$ pod repo add DRSpecs https://gitlab.com/PrivateSpecs/DRSpecs.git

然後會要求輸入賬號和密碼
這裏寫圖片描述
然後查看本地的索引庫

$ pod repo

在這裏插入圖片描述
在 Finder 目錄 ~/.cocoapods/repos,master是cocoaPods上版本庫的列表
這裏寫圖片描述

2)、創建代碼庫

1、這裏我是把代碼與版本描述文件分離,這樣結構就比較清晰

代碼庫創建與版本庫創建是一樣的,不同之處就是多了項目工程文件,這裏的項目工程是編輯我們的私有版本庫文件

這裏寫圖片描述
這裏寫圖片描述

先cd到要創建項目的目錄合適位置然後執行

$ pod lib create DRCategories

之後它會問你5個問題:
What language do you want to use?? [ Swift / ObjC ]
Would you like to include a demo application with your library? [ Yes / No ]
Which testing frameworks will you use? [ Specta / Kiwi / None ]
Would you like to do view based testing? [ Yes / No ]
What is your class prefix?
1.你用什麼語言?
2.是否需要一個例子工程;
3.選擇一個測試框架;
4.是否基於View測試;
5.類的前綴;

5個問題的具體介紹可以去看官方文檔,我這裏選擇的是1.ObjC; 2.yes; 3.Specta/Expecta; 4.yes; 5.DR。 問完這4個問題他會自動執行pod install命令創建項目並生成依賴。

然後本地可以看到
這裏寫圖片描述

提供給外部使用的文件都放在這裏,可供外部使用的文件全部放在Classes文件夾中
這裏寫圖片描述

這裏替換試一下
這裏寫圖片描述

如果前面的步驟裏面選擇了添加demo,模板庫已經默認幫我們在Podfile中指定了DRCategories.podspec的位置

pod ‘DRCategories’, :path => ‘…/’

那麼在Example路徑下,執行pod install操作,使得可以安裝和測試我們的代碼
$ cd /Users/maple_li/DRCategories/Example

$ pod install

到這裏,我們可以在模板測試工程中進行修改和操作!如果測試沒什麼問題,就可以將podspec文件上傳到私有索引庫,在此之前需要對spec修改。

2、編輯podspec文件,直接雙擊DRCategories.podspec文件,用Xcode打開進行編輯

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  s.name         = "DRCategories" #名稱
  s.version      = "0.1.0"        #版本
  s.summary      = "常用類別"      #簡介

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!
  s.description  = "集合了開發中常用的類別,包括NSSting、UIView、NSObject、NSArray、NSDictionary……"  #詳細描述

  s.homepage     = "https://gitlab.com/ModulePlugin/DRCategories"   #主頁地址
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" #截圖地址


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See http://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = { :type => 'MIT', :file => 'LICENSE' }         #開源協議
  # s.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  s.author             = { "LeeCoach" => "********@yeah.net" }     #作者
  # Or just: s.author    = "LeeCoach"
  # s.authors            = { "LeeCoach" => "DRCategories" }
  # s.social_media_url   = "http://twitter.com/XXX"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # s.platform     = :ios
   s.platform     = :ios, "8.0"  #支持的平臺及版本

  #  When using multiple platforms
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "https://gitlab.com/ModulePlugin/DRCategories.git", :tag => s.version.to_s }   #代碼地址


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  s.source_files  = "DRCategories", "DRCategories/**/*.{h,m}" #代碼源文件地址,**/*表示Classes目錄及其子目錄下所有文件,如果有多個目錄下則用逗號分開,如果需要在項目中分組顯示,這裏也要做相應的設置
  s.exclude_files = "DRCategories/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # s.resource  = "icon.png"  #資源路徑
  # s.resources = "Resources/*.png"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

   s.framework  = "UIKit"  #使用到的系統庫
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

   s.requires_arc = true      #是否使用ARC,如果指定具體文件,則具體的問題使用ARC

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"    #依賴關係,該項目所依賴的其他庫,如果有多個需要填寫多個s.dependency

3、編輯完podspec文件後,需要驗證一下這個文件是否可用,如果有任何WARNING或者ERROR都是不可以的,它就不能被添加到Spec Repo中,不過xcode的WARNING是可以存在的,驗證需要執行一下命令

$ pod lib lint

當你看到

 -> DRCategories (0.1.0)

DRCategories passed validation.

時,說明驗證通過了

4、因爲podspec文件中獲取Git版本控制的項目還需要tag號,所以我們要打上一個tag

$ git status
$ git add .
$ git commit -m '編輯spec文件'
$ git remote add origin https://gitlab.com/ModulePlugin/DRCategories.git #添加遠端倉庫
$ git push origin master  #提交到遠端
$ git tag -m "first release" "0.1.0" (要與DRCategories.podspec文件中的tag值保持一致)
$ git push --tags     #推送tag到遠端倉庫

5、向Spec Repo提交podspec
向Spec Repo提交podspec需要完成兩點一個是podspec必須通過驗證無誤,在一個就是刪掉無用的註釋(這個不是必須的,爲了規範還是刪掉吧)。 向我們的私有Spec Repo提交podspec只需要一個命令

$ pod repo push DRSpecs DRCategories.podspec

執行後,當出現下面這樣纔算是成功了

Validating spec
 -> DRCategories (0.1.0)

Updating the `DRSpecs' repo

From https://gitlab.com/PrivateSpecs/DRSpecs
 * [new branch]      master     -> origin/master

Adding the spec to the `DRSpecs' repo

 - [Add] DRCategories (0.1.0)

Pushing the `DRSpecs' repo

GitLab端
這裏寫圖片描述

本地Repos
這裏寫圖片描述


二、私有庫的使用

1、用Xcode打開編輯Podfile文件
這裏寫圖片描述

2、添加私有庫索引到本地,先$ cd 到項目路徑,然後執行下面語句

$ pod install

加載完成可以看到代碼已經整合到我們的項目中了

3、查看本地的pod庫索引

$ pod repo 

這裏寫圖片描述

通過$ pod search DRCategories 可以查詢到自己維護的私有庫
這裏寫圖片描述

4、使用與其它第三方庫一樣,只是私有庫版本更新維護是自己處理


三、私有庫維護

1.添加新文件,還是依然放在Classes文件夾下
在這裏插入圖片描述
2. cd 到 Example文件夾下,即測試的demo目錄位置,執行

$ pod install

進行測試代碼有無問題

3.修改podspec文件,最重新的版本號
在這裏插入圖片描述
4.校驗podspec文件,本地難文件有效性

$ pod lib lint

或加上代碼庫名

$ pod lib lint DRCategories.podspec

若要忽略警告

$ pod lib lint DRCategories.podspec --allow-warnings

出現如下,表示驗證通過
在這裏插入圖片描述
5.提交項目到遠程代碼庫

$ git add .
$ git commit -m “更新提交”
//$ git pull origin master //如果是多人協同工作,先拉一下
$ git push origin master

6.打標籤

$ git tag -m “更新版本標籤” -a 0.1.4
$ git push --tags

查看所有標籤 git tag
在這裏插入圖片描述

7.驗證本地和遠程文件有效性 ,忽略警告可加--allow-warnings

$ pod spec lint --allow-warnings

驗證如果成功如下:
在這裏插入圖片描述
8.向私有的spec repo遠程倉庫提交podspec

$ pod repo push DRSpecs DRCategories.podspec

出現如下,說明成功了
在這裏插入圖片描述

9.查看是否已經成功

$ pod search DRCategories

在這裏插入圖片描述

10.驗證podspec報錯

  • WARN | summary: The summary is not meaningful.
    - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use --verbose for more information.
    - NOTE | xcodebuild: note: Using new build system
    - NOTE | [iOS] xcodebuild: note: Planning build
    - NOTE | [iOS] xcodebuild: note: Constructing build description
    - NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target ‘App’)
    - NOTE | [iOS] xcodebuild: fatal error: ‘UIFont+DRFont.h’ file not found
    - NOTE | [iOS] xcodebuild: Headers/Public/DRCategories/DRCategories.h:12:9: fatal error: ‘UIFont+DRFont.h’ file not found

一般都podspec配置沒改好,根據提示修改,再驗證

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