iOS 组件化搭建-创建Pod

总体步骤

1.创建私有 Spec Repo 仓库来管理私有 podspec 文件;
2.创建私有 Pod 工程文件,并提交远程 git 托管平台;
3.创建私有 Pod 对应的 podspec 文件;
4.验证 podspec 文件有效性;
5.提交 podspec 至私有 Spec 仓库;
6.新建项目测试私有 Pod;
7.更新私有 Pod 的版本;

1. 创建私有 Spec Repo 仓库

  1. 私有 Spec 仓库需要在 git 托管平台托管,在 github上创建新的的 repository,例如/CustomPrivatePods。尽量勾选 Initialize this repository with a README

2.添加您的私有 repo 到 CocoaPods
在终端运行下面代码:

 pod repo add REPO_NAME  SOURCE_URL

#例如 pod repo add CustomPrivatePods https://github.com/Ray0218/CustomPrivatePods.git


REPO_NAME:你创建的 repo 的名称
SOURCE_URL:源地址,如果是 gitHub 的地址就直接将那个地址 clone 。
执行完毕后,cocoapods 会把 CustomPrivatePods clone 在 ~/.cocoapods/repos 目录。


截屏2019-12-05上午9.50.41.png

2.创建私有Pod 工程文件

1. 在 github 上新建项目用于存放 Pod 工程源码,最好创建一个License,以免后续需要
截屏2019-12-05上午11.03.45.png
2. 本地创建工程文件(KLLogin),添加至 git 管理,并 push 到远端仓库。

切换到本地工程文件夹下,执行以下代码,就会把本地工程关联到github

 
git init
git remote add origin https://github.com/Ray0218/KLLogin.git
git pull origin master 
git add .
git commit -m "first commit"
git push -u origin master

3. 创建私有 Spec 仓库

cd 切换到本地项目( KLLogin)的根目录下然后

pod spec create 工程名
#如 pod spec create  KLLogin

创建成功后会在根目录下多出 "工程名.podspec '" 文件。


截屏2019-12-05上午10.10.12.png

根据实际情况配置 podsec 文件:

  
Pod::Spec.new do |s|

s.name         = "KLLogin"
s.version      = "0.0.1"
s.summary      = "A short description of KLLogin."

s.description  = <<-DESC
描述文字
DESC

s.homepage     = "https://github.com/Ray0218/KLLogin"

s.license      = { :type => "MIT", :file => "LICENSE" }
s.author             = { "Ray0218" => "ray_ios@163.com" }
s.platform     = :ios, "8.0"

s.source       = { :git => "https://github.com/Ray0218/KLLogin.git", :tag => "#{s.version}" }

#只获取项目下Class目录中的文件
s.source_files  =   "KLLogin/Class/**/*.{h,m,swift}"
#s.exclude_files = "Classes/Exclude"

# s.public_header_files = "Classes/**/*.h"

s.dependency "Masonry"

end

4.验证 podspec 文件有效性

本地检测代码仓库是否有问题,没问题先将本地文件提交至github,同时添加tag, tag值要与 KLLogin.podspec中的version保持一致

pod lib lint   KLLogin.podspec --allow-warnings --use-libraries
截屏2019-12-05上午10.37.33.png

远程检测代码仓库是否有问题

pod spec lint   KLLogin.podspec --allow-warnings --use-libraries

5. 添加 PodSpec 文件到自己的私有spec repo 仓库

在 podspec 文件所在的目录执行:

pod repo push CustomPrivatePods KLLogin.podspec,
# CustomPrivatePods私有仓库名 

执行完毕后会在 CustomPrivatePods 仓库中出现 KLLogin 目录。至此,私有化pod就创建完成.

如果想直接发布到CocoaPods/Specs.git,成为公有库可以使用

pod trunk push KLLogin.podspec --allow-warnings --use-libraries

但前提是必须是注册过了Trunk,如果未注册

// 加上--verbose可以输出详细debug信息,方便出错时查看。
pod trunk register XXX@163.com "用户名" --verbose

6.新建项目导入私有的pod工程

在需要引用KLLogin工程的 Podfile 文件开头中添加:

source 'https://github.com/Ray0218/CustomPrivatePods.git' # 私有repo仓库地址
source 'https://github.com/CocoaPods/Specs.git'

注:如果不添加官方库地址,若私有库的类库的子依赖,依赖了公有库某个类库,会导致pod install失败
在 Target 下添加依赖:

pod 'KLLogin' 

执行 pod install 完成安装,就能看到放在spec.source_files = "KLLogin/Class/*/.{h,m,swift}"中的文件了

自动上传podsepc脚本

创建.sh文件

  • 版本号自动增加
#!/bin/bash

VersionString=`grep -E 's.version.*=' KLLogin.podspec`
VersionNumber=`tr -cd 0-9 <<<"$VersionString"`

NewVersionNumber=$(($VersionNumber + 1))
LineNumber=`grep -nE 's.version.*=' KLLogin.podspec | cut -d : -f1`
sed -i "" "${LineNumber}s/${VersionNumber}/${NewVersionNumber}/g" KLLogin.podspec

echo "current version is ${VersionNumber}, new version is ${NewVersionNumber}"

git add .
git commit -am ${NewVersionNumber}
git tag ${NewVersionNumber}
git push origin master --tags
pod repo push CustomPrivatePods KLLogin.podspec --allow-warnings --use-libraries --use-modular-headers

  • 手动设置版本号
#!/bin/bash
#git操作
git stash
git pull origin master --tags
git stash pop

confirmed="n"
NewVersionNumber=""
ProjectName="KLLogin"

getNewVersion() {
read -p "请输入新的版本号: " NewVersionNumber

if test -z "$NewVersionNumber"; then
getNewVersion
fi
}

#获取版本号并显示
VersionString=`grep -E 's.version.*='  ${ProjectName}.podspec`

VersionNumberDot=`tr -cd "[0-9.]" <<<"$VersionString"`
VersionNumber=`sed 's/^.//' <<<"$VersionNumberDot"`

echo -e "\n${Default}================================================"
echo -e " Current Version   :  ${Cyan}${VersionNumber}${Default}"
echo -e "================================================\n"

getInfomation() {
getNewVersion
#输出当前版本号
echo -e "\n${Default}================================================"
echo -e " New Version IS :  ${Cyan}${NewVersionNumber}${Default}"
echo -e "================================================\n"
}

#请求输入新的版本号
while [ "$confirmed" != "y" -a "$confirmed" != "Y" ]
do
if [ "$confirmed" == "n" -o "$confirmed" == "N" ]; then
getInfomation
fi
read -p "确定? (y/n):" confirmed
done

LineNumber=`grep -nE 's.version.*=' ${ProjectName}.podspec | cut -d : -f1`
sed -i "" "${LineNumber}s/${VersionNumber}/${NewVersionNumber}/g" ${ProjectName}.podspec

echo -e "\n${Default}================================================"
echo -e "current version is ${VersionNumber}, new version is ${NewVersionNumber}"
echo -e "================================================\n"

git add .
git commit -am ${NewVersionNumber}
git tag ${NewVersionNumber}
git push origin master --tags
pod repo push CustomPrivatePods ${ProjectName}.podspec --allow-warnings --use-libraries --use-modular-headers

pod 操作命令

显示本地repo列表

pod repo list

添加远程repo

pod repo add REPO_NAME  SOURCE_URL

删除本地 repo

pod repo remove <REPO_NAME>

更新某个 repo

pod repo update <REPO_NAME>

参考:https://www.jianshu.com/p/2140893dc639
https://www.jianshu.com/p/475d6b6d5600
https://casatwy.com/modulization_in_action.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容