Cocoapods创建私有Specs(一)

Cocoapods创建私有Specs(二)

Pod, One Pod, over the course of time, will have many Specs. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description. 我们平常使用的是Cocoapods官方维护的一个开源的Pod库,它本身是一个git管理的Ruby项目。我们可以自己创建一个公开Pod或私有Pod,在公司开发内部我们一般会维护一个私有的Pod项目,其中会包含内部使用的二方(与三方相对)项目。

引用私有Spec(:git)

1. 名词解析
  • Podfile,
  • Podfile.lock,
  • repos, repository (仓库)的缩写。一个Pod项目在本地的clone,默认一般位于~/.cocoapods/repos/目录,内部存放多个Pod项目,master是Cocoapods官方维护的Pod在本地的Clone,第一次pod install时会从Github下载。
  • pod library, 支持cocoapods引用的开源(或私有)项目
  • project, 此处Xocde项目
  • Podspec或者Spec,是对一个pod库的版本描述。每一个支持pod的库都必须包含.podsepc文件
  • Specs,多个podspec的集合
2. 创建一个支持cocoapods的pod library项目
  • 创建一个Xcode项目,并git初始化
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
emoji:MyLib penn$ ls
MyLib MyLib.xcodeproj MyLibTests MyLibUITests
emoji:MyLib penn$ git init
Initialized empty Git repository in /Users/penn/Desktop/MyLib/.git/
emoji:MyLib penn$ git add .
emoji:MyLib penn$ git commit -m "init"
[master (root-commit) 12e3f57] init
19 files changed, 1052 insertions(+)
create mode 100644 MyLib.xcodeproj/project.pbxproj
create mode 100644 MyLib.xcodeproj/project.xcworkspace/contents.xcworkspacedata
create mode 100644 MyLib.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
create mode 100644 MyLib.xcodeproj/project.xcworkspace/xcuserdata/penn.xcuserdatad/UserInterfaceState.xcuserstate
create mode 100644 MyLib.xcodeproj/xcuserdata/penn.xcuserdatad/xcschemes/xcschememanagement.plist
create mode 100644 MyLib/AppDelegate.h
create mode 100644 MyLib/AppDelegate.m
create mode 100644 MyLib/Assets.xcassets/AppIcon.appiconset/Contents.json
create mode 100644 MyLib/Assets.xcassets/Contents.json
create mode 100644 MyLib/Base.lproj/LaunchScreen.storyboard
create mode 100644 MyLib/Base.lproj/Main.storyboard
create mode 100644 MyLib/Info.plist
create mode 100644 MyLib/ViewController.h
create mode 100644 MyLib/ViewController.m
create mode 100644 MyLib/main.m
create mode 100644 MyLibTests/Info.plist
create mode 100644 MyLibTests/MyLibTests.m
create mode 100644 MyLibUITests/Info.plist
create mode 100644 MyLibUITests/MyLibUITests.m
  • 与gitlab上的项目关联
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
emoji:MyLib penn$ git remote add origin git@127.0.0.1:xxxxxx/mylib.git
emoji:MyLib penn$ git pull --rebase origin master
warning: no common commits
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (9/9), done.
From 127.0.0.1:xxxxxx/mylib
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: init
emoji:MyLib penn$ git push -u origin master
Counting objects: 34, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (30/30), done.
Writing objects: 100% (34/34), 13.13 KiB | 2.63 MiB/s, done.
Total 34 (delta 3), reused 0 (delta 0)
To 127.0.0.1:xxxxxx/mylib.git
f5b531d..20d4161 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
emoji:MyLib penn$
  • 为项目添加podspec文件
    pod spec create mylib
1
2
3
emoji:MyLib penn$ pod spec create mylib

Specification created at mylib.podspec
  • 在项目中添加可以被他人引用的代码,eg: mylib/Open/Replace.h、Replace.m,并添加提交.

修改mylib.podspec文件

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

s.name = "mylib"
s.version = "0.0.1"
s.summary = "MyLib简介"

s.description = <<-DESC
必填,。。。。。。。
* 随便写,支持Markdown
DESC
s.platform = :ios

s.platform = :ios, "9.0"

s.homepage = "http://127.0.0.1/xxxxxx/mylib.git"

s.license = { :type => "MIT", :file => "LICENSE" }

s.author = { "xxxxxx" => "xxxxxx@gmail.com" }

s.source = { :git => "http://127.0.0.1/xxxxxx/mylib.git"}

s.source_files = "mylib/Open/*.{h,m}"

s.requires_arc = true

end

添加Replace类,并验证podspec文件的有效性
发现有警告,但没有错误。

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
emoji:MyLib penn$ git add .
emoji:MyLib penn$ git commit -m "添加open代码"
[master 8a97f0d] 添加open代码
5 files changed, 72 insertions(+)
rewrite MyLib.xcodeproj/project.xcworkspace/xcuserdata/penn.xcuserdatad/UserInterfaceState.xcuserstate (90%)
create mode 100644 MyLib/Open/ReplaceMe.h
create mode 100644 MyLib/Open/ReplaceMe.m
create mode 100644 mylib.podspec
emoji:MyLib penn$ git push
Counting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 7.44 KiB | 7.44 MiB/s, done.
Total 13 (delta 4), reused 0 (delta 0)
To 127.0.0.1:xxxxxx/mylib.git
20d4161..8a97f0d master -> master
emoji:MyLib penn$ pod spec lint mylib.podspec

-> mylib (0.0.1)
- WARN | source: Git sources should specify a tag.
- WARN | url: The URL (http://127.0.0.1/xxxxxx/mylib.git) is not reachable.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')

Analyzed 1 podspec.

[!] The spec did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
emoji:MyLib penn$

Podfile:添加引用
pod 'mylib', :git => 'http://127.0.0.1/xxxxxx/mylib.git'

创建的PodProject项目引用新创建的mylib库,pod install成功

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`
Fetching podspec for `YYText` from `/Users/penn/Desktop/LocalDep/YYText`
Pre-downloading: `mylib` from `http://127.0.0.1/xxxxxx/mylib.git`
Downloading dependencies
Using PNText (0.0.1)
Using YYText (1.0.7)
Installing mylib (0.0.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.
emoji:PodProject penn$

上述步骤已经可以通过pod引用

3. 为MyLib添加标签

实际使用过程中并不会像上面那样使用,一般会添加tag,这样在使用过程中就可以指定特定的版本。添加标签并推送到服务器

1
2
3
4
5
6
7
8
9
emoji:MyLib penn$ git tag 0.0.1
emoji:MyLib penn$ git push --tags
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 1.84 KiB | 1.84 MiB/s, done.
Total 8 (delta 4), reused 0 (delta 0)
To 127.0.0.1:xxxxxx/mylib.git
* [new tag] 0.0.1 -> 0.0.1
4. 创建一个私有的Specs仓库
  • 在gitlab创建一个私有的空的仓库
  • 添加私有Repo到Cocoapods
    $ pod repo add REPO_NAME SOURCE_URL
    可以看到/Users/penn/.cocoapods/repos目录下多了一个MyLib的Pod ,master是Cocoapods官网维护的Pod在本地的clone。
1
2
3
4
emoji:repos penn$ pwd
/Users/penn/.cocoapods/repos
emoji:repos penn$ ls
MySpeces master
5. 添加podspec到Repo

修改podspec文件,指定tag
s.source = { :git => "http://127.0.0.1/xxxxxx/mylib.git", :tag => "0.0.1" }

pod repo push REPO_NAME SPEC_NAME.podspec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

emoji:MyLib penn$ pod repo push MySpeces mylib.podspec

Validating spec
-> mylib (0.0.1)
- WARN | url: The URL (http://127.0.0.1/xxxxxx/mylib.git) is not reachable.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')

Updating the `MySpeces' repo

Already up to date.

Adding the spec to the `MySpeces' repo

- [Add] mylib (0.0.1)

Pushing the `MySpeces' repo

emoji:MyLib penn$

结果在自己创建的Pod即MySpecs下多了一个自己要开源的二方项目

1
2
3
4
emoji:repos penn$ cd MySpeces/
emoji:MySpeces penn$ ls
README.md mylib
emoji:MySpeces penn$

6. 修改Podfile
  • 添加source
  • 添加依赖
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

# Uncomment the next line to define a global platform for your project

#指定pods的搜索路径
source 'http://127.0.0.1/x'x'x'x/MySpecs.git'
source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '9.0'
target 'PodProject' do

#引用开源pod,如果有私有source时,也需要添加公有开源source
#pod 'AFNetworking'

#本地pod引用
pod 'YYText', :path=>'/Users/penn/Desktop/LocalDep/YYText'
pod 'PNText', :path=>'/Users/penn/Desktop/LocalDep/PNText'

#没有私有的Specs时,指定Git URL
#pod 'mylib', :git => 'http://127.0.0.1/xxxxxx/mylib.git'

#创建Specs并添加source后,直接使用私有仓库
pod 'mylib'

#####所有的pod都必须有NAME.podspec文件.#######
end
  • cd到PodProject项目根目录,pod install.
    可以看到我们新建的私有Cocoapods项目mylib已经可以通过pod使用
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`
Fetching podspec for `YYText` from `/Users/penn/Desktop/LocalDep/YYText`
Cloning spec repo `0-xxxxxx-myspecs` from `http://127.0.0.1/xxxxx/MySpecs.git`
Downloading dependencies
Using PNText (0.0.1)
Using YYText (1.0.7)
Installing mylib 0.0.1
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.
emoji:PodProject penn$
7. Over
起风了,您的支持将鼓励我继续创作!