CocoaPods私有仓库搭建

一. CocoaPods公共仓库

查看CocoaPods 本地目录

打开finder command + shift + g ~/.cocoapods

  • master是CocoaPos对应的公共git仓库
  • mySpecs是自己创建的私有仓库地址,后文会提到。

cd 到 master目录

$ cd ~/.cocoapods/repos/master

$ git remote -v

打印台显示:

origin  https://github.com/CocoaPods/Specs.git (fetch)
origin  https://github.com/CocoaPods/Specs.git (push)

我们点开master下的Specs目录

打开其中一个.podspec.json文件

会发现CocosPods管理的很多框架以及对应的版本号,每一个json文件对应该框架的一个版本,它描述了每个对应版本的框架的信息、配置、及源码下载地址。

二. 搭建自己的pods私有库

1、创建私有的Spec Repo

Spec Repo 是所有公开的Pods 的podspec文件的一个git仓库,当使用Cocoapods后它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。因此我们需要创建一个类似于master的私有Spec Repo 。同理这个私有Spec Repo我们也要有一个远程端。那么我们需要创建一个 Git仓库,这个仓库你可以创建私有的也可以创建公开的。如果是私有的话,项目中其他同事,你要给他这个Git仓库的权限。

在github上创建一个mySpecs仓库

在终端执行命令:

$pod repo add mySpecs https://github.com/xxx/mySpecs.git

之后在~/.cocoapods/repos目录下就能看到mySpecs

2、创建Pods工程

在本地创建一个test目录
cd到test目录并执行终端命令

$pod lib create pod_test

终端会显示

Cloning `https://github.com/CocoaPods/pod-template.git` into `pod_test`.
Configuring pod_test template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

2018-03-01 15:35:21.732 defaults[11084:244747] 
The domain/default pair of (org.cocoapods.pod-template, HasRunbefore) does not exist
If this is your first time we recommend running through with the guide: 
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )

 Press return to continue.

依次填写iOS、ObjC、Yes、Specta、Yes、JkW

What platform do you want to use?? [ 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 ]
 > Specta

Would you like to do view based testing? [ Yes / No ]
 > Yes

What is your class prefix?
 > JKW
 

设置完成后打印台会输出

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `pod_test` from `../`
Downloading dependencies
Installing Expecta (1.0.6)
Installing Expecta+Snapshots (3.1.1)
Installing FBSnapshotTestCase (2.1.4)
Installing Specta (1.0.7)
Installing pod_test (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `pod_test.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'pod_test/Example/pod_test.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.

成功后会在test目录下创建一个pod_test工程

目录如下:

我们来添加一个分类文件

NSString+Test分类文件如下

#import <Foundation/Foundation.h>

@interface NSString (Test)
- (void)test;
@end
#import "NSString+Test.h"

@implementation NSString (Test)
- (void)test{
    NSLog(@"只是一个测试");
}
@end

cd 在Example工程目录下执行 pod update命令

打开项目工程,可以看到库文件都被加载到Pods子项目中了
不过它们并没有在Pods目录下,而是跟测试项目一样存在于Development Pods/MyLib中,这是因为我们是在本地测试,而没有把podspec文件添加到Spec Repo中的缘故。

注:这里需要注意的是每当添加了新的文件或者以后更新了podspec的版本都需要重新执行一遍pod update命令。

4、提交pods库到github上

cd 到pod_test目录下

$git init
$git add .
$ git commit -am "第一次提交" 
$ git remote add origin https://github.com/xxx/pod_test
$ git push origin master 
//一定要有标签,不然会有下面的警告
//podspec文件中获取Git版本控制的项目需要tag号,
$ git tag -m "first release" "0.1.0" 
$ git push --tags 

5、配置pod_test的podspec文件

该pod_test.podspec文件在执行$pod lib create pod_test已经被自动创建

podspec 文件格式

Pod::Spec.new do |s|
  s.name             = 'pod_test'
  s.version          = '0.1.0'
  s.summary          = '这是我第一个私有库项目demo'

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/kamto6/pod_test'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'kwjie5566@163.com' => 'kangwei.jie@mydeertrip.com' }
  s.source           = { :git => 'https://github.com/xxx/pod_test.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'pod_test/Classes/**/*'
  
  # s.resource_bundles = {
  #   'pod_test' => ['pod_test/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end
  • s.name :pod search 搜索的关键词,注意这里一定要和.podspec的名称一样

  • s.version :版本号,每一个版本对应一个tag

  • s.summary : 简介

  • s.homepage : 项目主页地址

  • s.license : 许可证

  • s.author : 作者

  • s.social_media_url : 社交网址

  • s.source : 项目的地址

  • s.source_files : 需要包含的源文件

  • s.resources: 资源文件

  • s.requires_arc : 是否支持ARC

  • s.dependency :依赖库

  • s.ios.deployment_target = '8.0' : 支持的pod最低版本

如:

s.source_files = 'pod_test/Classes/**/*'

“*” 表示匹配所有文件
“**” 表示匹配所有子目录

验证该podspec文件,cd到pod_test目录

$ pod spec lint

pod spec相对于pod lib会更为精确,pod lib相当于只验证一个本地仓库,pod spec会同时验证本地仓库和远程仓库。

打印台提示:

 -> pod_test (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | url: The URL (https://github.com/kwjie5566@163.com/pod_test) is not reachable.

[!] pod_test did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.

当然我们验证时可以忽略这些警告

$ pod spec lint --allow-warnings

--allow-warnings是允许warning的存在,也就是说当你在pod spec lint验证podspec的时候,如果不加这句,而你的代码里又一些警告的话,是验证不通过的。而加上这句话的话,有警告也能验证通过。

终端输入:vim pod_test.podspec

按i进入编辑状态修改:summary (随便填写即可) 和 url(post_test对应的仓库) ,再按esc + : + wq 退出
终端再执行$ pod spec lint

打印台提示:

 -> pod_test (0.1.0)

pod_test passed validation.

表示这个podspec文件验证通过,否则修改到验证通过为止

6、在Example工程修改podfile文件
#pod 'pod_test', :path => '../'
pod 'pod_test',:podspec => '../pod_test.podspec'

再执行pod update

有时候当你使用pod update时会发现特别慢,那是因为pod会默认先更新一次podspec索引。使用--no-repo-update参数可以禁止其做索引更新操作。

如:pod update --no-repo-update

$cd Example/
$ pod update
Installing pod_test (0.1.0)

[!] Error installing pod_test
[!] /usr/bin/git clone https://github.com/kamto6/pod_test.git /var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj --template= --single-branch --depth 1 --branch 0.1.0

Cloning into '/var/folders/7k/1bb1m_fx0xs3r8pzl28mykz00000gn/T/d20180301-13092-1rajecj'...
warning: Could not find remote branch 0.1.0 to clone.
fatal: Remote branch 0.1.0 not found in upstream origin


[!] Automatically assigning platform `ios` with version `9.3` on target `pod_test_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

cd 到pod_test目录

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

然后再执行

$cd Example/
$ pod update

再打开Example工程,发现库文件都被加载到Pods项目下

6、把pod_test.podspec提交到Spec Repo仓库
pod repo push [本地Spec Repo名称][podspec文件路径]
$ pod repo push mySpecs pod_test.podspec  
Validating spec
 -> pod_test (0.1.0)

Updating the `mySpecs' repo

Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.

Adding the spec to the `mySpecs' repo

 - [Add] pod_test (0.1.0)

Pushing the `mySpecs' repo

表示这个库已经提交到Spec Repo上了

在对应的spec仓库地址也能看到提交记录。

这里说的添加到私有的Spec Repo,如果要添加到CocoaPods的官方库,可以用到trunk工具。

使用终端 pod search pod_test

注意:这里搜索repos目录,如果缓存里没有该spec文件,则不会搜索到

-> pod_test (0.1.0)
   私有库项目demo
   pod 'pod_test', '~> 0.1.0'
   - Homepage: https://github.com/xxx/pod_test
   - Source:   https://github.com/xxx/pod_test.git
   - Versions: 0.1.0 [mySpecs repo]

此时到~/.cocoapods/repos/mySpecs

如图:

查看本地repo信息

$ pod repo

接下来我们再新建一个工程来测试一下

新建一个工程,在项目的podfile里添加

#私有spec仓库的地址,而不是某个pod仓库的地址
source 'https://github.com/xxx/mySpecs'
pod 'pod_test'

注意:如果该仓库没有权限,则会失败

到此,算是完成pods私有库的搭建了。

注:如果添加到CocoaPods官方库上,别人都可以search或者install

cd 到pod_test目录,执行

pod trunk push pod_test.podspec

红色警告:

[!] You need to register a session first.
$ pod trunk register xxx@163.com 'xxx' --description='pro'

再执行

$pod trunk push pod_test.podspec

如果提示:

[!] You (xxx@163.com) are not allowed to push new versions for this pod. The owners of this pod are aaaa@126.com.

呵呵。大致的意思是repo 已经被这个所有者占有了

$pod trunk info pod_test
 
终端打印:

pod_test
    - Versions:
      - 0.0.2 (2017-01-13 07:03:27 UTC)
    - Owners:
      - aaa <aaa@126.com>
 

如果没有问题,那么发布到Cocoapods后,在终端更新本地pods仓库信息

$ pod setup

然后查询

$ pod search pod_test

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容