×

CocoaPods制作第三方代码库,发布到官方Pod和自己的私有库(模块化、组件化)

96
Miaoz0070
2018.03.05 19:24* 字数 1971
萌图镇楼

CocoaPods在这里就不多做介绍了,相信大家都对此都使用过,也知道其对我们的好处。CocoaPods可以使我们项目管理第三方更容易,平时第三方库都是pod install下来的,再也不用之前的手动拖进工程了。那么我们今天就来说下我们自己怎么制作第三方并发布到CocoaPods官方;还有项目组件化,把自己的模块封装成库并发布到私有的git上。

*环境安装

首先我们需要安装CocoaPods环境,之前写过CocoaPods环境安装,没安装的同学可以跟着安装下。最好安装最新的环境,否则执行下边步骤可能会出现错误。

*制作本地库

打开目录,cd到指定目录,输入指令创建库。“ZMBase”是库的名字。

pod lib create ZMBase

输入命令后会显示下载模板,会有几秒钟等待。

Cloning `https://github.com/CocoaPods/pod-template.git` into `ZMBase`.

模板下载好以后,需要你回答几个问题:

1.你要使用哪个平台?iOS
2.你要使用哪种语言?
3.库中是否包含一个实例程序?(一般选择示例程序)
4.你要使用哪个测试框架?(没有就写None)
5.是否要UI测试?(我一直都是NO,没有测试过)
6.类名前缀是什么?(这个我创建Swift库的时候没有这一项)
------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > Swift

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Quick / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > No

Running pod install on your new library.

接下来会自动执行pod install。

Analyzing dependencies
Fetching podspec for `ZMBase` from `../`
Downloading dependencies
Installing ZMBase (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `ZMBase.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `ZMBase_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

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

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

执行完后会自动打开工程,如果没有自动打开,我们可以手动打开ZMBase.xcworkspace,到此为止一个空的本地库已经出现了。你会看到如下目录结构


1.jpg

我们可以在ReplaceMe.swift位置添加我们的代码,添加自己的代码。


2.jpg
*注意点:
1、我们自己的代码不管是新添加文件、还是现有代码有改动,都需要需要在cd到Example下执行pod install后,才能导入头文件import ZMBase,代码才会更新。
2.我们自己的代码文件最多一层文件结构即Animation文件夹内只能放代码,不能再创建子文件夹。
3.每个类文件如果想暴露出去,则需要在代码类名前添加修饰符即(public class xxx或者open extension xxx),添加public、open,而extension不能用open修饰所以用plublic修饰。
4.如果已经暴露的类,想暴露里边的函数方法,则也需要在方法 func 前加public,否则外部调用会找不到方法名。
MiaozdeMacBook-Pro:Example miaoz$ pod install
Analyzing dependencies
Fetching podspec for `ZMBase` from `../`
Downloading dependencies
Using SnapKit (3.2.0)
Using Then (2.3.0)
Using ZMBase (0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 3 total pods installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `ZMBase_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
MiaozdeMacBook-Pro:Example miaoz$

到这里就可以导入import ZMBase到ViewController.


3.jpg

到这里本地库我们已经制作好了,如果把我们本地当做git库的话,这时候我们只需要在你其他的项目或者工程的Podfile文件里添加如下:
ZMTest:是项目名称
ZMBase:本地私有库
path:是指定本地库所在的位置

target 'ZMTest' do
  pod 'ZMBase', :path => '/Users/miaoz/Desktop/ZMPods/ZMBase'
end
*本地库提交到CocoaPods远程仓库

首先我们先了解下Specs和Specs Repo、Code和Code Repo。
Spec:我们使用CocoaPods的指令创建好本地库之后,默认生成的podspec文件。它描述该库某一个版本的信息,比如库的名字、版本号、描述、依赖库等等。

#
# Be sure to run `pod lib lint ZMBase.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 http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  #库名
  s.name             = 'ZMBase'
  #库版本
  s.version          = '0.1.0'
  #库简短描述
  s.summary          = 'A short description of ZMBase.'

# 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
TODO: Add long description of the pod here.
                       DESC
  #库介绍主页地址
  s.homepage         = 'https://github.com/miaozhang9/ZMBase'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  #库开源许可
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  #作者信息
  s.author           = { 'miaozhang9' => '395052985@qq.com' }
  #源码git地址
  s.source           = { :git => 'https://github.com/miaozhang9/ZMBase.git', :branch => 'master', :tag => s.version.to_s}
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
  #库依赖系统版本
  s.ios.deployment_target = '8.0'
#“*” 表示匹配所有文件
#“*.{h,m}” 表示匹配所有以.h和.m结尾的文件
#“**” 表示匹配所有子目录
  #源码文件配置
  s.source_files = 'ZMBase/Classes/**/*.swift'
  #资源文件配置
  # s.resource_bundles = {
  #   'ZMBase' => ['ZMBase/Assets/*.png']
  # }
  #源码头文件配置
  # s.public_header_files = 'Pod/Classes/**/*.h'
  #系统框架依赖
  # s.frameworks = 'UIKit', 'MapKit'
  #依赖第三方库
  # s.dependency 'AFNetworking', '~> 2.3'
   s.dependency 'SnapKit', '~> 3.2.0'
   s.dependency 'Then', '~> 2.3.0'
end

Specs Repo:是Git库存放Specs文件,它包含了所有可用的第三方库。每个库都会有独立的文件夹,每个版本以独立的子文件夹保存。文件夹中只有一个描述文件,也就是Spec。
Code:就是我们自己库的源代码。
Code Repo:就是Git库存放源代码的地方。

*注意:Specs Repo和Code Repo是不一样的,存放的地址也不一样,不能存放到同个git库下

*提交到官方的CocoaPods

1.提交源代码到git
git remote add origin https://github.com/miaozhang9/ZMBase
git add .
git commit -a -m "0.1.0"
git pull origin master
#git push -u origin master
git push origin master
git tag 0.0.1
git push origin 0.0.1
#如果有冲突errorUpdates were rejected because the tip of your current branch is behind
git push -u origin master -f 

接下来我们先建个git仓库来存放代码。


4.jpg

出现error的原因是,我之前已经创建了ZMBase的repository。

Last login: Mon Mar  5 16:20:24 on ttys006
MiaozdeMacBook-Pro:~ miaoz$ cd /Users/miaoz/Desktop/ZMPods/ZMBase
MiaozdeMacBook-Pro:ZMBase miaoz$ git remote add origin https://github.com/miaozhang9/ZMBase
fatal: remote origin already exists.
MiaozdeMacBook-Pro:ZMBase miaoz$ git pull origin master
From https://github.com/miaozhang9/ZMBase
 * branch            master     -> FETCH_HEAD
Already up-to-date.
MiaozdeMacBook-Pro:ZMBase miaoz$ git add .
MiaozdeMacBook-Pro:ZMBase miaoz$ git commit -a -m "commit"
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
MiaozdeMacBook-Pro:ZMBase miaoz$ git add .
MiaozdeMacBook-Pro:ZMBase miaoz$ git commit -a -m "commit"
[master fa59f5b] commit
 1 file changed, 1 insertion(+), 1 deletion(-)
MiaozdeMacBook-Pro:ZMBase miaoz$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 365 bytes | 365.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/miaozhang9/ZMBase
   331d7e9..fa59f5b  master -> master
MiaozdeMacBook-Pro:ZMBase miaoz$ git tag 0.1.0
MiaozdeMacBook-Pro:ZMBase miaoz$ git push origin 0.1.0
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/miaozhang9/ZMBase
 * [new tag]         0.1.0 -> 0.1.0
MiaozdeMacBook-Pro:ZMBase miaoz$ 

执行完命令后,这时候已经把代码提交到了git上,如下图:


5.jpg
2.提交Spec文件到Specs Repo

--1>提交到官方的Specs上

首先先用cd到Spec文件所在的目录,然后执行pod lib lint

MiaozdeMacBook-Pro:ZMBase miaoz$ pod lib lint

 -> ZMBase (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | [iOS] swift: The validator used Swift 3.2 by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_version` attribute in your podspec. Note that usage of the `--swift-version` parameter or a `.swift-version` file is now deprecated.
    - WARN  | xcodebuild:  /Users/miaoz/Desktop/ZMPods/ZMBase/ZMBase/Classes/Animation/DismissAnimation.swift:65:133: warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.
    - WARN  | xcodebuild:  /Users/miaoz/Desktop/ZMPods/ZMBase/ZMBase/Classes/Animation/ModalAnimation.swift:128:133: warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

[!] ZMBase did not pass validation, due to 4 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.

库没有通过验证,说明Spec配置有问题,在Spec出现warnings是不行的,我们可以根据警告进行修改,也可以我们执行pod lib lint --allow-warnings

MiaozdeMacBook-Pro:ZMBase miaoz$ pod lib lint --allow-warnings

 -> ZMBase (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | [iOS] swift: The validator used Swift 3.2 by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_version` attribute in your podspec. Note that usage of the `--swift-version` parameter or a `.swift-version` file is now deprecated.
    - WARN  | xcodebuild:  /Users/miaoz/Desktop/ZMPods/ZMBase/ZMBase/Classes/Animation/DismissAnimation.swift:65:133: warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.
    - WARN  | xcodebuild:  /Users/miaoz/Desktop/ZMPods/ZMBase/ZMBase/Classes/Animation/ModalAnimation.swift:128:133: warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

ZMBase passed validation.
MiaozdeMacBook-Pro:ZMBase miaoz$ 

当出现ZMBase passed validation.的时候,说明验证通过。

接下来要做第二件事,把Spec提交到官方的CocoaPods的Specs Repo
提交之前,你需要注册pod账号。实质上是将"邮箱--名称--电脑"绑定在一起,所以这里不需要密码。

pod trunk register EMAIL [NAME]

以下是该指令的帮助文档,大概意思是:
如果第一次注册,邮箱和名称都是必须的。如果已经注册过,可以省略名称(除非你想修改名称)。推荐填写描述信息,以及给出了一些Examples,按着Example做就可以了。

pod trunk register EMAIL [NAME]

      Register a new account, or create a new session.

      If this is your first registration, both an `EMAIL` address and your
      `NAME` are required. If you’ve already registered with trunk, you may omit
      the `NAME` (unless you would like to change it).

      It is recommended that you provide a description of the session, so that
      it will be easier to identify later on. For instance, when you would like
      to clean-up your sessions. A common example is to specify the location
      where the machine, that you are using the session for, is physically
      located.

      Examples:

          $ pod trunk register eloy@example.com 'Eloy Durán' --description='Personal Laptop'
          $ pod trunk register eloy@example.com --description='Work Laptop'
          $ pod trunk register eloy@example.com

执行
pod trunk register xxx@xx.com Miaoz
结果
[!] Please verify the session by clicking the link in the verification email that has been sent to xxx@xx.com
去邮箱认证即可。
注册账号之后,我们可以尝试将Spec提交到Specs Repo:
执行命令:

pod trunk push ZMBase.podspec --allow-warnings
--------------------------------------------------------------------------------
 🎉  Congrats

 🚀  ZMBase (0.1.0) successfully published
 📅  March 5th, 03:05
 🌎  https://cocoapods.org/pods/ZMBase
 👍  Tell your friends!
--------------------------------------------------------------------------------

如果出现以上图示则说明提交到官方CocoaPods已经成功,可能是用pod search ZMBase 这时候不会搜索的,原因可能有延迟,需要等待一个小时左右,才会搜到。

总结提交Spec步骤:
1.给项目打tag(必须要打tag,CocoaPods中的不同版本,实质是不同的tag,不打tag会导致下一步验证不通过)

git tag 0.1.0
git push origin 0.1.0

2.检查合法性

## 指令
pod spec lint ZMBase.podspec --allow-warnings

结果

-> ZMBase (0.1.0)
Analyzed 1 podspec.
ZMBase.podspec passed validation.

3.提交到Specs Repo

## 指令
pod trunk push ZMBase.podspec --allow-warnings

--2>提交到自己私有的Specs上
首先我们先创建一个git库,用于管理我们的Specs。
创建后的git地址https://github.com/miaozhang9/ZMSpecccc.git
在终端执行

pod repo add ZMSpec https://github.com/miaozhang9/ZMSpecccc.git

添加成功后我们需要把ZMBase.podspec推送到ZMSpec库中

pod repo push ZMSpec ZMBase.podspec --allow-warnings

如果出现warning可以执行

pod repo push ZMSpec ZMBase.podspec --allow-warnings

MiaozdeMacBook-Pro:ZMBase miaoz$ pod repo push ZMSpec ZMBase.podspec --allow-warnings

Validating spec
 -> ZMBase (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | [iOS] swift: The validator used Swift 3.2 by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_version` attribute in your podspec. Note that usage of the `--swift-version` parameter or a `.swift-version` file is now deprecated.
    - WARN  | xcodebuild:  ZMBase/ZMBase/Classes/Animation/DismissAnimation.swift:65:133: warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.
    - WARN  | xcodebuild:  ZMBase/ZMBase/Classes/Animation/ModalAnimation.swift:128:133: warning: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

Updating the `ZMSpec' repo

Already up-to-date.

Adding the spec to the `ZMSpec' repo

 - [No change] ZMBase (0.1.0)

Pushing the `ZMSpec' repo

MiaozdeMacBook-Pro:ZMBase miaoz$ 

到这里我们已经成功发布到ZMSpec git上了,我们来看一下我们的ZMSpec的git项目:


6jpg

这里的OTBase是之前发布的OC库。

查看我们本地的Specs库:

直接Findle ->右键 -> 前往文件夹 -> 输入:~/.cocoapods/repos ->点击前往


7.jpg

至此,我们的私有库创建发布结束。但是我们注意到,LATSpecs的README.md文件是空的,为了后期的维护更新,以及团队小伙伴的使用方便,建议完备一下信息。
终端执行

cd  xxx
git add . 
git commit -a -m "add Readme"
git pull origin master
git push origin master
终端执行一下进入我们的私有库管理Specs,git更新提交下:

MiaozdeMacBook-Pro:~ miaoz$ cd /Users/miaoz/.cocoapods/repos/ZMSpec 
MiaozdeMacBook-Pro:ZMSpec miaoz$ git add . 
MiaozdeMacBook-Pro:ZMSpec miaoz$ git commit -a -m "add Readme"
[master 8e3a72a] add Readme
 1 file changed, 12 insertions(+), 2 deletions(-)
MiaozdeMacBook-Pro:ZMSpec miaoz$ git pull origin master
From https://github.com/miaozhang9/ZMSpecccc
 * branch            master     -> FETCH_HEAD
Already up-to-date.
MiaozdeMacBook-Pro:ZMSpec miaoz$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 625 bytes | 625.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/miaozhang9/ZMSpecccc.git
   cbc413e..8e3a72a  master -> master
MiaozdeMacBook-Pro:ZMSpec miaoz$ 
readme.jpg

我们发布的私有库和发布到官方的库都已经完成,那么我们接下来,来验证一下。

*验证发布的私有库

新建工程TestPodLib然后,cd 到主目录

pod init // 1.初始化pod
touch podfile // 2.生成podfile文件
open podfile

编辑podfile

# Uncomment the next line to define a global platform for your project
#因为我们的库在官方Specs和自己的Specs上都上传了,在这里我们只测试官方的
source 'https://github.com/CocoaPods/Specs.git'
#如果需要测试自己私有的,需要把上传到官方Specs的移除
#source 'https://github.com/miaozhang9/ZMSpecccc.git'
platform :ios, '9.0'

target 'TestPodLib' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for TestPodLib
pod 'ZMBase','~> 0.1.0'
end
注意:podfile文件里要加上
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/miaozhang9/ZMSpecccc.git'
#如果你不配置系统的索引库 会找不到其他的库 只能找到你自己创建的仓库
1520246133177.jpg
出现这种情况的原因就是我们官方Specs和自己的Specs都上传了。
[!] Found multiple specifications for `ZMBase (0.1.0)`:
- /Users/miaoz/.cocoapods/repos/master/Specs/a/f/b/ZMBase/0.1.0/ZMBase.podspec.json
- /Users/miaoz/.cocoapods/repos/ZMSpec/ZMBase/0.1.0/ZMBase.podspec

pod install后我们来看下工程目录

pod install后的目录.jpg

我们会看到.h、.m及源码都能暴露出来,是因为我们spec文件

 s.source_files = 'ZMBase/Classes/**/*.swift'
 s.source_files = 'OTBase/Classes/**/*.{h,m}'
  #源码头文件配置
  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.public_header_files = 'Pod/Classes/**/*.swift'

总结要点cocoapods创建Swift(OC)私有库:

创建私有spec repo

$ pod repo add [privateSpecRepoName] [privateGitPath]
创建私有spec repo 中的lib

$ pod lib create [libName]

提交到创库
$ git add .
$ git commit -s -m "初始导入"
$ git remote add origin [创库ssh地址]           #添加远端仓库
$ git push origin master     #提交到远端仓库
$ git tag -m "发布" "0.1.0"
$ git push --tags     #推送tag到远端仓库
提交podspec到私有repo

$ pod repo push [privateSpecRepoName] [libName].podspec

至此,我们的私有库、发布到官方cocoaPods库已经发布结束。具体更详细的信息,建议大家多多参考CocoaPods官网。

*更新版本

--code
 git add .     //新增的文件 加入仓库 
 git commit -a -m "0.1.2"      "0.1.2" -- tag 号 //  需要提交的文件上传确认
git pull origin master         //下拉最新资源
 git push origin master         //上传本地最新代码
 git tag 0.1.2      //打tag
 git push --tags   

 --spec
pod lib lint --allow-warnings     //验证本地库是否正确    lib  换成 spec 的 话 就是 就是验证远程服务器的 库 是否正确

一些需要选项:

--verbose:打印错误
--allow-warnings:允许警告
--use-libraries:如果自己私有库包含library,则需要
--no-clean:检查问题
--sources:如果依赖别的第三方库则需要
pod repo push ZMBase ZMSpec.podspec --allow-warnings

lint完整写法(有依赖库、允许警告):

pod lib lint YYStudio_LoanSDK.podspec --sources='http://10.11.180.29/mobileDevelopers/YZT-Loan-Pod-Spec.git' --verbose --allow-warnings --use-libraries --no-clean

上传完整写法(有依赖库、允许警告):

pod repo push com-mobiledevelopers-yzt-loan-pod-spec YYStudio_ACFaceCheckSDK.podspec --sources='https://github.com/CocoaPods/Specs.git' --allow-warnings --use-libraries
命令行指令:

history    查看自己用过的历史指令

pod repo list   查看本地repo私有库资源信息

open ~/.cocoapods   打开本地私有库资源的位置

若是出现错误信息
[!] The repo MyRepo at ../.cocoapods/repos/MyRepo is not clean
更新下我们的版本库,

$ pod repo update [你的repo]

demo地址
Specs: https://github.com/miaozhang9/ZMSpecccc.git
Swift: https://github.com/miaozhang9/ZMBase.git
OC: https://github.com/miaozhang9/OTBase.git

下边是一些podSpec相关的文章,不太懂得可以看下。
配置Spec踩过的坑
podspec文件讲解

有问题大家可以留言!!!
技术
Web note ad 1