引
本文主要论述:
一、如何创建一个私有的索引库,类似于Cocoapod master Specs库,一众主流开源库都在此处“备案”。
二、创建私有库,需要代码共用、协同编写,创建私有库,通过Cocoapods来协作,简单易用。
目的,在于解决公司内部库代码管理,最终目的实现模块化开发。
一、创建自己的索引库
创建一个索引库,名为:BFSpecs,后面发布会用到。
二、创建私有库
创建私有库,假如已经有工程代码:
2.1 已有工程
工程目录如下:
对应的远程目录:
可以在任何地方创建你的仓库,笔者在Github上创建,当然Gitlab也是不错的选择。
可以按照以下步骤:
1. 创建Podspec文件1
2//pod spec create [库名称]
$ pod spec create BeeFun
该文件内容如下,作了删减,突出重点,根据自己的项目调整。
详细配置,参考☞。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59od::Spec.new do |s|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.name = "BeeHub"
s.version = "0.0.2"
s.summary = "Github resetful api in swift language."
# This description is used to generate tags and improve search results.
s.description = "Github resetful api in swift language.The network layer is Alamofire and Moya, and json to model library is ObjectMapper"
s.homepage = "https://github.com/wenghengcong/BeeHub"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.license = { :type => "MIT", :file => "LICENSE" }
# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.author = { "wenghengcong" => "wenghengcong@gmail.com" }
s.social_media_url = "https://weibo.com/wenghengcong"
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
# s.platform = :ios
# s.platform = :ios, "5.0"
# When using multiple platforms
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.source = { :git => "https://github.com/wenghengcong/Bee.git", :tag => "#{s.version}" }
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
//库源码,目录与podspec目录同级,所以以下,Source与本文件属于同级目录。
s.source_files = "Classes", "Source/**/*.{h,m,md,swift}"
//排除的文件目录
s.exclude_files = "Classes/Exclude"
s.swift_version = '4.1'
# s.public_header_files = "Classes/**/*.h"
# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
# s.resource = "icon.png"
# s.resources = "Resources/*.png"
# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
# s.frameworks = "SomeFramework", "AnotherFramework"
# s.libraries = "iconv", "xml2"
# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.requires_arc = true
# s.dependency "JSONKit", "~> 1.4"
s.dependency 'Moya'
s.dependency 'ObjectMapper'
end
其中s.source_files需要主要不要指定错误目录路径。在校验时遇到以下错误。
ERROR | [iOS] file patterns: The source_files pattern did not match any file.
s.source_file指定的目录与podspec文件同级,所以上面的文件,Source与本文件属于同级目录。
2. 修改代码,发布版本
之后,完成库代码的编写。
1
2
3
4
5
6// 推送
$ git add -A && git commit -m "v0.0.2"
$ git push origin master
// tag
$ git tag '0.0.2'
$ git push --tags
3. podspec校验
配置好之后,开始校验,校验有两步:
本地校验:pod lib lint,主要校验语法。
远程校验:pod spec lint,会校验依赖库及版本tag。
本地校验1
2
3$ pod lib lint BeeHub.podspec
//如果需要进行详细的输出
$ pod lib lint BeeHub.podspec --verbose
假如lint过程中,输出error或者warning,根据错误进行修改即可。
警告:假如没有error,只有warning,可以通过--allow-warnings,跳过lint。如果passed validation,就验证通过了。
1$ pod lib lint BeeHub.podspec --allow-warnings
错误:
如果这一步提示你swift版本:
[!] Invalid BeeHub.podspec file: undefined method `swift_version=’ for #<:specification name="”BeeHub”">.
通过以下步骤修复:
1
2$ echo "4.2" > .swift-version
$ pod lib lint BeeHub.podspec
错误2
-ERROR | [tvOS] unknown: Encountered an unknown error (Malformed version number string ) during validation.
最终的解决方案是:
$ gem update –system 2.7.6
因为gem 2.7.7版本有问题。
其他方案:
或者尝试
Brew update instructions update Homebrew(because there is no prompt, it will wait for a while to be completed)
Install RVM
Use RVM to install the latest version of ruby(this time using ruby-2.4.1)
Use the command sudo gem install -n /usr/local/bin cocoapods to reload cocoapods.
或者删除Xcode 10
远程校验1
2
3
4
5
6
7
8
9
10
11
12
13$ pod spec lint BeeHub.podspec
-> BeeHub (0.0.2)
- ERROR | [iOS] unknown: Encountered an unknown error ([!] /usr/bin/git clone https://github.com/wenghengcong/BeeHub.git /var/folders/8s/6g8sfwqd3w18n0wv9nb3tqgd34sx35/T/d20181012-12077-1gktz3z --template= --single-branch --depth 1 --branch 0.0.2
Cloning into '/var/folders/8s/6g8sfwqd3w18n0wv9nb3tqgd34sx35/T/d20181012-12077-1gktz3z'...
warning: Could not find remote branch 0.0.2 to clone.
fatal: Remote branch 0.0.2 not found in upstream origin
) during validation.
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error.
校验过程中,提醒没有0.0.2版本的tag。
GitHub repository发布
在对应的Github repository发布release版本,并对应为上面的s.version。此处,发布了0.0.2 release版本。
再次校验。
3. 发布
我们先讲述,如何发布到Cocoapods索引库
发布到公有索引库,需要先注册,再发布。
1 )注册1$ pod trunk register wenghengcong@gmail.com 'wenghengcong' --description='MacWeng'
去邮箱查收邮件点击即可。
然后可以查看是否注册成功,可以看到对应邮件的历史注册信息及拥有的库。
1$ pod trunk me
2 )发布到Cocoapods1$ pod trunk push BeeHub.podspec
若遇到
[!] You (wenghengcong@gmail.com) are not allowed to push new versions for this pod. The owners of this pod are …
假如是自己的库:
1$ pod trunk add-owner BeeHub wenghengcong@gmail.com
或者只能更换库的名称了。
若遇到
[!] CocoaPods was not able to update the master repo. If this is an unexpected issue and persists you can inspect it running `pod repo update –verbose
忽略警告,继续上传:
1$ pod trunk push BeeHub.podspec --allow-warnings
3 )搜索自己的库!
☞ pod search BeeHub
1
2
3
4
5
6
7>-> BeeHub (0.0.1)
> Github resetful api in swift language.
> pod 'BeeHub', '~> 0.0.1'
>
> - Homepage: https://github.com/wenghengcong/BeeHub
> - Source: https://github.com/wenghengcong/BeeHub.git
> - Versions: 0.0.1 [master repo]
4 )删除某版本库
可以删除某个版本的库。
1$ pod trunk delete {podname} {version}
当然需要完全删除该库,需要去Cocoapods发issue。
2.2 模板创建工程
2.2.1 Clone 模板工程1$ pod lib create BeeHub
之后,会让你做出一系列选择。
Clone完成后,可以看到一个工程目录,其结构如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18tree -L 2
.
├── .travis.yml
├── BeeHub
│ ├── Assets
│ └── Classes
├── BeeHub.podspec
├── Example
│ ├── BeeHub
│ ├── BeeHub.xcodeproj
│ ├── BeeHub.xcworkspace
│ ├── Podfile
│ ├── Podfile.lock
│ ├── Pods
│ └── Tests
├── LICENSE
├── README.md
└── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
上面的路径分别是:
路径
说明
- BeeFun
库源码、资源路径,其在工程中有个替身。
- Example
Demo 路径,方便进行开发、测试。
.travis.yml
Travis ci文件
_Pods.xcodeproj
指向Example/Pods/Pods.xcodeproj
打开Example中的工程:
之后,所有的开发均在Development Pods中开发,其中的BeeHub路径,就是工程目录下的BeeHub替身。
每次BeeHub中文件有变化,执行pod isntall,就能同步到Development Pods中看到。
2.2.2 修改代码,发布版本
假如我们修改完代码之后,要发布0.0.3:
1
2
3
4
5
6// 推送
$ git add -A && git commit -m "v0.0.3."
$ git push origin master
// tag
$ git tag '0.0.3'
$ git push --tags
2.2.3 发布到私有索引库
首先将自己的索引库,添加到本地repo list。
1$ pod repo add BFSpecs https://github.com/wenghengcong/BFSpecs.git
然后,和发布到Cocoapods类似,将Specs文件发布到自己的私有索引库中即可。
1$ pod repo push BFSpecs BeeHub.podspec
发布是否成功了呢?
看仓库,或者搜索。
三、创建自己的模板
上面,我们根据默认模板创建了代码库。
1$ pod lib create BeeHub
该命令的完整形式是:
1$ pod lib create ProjectName --template-url=https://github.com/CocoaPods/pod-template.git
只要再对应的url填写上自己的模板地址即可。
那么如何创建一个可用、高效的模板?
参考