cocoapods私有庫的創建和使用

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

官網鏈接

1.私有倉庫pod repo的創建

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

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

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

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

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

2.代碼倉庫的創建

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

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

  • 根據出現的下拉菜單,按需填寫
屏幕快照 2019-02-15 下午2.08.42.png

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

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

  • cd 到 Example 然后執行 pod install/pod update

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

  • cd 到 NJFLibTest 把代碼上傳到遠程倉庫

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

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

刪除遠程倉庫地址  $git remote rm origin

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

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

查看本地tag
$ git tag

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

  • 修改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對應的版本號就是上面打的tag
s.homepages.source里面的路徑要替換成你的 遠程倉庫地址
**表示所有文件
*表示通配符,可有可無.

  • 執行$ pod lib lint 本地檢查pod spec合法性

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

  • 執行$ pod spec lint 遠程檢查pod spec合法性

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

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

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

如果執行成功,刷新一下遠程索引庫,可以看到你更新的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'  //自己創建的組件
end

然后pod install就可以了??

5.進階

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

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

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

添加依賴庫: 打開.podspec文件 執行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

因為這里已經分離出子庫了,所以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

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

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

搜索私有庫

$ pod search NJFLibTest

可以看到最終結果:

-> 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)

在項目中如果引用所有的庫目錄結構應該是這個樣子,引用個別子庫我就不貼了,大家試一下就知道了


屏幕快照 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.

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

$ git pull origin master 

如果發現

fatal: refusing to merge unrelated histories //'分支拒絕了無歷史關聯的合并'

我們可以使用下面的命令

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

這個時候我們要查看一下狀態

$ 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簡單粗暴直接本地文件覆蓋遠程

$ git push -u origin master -f

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

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

參考鏈接

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

推薦閱讀更多精彩內容