Cocoapods工具03 -- 创建pod库

Cocoapods的本地文件目录

  • 在命令行中依次执行下列命令,可查看到Cocoapods的本地文件目录详细内容;
Snip20210822_179.png
  • 输入git remote -v 可以看到github远程仓库地址如下所示:
origin  https://github.com/CocoaPods/Specs.git (fetch)
origin  https://github.com/CocoaPods/Specs.git (push)
  • pwd打印出本地文件路径,,进入Spec文件夹,如下所示:
Snip20210822_183.png
  • /Users/ljj/.cocoapods/repos/master/Specs路径下存储了各种不同第三方库的不同版本的信息,每个第三方库会有不同的版本号,每个版本号对应一个Json文件,其描述了每个版本的框架信息,配置,源码的下载地址等等,如下所示:
{
  "name": "CAIStatusBar",
  "version": "0.0.1",
  "summary": "A simple indicator",
  "homepage": "https://github.com/apple5566/CAIStatusBar.git",
  "license": "MIT",
  "authors": {
    "apple5566": "[email protected]"
  },
  "platforms": {
    "ios": "6.0"
  },
  "source": {
    "git": "https://github.com/apple5566/CAIStatusBar.git",
    "tag": "0.0.1"
  },
  "source_files": "CAIStatusBar/**/*.{h,m}",
  "resources": "CAIStatusBar/CAIStatusBar.bundle",
  "requires_arc": true
}

如何创建与编写podspec文件

创建自己的pod私有库

  • 第一步:创建远程GitHub索引库;
Snip20210823_193.png
Snip20210823_194.png
  • 远程GitHub索引库的地址为https://github.com/Liyanyan33/YYPrivateSpecRepo.git
  • 第二步:将GitHub远程索引库下载到本地,终端执行pod repo add YYPrivateSpecRepo https://github.com/Liyanyan33/YYPrivateSpecRepo.git
Snip20210823_195.png
  • 执行完成之后,本地的pod索引库如下所示:
Snip20210823_197.png
  • 此时的本地的YYPrivateSpecRepo索引库是空的,还没有实质性的内容;
  • 第三步:创建远程GitHub代码库YYArrayLib,如下所示:
Snip20210823_198.png
Snip20210823_199.png
  • 远程代码仓库地址为:https://github.com/Liyanyan33/YYArrayLib.git
  • 第四步:创建本地代码库YYArrayLib,利用pod模版进行创建 pod lib create YYArrayLib回答相关问题,如下所示:
Snip20210823_200.png
  • 创建成功后的本地代码库YYArrayLib的文件结构如下:
Snip20210823_201.png
  • 删除YYArrayLib/Classes文件夹中的ReplaceMe.m文件,导入自己的代码文件;
Snip20210823_202.png
Snip20210823_203.png
  • 编辑YYArrayLib.podspec文件,编辑之后的文件内容如下:
#
# Be sure to run `pod lib lint YYArrayLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'YYArrayLib'
  s.version          = '0.1.0'
  s.summary          = 'iOS about array lib'

# 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      = <<-DESC
iOS OC array lib of liyanyan
                       DESC

  s.homepage         = 'https://github.com/Liyanyan33/YYArrayLib.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '19053601' => '[email protected]' }
  s.source           = { :git => 'https://github.com/Liyanyan33/YYArrayLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '9.0'

  s.source_files = 'YYArrayLib/Classes/**/*'
  
  # s.resource_bundles = {
  #   'YYArrayLib' => ['YYArrayLib/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end
  • cd 到Example文件目录下,执行pod install,让Example工程能成功执行,Example是Demo工程;
  • cd到YYArrayLib文件路径下, 本地验证YYArrayLib.podspec文件,执行pod lib lint --allow-warnings,注意当当前私有库依赖于自己创建的其他私有库,在进行spec文件本地验证时,需要通过sources关键字,指明其他私有库的地址,否则会因为找不到其他私有库而报错,例如pod lib lint --sources=https://gitee.com/liyanyan3377/xsfprivate-repo-spec.git,https://github.com/CocoaPods/Specs.git --allow-warnings,将自己的私有库地址与公有库地址都写明,中间用逗号隔开;
Snip20210823_204.png
  • 第五步:将本地代码YYArrayLib,提交到远程GitHub仓库,https://github.com/Liyanyan33/YYArrayLib.git,依次执行以下指令;
    • git remote add origin https://github.com/Liyanyan33/YYArrayLib.git
    • git add .
    • git commit -m 'first commit'
    • git push -u origin master 会报错
Snip20210823_206.png
  • 出现报错的原因在于远程仓库已经有README.mdLICENSE文件与本地代码库的文件存在冲突,所以必须要合并成一个文件,需执行以下命令,进行强制合并;
    • git push -f origin master
  • 提交成功后,远程仓库的状态如下:
Snip20210823_207.png
  • 需要创建一个README.md文件,点击Add a README,然后将本地代码库的README.md文件的内容复制过来,创建提交成功后,远程仓库的状态如下:
Snip20210823_208.png
  • 将本地的代码删除,再利用sourceTree从远程仓库重新下载一份,如下所示:
Snip20210823_209.png
  • 然后必须打tag,上传版本号;
    • git tag -a 0.1.0 -m "版本0.1.0"
    • git push --tags
  • 必须打tag 且版本号与索引文件YYArrayLib.podspec中的s.version = '0.1.0'保持一致
  • 再看远程仓库的状态,如下所示:
Snip20210823_210.png
  • 第六步:远程验证YYArrayLib.podspec文件,执行pod spec lint --use-libraries --allow-warnings,若依赖其他自建的私有库需使用sources关键字指明其他私有库的地址,例如pod spec lint --sources=https://gitee.com/liyanyan3377/xsfprivate-repo-spec.git,https://github.com/CocoaPods/Specs.git --allow-warnings
Snip20210823_212.png
  • 第七步:将本地索引文件YYArrayLib.podspec,推送到本地pod索引库与GitHub远程索引仓库,执行pod repo push YYPrivateSpecRepo YYArrayLib.podspec --use-libraries --allow-warnings
Snip20210823_213.png
  • 查看本地私有pod索引库YYPrivateSpecRepo的状态如下:
Snip20210823_214.png
  • 查看远程pod索引仓库的状态,如下所示:
Snip20210823_215.png
  • 至此属于自己私有的pod库就创建完毕了,下面通过一个测试工程来导入自己私有的pod库YYArrayLib
  • 创建一个新的工程Test_ArrayLib,然后cd到工程路径下执行pod init命令,生成一个Podfile文件,编辑Podfile文件,内容如下:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

source 'https://github.com/Liyanyan33/YYPrivateSpecRepo.git'

target 'Test_ArrayLib' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Test_ArrayLib

  pod 'YYArrayLib'

end
  • YYPrivateSpecRepo是自己的私有库,所以必须通过source关键字,导入私有库的远程仓库路径;
  • 最后执行pod install,执行结果如下所示:
Snip20210823_216.png
Snip20210823_218.png
  • 后续如果自己的pod私有库 有代码更新,则需要完成以下两个步骤:
  • 第一个步骤:修改本地库的代码完成之后,再推送到远程代码仓库,依次执行下面的指令:
    • git status 查看修改的所有文件
    • git add .
    • git commit -m '提交0.1.1版本'
    • git push origin master
    • git tag -a 0.1.1 -m "版本0.1.1"
    • git push --tags 必须打tag
  • 执行完成后,远程代码仓库的状态如下:
Snip20210824_220.png
  • 第二个步骤:修改本地库的索引文件YYArrayLib.podspec,将s.version = '0.1.1' ,如下所示:
#
# Be sure to run `pod lib lint YYArrayLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'YYArrayLib'
  s.version          = '0.1.1'
  s.summary          = 'iOS about array lib'

# 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      = <<-DESC
iOS OC array lib of liyanyan
                       DESC

  s.homepage         = 'https://github.com/Liyanyan33/YYArrayLib.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '19053601' => '[email protected]' }
  s.source           = { :git => 'https://github.com/Liyanyan33/YYArrayLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '9.0'

  s.source_files = 'YYArrayLib/Classes/**/*'
  
  # s.resource_bundles = {
  #   'YYArrayLib' => ['YYArrayLib/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end
  • 然后命令行执行pod repo push YYPrivateSpecRepo YYArrayLib.podspec --use-libraries --allow-warnings,将索引文件YYArrayLib.podspec 推送到本地私有pod索引仓库YYPrivateSpecRepo和远程私有pod索引仓库,如下所示:
Snip20210824_223.png
  • 成功后查看本地索引库状态,如下所示:
Snip20210824_222.png
  • 远程索引库状态:
Snip20210824_221.png
  • 用一张图描述创建私有pod库过程中涉及的元素,如下所示:
Snip20210824_219.png

创建自己的pod公有库

  • 创建pod公有库,例如像AFNetworking那样的公有的网络库,其创建步骤与创建私有pod库的步骤类似,且相对还要简单一点,因为pod公有库不需要我们自己创建pod索引库,直接利用官方提供的索引库就可以了,我们只需要创建代码仓库;
  • 第一步:创建远程代码仓库,其地址为:https://github.com/Liyanyan33/iOS-OC-Public-YYNSStringLib.git
  • 第二步:利用pod模版,在本地创建代码工程,执行pod lib create YYNSStringLib,回答相关问题;
  • 删除YYNSStringLib/Classes文件夹中的ReplaceMe.m文件,导入自己的代码文件;
Snip20210824_224.png
  • 编辑YYNSStringLib.podspec文件,编辑之后的文件内容如下:
#
# Be sure to run `pod lib lint YYNSStringLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'YYNSStringLib'
  s.version          = '0.1.0'
  s.summary          = 'YYNSStringLib of NSString'

# 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      = <<-DESC
YYNSStringLib is about OC NSString add extra extesion
                       DESC
                       
  s.homepage         = 'https://github.com/Liyanyan33/iOS-OC-Public-YYNSStringLib.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'liyanyan33' => '[email protected]' }
  s.source           = { :git => 'https://github.com/Liyanyan33/iOS-OC-Public-YYNSStringLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '9.0'

  s.source_files = 'YYNSStringLib/Classes/**/*'
  
  # s.resource_bundles = {
  #   'YYNSStringLib' => ['YYNSStringLib/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end
  • cd 到Example文件目录下,执行pod install,让Example工程能成功执行,Example是Demo工程;
  • cd到YYNSStringLib文件路径下, 本地验证YYNSStringLib.podspec文件,执行pod lib lint --allow-warnings,验证通过后;
  • 第三步:将本地代码YYNSStringLib,提交到远程GitHub仓库,https://github.com/Liyanyan33/iOS-OC-Public-YYNSStringLib.git,依次执行以下指令;
    • git remote add origin https://github.com/Liyanyan33/iOS-OC-Public-YYNSStringLib.git
    • git add .
    • git commit -m 'first commit'
    • git push -f origin master //强制执行
    • git tag -a 0.1.0 -m "版本0.1.0"
    • git push --tags //必须打tag
  • 然后在GitHub远程仓库,添加README.md文件,复制拷贝本地的README.md文件内容,创建提交成功后,删除本地的代码,重新再下载一份代码工程;
  • 第四步:验证 远程的YYNSStringLib.podspec文件,执行pod spec lint --use-libraries --allow-warnings,验证成功后;
  • 第五步:将本地的YYNSStringLib.podspec索引文件 提交到推送到
    Cocoapods的官方索引库,这是与制作私有pod的主要区别,我们需要向Cocoapods注册自己的账号信息,执行pod trunk register 邮箱 '用户名' --description='MacBook Pro',之后会邮箱会收到一封邮件,点击邮件中的链接就可注册成功,然后可通过pod trunk me,查看自己的Cocoapods的账号信息;
Snip20210824_226.png
  • 执行pod trunk push YYNSStringLib.podspec --allow-warnings 可将YYNSStringLib.podspec文件提交到Cocoapods官方索引库;
Snip20210824_228.png
  • 第六步:执行pod search YYNSStringLib 即搜索YYNSStringLib第三方库,如果没有搜索到,报错如下:
    Snip20210824_229.png
  • 如果远程验证通过,但是搜索不到,是因为没有添加进pod search缓存文件,删掉缓存重建即可!命令如下:
# 切换到CocoaPods目录
cd ~/Library/Caches/CocoaPods/
# 查看该目录下有Pods和search_index.json两个文件
ls

# 删除缓存文件
rm search_index.json

# 重新搜索
pod search YYNSStringLib
Snip20210824_230.png
  • 表明向Cocoapods官方提交YYNSStringLib库 成功;
  • 若公有库YYNSStringLib需要更新,同样需要执行两个步骤,第一个步骤与更新私有库的完全相同,不同点在于第二个步骤,将修改后的YYNSStringLib.podspec提交给Cocoapods官方索引库,执行pod trunk push YYNSStringLib.podspec --allow-warnings 即可!

pod的常见命令

  • pod repo list 查看本地所有的索引库
Snip20210824_231.png
  • 看到我的Mac Pro本地有5个Cocoapods索引库;
  • pod repo remove [xxx] 移除本地指定的索引库xxx
  • pod repo update 更新本地所有的索引库
  • pod repo update [xxx] 更新本地指定的索引库xxx
参考文章

https://leomobiledeveloper.blog.csdn.net/article/details/85226147

你可能感兴趣的:(Cocoapods工具03 -- 创建pod库)