工程组件化--远程私有库及索引库的建立与使用(一)

1,384 阅读3分钟

在GitLab或者GitHub上创建一个远程索引库,即创建一个私有工程 名称为MyPrivateRepo

一.索引库的建立

1.打开GitLab或者GitHub 都可以 点击下图New repository 新建仓库

2.新建仓库配置 如下:

3.点击 Create respository,这时存放组件的spec文件的索引库已经完成

4.将远程索引库地址加入repo

命令:pod repo add MyPrivateRepo http://xxxxxxx/xxxxxx/MyPrivateRepo.git
注意:MyPrivateRepo 为私有库名称
    http://xxxxxxx/xxxxxx/MyPrivateRepo.git为私有库地址,地址如下:

5.在终端下的运行结果,此时索引库已经制作完成,可以进行创建组件工程了

二.创建私有库(组件)

1.创建本地组件工程

命令:pod lib create ComponentPro
注意:ComponentPro 为组件工程名称

2.创建完成的工程如下,需要添加到的类写在ComponentPro文件夹下

3.在GitLab上创建组件工程远程库,即创建一个私有工程 名称为ComponentPro

4.将组件工程远程库与本地组件工程做关联同步

cd ComponentPro
git add .
git commit -m '初始化'
git remote add origin http://xxxxx/xxxxx/ComponentPro.git //地址如下

5.打开ComponentPro组件,修改组件工程的spec文件

spec文件的位置

s.version = '0.1.0'
s.summary = '组件描述'
s.homepage = 'http://xxxxxxx/xxxxxxx/ComponentPro'
s.source = { :git => 'http://xxxxxx/xxxxx/ComponentPro.git', :tag => s.version.to_s }

注意:
    s.version 为版本号 一定要与 组件工程的tag一致,否则会报错
    s.summary 组件的描述文字放在这里
    s.homepage 网页网址搜索栏里面的地址
    s.source 组件工程的远程地址

配置主要是如下两项:

注意: 以上为最简单配置, 开发中还有其他依赖,资源库等设置,暂时不讲,后续文章再写出来

6.把修改进行提交并更新到远程仓库

git add .
git commit -m '修改spec文件'
git push origin master

在推拉代码的过程中可能会遇到 仓库权限于冲突问题 大家可以自己搜一下解决

7.验证本地spec文件

pod lib lint

8是.提交版本tag

git tag 0.1.0
git push --tags

注意:git tag 0.1.0必须要与组件中 s.version= '0.1.0'的版本号一致

9.验证远程spec文件

在终端输入
pod spec lint 
如果验证错误 可以使用下面的命令
pod lib lint --allow-warnings --use-libraries

10.将组件工程的spec文件推送到远程索引库

在终端输入:
pod repo push MyPrivateRepo ComponentPro.podspec

如果出错,就在终端输入:

pod repo push MyPrivateRepo ComponentPro.podspec --allow-warnings --use-libraries

注意:MyPrivateRepo为远程索引库的名称;ComponentPro.podspec为组件工程里的spec文件

成功之后, 我们的组件及索引就已经完成了,下面集成到主工程中.

三.主工程中加载组件库

1.主工程使用COCOPODS管理库

cd /Users/xxxx/Desktop/MyProject/MyProject
pod init
pod install

打开工程:

2.修改出现的podfile文件

注意:修改为如下

source 'http://xxxxx/xxxxx/MyPrivateRepo.git'
source 'https://github.com/CocoaPods/Specs.git'

target 'MyProject' do
pod 'ComponentPro'
end

注意:http://xxxxx/xxxxx/MyPrivateRepo.git 为存放索引库的地址
https://github.com/CocoaPods/Specs.git 这个地址不要动,就写这个

3.安装

终端中:

cd 到项目目录下
pod install

4.查看MyProject工程中的Pods中出现了我们制作的组件私有库