最近接到一个需求,开发一个组件库,从网上找了一堆资料,大部分讲的很粗略,因此我把我创建组件库的完整过程,和中间的遇到的问题记录下来。 一、在terminal 使用pod 命令创建组件库
pod lib create ‘工程名称’
然后进程就开始了:
book@bookdeMBP desktop % pod lib create PPDemo
Cloning `https://github.com/CocoaPods/pod-template.git` into `PPDemo`.
Configuring PPDemo template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
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. )
首先会 clone 一个github.com/CocoaPods/p… 模板。 我们看看这个模板的内容,看看它是什么内容,知其所以然。 我当时遇到的一个疑惑是:
这个路径和名称是我之前已经废弃的 github 账号名称,已经改了新的名字,如果ruby 从github去获取,那么也是应该最新的名称。于是乎,我的好奇心陡的就升上来了,它怎么来的。
二、pod lib create 的第二步骤 Configuring PPDemo template.
这三个文件/文件夹很关键:
首先来看configure
获取当前路径,并获得setup的相对路径。
TemplateConfigurator 类初始化 podfile 并且 run(pod install )。
这三个文件完成初始化和配置。
从这里,我们似乎看到了什么端倪
第一句,是获取的user_name,即替换后的PROJECT_OWNER。
第二句是从钥匙串去获取github的用户名,得到的第一个作为返回值。
三、根据提示选择你要的平台、语言、测试库、是否需要demo、是否需要单元测试、类前缀。
To get you started we need to ask a few questions, this should only take a minute.
What platform do you want to use?? [ iOS / macOS ]
> ios
Possible answers are [ 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?
> DH
选完了,pod 就开始一系列配置和替换。
第一步。证实了,的确从钥匙串去读取数据了。 第二步。一系列的分支创建提示。 第三步。在Example 目录下执行pod install。
第一个框,是提示 specs 库的指向不对,需要添加那个命令行到你的podfile。
第二个框,是告诉你,你成功了,并自动open 了你创建的组件库工程。你可以愉快的编码了。
四、开发中遇到的一些问题
1.创建的文件夹以及文件,一定要放在 Class目录下。编译的时候才能找到,否则你需要更改源文件路径配置
2.共外部调用的接口文件.h一定是 public,否则报错文件找不到
3.当你更改.h文件为 public后,pod install 会自动帮你在umbrella文件添加头文件导入 。不对外提供接口的类文件不需要public。
- pod lib lint 可以验证本地库是否完整,build 是否能够通过。 添加 --verbose 可以查看详细过程。
5.看到如下提示,是告诉你build 成功,但是 验证未通过。
你需要再添加一个参数,--allow-warnings。
pod lib lint --verbose --allow-warnings
6.如下提示,告诉你build和 validate 都通过了。
- 如果你要发布到私域,那么你需要在你的代码管理平台新建一个git仓库,并在本地关联远程仓库。
git remote add origin https://xxxxx/xxxx.git
git pull origin master --allow-unrelated-histories
git push -u origin master //如果首次失败可以强制执行 git push -f origin master
- 然后需要在 podspec文件 更改tag值为你需要的。并且执行:
git add . //添加文件到缓冲区
git commit -m "描述" //从缓冲区提交代码到仓库
git tag -a '1.0.1' -m '描述'
git push --tags
- 本地repo 添加远程git 地址
pod repo add XXXComponent https://XXXX/XXXComponent.git
执行pod repo 看下所引库是否添加成功
- 推送本地组件库到远程私域仓库
pod repo push XXXComponet
致此,你的组件库可以使用了。使用的时候要注意的是,需要在你的podfile 添加
sources "https://XXXX/XXXComponent.git"
如果遇到镜像源更新失败,再添加
sources "https://cdn.cocoapods.org"
如果还有别的问题,请留言,随时回答。