搭建私有庫(PrivateSpec)
前言:
多項(xiàng)目多工程組件化之路,之前搭建了公司內(nèi)部私有庫,有空整理了下資料
1.名詞解釋
1.1 索引庫 repo
1.2 索引庫中的索引文件 .podspec.json
例子 AFNetworking.podspec.json
{
"name": "AFNetworking",
"version": "4.0.1",
"license": "MIT",
"summary": "A delightful networking framework for Apple platforms.",
"homepage": "https://github.com/AFNetworking/AFNetworking",
"social_media_url": "https://twitter.com/AFNetworking",
"authors": {
"Mattt Thompson": "m@mattt.me"
},
"source": {
"git": "https://github.com/AFNetworking/AFNetworking.git",
"tag": "4.0.1"
},
"platforms": {
"ios": "9.0",
"osx": "10.10",
"watchos": "2.0",
"tvos": "9.0"
},
"ios": {
"pod_target_xcconfig": {
"PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
}
},
"osx": {
"pod_target_xcconfig": {
"PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
}
},
"watchos": {
"pod_target_xcconfig": {
"PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking-watchOS"
}
},
"tvos": {
"pod_target_xcconfig": {
"PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
}
},
"source_files": "AFNetworking/AFNetworking.h",
"subspecs": [
{
"name": "Serialization",
"source_files": "AFNetworking/AFURL{Request,Response}Serialization.{h,m}"
},
{
"name": "Security",
"source_files": "AFNetworking/AFSecurityPolicy.{h,m}"
},
{
"name": "Reachability",
"platforms": {
"ios": "9.0",
"osx": "10.10",
"tvos": "9.0"
},
"source_files": "AFNetworking/AFNetworkReachabilityManager.{h,m}"
},
{
"name": "NSURLSession",
"dependencies": {
"AFNetworking/Serialization": [
],
"AFNetworking/Security": [
]
},
"ios": {
"dependencies": {
"AFNetworking/Reachability": [
]
}
},
"osx": {
"dependencies": {
"AFNetworking/Reachability": [
]
}
},
"tvos": {
"dependencies": {
"AFNetworking/Reachability": [
]
}
},
"source_files": [
"AFNetworking/AF{URL,HTTP}SessionManager.{h,m}",
"AFNetworking/AFCompatibilityMacros.h"
]
},
{
"name": "UIKit",
"platforms": {
"ios": "9.0",
"tvos": "9.0"
},
"dependencies": {
"AFNetworking/NSURLSession": [
]
},
"source_files": "UIKit+AFNetworking"
}
]
}
1.3 代碼庫
code.podspec- Example
- 其他文件
licensereadme.md等
1.4 代碼庫中的索引文件 .podspec
例子 AFNetworking.podspec
Pod::Spec.new do |s|
s.name = 'AFNetworking'
s.version = '4.0.1'
s.license = 'MIT'
s.summary = 'A delightful networking framework for Apple platforms.'
s.homepage = 'https://github.com/AFNetworking/AFNetworking'
s.social_media_url = 'https://twitter.com/AFNetworking'
s.authors = { 'Mattt Thompson' => 'm@mattt.me' }
s.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => s.version }
s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.10'
s.watchos.deployment_target = '2.0'
s.tvos.deployment_target = '9.0'
s.ios.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
s.osx.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
s.watchos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking-watchOS' }
s.tvos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
s.source_files = 'AFNetworking/AFNetworking.h'
s.subspec 'Serialization' do |ss|
ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
end
s.subspec 'Security' do |ss|
ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
end
s.subspec 'Reachability' do |ss|
ss.ios.deployment_target = '9.0'
ss.osx.deployment_target = '10.10'
ss.tvos.deployment_target = '9.0'
ss.source_files = 'AFNetworking/AFNetworkReachabilityManager.{h,m}'
end
s.subspec 'NSURLSession' do |ss|
ss.dependency 'AFNetworking/Serialization'
ss.ios.dependency 'AFNetworking/Reachability'
ss.osx.dependency 'AFNetworking/Reachability'
ss.tvos.dependency 'AFNetworking/Reachability'
ss.dependency 'AFNetworking/Security'
ss.source_files = 'AFNetworking/AF{URL,HTTP}SessionManager.{h,m}', 'AFNetworking/AFCompatibilityMacros.h'
end
s.subspec 'UIKit' do |ss|
ss.ios.deployment_target = '9.0'
ss.tvos.deployment_target = '9.0'
ss.dependency 'AFNetworking/NSURLSession'
ss.source_files = 'UIKit+AFNetworking'
end
end
2、創(chuàng)建索引庫
1)在gitlab上創(chuàng)建一個(gè)新的庫,這個(gè)庫用來保存私有庫的podspec文件,索引庫取名為PrivateSpec。
2)創(chuàng)建本地索引庫文件,然后將其于剛才創(chuàng)建的遠(yuǎn)程索引庫相關(guān)聯(lián)。
pod repo add PrivateSpec https://gitlab.mydomain.com/private-cocoapods/privatespec.git
pod repo add 本地索引庫名稱 遠(yuǎn)程索引庫的git地址
> 注意?。?!此時(shí)的遠(yuǎn)程索引庫是空的!有master分支,可以添加一個(gè)readme.md文件。
3、創(chuàng)建本地私有庫
- 手動(dòng)添加
- 使用pod創(chuàng)建
1)創(chuàng)建本地私有庫
cd 本地私有庫文件夾目錄
pod lib create WLTestTool
pod lib create 私有庫名稱
3)配置完成后會(huì)生成一些內(nèi)容,層級如圖
code.podspec- Example
- 其他文件
licensereadme.md等
4)查看并編寫podspec文件里面的內(nèi)容
#
# Be sure to run `pod lib lint WLTestTool.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|
s.name = 'WLTestTool' #私有庫名稱
s.version = '1.0.0' #版本號,與下面的git tag版本對應(yīng)
s.summary = '這個(gè)是測試庫WLTestTool.'
# 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://gitlab.mydomain.com/private-cocoapods/privatespec' #倉庫首頁地址
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '園丁云' => 'gardeneryun@foxmail.com' }
s.source = { :git => 'https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git', :tag => s.version.to_s } #倉庫地址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
s.source_files = 'WLTestTool/Classes/**/*' #私有庫代碼文件目錄
# s.resource_bundles = {
# 'WLTestTool' => ['WLTestTool/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
5) 將Classes文件夾下面的ReplaceMe.m文件刪除掉,替換成你要上傳私有庫的代碼。
4.驗(yàn)證podspec文件合法性
兩種驗(yàn)證方式,建議先本地校驗(yàn),代碼本地庫push到遠(yuǎn)程倉庫再使用遠(yuǎn)程驗(yàn)證。
1)本地驗(yàn)證,驗(yàn)證文件格式、語法等
pod lib lint podName.podspec
2)遠(yuǎn)程驗(yàn)證,遠(yuǎn)程倉庫地址、驗(yàn)證文件格式、語法等
pod spec lint podName.podspec
3)可選參數(shù)
--allow-warnings:允許警告
--sources=‘master,privateSpecs':指定源,比如你的私有pod同時(shí)依賴了公有庫和私有庫,你必須指定源才行,因?yàn)槟J(rèn)只會(huì)去在公有源中查找對應(yīng)的依賴
--use-libraries:如果使用了靜態(tài)庫,記得加上它
pod lib lint WLTestTool.podspec
pod spec lint WLTestTool.podspec
pod spec lint WLTestTool.podspec --allow-warnings
pod spec lint --sources="https://github.com/CocoaPods/Specs.git,https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git" --allow-warnings
5.代碼本地庫push到遠(yuǎn)程倉庫
1)在Gitlab上創(chuàng)建遠(yuǎn)程私有庫WLTestTool。
2)將代碼本地庫推送到遠(yuǎn)程私有庫。
3)本地驗(yàn)證之后,打tag。然后進(jìn)行遠(yuǎn)程驗(yàn)證。
注意這里添加的tag要跟剛才在spec文件里面寫的版本號一致
6.將代碼庫中的.podspec文件push到私有索引庫
這里也會(huì)做一遍第四點(diǎn)驗(yàn)證合法性,也可以使用
pod repo push PrivateSpecName podName.podspec
pod repo push PrivateSpec WLTestTool.podspec
7.更新本地索引庫
先更新pod庫,不然找不到你剛上傳的私有庫。(此操作也會(huì)更新公共索引庫)
pod repo update (此操作也會(huì)更新公共索引庫)
pod repo update PrivateSpec
8.使用私有庫
使用私有庫需要指定私有索引庫地址。
cocoapods 官方source是隱式的,一旦指定了其他source 就需要把官方的指定上。
source 'https://github.com/CocoaPods/Specs.git'
[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via 'pod repo remove master'. To suppress this warning please add 'warn_for_unused_master_specs_repo => false' to your Podfile.
Podfile 示例
source 'https://github.com/CocoaPods/Specs.git'
source 'https://gitlab.mydomain.com/private-cocoapods/privatespec.git'
use_frameworks!
platform :ios, '9.0'
target 'WLTestTool_Example' do
pod 'WLTestTool', '1.0.0'
target 'WLTestTool_Tests' do
inherit! :search_paths
end
end
use_frameworks!
platform :ios, '9.0'
target 'WLTestTool_Example' do
pod 'WLTestTool', '1.0.0'
pod 'test', :git => 'https://gitlab.mydomain.com/private-cocoapods/test.git', :tag => '1.0.0'
target 'WLTestTool_Tests' do
inherit! :search_paths
end
end



