cocoapods私有庫的創(chuàng)建和使用

使用cocoapods私有庫能夠更好的幫助我們實現(xiàn)組件化開發(fā),大體分為五部分+坑??

官網(wǎng)鏈接

1.私有倉庫pod repo的創(chuàng)建

  • 私有倉庫本地安裝
$ pod repo add YourRepo xxx.xxx.git

YourRepo是你自己私有庫的名稱
xxx.xxx.git是你私有庫的地址
私有庫是用來存放podspec索引文件的倉庫,不存放代碼,一個私有庫可以對應(yīng)Npodspec

  • 查看本地私有倉庫
$ cd ~/.cocoapods/repos
$ ls
或者
$ pod repo

可查看此文件夾下,除了官方的master倉庫,還會有自己剛剛安裝的YourRepo私有倉庫。

  • 私有倉庫的移除
pod repo remove [name]

2.代碼倉庫的創(chuàng)建

  • cd 本地創(chuàng)建的文件夾
  • 使用cocoapods命令:pod lib create xxxxLib

xxxxLib是你自己組件代碼的名稱
這個命令會自動生成一套組件代碼工程測試代碼,并且有Git管理
還會生成podspec索引文件.

  • 根據(jù)出現(xiàn)的下拉菜單,按需填寫
屏幕快照 2019-02-15 下午2.08.42.png

上面依次對應(yīng)平臺類型 、 語言 、 Demo 、 測試框架 、 界面測試 、 類前綴 等,填完之后Enter,會生成如下圖的工程目錄代碼。

屏幕快照 2019-02-15 下午2.15.06.png
  • 把 ReplaceMe.m 文件替換你自己的組件代碼

  • cd 到 Example 然后執(zhí)行 pod install/pod update

需要需要重新pod install,因為不重新pod install,Example工程根本不知道Pod更新了,pod install的作用:重新讓pod庫與所依賴的工程文件產(chǎn)生關(guān)聯(lián)。

  • cd 到 NJFLibTest 把代碼上傳到遠(yuǎn)程倉庫

注意文件層級關(guān)系 NJFLibTest 不是與Example 同級的文件,是上一層文件 ,我們可以用 command+shift+.查看,一般是有.git隱藏文件的上一級文件
遠(yuǎn)程倉庫不需要創(chuàng)建gitignore文件,因為pod lib創(chuàng)建了
提交自己倉庫代碼到遠(yuǎn)程倉庫

git status : 查看狀態(tài),如果有不想要的文件,可以用gitignore忽略掉
提交到本地緩存區(qū)  $git add .
提交到本地倉庫    $git commit -m "初始化提交"
查看遠(yuǎn)程倉庫地址  $git remote -v(查看有沒有遠(yuǎn)程地址)
綁定遠(yuǎn)程地址      $git remote add origin xxxx.xxx.git
推送自己代碼到遠(yuǎn)程倉庫 $git push -u origin master

刪除遠(yuǎn)程倉庫地址  $git remote rm origin

如果推送成功,可以在遠(yuǎn)程倉庫看到自己的提交

  • 添加tag
$ git tag -a 0.0.1 -m '0.1.0'
$ git push --tags

查看本地tag
$ git tag

需要注意的是,這個tag需要與podspec里的version號一致,否則在提交podspecpod遠(yuǎn)程倉庫的時候會出錯。

  • 修改podspec索引文件
Pod::Spec.new do |s|
  s.name             = 'NJFLibTest'
  s.version          = '0.0.1'                ///'注意,要與你打的tag一致'
  s.summary          = 'A short description of NJFLibTest.'
s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/XXXXX/NJFLibTest'   ///'后綴不要帶.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Miko-J' => 'niujf@kingnet.com' }
  s.source           = { :git => 'https://github.com/XXXX/NJFLibTest.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
  s.ios.deployment_target = '8.0'
  s.source_files = 'NJFLibTest/Classes/**/*'
  # s.resource_bundles = {
  #   'NJFLibTest' => ['NJFLibTest/Assets/*.png']
  # }
  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 3.2.1'

s.version對應(yīng)的版本號就是上面打的tag
s.homepages.source里面的路徑要替換成你的 遠(yuǎn)程倉庫地址
**表示所有文件
*表示通配符,可有可無.

  • 執(zhí)行$ pod lib lint 本地檢查pod spec合法性

如果要出現(xiàn)error要修改,如果只有警告可以使用
$ pod lib lint --allow-warnings
如果出現(xiàn) NJFLibTest passed validation 則說明驗證通過

  • 執(zhí)行$ pod spec lint 遠(yuǎn)程檢查pod spec合法性

如果要出現(xiàn)error要修改,如果只有警告可以使用
$ pod spec lint --allow-warnings
如果出現(xiàn) NJFLibTest.podsepc passed validation 則說明驗證通過

3.把自己私有庫的索引podsepc添加到自己私有庫中

$ pod repo push YourRepo ~/Desktop/xxxx/NJFLibTest.podspec

如果執(zhí)行成功,刷新一下遠(yuǎn)程索引庫,可以看到你更新的NJFLibTest/0.01

4.新建項目repoTest,引用自己的私有索引倉庫

Podfile文件中編輯如下

# Uncomment the next line to define a global platform for your project
 platform :ios, '9.0'
 use_frameworks!
source 'https://github.com/xxxxxx/xx.git'  //自己私有庫source源
source 'https://github.com/CocoaPods/Specs.git' //官方的source源
target 'repoTest' do
   pod 'NJFLibTest'  //自己創(chuàng)建的組件
end

然后pod install就可以了??

5.進(jìn)階

  • 更新代碼倉庫或者添加依賴庫

更新代碼倉庫:很簡單,從 ReplaceMe.m 文件替換你自己的組件代碼,這里開始,跟上面的步驟一樣

1.更新代碼
2.進(jìn)Example,執(zhí)行pod update
3.提交代碼到遠(yuǎn)程倉庫
4.打tag,更新.podspec文件,檢查pod sepc的合法性
5.添加索引到自己的私有庫中

添加依賴庫: 打開.podspec文件 執(zhí)行s.dependency 'AFNetworking', '~> 3.2.1'

  • 子庫Subspecs

先看一個簡單的例子:

$ pod search 'SDWebImage'
image.png

可以看到,如果我們只需要用到SDWebImage中的Core功能,那么并不需要將整個SDWebImage都下載下來,在Podfile中將pod 'SDWebImage'改為 pod SDWebImage/Core即可單獨使用這一功能

子庫格式:

s.subspec '子庫名稱' do |別名|

end

因為這里已經(jīng)分離出子庫了,所以s.source_filess.dependency就不能這么使用了,需要我們在子庫里分別指定,所以我們直接把原來的s.source_filess.dependency都注釋掉。寫法參考如下:

屏幕快照 2019-04-04 下午4.40.04.png

  # s.source_files = 'NJFLibTest/Classes/**/*'
  # s.dependency 'AFNetworking', '~> 3.2.1'

  s.subspec 'ThirdLibs' do |t|
    t.source_files = 'NJFLibTest/Classes/ThirdLibs/**/*'
    t.dependency 'AFNetworking', '~> 3.2.1'
end
  s.subspec 'Category' do |c|
    c.source_files = 'NJFLibTest/Classes/Category/**/*'
end
  s.subspec 'Configuration' do |c|
    c.source_files = 'NJFLibTest/Classes/Configuration/**/*'
end

如果更新索引庫后,可以在遠(yuǎn)程索引庫中看到最新的版本

每次改動記得tag值要變一下,如果執(zhí)行 $ pod spec lint 遇到錯誤file patterns: The source_files pattern did not match any file. 更新tag在嘗試一下

搜索私有庫

$ pod search NJFLibTest

可以看到最終結(jié)果:

-> NJFLibTest (0.0.3)
   A short description of NJFLibTest.
   pod 'NJFLibTest', '~> 0.0.3'
   - Homepage: https://github.com/XXXXXX
   - Source:   https://github.com/XXXXX.git
   - Versions: 0.0.3, 0.0.1 [NJF_PrivateRepo repo]
   - Subspecs:
     - NJFLibTest/AFNTool (0.0.3)
     - NJFLibTest/Category (0.0.3)
     - NJFLibTest/Configuration (0.0.3)

在項目中如果引用所有的庫目錄結(jié)構(gòu)應(yīng)該是這個樣子,引用個別子庫我就不貼了,大家試一下就知道了


屏幕快照 2019-04-04 下午4.59.42.png

可能遇到的坑

  • 沖突
To https://github.com/Miko-J/NJF_RepoCode.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/Miko-J/NJF_RepoCode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

遇到這種情況,我們先拉一下遠(yuǎn)程倉庫的代碼

$ git pull origin master 

如果發(fā)現(xiàn)

fatal: refusing to merge unrelated histories //'分支拒絕了無歷史關(guān)聯(lián)的合并'

我們可以使用下面的命令

$ git pull origin master --allow-unrelated-histories

這個時候我們要查看一下狀態(tài)

$ git status

如果有沖突,就解決沖突

On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both added:      .gitignore
    both added:      LICENSE

這里lz簡單粗暴直接本地文件覆蓋遠(yuǎn)程

$ git push -u origin master -f

如果能提交,可以刷新一下遠(yuǎn)程倉庫的代碼看一下

  • 庫的索引podsepc添加到自己私有庫中出現(xiàn)XXXXXX.podspec specification does not validate.
$ pod repo push YourRepo ~/Desktop/xxxx/NJFLibTest.podspec --verbose --use-libraries --allow-warnings

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容