Cocoapods创建私有Specs(一)

Cocoapods创建私有Specs(一)

  • 本文全程在本地测试,通过Mac+Docker+GitLab 构建本地Git服务器。

  • 整个过程我们会创建多个项目,包括:
    PodProject:一个Xcode项目用来测试我们的开源代码能否通过pod使用。对应我们公司的APP项目。
    MyLib: 一个Git通过管理的Xocde项目,主要包括要开源的代码。对应‘AFNetworking,SDWebimage等第三方开源项目。
    MySpecs: 私有的Specs仓库,也是一个Git项目,对应Cocoapods官网维护的Specs

引用本地文件(:path)

1. 创建xcode项目,初始化pod init,编辑Podfile文件
2. 临时先下载YYKit放在本地,使用:path引用, 同时创建任意文件夹(eg:PNText)也使用:path引用。:path选项会到指定的路径下寻找文件,并将这些文件直接链接到Pods项目中。
1
2
3
4
5
6
7
platform :ios, '9.0'
target 'PodProject' do

pod 'YYText', :path=>'/Users/xxx/Desktop/LocalDep/YYText/'
pod 'PNText', :path=>'/Users/xxx/Desktop/LocalDep/PNText/'

end
3. pod install 失败,因为PNText库缺少.podspec文件
1
2
3
4
Analyzing dependencies
Fetching podspec for `PNText` from `/Users/penn/Desktop/LocalDep/PNText/`
[!] No podspec found for `PNText` in `/Users/penn/Desktop/LocalDep/PNText/`
emoji:PodProject penn$

注释掉PNText,继续pod install,成功.

1
2
3
4
5
6
7
8
9
10
11
12
emoji:PodProject penn$ pod install
Analyzing dependencies
Fetching podspec for `YYText` from `/Users/penn/Desktop/LocalDep/YYText/`
Downloading dependencies
Installing YYText (1.0.7)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `PodProject.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
emoji:PodProject penn$

注意,podspec的位置

4. 创建podspec文件 pod spec create NAME.podspec

为PNText添加podspec文件:

1
2
3
4
5
6
7
8
9
emoji:~ penn$ cd Desktop/LocalDep/PNText/
emoji:PNText penn$ ls
ReplaceMe.m
emoji:PNText penn$ pod spec create pntext

Specification created at pntext.podspec
emoji:PNText penn$ ls
ReplaceMe.m pntext.podspec
emoji:PNText penn$

保留自动生成的podspec文件的第一行和最后一行,

1
2
3
Pod::Spec.new do |s|

end

去引用PNText的项目中pod install,可以看到podspec文件需要的最简内容,ERRO项(忽略警告)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
emoji:PodProject penn$ pod install
Analyzing dependencies
Fetching podspec for `PNText` from `/Users/penn/Desktop/LocalDep/PNText`
[!] The `PNText` pod failed to validate due to 7 errors:
- ERROR | attributes: Missing required attribute `name`.
- ERROR | attributes: Missing required attribute `authors`.
- WARN | attributes: Missing required attribute `license`.
- ERROR | attributes: Missing required attribute `homepage`.
- ERROR | attributes: Missing required attribute `source`.
- ERROR | attributes: Missing required attribute `summary`.
- ERROR | version: A version is required.
- WARN | license: Missing license type.
- ERROR | File Patterns: The No-name spec is empty (no source files, resources, resource_bundles, preserve paths, vendored_libraries, vendored_frameworks, dependencies, nor subspecs).
emoji:PodProject penn$

根据上面提示编辑podsepec文件,如下:

  • 引用本地pods时最简podspec文件
1
2
3
4
5
6
7
8
9
10
11
# s.name与s.source_files的第一个路径(PNText)一定要相同,区分大小写
Pod::Spec.new do |s|
s.name = "PNText"
s.version = "0.0.1"
s.summary = "A short description of pntext."
s.homepage = "http://baidu.com"
s.author = { "" => "" }
s.source = { :git => ""}
s.source_files = "PNText/*"

end
5. 验证podspec的合法性

pod lib lint 上面最简podsepec文件验证不能通过,但是依然可以在项目中pod install成功

相对完整podspec文件,可以在私有Spec中使用

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
Pod::Spec.new do |s|

s.name = "pnlib"
s.version = "0.0.1"
s.summary = "简介"

s.description = <<-DESC
描述,必填,。。。。。。。支持Markdown语法
* 随便写
* 千里之堤毁于蚁穴
* 鞋垫什么吧。。。。滴滴滴滴滴滴
DESC

s.platform = :ios

s.platform = :ios, "9.0"

s.homepage = "http://127.0.0.1/xxxxx/pnlib.git" #必须能够访问

s.license = { :type => "MIT", :file => "LICENSE" } #开原协议,文件名称必须与本地对应

s.author = { "xxxx" => "pennemoji@gmail.com" } #作者信息

s.source = { :git => "http://127.0.0.1/xxxxxx/pnlib.git", :tag => "0.0.1" } #

s.source_files = "pnlib/Trans/*.{h,m}" #需要开源的文件

s.requires_arc = true # 内存管理方式是ARC

end

6.使用最简podsepc,继续pod install
1
2
3
4
5
6
7
8
9
10
11
12
emoji:PodProject penn$ pod install
Analyzing dependencies
Fetching podspec for `PNText` from `/Users/penn/Desktop/LocalDep/PNText`
Fetching podspec for `YYText` from `/Users/penn/Desktop/LocalDep/YYText`
Downloading dependencies
Installing PNText (0.0.1)
Installing YYText (1.0.7)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
emoji:PodProject penn$
7. Success Over,通过gitlab管理私有仓库见下篇。😆😆😯

引用repo库的根目录的特定podspec

  • 引用master
    pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'

  • 引用repo的不同分支
    pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'

  • 引用repo的特定tag
    pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'
  • 引用repo的某次提交
    pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'
起风了,您的支持将鼓励我继续创作!