使用模板库快速创建公有或私有库

其实公有库和私有库,原理都是一样的,只是podspec索引库一个是公有的,一个是私有的,公有的是上传到【https://github.com/CocoaPods/Specs.git】,私有的是上传到自己的git服务器【http://git.test.net/TestGroup/TestSpecs.git
】不公开而已。现在跟着步骤一起试试吧!

一、创建两个Git项目TestKit和TestSpecs

在git服务器创建这两个项目。

TestKit:存放源代码的项目
TestSpecs:存放索引库文件的项目
Git服务器:可以是自己公司的gitlab、也可以是github、也可以是其他第三方git:

1、创建组件项目TestKit

https://test.com/test/TestKit.git

1677656-ab0d65b418d77fe5.png
image.png

2、创建私有索引库项目TestSpecs

如果是公有库,不需要创建此项目。

http://git.test.net/TestGroup/TestSpecs.git

1677656-88fdb6d97de01c4a.png
image.png


二、本地添加私有索引库

1、查看本地索引库

pod repo

2、添加私有索引库

将刚刚新建的私有索引库TestSpecs添加到本地

// pod repo add 索引库名称 索引库地址
pod repo add TestSpecs http://git.test.net/TestGroup/TestSpecs.git


三、快速创建模板库

1、使用cocoapods模板库快速创建工程

pod lib create TestKit

终端输入上面命令后,回车,按照提示,输入对应配置:

$ pod lib create TestKit
Cloning `https://github.com/CocoaPods/pod-template.git` into `TestKit`.
Configuring TestKit template.

------------------------------

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: 
 - https://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 ]
 > ObjC

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

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

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

What is your class prefix?
 > SJ

Running pod install on your new library.

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

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

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'TestKit/Example/TestKit.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`.

2、修改spec文件

#
# Be sure to run `pod lib lint MAVersionUpdateKit.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             = 'TestKit'
  s.version          = '0.1.0'
  s.summary          = '测试私有spec库'
  s.description      = <<-DESC
测试私有spec库的具体描述
                       DESC

  s.homepage         = 'http://git.test.net/testGroup/TestKit'
  s.license          = { :type => 'Copyright', :file => 'Copyright 2018-2020 company Properties Co.Ltd.' }
  s.author           = { '姓名' => '邮箱' }
  s.source           = { :git => 'http://git.test.net/testGroup/TestKit.git', :tag => s.version.to_s }

  s.ios.deployment_target = '8.0'

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

  s.public_header_files = 'TestKit/Classes/Public/**/*.h'
  
  # s.frameworks = 'UIKit', 'MapKit'
  
  s.dependency 'TestOtherKit'
end


四、模板工程源代码push到刚创建的工程

1、将代码提交到组件仓库

git add .
git commit -m '第一次提交'
git remote add origin https://test.com/test/TestKit.git
git push origin master
// 第一次push如果报错的话可以加上-f,强制提交
// git push -f origin master

2、打标签

标签0.1.0与TestKit.podspec中的s.version保持一致

git tag '0.1.0'
git push --tags


五、提交podspec到私有索引库

在上传spec文件前我们可以做一个验证来节省时间。如果不先本地验证,直接远程验证,比较费时。

1、本地验证Spec

// 本地验证不会验证 s.source 中的tag
// 如果是公有库,只需要此命令即可
pod lib lint

// 如果依赖的库有私有库,需要加上私有库的地址
pod lib lint --sources=http://git.test.net/TestGroup/TestSpecs.git,https://github.com/CocoaPods/Specs.git

2、远程验证Spec

// 远程验证会验证 s.source 中的tag,如果此时没有打上相应的标签则会报错
pod spec lint

// 如果依赖的库有私有库,需要加上私有库的地址
pod spec lint --sources=http://git.test.net/TestGroup/TestSpecs.git,https://github.com/CocoaPods/Specs.git

验证私有库提示

如果验证的是私有库,则在后面加上--private,否则会有警告,你可以选择--allow-warnings来忽略该警告

pod lib lint --private
pod spec lint --private

//如果有警告, 而我们又不想处理, 可用—allow-warnings忽略, 即:
pod lib lint --allow-warnings
pod spec lint --allow-warnings

验证通过之后如下:

$ pod spec lint --sources=http://git.test.net/TestGroup/TestSpecs.git
,https://github.com/CocoaPods/Specs.git

 -> TestKit (0.1.0)
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')

Analyzed 1 podspec.

TestKit.podspec passed validation.

3、提交podspec文件到索引库

初次提交或者以后发布新版,都可以使用如下的方式,进行podspec文件的提交。

3.1 提交到私有索引库

  • 方法一:使用命令提交
// pod repo push 私有索引库名称 spec名称.podspec 
pod repo push TestSpecs TestKit.podspec 

pod repo push TestSpecs TestKit.podspec --allow-warnings --use-libraries --skip-import-validation

这里的操作过程:先将我们的代码直接push到本地索引库LXFSpecs,推送后会自动帮我们同步到远程索引库

  • 方法二:直接使用可视化工具提交
    新建版本,将TestKit.podspec文件push到索引库

3.2 提交到公有索引库

这会使用到pod trunk命令。

pod trunk push TestKit.podspec

如果有警告不想处理,可以使用如下命令

pod trunk push TestKit.podspec --allow-warnings

如果没有cocoapods账户,需要执行以下逻辑进行注册:

  • 注册cocoapods账户
    如果是首次使用:
// 邮箱  用户名   描述信息
pod trunk register [email protected] 'name' --description='Test'

如果已经注册, 可使用下面的指令:

pod trunk register [email protected] --description='描述信息'
pod trunk register [email protected]

不出错的话会输出:

1677656-4cad8dc98fe22be1.png
image.png

根据提示, 到你的邮箱里去确认邮件即可!
确认结果如下所示:

1677656-d39a8029117d3a5d.png
image.png

确认之后, 可以使用下面的指令来确认账号是否可用:

pod trunk me
$ pod trunk me
  - Name:     email
  - Email:    [email protected]
  - Since:    April 17th, 21:15
  - Pods:
    - TestKit
  - Sessions:
    - April 17th, 21:15 - October 5th, 02:22. IP: 106.38.53.107
    Description: test

4、搜索自己的库

pod search 'TestKit'


六、使用私有库

我们可以通过pod形式来添加组件TestKit,创建一个新的项目,

1、添加Podfile文件

pod init

2、在Podfile的最顶部添加如下描述

// 第二行是为了保证公有库的正常使用
source 'http://git.test.net/TestGroup/TestSpecs.git'
source 'https://github.com/CocoaPods/Specs.git'

3、添加使用组件TestKit

pod 'TestKit'

4、安装组件

pod install




附录:podspec文件验证不通过常见问题

参考

1、找不到依赖框架

因为依赖框架是私有的,所以需要指定私有spec地址的sources

// 私有库不加私有spec地址,就会报错:
 -> TestKit (0.1.0)
    - ERROR | [iOS] unknown: Encountered an unknown error (Unable to find a specification for `SJNetwork` depended upon by `TestKit`

2、文件夹层次太多导致的

默认的头文件目录设置为:s.public_header_files = ‘Pod/Classes/*/.h’;但是如果Classes目录中,你的代码文件夹层次结构超过两级,就会出现以下错误:

- ERROR | [iOS] file patterns: The public_header_files pattern did not match any file. 

这是因为文件夹层次结构过浅,导致无法找到对应的文件;如果你的文件夹层次结构有三级,就应该修改成:s.public_header_files = ‘Pod/Classes/* * /* * /*.h’;

3、远程验证报错

pod spec lint命令,校验pod的代码和配置时是从git上拉取的代码进行编译;所以如果未创建git,会报以下错误:

-> TestKit (0.1.0) 
– ERROR | [iOS] unknown: Encountered an unknown error ([!] /Applications/Xcode.app/Contents/Developer/usr/bin/git clone http://192.168.110.114/jihq/ServyouCocoaPodDynamicLayout.git /var/folders/rg/f1ycrs553dq2z2dq7mt0mbh80000gn/T/d20160921-12602-1bx4f8v –template= –single-branch –depth 1 –branch 0.1.0
Cloning into ‘/var/folders/rg/f1ycrs553dq2z2dq7mt0mbh80000gn/T/d20160921-12602-1bx4f8v’… 
remote: Not Found 
fatal: repository ‘http://192.168.110.114/jihq/ServyouCocoaPodDynamicLayout.git/’ not found 
) during validation. 

如果未将代码上传到git上,或者未在git上增加对应的tag值,会报以下错误:

-> TestKit (0.1.0) 
– ERROR | [iOS] unknown: Encountered an unknown error ([!] /Applications/Xcode.app/Contents/Developer/usr/bin/git clone http://192.168.110.114/jihq/servyoucocoapoddynamiclayout.git /var/folders/rg/f1ycrs553dq2z2dq7mt0mbh80000gn/T/d20160921-12686-1o6vj1q –template= –single-branch –depth 1 –branch 0.1.0

Cloning into ‘/var/folders/rg/f1ycrs553dq2z2dq7mt0mbh80000gn/T/d20160921-12686-1o6vj1q’… 
fatal: Remote branch 0.1.0 not found in upstream origin 
) during validation. 
 

4. 修改代码文件后需要提交

lint命令是从git上拉取文件,所以文件是以git为准。比如你在本地的Assets目录下新增了图片文件,但是没有push到git上,就会出现以下问题:

- ERROR | [iOS] file patterns: The resource_bundles pattern for ServyouCocoaPodDynamicLayout did not match any file. 

明明podspec文件中的配置正确,本地的pod update —no-repo-update也是可以的,但lint就是不成功,这就是因为git上没有对应文件的原因;

参考文章:
远程私有库的基本使用

猜你喜欢

转载自blog.csdn.net/weixin_33750452/article/details/90940388
今日推荐