美文网首页
Cocoapods 构建私有podspec

Cocoapods 构建私有podspec

作者: hehtao | 来源:发表于2017-11-23 17:57 被阅读119次

整体先说明一下创建一个私有的podspec包括如下那么几个步骤:

  • 1.创建并设置一个私有的Spec Repo。
  • 2.创建Pod所对应的podspec文件。
  • 3.本地测试配置好的podspec文件是否可用。
  • 4.向私有的Spec Repo中提交podspec。
  • 5.使用制作的好的Pod。
  • 7.更新维护podspec。

项目中需要两个git仓库,一个对应Spec Repo(步骤创建所需),另一个是存放你源代码的地方;

一.创建并设置一个私有的Spec Repo

Spec Repo是所有的Pods的一个索引,就是一个容器,所有公开的Pods都在这个里面,他实际是一个Git仓库remote端在GitHub上,但是当你使用了Cocoapods后他会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。如下:


Snip20171123_2.png

其中GINSpecs 是我自建的Spec Repo;

创建Spec Repo的方式如下:

$ pod repo add <Spec Repo Name>  <Spec Repo 仓库地址>
$ pod repo add GINSpecs https://github.com/***/GINSpecs.git

注意:如果有其他合作人员共同使用这个私有Spec Repo的话在他有对应上述"<Spec Repo 仓库地址>"仓库的权限的前提下执行相同的命令添加这个Spec Repo即可。
特别提醒:此处 <Spec Repo 仓库地址>不是你的代码库地址哦,这个只是一个单独的git地址,类似官方库https://github.com/CocoaPods/Specs.git的一个东西 ,就是你自个的Spec Repo啦;

二.创建Pod所对应的podspec文件。

分两种情况讨论:

case one:已有git,coding等可控制版本的仓库:

在clone 下来的项目根目录执行:

$ pod spec create <repo spec Name>
$ pod spec create GINLibrary
case two:没有git,coding等可控制版本的仓库:

在需要创建项目的目录执行如下命令:

$ pod lib create < podspec名称,比如Masonry>

$ pod lib create GINLibrary  # 我的叫GINLibrary

case two 会问你四个问题,
1.是否需要一个例子工程;
2.选择一个测试框架;
3.是否基于View测试;
4.类的前缀;
根据个人喜好选择即可, 问完这4个问题他会自动执行pod install命令创建项目并生成依赖。
最终生成的目录如下:


Snip20171123_3.png

上述四个问题回答不同,目录会稍有不同,重点是 GINLibrary.podspec 这个东西;
此处说明一点:"case two"生成的文件夹名称一般与podspecName相同,包含Assets和Classes文件夹,用来存放抽离出来封装好的模块源代码;但是在"case one" 时 不会创建类似 Pod这个文件夹,需要手动创建一个文件夹,比如我是自个建的 Pod/Classes;

此时将抽离的源码(无Bug的代码)放入Classes文件夹中,push 到自己的仓库,然后打上tag就基本完成了;

push 方式:

$ git add .
$ git commit -s -m "Initial  Library"
$ git remote add origin git@coding.net/***/GINLibrary.git  #添加源代码远端仓库
$ git push origin master     #提交到源代码远端仓库

此处的问题:新建的项目, 执行git push origin master时,可能提示你"xxx文件已存在的问题",简单粗暴 加上 -f 强行覆盖即可;
当然,如果是已经存在的项目colne 到本地,直接依次 add commit push即可;当然你也可以是用github Desktop,sourcetree等可视化工具;

再来看一下如何打 Tag:

$  git tag -m "first release" 0.0.1
$  git push --tags 

手一抖打错了,怎么办? 莫慌:

//查看tag
$ git tag
//删除一个指定的tag,并上传一个空tag到远程tag
$ git tag -d <tagname>
$ git push origin :refs/tags/<tagname> // 一定记得推一个空的tag到远端

三.配置podspec文件并检测是否可用:

Xcode 打开GINLibrary.podspec,逐项配置即可:

Pod::Spec.new do |s|
s.name             = 'GINLibrary'
s.version          = '0.0.3'
s.summary          = 'GINS Library.' # 简要信息

s.description      = <<-DESC     # 描述信息
Copyright © GIN. All rights reserved;
DESC

s.homepage         = 'https://github.com/hehtao/GINLibrary'  #工程源码主页

s.license          = { :type => 'MIT', :file => 'LICENSE' }
s.author           = { 'hehtao' => 'hehtao@163.com' }
s.source           = { :git => 'https://github.com/hehtao/GINLibrary.git', :tag => s.version.to_s }  #工程源码位置
s.social_media_url = 'http://www.baidu.com'  # 联系网址
s.ios.deployment_target = '9.0'  # 版本 平台
s.requires_arc = true
s.source_files = 'Pod/Classes/GINCacheSwipe/**/*' # 重点: 抽离的代码所在位置
#s.public_header_files = 'Pod/Classes/*.h' # 重点: 抽离的代码公开有文件
s.dependency 'MBProgressHUD', '~> 1.1.0' # 重点: 抽离的代码所依赖的其他第三方
s.dependency 'Masonry', '~> 1.0.0'
# s.frameworks = 'UIKit', 'MapKit'  # 重点: 抽离的代码所依赖的框架

# 创建subspec
#s.subspec 'GINWipeCache' do |wipeCache|
#      wipeCache.source_files = 'GINLibrary/GINWipeCache/**/*'
#      wipeCache.public_header_files = 'GINLibrary/GINWipeCache/*.h'
#      wipeCache.dependency 'MBProgressHUD', '~> 1.1.0'
#      #NetWorkEngine.resource = "Pod/Assets/MLSUIKitResource.bundle"
#    end

# s.resource_bundles = {  # 资源文件
#   'GINLibrary' => ['GINLibrary/Assets/*.png']
# }

end

更多配置参考:cocoapods 官方介绍

接下来验证配置的podspec 是否可用:

$ pod lib lint  
$ pod spec lint

上述两个命名都可,建议使用pod spec lint,为什么呢? 因为第一个不够精细(暂且这么说吧),即使检测通过,可能后面push 依然无法通过;当出现下面字段说明检测通过,否则会有error 或是warning,上述命令追加 --verbose 可查看错误详情;

 -> GINLibrary (0.0.1)

GINLibrary passed validation.
  • 典型错误:
- ERROR | [iOS] file patterns: The `XXX` pattern did not match any file.

该错误在已有工程,手动建立 podspec时容易出现,在后面更新维护是也时常出现,直接给出解决方案:

open /Users/userName/Library/Caches/CocoaPods/Pods/External/GINLibrary/035cb9aa62b9d49f904fad1119b83da4-aebfe

打开文件夹,创建文件夹与source_files文件路径对应:

Pod/Classes/GINCacheSwipe

简单点说: 直接把 s.source_files 的顶层文件夹(我的叫Pod)拷贝进去即可;也许又有新问题了, GINLibrary下有很多个类似035cb9aa62b9d49f904fad1119b83da4-aebfe这样的文件夹,我该改动哪一个呢? 还是简单粗暴, rm -rf .....GINLibrary/,然后重新执行 pod spec lint ,就会出现一个新的了

  • 常见的warning:
    xxxx的网址无法访问,类似的东西,一般是网络不稳引起的,重试即可;重试还不行,如果你确定在Safari能正常访问,辣么直接忽略去提交吧;

四.向私有的Spec Repo中提交podspec。

上述podspec检测通过之后直接向私有的Spec Repo中提交podspec。

pod repo push GINSpecs GINLibrary.podspec

成功之后 pod update 之后就可以 pod search 到你的 GINLibrary 喽
GINSpecs 就是第一步中建立的那个东东!!!

五.使用制作的好的Pod。

use_frameworks!
platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git'  # 官方库 一定要指明
source 'https://github.com/hehtao/GINSpecs.git'   # 私有库  一定要指明
target 'GINLibrary_Example' do
#  pod 'GINLibrary', :podspec => '../GINLibrary.podspec'
  pod 'GINLibrary' , '~> 0.0.3'  # 自己的pod
  pod 'MBProgressHUD'
  target 'GINLibrary_Tests' do
    inherit! :search_paths

  pod 'Specta'
  pod 'Expecta'
  pod 'FBSnapshotTestCase'
  pod 'Expecta+Snapshots'
  end
end

七.更新维护podspec。

重复3-6就好了呦;

相关文章

网友评论

      本文标题:Cocoapods 构建私有podspec

      本文链接:https://www.haomeiwen.com/subject/crnzvxtx.html