一所森、前言
公司項目繁多,為了框架的統(tǒng)一和更好維護夯接,需要將自己的業(yè)務焕济,封裝為私有庫,上傳到公司私有git上盔几,利用cocoapods統(tǒng)一管理晴弃。
分解需求:
- 1、創(chuàng)建私有spec repo(相當于cocoapods私有庫資源中心)逊拍, 所有的私有庫上傳記錄在私有spec中
- 2上鞠、工程封裝為私有庫,上傳到私有spec repo中
- 3芯丧、項目工程集成私有庫
二芍阎、創(chuàng)建私有索引庫Spec Repo
1. 什么是spec repo?
它是所有的Pods的一個索引缨恒,就是一個容器谴咸,所有公開的Pods都在這個里面轮听,它實際是一個Git倉庫remote端在GitHub上。
在Podfile中岭佳,我們通過
pod 'AFNetworking'
它會被clone到本地的~/.cocoapods/repos目錄下,可以進入到這個目錄看到master文件夾就是這個官方的Spec Repo了血巍。
我們可以通過命令,
pod repo list
查看mac 上的repo列表:
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/louielee/.cocoapods/repos/master
Spec Repo了珊随,目錄的結構如下:
.
├── YTestPod
└── 0.0.3
└── YTestPod.podspec
2. 創(chuàng)建遠端私有索引庫
在git服務器上創(chuàng)建一個私有倉庫述寡,私有Spec Repo取名為YTestSpec。
pod repo add [Private Repo Name] [Git HTTPS clone URL]
示例
pod repo add YTestSpec https://e.coding.net/LouieLee/lypodtest/YTestSpec.git
完成后使用pod repo list查看如下:
MacBook-Pro$ pod repo list
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/louielee/.cocoapods/repos/master
YTestSpec
- Type: git (master)
- URL: https://e.coding.net/LouieLee/lypodtest/YTestSpec.git
- Path: /Users/louielee/.cocoapods/repos/YTestSpec
...
查看本地pod repos路徑如下:
MacBook-Pro$ cd /Users/xxx/.cocoapods/repos
MacBook-Pro$ ls
Spec_Lock YTestSpec master trunk
此時可以看到YTestSpec已經在本地創(chuàng)建叶洞。
注:這一步是在本地添加私有庫source鲫凶,其他開發(fā)人員Podfile需要集成私有spec上的私有庫,都需要首先通過pod repo add命令京办,本地clone spec repo掀序。
附上其他常用命令:
更新本地[Private Repo Name]的spec repo。私有庫podspec文件更改并push到spec
pod repo update [Private Repo Name]
本地刪除spec repo
pod repo remove [Private Repo Name]
三惭婿、創(chuàng)建私有庫
1. 什么是私有庫不恭?
包含具體功能的項目代碼,使用Git進行代碼管理财饥,被加入私有倉庫來實現(xiàn)cocoapod版本管理和安裝使用换吧。
2. 創(chuàng)建私有庫
創(chuàng)建私有庫的標準格式,會使用git-template默認的模板創(chuàng)建私有庫
pod lib create [Private Lib name]
示例
pod lib create YTestPod
通過--template-url參數(shù)钥星,指定私有庫模板地址
pod lib create --verbose --template-url=[template URL] [Private pod name]
pod-template模板來生成私有庫沾瓦,創(chuàng)建過程中需要確定以下問題:
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 ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
What is your class prefix?
> Y
Running pod install on your new library.
...
注意:在clone pod-template最好使用代理,否則可能會失敗
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
成功后我們看到的目錄結構如下:
MacBook-Pro$ tree -L 2
.
├── Example
│ ├── YTestPod
│ ├── YTestPod.xcodeproj
│ ├── YTestPod.xcworkspace
│ ├── Podfile
│ ├── Podfile.lock
│ ├── Pods
│ └── Tests
├── YTestPod
│ ├── Assets
│ └── Classes
├── YTestPod.podspec
├── LICENSE
├── README.md
└── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
10 directories, 5 files
注:tree需要自行安裝
- YTestPod.podspec是私有庫的配置文件
- YTestPod是私有庫代碼文件谦炒,Assets是放資源贯莺,Classes下是編譯的源文件
- Example是自動生成的測試工程。
3. 編輯私有庫的配置文件
如有疑問可參照官方文檔
#
# Be sure to run `pod lib lint YTestPod.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
# 名稱宁改,pod search搜索的關鍵詞缕探,注意這里一定要和.podspec一致
s.name = 'YTestPod'
# 版本號,每一個版本對應一個tag
s.version = '0.1.0'
# 私有庫摘要
s.summary = 'A short description of HTDNetworkManage.'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
# 這里是私有庫描述說明
DESC
# 項目主頁地址
s.homepage = 'https://github.com/Louie/YTestPod'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
# 許可證
s.license = { :type => 'MIT', :file => 'LICENSE' }
# 作者
s.author = { 'Louie' => 'louie@126.com' }
# 項目倉庫地址(重要)
# 私有庫的remote地址和tag还蹲。也可以:branch => 'master'設置分支爹耗。
s.source = { :git => 'https://github.com/Louie/YTestPod.git', :tag => s.version.to_s }
# 個人主頁網址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
# 源文件路徑
s.source_files = 'YTestPod/Classes/**/*'
# * 表示匹配所有文件
# ** 表示匹配所有子目錄
# 如果有多個目錄下則用逗號分開如['','','']形式
# 資源文件路徑
# s.resource_bundles = {
# 'YTestPod' => ['YTestPod/Assets/*.png']
# }
# 公開的頭文件地址
# s.public_header_files = 'Pod/Classes/**/*.h'
# 所需的framework,多個用逗號隔開
# s.frameworks = 'UIKit', 'MapKit'
# 依賴關系谜喊,該項目所依賴的其他庫潭兽,
# s.dependency 'AFNetworking', '~> 2.3'
# 如果有多個需要填寫多個s.dependency。cocoapods會自動將依賴的其他庫集成到工程中斗遏。
end
3. 編寫私有庫代碼
私有庫源碼放在前面說的/YTestPod/Classes中
4. 提交私有庫代碼到git
可以使用自己平時習慣的方式提交山卦,以下我使用命令提交。
附上git命令:
<!-- 查看狀態(tài) -->
git status
<!-- 添加文件到緩沖區(qū) -->
git add .
<!-- 從緩沖區(qū)提交代碼到倉庫 -->
git commit -m "描述"
<!-- 將本地庫的代碼推到遠程庫 -->
git push -f origin master
<!-- 添加tag -->
git tag -a '0.0.1' -m '描述'
<!-- 查看tag -->
git tag
<!-- 將本地創(chuàng)建的tag推到遠程庫 -->
git push --tags
<!-- 刪除tag -->
git tag -d '0.0.1'
注:podspec配置文件中 s.source設置了:tag => s.version.to_s诵次,version是0.1.0怒坯,所以需要打tag炫狱,tag必須version保持一致
5. 校驗私有庫
注:當代碼里有警告時會驗證失敗,可通過參數(shù)
參數(shù)解釋
--verbose 輸出詳情
--allow-warnings 允許警告
--use-libraries 允許使用靜態(tài)庫
--skip-import-validation 跳過驗證
5.1. 驗證本地podspec文件
本地驗證不會驗證 s.source 中的tag
MacBook-Pro$ pod lib lint
-> YTestPod (0.0.1)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- 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 and one is not being generated automatically. (in target 'App' from project 'App')
YTestPod passed validation.
注:pod spec lint只驗證一個本地倉庫
5.2. 驗證遠程podspec
遠程驗證會驗證 s.source 中的tag剔猿,如果此時沒有打上相應的標簽則會報錯
MacBook-Pro$ pod spec lint
-> YTestPod (0.0.1)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- 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 and one is not being generated automatically. (in target 'App' from project 'App')
Analyzed 1 podspec.
YTestPod passed validation.
注:pod spec lint更為精確视译,會同時驗證本地倉庫和遠程倉庫。
6. 發(fā)布私有庫
<!-- 跳轉到本地倉路徑提交本地倉內容 -->
cd /Users/xxx/.cocoapods/repos/[Private Repo Name]
<!-- 允許空提交 -->
git commit --allow-empty
<!-- 將本地庫的代碼推到遠程庫 -->
git push -f origin master
<!-- 創(chuàng)建庫 -->
pod lib create [Private Repo Name]
<!-- 更新庫 -->
<!--[Private Repo Name] 遠端倉庫名
[Private Lib Name].podspec 要上傳的podspec 注意[Private Lib Name].podspec所在的路徑归敬,我這里使用的是相對路徑酷含,你也可以使用絕對路徑比如:/Users/xxx/Documents/[Private Lib Name].podspec
--sources是索引庫對應的遠端倉庫地址 -->
pod repo push [Private Repo Name] [Private Lib Name].podspec --sources='[Private Repo clone HTTPS URL]'
注:如果有警告導致發(fā)布失敗,需要使用 --verbose --allow-warnings忽略警告
示例
MacBook-Pro$ pod repo push YTestSpec YTestPod.podspec --sources='https://e.coding.net/LouieLee/lypodtest/YTestSpec.git'
Validating spec
-> YTestPod (0.0.1)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- 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 and one is not being generated automatically. (in target 'App' from project 'App')
Updating the `YTestSpec' repo
Adding the spec to the `YTestSpec' repo
- [Add] YTestPod (0.0.1)
Pushing the `YTestSpec' repo
注: 如果設置代理可能會出現(xiàn)發(fā)布失敗需要關閉
關閉代理
unset http_proxy unset https_proxy
發(fā)布成功可以通過pod search查到該私有庫
MacBook-Pro$ pod search YTestPod
-> YTestPod (0.0.3)
A short description of YTestPod.
pod 'YTestPod', '~> 0.0.3'
- Homepage: https://louielee.coding.net/p/lypodtest/d/YTestPod/git
- Source: https://e.coding.net/LouieLee/lypodtest/YTestPod.git
- Versions: 0.0.3 [YTestSpec repo]
四汪茧、集成測試
在Podfile文件最頂部添加如下描述椅亚,然后執(zhí)行pod install
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
source 'http://xxx/HTDNetworkManageSpec.git'
target 'YBRouterAndDecouplingDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'HTDNetworkManage'
# Pods for YBRouterAndDecouplingDemo
end
私有索引庫和私有庫是一個git地址。會導致以下問題
An unexpected version directory ‘Classes’ was encountered for the /Users/name/.cocoapods/repos/** Pod in the xxx repository.
[!] Unable to find a pod with name, author, summary, or description matching 'xxx'
私有庫完成舱污。