|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: CocoaPods私有仓库的创建 |
| 4 | +subtitle: 手把手教你创建 CocoaPods 公有仓库 |
| 5 | +date: 2017-03-10 |
| 6 | +author: BY |
| 7 | +header-img: img/post-bg-iWatch.jpg |
| 8 | +catalog: true |
| 9 | +tags: |
| 10 | + - iOS |
| 11 | + - CocoaPods |
| 12 | + - Git |
| 13 | +--- |
| 14 | + |
| 15 | +> 本文发布于 [BY Blog](http://qiubaiying.github.io)、[简书](http://www.jianshu.com/p/d2d98298b1b8) 转载请保留链接 |
| 16 | +> |
| 17 | +> 上一篇文章 [《CocoaPods公有仓库的创建》](http://qiubaiying.top/2017/03/08/CocoaPods公有仓库的创建/) |
| 18 | +
|
| 19 | +# 前言 |
| 20 | + |
| 21 | +最近参照了网上一大堆 CocoaPods私有仓库 的教程,按教程操作得到的pod仓库里面是这样的~ |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +代码和版本描述居然混在了一起,简直太糟糕~ |
| 26 | + |
| 27 | +虽然也能用,但是和CocoaPods本身的结构设计就不相符。 |
| 28 | + |
| 29 | +在上一篇[《CocoaPods公有仓库的创建》](http://qiubaiying.top/2017/03/08/CocoaPods公有仓库的创建/)中我们了解到,`master` 目录中只存放 代码库 的描述文件,而不是存放代码。就像这样 |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +代码我们另外存放在代码仓库中 |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +很多人不了解CocoaPods的工作原理就复制粘贴别人的教程来做教程~ |
| 38 | + |
| 39 | +吐槽结束,进入正文 |
| 40 | + |
| 41 | +# 正文 |
| 42 | + |
| 43 | +#### 创建版本库(**repo**) |
| 44 | + |
| 45 | +首先,创建一个像 `master` 一样的存放版本描述文件的git仓库,因为是私人git仓库,我们选择 [oschina](http://git.oschina.net/) 创建远程私有仓库(因为是免费的)或者也可以在GitHub上创建(**$7/month**)。 |
| 46 | + |
| 47 | +下面以 [oschina](http://git.oschina.net/) 为例 |
| 48 | + |
| 49 | +创建版本描述仓库 |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +回到终端,将这个远程的私有版本仓库添加到本地,`repo` 就是 repository 储存库的缩写。 |
| 55 | + |
| 56 | + $ pod repo add MyRepo https://git.oschina.net/baiyingqiu/MyRepo.git |
| 57 | + |
| 58 | +查看在 Finder 目录 `~/.cocoapods/repos`, 可以发现增加了一个 MyRepo 的储存库 |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +#### 创建代码库 |
| 63 | + |
| 64 | +回到 [oschina](http://git.oschina.net/) 创建私人代码库 |
| 65 | + |
| 66 | +创建时添加 `MIT License` 和 `README` |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +将仓库克隆到本地,添加`你的代码文件`、`仓库名.podspec` 描述文件,还有`.swift-version`. |
| 73 | + |
| 74 | +如下 |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +`.swift-version`文件用来知道swift版本,用命令行创建 |
| 79 | + |
| 80 | + $ echo "3.0" > .swift-version |
| 81 | + |
| 82 | +**`.podspec`** 文件是你这个代码库的pod描述文件,可以通过pod指令创建空白模板: |
| 83 | + |
| 84 | + $ pod spec create MyAdditions |
| 85 | + |
| 86 | +或者 **强烈建议** 直接拷贝下面的模板进行修改 |
| 87 | + |
| 88 | +```ruby |
| 89 | +Pod::Spec.new do |s| |
| 90 | + s.name = "MyAdditions" # 项目名称 |
| 91 | + s.version = "0.0.1" # 版本号 与 你仓库的 标签号 对应 |
| 92 | + s.license = "MIT" # 开源证书 |
| 93 | + s.summary = "私人pod代码" # 项目简介 |
| 94 | + |
| 95 | + s.homepage = "https://git.oschina.net/baiyingqiu/MyAdditions" # 仓库的主页 |
| 96 | + s.source = { :git => "https://git.oschina.net/baiyingqiu/MyAdditions.git", :tag => "#{s.version}" }#你的仓库地址,不能用SSH地址 |
| 97 | + s.source_files = "MyAdditions/*.{h,m}" # 你代码的位置, BYPhoneNumTF/*.{h,m} 表示 BYPhoneNumTF 文件夹下所有的.h和.m文件 |
| 98 | + s.requires_arc = true # 是否启用ARC |
| 99 | + s.platform = :ios, "7.0" #平台及支持的最低版本 |
| 100 | + # s.frameworks = "UIKit", "Foundation" #支持的框架 |
| 101 | + # s.dependency = "AFNetworking" # 依赖库 |
| 102 | + |
| 103 | + # User |
| 104 | + s.author = { "BY" => "qiubaiyingios@163.com" } # 作者信息 |
| 105 | + s.social_media_url = "http://qiubaiying.github.io" # 个人主页 |
| 106 | + |
| 107 | +end |
| 108 | +``` |
| 109 | +这里我要说一下一个坑,用 [oschina](http://git.oschina.net/) 创建私人仓库时, 在验证时可能会找不到 `MIT LICENSE`证书,将其中的 |
| 110 | + |
| 111 | + s.license = "MIT" |
| 112 | + 修改为,指定文件 |
| 113 | + s.license = { :type => "MIT", :file => "LICENSE" } |
| 114 | + |
| 115 | +然后开始验证我们的仓库配置是否正确,并按照要求进行修改 |
| 116 | + |
| 117 | + $ pod lib lint |
| 118 | + |
| 119 | +一般出现错误警告,需要添加 `--private` 或者 `--allow-warnings`,就可以通过验证 |
| 120 | + |
| 121 | + $ pod lib lint --private |
| 122 | + |
| 123 | +验证成功后出现 |
| 124 | + |
| 125 | + -> MyAdditions (0.0.1) |
| 126 | + |
| 127 | + MyAdditions passed validation. |
| 128 | + |
| 129 | +#### 将描述文件推送到版本库 |
| 130 | + |
| 131 | +将项目打上标签推到远程仓库,标签号 和 版本号对应 都是`0.0.1` |
| 132 | + |
| 133 | +最后将我们的代码仓库的描述信息,push 到我们的版本仓库中 |
| 134 | + |
| 135 | + $ pod repo push MyRepo MyAdditions.podspec |
| 136 | + |
| 137 | +这时会对远程仓库进行验证,成功的话就会在 `~/.cocoapods/repos/MyRep`中发现新增的仓库描述信息了 |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +若是出现错误信息 |
| 142 | + |
| 143 | + [!] The repo `MyRepo` at `../.cocoapods/repos/MyRepo` is not clean |
| 144 | + |
| 145 | +更新下我们的版本库, |
| 146 | + |
| 147 | + $ pod repo update MyRepo |
| 148 | + |
| 149 | + |
| 150 | +再继续上传即可。 |
| 151 | + |
| 152 | +`pod repo push MyRepo MyAdditions.podspec` 的过程就是 |
| 153 | + |
| 154 | +1. 验证 `MyAdditions.podspec` 文件 |
| 155 | +- 拉取远程版本库 `MyRepo` |
| 156 | +- 添加 `MyAdditions.podspec` 到版本库中 |
| 157 | +- push 到远程 |
| 158 | + |
| 159 | +添加完成后我们就可以在pod中搜索 |
| 160 | + |
| 161 | + $ pod search MyAdditions |
| 162 | +--- |
| 163 | + -> MyAdditions (0.0.1) |
| 164 | + Some category of the framework and UIKit |
| 165 | + pod 'MyAdditions', '~> 0.0.1' |
| 166 | + - Homepage: https://git.oschina.net/baiyingqiu/MyAdditions |
| 167 | + - Source: https://git.oschina.net/baiyingqiu/MyAdditions.git |
| 168 | + - Versions: 0.0.1 [MyRepo repo] |
| 169 | + (END) |
| 170 | + |
| 171 | +### 私人pod库的使用 |
| 172 | + |
| 173 | +使用私人pod库的需要在`Podflie`中添加这句话,指明你的版本库地址。 |
| 174 | + |
| 175 | + source ‘https://git.oschina.net/baiyingqiu/MyRepo.git’ |
| 176 | +**注意**是版本库的地址,而不是代码库的地址,很多教程都把我搞晕了~ |
| 177 | + |
| 178 | + |
| 179 | +若有还使用了公有的pod库,需要把公有库地址也带上 |
| 180 | + |
| 181 | + source ‘https://github.com/CocoaPods/Specs.git’ |
| 182 | + |
| 183 | +最后的`Podflie`文件变成这个样子 |
| 184 | + |
| 185 | + source ‘https://github.com/CocoaPods/Specs.git’ |
| 186 | + source ‘https://git.oschina.net/baiyingqiu/MyRepo.git’ |
| 187 | + |
| 188 | + platform :ios, '8.0' |
| 189 | + |
| 190 | + target ‘MyPodTest’ do |
| 191 | + use_frameworks! |
| 192 | + |
| 193 | + pod “BYPhoneNumTF” #公有库 |
| 194 | + pod ‘MyAdditions’ #我们的私有库 |
| 195 | + pod ‘BYAdditions’ #这是我又添加到版本库中的另一个代码库 |
| 196 | + |
| 197 | + end |
| 198 | + |
| 199 | +测试: |
| 200 | + |
| 201 | + $ pod install |
| 202 | + |
| 203 | +加载完成可以看到代码已经整合到我们的项目中了 |
| 204 | + |
| 205 | +**perfect!** |
| 206 | + |
| 207 | +<img src="https://ww4.sinaimg.cn/large/006tKfTcgy1fdhkgtfn98j30ee0hwq6y.jpg" width="250"> |
| 208 | + |
| 209 | +回到Fender中 `~/.cocoapods/repos`,会发现 repos 中增加了一个pod版本库。 |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +执行 `pod install` 命令时 |
| 214 | + |
| 215 | +- 会拉取远程 `Podflie` 中 `source` 标记 版本库 到本地的 repos 文件夹中 |
| 216 | + |
| 217 | +- 在 版本库 中搜索我们`pod ‘MyAdditions’` 的 `MyAdditions.podspec` 文件。 |
| 218 | +- 根据 `MyAdditions.podspec` 文件中描述的源码地址下载并整合到项目中 |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +# 结语 |
| 223 | + |
| 224 | +通过 [《CocoaPods私有仓库的创建》](http://qiubaiying.top/2017-03-10-CocoaPods私有仓库的创建/) 和 [《CocoaPods公有仓库的创建](http://qiubaiying.top/2017/03/08/CocoaPods公有仓库的创建/)》这两篇文章,相信大家对CocoaPods的工作原理都有了更深层次的了解。 |
| 225 | + |
| 226 | +在写博客和和创建的过程中,踩了不少的坑(😀前人教程留下的),很多的东西只有自己操作完才能真正的领会。 |
| 227 | + |
| 228 | +最后,如果本文有什么错误或者有什么不同的观点欢迎提出交流。😉 |
| 229 | + |
0 commit comments