創(chuàng)建私有Spec Repo
先建立一個(gè)私有g(shù)it倉(cāng)庫(kù)频祝,用來(lái)存放podspec口锭。我是建在碼云上。
然后在終端執(zhí)行命令:
$ pod repo add FCPrivateRepo https://gitee.com/sergeant/fcprivate-repo.git
執(zhí)行成功后糊肠,可以在~/.cocoapods/repos目錄下看到FCPrivateRepo文件夾闸盔。
也可以在終端執(zhí)行命令查看所有的repo:
$ pod repo
FCPrivateRepo
- Type: git (master)
- URL: https://gitee.com/sergeant/fcprivate-repo.git
- Path: /Users/zhijiazhong/.cocoapods/repos/FCPrivateRepo
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/zhijiazhong/.cocoapods/repos/master
trunk
- Type: CDN
- URL: https://cdn.cocoapods.org/
- Path: /Users/zhijiazhong/.cocoapods/repos/trunk
3 repos
創(chuàng)建私有組件
我下面新建一個(gè)項(xiàng)目FCPrivateComponentA:
$ pod lib create FCPrivateComponentA
執(zhí)行過(guò)程中會(huì)問(wèn)以下問(wèn)題悯辙,按需回答即可:
What platform do you want to use?? [ iOS / macOS ]
> ios
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?
> FC
完成之后就可以添加組件代碼了,加在FCPrivateComponentA/Classes目錄下迎吵。
然后我們需要再創(chuàng)建一個(gè)私有倉(cāng)庫(kù)存放這個(gè)組件:
https://gitee.com/sergeant/fcprivate-component-a.git
之后我們就可以編輯podspec文件了躲撰。
至少需要修改以下4個(gè)字段:
- s.summary # 簡(jiǎn)介
- s.description # 詳細(xì)描述,要比簡(jiǎn)介長(zhǎng)
- s.homepage # 主頁(yè)
- s.source # 倉(cāng)庫(kù)地址
編輯完如下:
#
# Be sure to run `pod lib lint FCPrivateComponentA.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 = 'FCPrivateComponentA'
s.version = '0.1.0'
s.summary = 'A private pod for test.'
# 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
A private pod which show a view controller with red background.
DESC
s.homepage = 'https://gitee.com/sergeant/fcprivate-component-a'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Finger Chung' => '260910416@qq.com' }
s.source = { :git => 'https://gitee.com/sergeant/fcprivate-component-a.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'FCPrivateComponentB/Classes/**/*'
# s.resource_bundles = {
# 'FCPrivateComponentB' => ['FCPrivateComponentB/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
然后我們就可以提交代碼了钓觉。先在.gitignore文件中忽略Pod/文件夾和.xcworkspace文件茴肥。
然后執(zhí)行命令:
$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin https://gitee.com/sergeant/fcprivate-component-a.git #添加遠(yuǎn)端倉(cāng)庫(kù)
$ git push origin master #提交到遠(yuǎn)端倉(cāng)庫(kù)
因?yàn)閜odspec文件中獲取Git版本控制的項(xiàng)目還需要tag號(hào),所以我們要打上一個(gè)tag:
$ git tag -m "first release" 0.1.0
$ git push --tags #推送tag到遠(yuǎn)端倉(cāng)庫(kù)
做完上面的工作荡灾,私有組件就做好了瓤狐,但是我們要驗(yàn)證一下是否可用。使用命令:
pod lib lint
執(zhí)行后可能出現(xiàn)警告導(dǎo)致驗(yàn)證不通過(guò):
$ pod lib lint
-> FCPrivateComponentA (0.1.0)
- WARN | url: The URL (https://gitee.com/sergeant/fcprivate-component-a) is not reachable.
如果跟我一樣提示主頁(yè)不可訪問(wèn)批幌,可能是因?yàn)槲覀兪褂玫氖撬接袔?kù)础锐,直接忽略就好:
$ pod lib lint --allow-warnings
-> FCPrivateComponentA (0.1.0)
- WARN | url: The URL (https://gitee.com/sergeant/fcprivate-component-a) 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 and one is not being generated automatically. (in target 'App' from project 'App')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'FCPrivateComponentA' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
FCPrivateComponentA passed validation.
這樣就驗(yàn)證通過(guò)了。
如果是其他警告荧缘,則需要一一修改皆警。一般都是podspec文件不規(guī)范導(dǎo)致的。
比如我一開(kāi)始沒(méi)有修改s.summary截粗,第二次s.description比s.summary短信姓,都被警告了鸵隧。
驗(yàn)證通過(guò)后,就可以嘗試使用了意推。
本地測(cè)試
先在本地測(cè)試下組件是否可用豆瘫。
新建一個(gè)工程。cd到工程根目錄菊值,然后使用命令 pod init 生成Podfile外驱。
使用本地路徑導(dǎo)入FCPrivateComponentA:
pod 'FCPrivateComponentA', :path => '../FCPrivateComponentA'
path 既可以使用相對(duì)路徑,也可以使用絕對(duì)路徑腻窒。
還可以用podsepc文件的路徑:
pod 'FCPrivateComponentA', :podspec => '../FCPrivateComponentA/FCPrivateComponentA.podspec'
然后執(zhí)行 pod install 安裝昵宇。
$ pod install
Analyzing dependencies
Fetching podspec for `FCPrivateComponentA` from `../FCPrivateComponentA/FCPrivateComponentA.podspec`
Downloading dependencies
Installing FCPrivateComponentA (0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
打開(kāi)項(xiàng)目工程,可以看到庫(kù)文件都被加載到Pods子項(xiàng)目中了儿子,不過(guò)它們并沒(méi)有在Pods目錄下瓦哎,而是跟測(cè)試項(xiàng)目一樣存在于Development Pods/FCPrivateComponentA,這是因?yàn)槲覀兪窃诒镜販y(cè)試典徊,而沒(méi)有把podspec文件添加到Spec Repo中的緣故杭煎。
添加到Spec Repo
現(xiàn)在,我們最開(kāi)始創(chuàng)建的Spec Repo派上用場(chǎng)了卒落。
只需要一個(gè)命令即可:
$ pod repo push FCPrivateRepo FCPrivateComponentA.podspec #前面是本地Repo名字 后面是podspec名字
執(zhí)行完后,我們可以在~/.cocoapods/repos/FCPrivateRepo目錄下看到FCPrivateComponentA蜂桶。
同樣儡毕,在FCPrivateRepo的遠(yuǎn)程倉(cāng)庫(kù)也可以看到FCPrivateComponentA.podspec被提交上去。
我們也可以搜索到這個(gè)組件:
$ pod search FCPrivateComponentA
-> FCPrivateComponentA (0.1.0)
A private pod for test.
pod 'FCPrivateComponentA', '~> 0.1.0'
- Homepage: https://gitee.com/sergeant/fcprivate-component-a
- Source: https://gitee.com/sergeant/fcprivate-component-a.git
- Versions: 0.1.0 [FCPrivateRepo repo]
到這里組件就制作完成了扑媚。
正式使用
重新打開(kāi)Podfile腰湾,將本地測(cè)試時(shí)指定的本地路徑去掉:
pod 'FCPrivateComponentA'
還需要在頭部加上我們的私有Spec Repo地址,注意不要寫(xiě)成了私有組件的倉(cāng)庫(kù):
source 'https://gitee.com/sergeant/fcprivate-repo.git'
完整的Podfile:
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
# 這里是私有索引庫(kù)的地址
# 而不是私有組件的地址 https://gitee.com/sergeant/fcprivate-component-a.git
source 'https://gitee.com/sergeant/fcprivate-repo.git'
target 'TestPrivatePod' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for TestPrivatePod
pod 'FCPrivateComponentA'
end
執(zhí)行 pod update:
$ pod update
Update all pods
Updating local specs repositories
$ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo fetch
origin --progress
$ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo rev-parse
--abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo reset
--hard origin/master
HEAD is now at 4941c37 [Add] FCPrivateComponentA (0.1.0)
Analyzing dependencies
Downloading dependencies
Installing FCPrivateComponentA 0.1.0
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
如有項(xiàng)目組的其他人需要使用疆股,需要給他對(duì)應(yīng)私有庫(kù)的權(quán)限费坊,然后也執(zhí)行命令:
$ pod repo add FCPrivateRepo https://gitee.com/sergeant/fcprivate-repo.git