前言
看本地cocoaPod庫(kù) cd ~/.cocoapods/repos在repos文件夾下面,可以看到你當(dāng)前存在的一些庫(kù),當(dāng)你創(chuàng)建了自己的私有倉(cāng)庫(kù)時(shí),也會(huì)在這里顯示。
按照正常邏輯來(lái)看,私有庫(kù)是一個(gè)地址,版本號(hào)管理庫(kù)是一個(gè)地址,最好不要把兩者放在一起,會(huì)顯得很亂。
所以創(chuàng)建私有庫(kù),我們需要?jiǎng)?chuàng)建兩個(gè)庫(kù):一個(gè)是私有倉(cāng)庫(kù)repo,用來(lái)做私有庫(kù)的版本管理;另一個(gè)是私有代碼庫(kù),用來(lái)做代碼編寫(xiě)。
創(chuàng)建私有倉(cāng)庫(kù) repo
這里的私有空間是指私有庫(kù)存放的位置,repo是repository(存儲(chǔ)庫(kù))的縮寫(xiě),如果私有庫(kù)的podSpec和代碼存放在一起就會(huì)顯得很亂,所有通常的做法就是再私有空間只存放私有庫(kù)的podSpec文件(可以理解為版本號(hào))。
創(chuàng)建一個(gè)私有倉(cāng)庫(kù)
這里使用碼云,可以免費(fèi)創(chuàng)建多個(gè)私有工程。例如創(chuàng)建了一個(gè)私有存儲(chǔ)庫(kù):https://gitee.com/devAlan/PrivateRepo。
添加到本地倉(cāng)庫(kù)中
pod repo add [本地倉(cāng)庫(kù)repo名稱] [遠(yuǎn)程repo地址]
pod repo add PrivateRepo https://gitee.com/devAlan/PrivateRepo
使用上面步驟就可以將遠(yuǎn)程的私有版本存儲(chǔ)倉(cāng)庫(kù)添加到本地。在Finder中查看repos中就可以發(fā)現(xiàn)增加了一個(gè)文件夾PrivateRepo。
創(chuàng)建私有代碼庫(kù)
創(chuàng)建一個(gè)私人代碼庫(kù)
在碼云上創(chuàng)建一個(gè)私人代碼庫(kù),例如已經(jīng)創(chuàng)建好的代碼庫(kù):https://gitee.com/devAlan/privateAdd。
在創(chuàng)建時(shí)添加MIT License和README。
將倉(cāng)庫(kù)clone到本地,添加代碼文件。
添加.podspec文件
“.podspec”文件是這個(gè)代碼庫(kù)的pod描述文件,使用pod命令創(chuàng)建空白模板。
pod spec create 項(xiàng)目名稱
這里使用上面的私有代碼庫(kù)創(chuàng)建pod spec create privateAdd。
需要注意一下:生成的默認(rèn)的podspec文件中,所有沒(méi)有注釋掉的都必須填寫(xiě)正確。
編輯“.podspec”文件
#
# Be sure to run `pod spec lint PrivateAdd_v2.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
# 聲明
Pod::Spec.new do |s|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# These will help people to find your library, and whilst it
# can feel like a chore to fill in it's definitely to your advantage. The
# summary should be tweet-length, and the description more in depth.
#
# 私有庫(kù)名稱
s.name = "PrivateAdd_v2"
# 版本號(hào),跟當(dāng)前版本tag有關(guān)
s.version = "0.0.1"
# 項(xiàng)目描述
s.summary = "A short description of PrivateAdd_v2."
# 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
DESC
# 主頁(yè),自動(dòng)生成的podspec文件中所有的EXAMPLE都需要改
s.homepage = "http://EXAMPLE/PrivateAdd_v2"
# 項(xiàng)目截圖
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See http://choosealicense.com for more info.
# CocoaPods will detect a license file if there is a named LICENSE*
# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
#
# 項(xiàng)目遵守的協(xié)議
s.license = "MIT (example)"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the authors of the library, with email addresses. Email addresses
# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
# accepts just a name if you'd rather not provide an email address.
#
# Specify a social_media_url where others can refer to, for example a twitter
# profile URL.
#
# 作者信息
s.author = { "liujiajia" => "liujiajia@gerrit.babytree-inc.com" }
# Or just: s.author = "liujiajia"
# s.authors = { "liujiajia" => "liujiajia@gerrit.babytree-inc.com" }
# s.social_media_url = "http://twitter.com/liujiajia"
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If this Pod runs only on iOS or OS X, then specify the platform and
# the deployment target. You can optionally include the target after the platform.
#
# s.platform = :ios # 平臺(tái)
# 平臺(tái)以及版本號(hào)
# s.platform = :ios, "5.0"
# When using multiple platforms
# s.ios.deployment_target = "5.0"
# s.osx.deployment_target = "10.7"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
#當(dāng)前私有庫(kù)資源的git地址,相應(yīng)的tag,這里直接使用代碼來(lái)標(biāo)識(shí)使用和當(dāng)前tag一樣的值
s.source = { :git => "http://EXAMPLE/PrivateAdd_v2.git", :tag => "#{s.version}" }
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# CocoaPods is smart about how it includes source code. For source files
# giving a folder will include any swift, h, m, mm, c & cpp files.
# For header files it will include any header in the folder.
# Not including the public_header_files will make all headers public.
#
# 需要引用的文件代碼,classes文件夾下的所有.h.m文件。這里的路徑都是相對(duì)路徑
s.source_files = "Classes", "Classes/**/*.{h,m}"
# 排除掉哪些文件,不被包含的文件,這里是Classes/Exclude文件夾下的所有
s.exclude_files = "Classes/Exclude"
# s.public_header_files = "Classes/**/*.h"
# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# A list of resources included with the Pod. These are copied into the
# target bundle with a build phase script. Anything else will be cleaned.
# You can preserve files from being cleaned, please don't preserve
# non-essential files like tests, examples and documentation.
#
# s.resource = "icon.png" # 資源文件
# s.resources = "Resources/*.png" # 所有資源文件
# s.preserve_paths = "FilesToSave", "MoreFilesToSave"
# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Link your library with frameworks, or libraries. Libraries do not include
# the lib prefix of their name.
#
# s.framework = "UIKit" # 依賴的系統(tǒng)framework,去掉后綴
# s.frameworks = "SomeFramework", "AnotherFramework"
# s.library = "iconv" # 依賴的系統(tǒng)library
# s.libraries = "iconv", "xml2", "z" # 依賴的library,去除前綴和后綴,例如“l(fā)ibz.tbd”,則直接引用為"z"
# s.vendored_frameworks = "*.framework", "*.framework" #依賴那些靜態(tài)庫(kù)
# s.vendored_libraries = "*.a", "*.a" # 依賴哪些靜態(tài)library
# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If your library depends on compiler flags you can set them in the xcconfig hash
# where they will only apply to your library. If you depend on other Podspecs
# you can include multiple dependencies to ensure it works.
# s.requires_arc = true # 是否開(kāi)啟ARC
# s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => 'HMTIMEINPREGNACYAPP=1', "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# 這里配置一些內(nèi)容,"GCC_PREPROCESSOR_DEFINITIONS":定義一個(gè)宏,
# "HEADER_SEARCH_PATHS":如果引用該私有庫(kù),則會(huì)在主工程中尋找文件夾下的內(nèi)容
# s.dependency "JSONKit", "~> 1.4" # 使用了pod,依賴哪些庫(kù)
# s.prefix_header_contents = <<-EOS #這里會(huì)生成一個(gè)默認(rèn)pch文件,作用于Xcode工程中添加的pch文件一樣。
# #import <UIKit/UIKit.h>
# EOS
end
這里有一個(gè)需要注意的地方,在創(chuàng)建私有庫(kù)的時(shí)候,找不到MIT LICENSE證書(shū),這里講license修改一下
s.license = { :type => "MIT", :file => "LICENSE" }
二級(jí)庫(kù)subspec
s.subspec 'UIView+Category' do |ss|
ss.source_files = "PrivateAdd_v3/PrivateAdd_v3/Classes/UIView+Category/*.{h,m}"
ss.framework = "UIKit"
# ss.vendored_libraries = '/*.a'
# ss.vendored_frameworks = '/*.framework'
# ss.resource = 'Moments/ForPregnacy/**/*.bundle', 'Moments/ForPregnacy/**/*.png', 'Moments/ForPregnacy/**/*.mp3'
end
驗(yàn)證私有庫(kù)
修改完相應(yīng)的代碼塊之后,開(kāi)始驗(yàn)證私有庫(kù)配置是否正確。
本地驗(yàn)證
pod lib lint
// 如果使用了私有庫(kù),則使用--sources來(lái)標(biāo)明,給出具體地址
pod lib lint --sources=[私有倉(cāng)庫(kù)repo地址],[私有庫(kù)git地址],https://github.com/CocoaPods/Specs.git
如果出現(xiàn)錯(cuò)誤,基本上都是podspec文件中的配置出現(xiàn)了問(wèn)題,例如:
$ pod lib lint
-> PrivateAdd_v2 (0.0.1)
- WARN | attributes: Missing required attribute `license`.
- WARN | license: Missing license type.
- ERROR | description: The description is empty.
- ERROR | [iOS] unknown: Encountered an unknown error (The `PrivateAdd_v2` pod failed to validate due to 1 error:
- WARN | attributes: Missing required attribute `license`.
- WARN | license: Missing license type.
- ERROR | description: The description is empty.
) during validation.
[!] PrivateAdd_v2 did not pass validation, due to 2 errors and 2 warnings.
You can use the `--no-clean` option to inspect any issue.
這就需要將上面的錯(cuò)誤修改掉。
一般情況下出現(xiàn)警告在后面添加pod lib lint --allow-warnings,就可以通過(guò)驗(yàn)證了。驗(yàn)證通過(guò)會(huì)提示passed validation。
如果依賴一些靜態(tài)庫(kù)(.framework,.a),則在命令后面添加--use-libraries,表示依賴了靜態(tài)庫(kù)。
如果報(bào)錯(cuò)不是很明顯,這命令后添加--verbose,會(huì)輸出驗(yàn)證的過(guò)程,并給出報(bào)錯(cuò)信息。
遠(yuǎn)端驗(yàn)證
pod spec lint,一般情況下私有庫(kù)都是存在遠(yuǎn)程git上的,所以基本上只在第一次驗(yàn)證使用本地驗(yàn)證之后,就可以直接使用遠(yuǎn)端驗(yàn)證了。
但是一般情況下,創(chuàng)建的私有代碼庫(kù)如果是私有的(并不是官方的),這時(shí)候,我們?cè)谧鲞h(yuǎn)端驗(yàn)證時(shí),需要添加代碼地址。,不然pod會(huì)默認(rèn)從官方repo查詢。
pod spec lint --sources=[私有倉(cāng)庫(kù)repo地址],[私有庫(kù)git地址],https://github.com/CocoaPods/Specs.git
# 或者只添加前面兩項(xiàng),最后一項(xiàng)可以忽略
pod spec lint --sources=[私有倉(cāng)庫(kù)repo地址],[私有庫(kù)git地址]
如果出現(xiàn)一些警告,可以直接在命令最后添加--allow-warnings來(lái)屏蔽警告。例如:
pod spec lint --sources=https://gitee.com/devAlan/PrivateRepo,https://gitee.com/devAlan/PrivateAdd_v3.git,https://github.com/CocoaPods/Specs.git --allow-warnings
pod spec lint --sources=https://gitee.com/devAlan/PrivateRepo,https://gitee.com/devAlan/PrivateAdd_v3.git --allow-warnings
發(fā)布私有庫(kù)
將podspec文件推送到私有空間repo,在發(fā)布時(shí)一定要記住,已經(jīng)做了tag處理。
pod repo push [私有倉(cāng)庫(kù)repo] [私有庫(kù)podspec文件地址] --allow-warnings
pod repo push [私有倉(cāng)庫(kù)repo] [私有庫(kù)podspec文件地址] --sources=[私有倉(cāng)庫(kù)repo地址],[私有庫(kù)地址],https://gitee.com/devAlan/PrivateAdd_v2.git --allow-warnings
由于基本上的所有命令操作都是在該私有庫(kù)文件夾下,所以不用管podspec的文件位置。
pod repo push PrivateRepo PrivateAdd.podspec --allow-warnings
執(zhí)行該命令時(shí),相當(dāng)于直接將podspec文件push到repo的Git地址上。
引用私有庫(kù)
本地路徑引用
做好驗(yàn)證之后,如果需要測(cè)試,可以直接在podfile中引用私有庫(kù)的本地路徑
// ../是回到上一級(jí)文件目錄,是以當(dāng)前podfile所在文件位置決定
pod 'Private_v2', :path => '../../Private_v2'
這個(gè)時(shí)候如果改動(dòng)Private_v2中的代碼就是直接改動(dòng)。
repo引用
在podfile中先添加source,然后直接pod引入,當(dāng)然在引用私有庫(kù)的時(shí)候,不僅需要需要私有倉(cāng)庫(kù)repo的權(quán)限,還需要有私有庫(kù)的權(quán)限。
source 'https://gitee.com/devAlan/PrivateRepo'
pod 'Private_v2'
#或者使用下面的方式
#pod 'Private_v2', :git=> 'https://gitee.com/devAlan/PrivateRepo'
pod 常用命令
安裝podfile中依賴的命令
pod install
pod update
--no-repo-update不需要更新repo
--repo-update更新reporepo相關(guān)
pod repo list
pod repo remove xxx移除一個(gè)本地repo
pod repo add [本地倉(cāng)庫(kù)repo名稱] [遠(yuǎn)程repo地址]遠(yuǎn)程版本空間添加到本地私有庫(kù)創(chuàng)建相關(guān)
pod lib lint本地驗(yàn)證
pod spec lint遠(yuǎn)程驗(yàn)證
pod repo push [本地倉(cāng)庫(kù)repo] [*.podspec]發(fā)布版本到repo
--source=[私有repo地址][私有代碼庫(kù)地址][pod地址]
--allow-warnings忽略warning
--use-libraries使用了第三方的framework或者.a文件
--verbose打印流程,可以快速定位失敗原因podspec中使用到的*
/**子目錄下所有文件夾
/*目錄下所有文件
遇到的問(wèn)題
-
拿我們的項(xiàng)目來(lái)說(shuō),一個(gè)項(xiàng)目依賴另一個(gè)項(xiàng)目中的某些文件,這些都是強(qiáng)制性的依賴。導(dǎo)致本地驗(yàn)證和遠(yuǎn)程驗(yàn)證都無(wú)法通過(guò),所以更別說(shuō)發(fā)布了。
這種是沒(méi)有完全的組件化。我們需要在podspec中添加HEADER_SEARCH_PATHS,添加指定位置文件路徑。這里的路徑也是相對(duì)路徑。s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => 'HMTIMEINPREGNACYAPP=1', "HEADER_SEARCH_PATHS" => "$(SRCROOT)/usr/include/libxml2", $(SRCROOT)/../Commen/** }但是這樣做仍然無(wú)法通過(guò)驗(yàn)證,這就需要我們將私有空間repo pull到本地,手動(dòng)創(chuàng)建版本號(hào)文件夾添加podspec文件,然后push。
-
在上面的代碼中還有一個(gè)
GCC_PREPROCESSOR_DEFINITIONS,這是定義一個(gè)宏,可以在Project-Build Settings - Preprocessor Macros下看到。可以配合一些沒(méi)有必要引入私有代碼庫(kù)中的文件來(lái)使用。