前言:
项目组件化的好处我就不不多说了,今天我就跟大家分享一下项目组件化第一步:创作自己的私有库,以及自己遇到的令人酸爽的bug.
之前当在项目中看到这样的目录后,是不是默默有一种高高在上,可远观而不可亵玩焉的感觉。当时我多想在这个目录下有自己的代码呀。只要不断努力,梦想并不遥远!

首先说一下cocoapoads怎么把我们想要的三方库下载到我们的工程里面的呢?理解了这个,下面对我们自己建立的私库就好理解了.
在GitHub 上https://github.com/CocoaPods/Specs链接下放着pod管理三方库的podspec文件,随便点开其中一个。索引库,如下图

这里面放着三方库URLComponentsBuilder的一下信息,name, version ,summary, authors,homepage等信息。
在我们在终端输入pod setup,会将远程索引库下载到我们本地方便pod search,cocapods本地spec索引库的位置:/Users/mac/.cocoapods/repos/master/Specs
pod search时会根据本地specs仓库去创建search_index.json的索引文件,索引文件中包含了一个个三方库的podspec文件中的信息,search_index.json文件的位置:/Users/mac/Library/Caches/cocoapods/search_index.json
pod install时候,会根据要安装的三方库的podspec索引文件,找到三方库的下载地址和版本信息,然后下载到我们工程里面。
开始步入正题

首先建立自己的私库
1、本地私有索引库
2、远程私有索引库
3、本地组件代码库
4、远程组件代码库
大致流程是,创建自己的远程索引库,创建本地索引库,本地远程索引库关联,创建自己的远程组件代码仓库,创建自己的本地的组件代码仓库,把自己的组件代码放进去提交到远程组件代码仓库,本地校验,远程校验,将自己创建的索引文件 push到远程索引库进行管理。
------------------------------------------------------------------------------------------------------
光说不练假把式,现在我们创建一个网络请求的组件,上传到自己的私有库。
1、在github上创建自己的远程索引库仓库,名字自己起,copy地址,我这已经创建过了,就不重复创建了,创建完一次,自己之后的远程代码索引都可以放在这里了。

注意事项:
这里面写个说明文件啥的,不要为空,或者接下来会出现不可描述的错误,这是我踩过的坑总结出来的教训。如果为空会出现下面的错误。
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
2、本地索引库和远程索引库关联
pod repo add JSpecs https://github.com/canlanrensheng/JSpecs.git
然后pod repo查看

会多出本地索引库JSpecs
3、创建自己的远程组件代码库,并copy地址

4、创建自己的本地组件代码仓库
cd ~/Desktop/
执行pod lib create JSHttpToolKit
根据需要填写下面的信息

成功之后把我们自己写的组件代码放在下面的目录下

cd 进入 Example 文件目录下
执行 pod install 把你封装的代码集成到Example中.
编辑JSHttpToolKit.podspec信息,如下图,因为我写的需要依赖AFNetworking库,所以s.dependency要加上相应的依赖库,不依赖的不用加。

记住s.version版本号,下边要用
cd 到本地组件目录下

开始提交代码到远程组件代码仓库
git add .
git commit -m '1.0.0代码提交'
git remote add origin https://github.com/canlanrensheng/JSHttpToolKit.git
git push origin master
git tag '1.0.0' (这里的tag要跟上面的JSHttpToolKit.podspec的version一致,要不然会出现不可描述的错误)
git push --tags
5、本地验证
pod lib lint JSHttpToolKit.podspec --allow-warnings

三方库包含静态库(如:xxxx.a),在验证的时候,不能验证通过,可使用如下命令:pod lib lint --use-libraries --allow-warnings
如果没有依赖三方库的话去掉 --use-libraries如下:
pod lib lint JSHttpToolKit.podspec --allow-warnings
6、远程验证
pod spec lint --allow-warnings

三方库包含静态库(如:xxxx.a),在验证的时候,不能验证通过,可使用如下命令:pod lib lint--use-libraries --allow-warnings
如果没有依赖三方库的话去掉 --use-libraries如下:
pod spec lint --allow-warnings
//编辑报错的问题 trunk URL couldn't be downloaded:xxx 应该是源的问题
pod lib lint JSVideoBrowserKit.podspec --use-libraries --allow-warnings --sources='https://github.com/CocoaPods/Specs.git'
7、将你的.podspec索引文件 push到索引库进行管理
pod repo push JSpecs JSHttpToolKit.podspec --use-libraries --allow-warnings
如果出现报错 runk URL couldn't be downloaded:xxx 应该是源的问题
pod repo push JSSpecs JSVideoBrowserKit.podspec --use-libraries --allow-warnings --sources='https://github.com/CocoaPods/Specs.git'

查看pod repo

cd到本地索引库的这个目录下cd /Users/zhangjinshan/.cocoapods/repos/JSpecs
ls查看

说明已经添加到本地的索引库里面了。Perfect!
到这里我们就完成了自己的网络请求组件的制作,接下来看怎么用呢?
新建一个测试工程,并在工程目录下创建podfile

引入Specs和自己的远程索引库的地址
cd到测试目录下,执行pod install

在需要的地方引入我们的头文件就可以了。

到此,创建自己的私有库就完成了。欢迎批评指正!
网友评论