创建私有Pod仓库

134 阅读3分钟

iOS创建私有库步骤

  1. 在终端上移动到你想创建的pod库工程路径,并执行pod库的创建
$ cd Desktop 
$ pod lib create TestB

回车后会有几个配置问题

# 选择平台
What platform do you want to use?? [ iOS / macOS ]
 > ios

# 选择编程语言
What language do you want to use?? [ Swift / ObjC ]
 > objc

# 是否创建一个demo,最好添加一个
Would you like to include a demo application with your library? [ Yes / No ]
 > yes

# 测试框架
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > none

# 要不要UI测试
Would you like to do view based testing? [ Yes / No ]
 > yes

# 类名前缀
What is your class prefix?
 > TR
Running pod install on your new library.

Analyzing dependencies
Downloading dependencies
Installing FBSnapshotTestCase (2.1.4)
Installing TestB (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `TestB.xcworkspace` for this project from now on.
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'TestB/Example/TestB.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.

恭喜你,Pod私有库创建成功。一般来说创建成功会自动打开项目

在TestB文件夹下面有两个子文件夹

Assets:存放图片资源等等
Classes:存放源码,默认会有一个ReplaceMe.m文件
  1. 添加你要添加的文件

\

  1. 编辑CocoaPods的配置文件(后缀名为podspec)

s.version:pod仓库版本号

s.summary:需要修改,不然会报错

s.source:git的仓库地址,pod仓库的源码存放位置

s.ios.deployment_target : 项目最低支持版本

s.source_files : 具体去哪个目录下下载特定共享代码(注意:这里的地址与 .podspec 文件同级目录开始)


在gitee上创建一个私有仓库

复制仓库地址,将.podspec文件内的s.source地址替换

\

移动到Example文件,更新pod

$ cd Example 
$ pod update --no-repo-update

打开Example工程测试一下

  1. 验证pod配置文件

为了保证项目的正确性,在提交之前,需要验证一下pod文件的配置

# 验证本地podspec文件是否正确
$ pod lib lint

  1. 项目发布
# 添加远程仓库地址
$ git remote add origin https://gitee.com/xiao__0927/test-b.git
# 提交修改信息
$ git add .
$ git commit -a -m "第一次提交 版本为0.1.0"
# 更新远程代码到本地仓库
$ git pull origin master --allow-unrelated-histories
# 推送到远程
$ git push origin master
$ git tag 0.1.0
$ git push origin 0.1.0
  1. 创建私有Spec管理库

创建步骤跟上面码云创建的git私有库同理

在终端执行Spec创建命令

# 这个命令的作用:(1)创建本地仓库。(2)同第一步创建的github项目关联起来
# pod repo add 私有库名字 github/gitlab 项目地址
$ pod repo add PrivatePodName https://gitee.com/xiao__0927/private-pod-name.git

现在,我们可以直接发布了

# 验证spec文件是否正确
$ pod lib lint TestB.podspec

# 验证通过后更新本地spec文件
# PrivatePodName是刚才上面添加的管理库名字
# TestB.podspec是TestB项目里面后缀为podspec的文件名
$ pod repo push PrivatePodName TestB.podspec

发布成功后,可以去远程仓库看看项目有没有提交成功

查看本地的spec库

~/.cocoapods/repos

到这里,我们的私有库发布已经全部完成了。

  1. 检验私有库发布

新建一个测试项目TestBDemo,创建Podfile文件并install

$ cd TestBDemo
$ pod init
$ vim Podfile
$ pod install

Podfile代码如下

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

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

  # Pods for TestBDemo
pod 'TestB', :git => 'https://gitee.com/xiao__0927/test-b.git'
end

成功结果如下

打开TestBDemo工程

测试一下

到这里,验证私有库发布就完成了!!

更新本地创建的私有pod库

通常情况下,我们都少不了在原来pod文件的基础上更新我们的代码库,修改完成之后,更新TestB.spec中的版本号

推送到远程

$ git add .
$ git commit -m "更新pod库"
$ git pull origin master
$ git push origin master
$ git tag 0.1.1
$ git push origin 0.1.1

更新本地spec仓库

# 验证本地spec文件是否正确
$ pod lib lint TestB.podspec
# 验证远程spec文件是否正确
$ pod spec lint TestB.podspec
# 推送到远程私有仓库
$ pod repo push PrivatePodName TestB.podspec

有时候会因为依赖第三方库而产生的警告,造成验证不通过,这时候要添加--allow-warnings

$ pod lib lint TestB.podspec --allow-warnings
# 提交的时候也要添加
$ pod repo push PrivatePodName TestB.podspec --allow-warnings

推送私有pod库到cocoapods

官方文档:guides.cocoapods.org/making/gett…

这个时候项目要使用pod时需要指定地址,并且并不能使用 pod search搜索到

$ pod 'TestB', :git => 'https://gitee.com/xiao__0927/test-b.git'

当我们将私有pod库推送到cocoapods后,就不需要指定git地址了

# 注册Trunk账号
# pod trunk register EMAIL [YOUR_NAME]
$ pod trunk register xiao.kim@tradego8.com 'kim' --description='macbook mini'

验证邮箱收到的链接,注册通过后就可以将私有仓库公开了

# 推送到cocoapods
# pod trunk push [NAME.podspec]
$ pod trunk push TestB.podspec

成功后是这样的

有时候,我们需要添加其他维护者,例如,添加kyle@cocoapods.org到ARAnalytics仓库,kyle@cocoapods.org必须是已经注册过trunk的

$ pod trunk add-owner ARAnalytics kyle@cocoapods.org

个人理解cocoapods管理源码是这样的

私有pod仓库依赖其他pod库

  1. 修改TestB.podspec文件

  1. 更新Example项目
$ cd Example
$ pod update --no-repo-update
  1. 增加我们自己的修改

  1. 提交修改到git仓库
$ git add .
$ git commit -m "添加其他pod库"
$ git pull origin master
$ git push origin master
$ git tag 0.1.3
$ git push origin 0.1.3
  1. 提交podspec修改
# 验证
$ pod lib lint TestB.podspec --allow-warnings
# sources:依赖的pod库的spec仓库地址,有多个的用;分割
$ pod repo push PrivatePodName TestB.podspec --sources=https://github.com/cocoaPods/Specs.git --verbose --use-libraries --allow-warnings
  1. 推送到cocoapods
pod trunk push TestB.podspec --verbose --use-libraries --allow-warnings