Cocoapods私有倉庫搭建

來自我的team
公共庫創(chuàng)建:
http://blog.csdn.net/yangyangzhang1990/article/details/52851328

簡介


目的

1、在開發(fā)過程中,會積累很多可以復用的代碼,有些我們不想開源,又想像開源庫一樣在CocoaPods中管理它們,那么就可以通過私有倉庫來管理
2、為后期的組件化方式實現(xiàn)做準備。
優(yōu)點:
減少項目合并過程中的沖突,讓項目結(jié)構(gòu)更加整潔,方便項目組件遷移使用。
缺點:
進行私有庫管理會比在項目開發(fā)中直接修復錯誤操作麻煩一些。

最坑的誤區(qū)

需要注意pods私有庫我們需要維護兩個庫:

一個是私有的Specs
一個是我們的代碼工程

不要把這兩個混在一起了。
參考地址: http://blog.csdn.net/yangyangzhang1990/article/details/52863220

如何制作出自己的私有庫(其實很簡單)

  • 建立私有的spec倉庫 (例如:WLProjectPodsRepos)
    https://coding.net/ 創(chuàng)建私有倉庫管理中心,主要用來管理我們所有私有庫的.podspec文件,項目名稱為:WLProjectPodsRepos
  • clone私有的spec倉庫到本地
    Clone 出錯怎么辦?
    Coding的使用幫助中心: https://coding.net/help/faq/git/git.html
    1、請確保 remote url (大小寫敏感)是正確的
$ git remote -v
# 查看目前使用的 remote url
origin https://git.coding.net/username/repo-name.git (fetch)
origin https://git.coding.net/username/repo-name.git (push)
$ git remote set-url origin https://git.coding.net:username/right-name.git
# 修改為正確的 remote url

2、請確保自己在項目中的權(quán)限非 受限;
3、如果使用 https 方式 clone,服務器端一直返回 403 并且客戶端不提示輸入密碼,則有可能是 git 客戶端緩存了錯誤的密碼,請清除已保存的密碼

# 下面依次輸入
$ git credential-osxkeychain erase
host=git.coding.net
protocol=https
[按回車]

克隆項目到本地

 $ cd WorkSpace/
 $ git clone https://git.coding.net/welian_xxx/WLProjectPodsRepos.git
# 輸入有權(quán)限用戶名密碼
# xxxxxx  xxx

把公有Specs里面的CocoaPods-version.yml文件拷貝到WLPodsrepo,CocoaPods-version.yml所在位置見下圖


屏幕快照 2016-12-10 下午12.44.55.png

并建立一個名為“Specs”的文件夾(文件夾里面可以放一個無關的小文件,否則推送遠程時候,可能會被忽略)

把添加的內(nèi)容推送到遠端倉庫,私有Specs創(chuàng)建完成
$ cd WLProjectPodsRepos/
$ git add .
$ git pull 
$ git commit -s -m "Add PodInfo"
$ git push origin master
將包裝好的遠程的私有Specs倉庫再次 clone到 本地
$ pod repo add  [SpecRepoName]  [GitAddress] clone 到 .cocoapods 目錄下
$ pod repo add WLProjectPodsRepos https://git.coding.net/welian_wu/WLProjectPodsRepos.git

執(zhí)行成功,在本地~/.cocoapods/repos路徑下就能發(fā)現(xiàn),多了一個 WLProjectPodsRepos的文件夾


屏幕快照 2016-12-10 下午1.19.47.png

自己的私有庫工程

創(chuàng)建遠程倉庫 本地工程,同步代碼,參考Cocoapods公有庫項目工程創(chuàng)建。詳見:http://blog.csdn.net/yangyangzhang1990/article/details/52851328

1、創(chuàng)建項目工程,并把項目工程上傳到git上面, 核心代碼添加到 Classes 文件中
屏幕快照 2016-12-10 下午4.48.30.png
2、把本地項目與Git倉庫進行關聯(lián)
$ cd /Users/liuwu/WorkSpace/WLCommonKit
$ git init
# 把整個目錄添加add 通配符
$ git status
$ git add . # 將本地文件添加到倉庫
$ git commit -m "commit all files"
# 將本地與Coding倉庫關聯(lián)起來
$ git remote add origin https://git.coding.net/welian_wu/WLCommonKit.git
$ git pull origin master
$ git push origin master

git pull的時候出現(xiàn)錯誤:

error:
fatal: refusing to merge unrelated histories
解決方案
$ git pull origin master --allow-unrelated-histories
之后,會進入vim編輯狀態(tài),如果合并信息沒有強求,直接輸入q:直接退出
3、把項目類文件放入Classes文件夾中

為項目工程打tag
pods管理下的庫,版本是根據(jù)git的tag來區(qū)分的,所以我們創(chuàng)建好工程之后需要為我們的倉庫打一個tag,并且推送到遠程倉庫

$ git tag 0.0.1 # 未本地倉庫打tag
$ git push --tags # 推送到遠程倉庫
工程中添加podspec (一定要 cd 到當前工程路徑,在添加 podspec)
  • 1、創(chuàng)建spec文件,文件內(nèi)容
    pod spec create WLCommonKit
    

會在當前目錄下生成 .podspec 文件

  • 2 、編輯.podspec文件,相應配置代表的意思 百度 谷歌 或者自己翻譯下吧

Be sure to run `pod spec lint WLCommonKit.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.

s.name = "WLCommonKit"
s.version = "0.0.1"
s.summary = "A short description of WLCommonKit."

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
s.homepage = "http://EXAMPLE/WLCommonKit"

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'.

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 = { "" => "" }

Or just: s.author = ""

s.authors = { "" => "" }

s.social_media_url = "http://twitter.com/"

――― 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

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.

s.source = { :git => "http://EXAMPLE/WLCommonKit.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.

s.source_files = "Classes", "Classes/*/.{h,m}"
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 = "SomeFramework"

s.frameworks = "SomeFramework", "AnotherFramework"

s.library = "iconv"

s.libraries = "iconv", "xml2"

――― 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

s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }

s.dependency "JSONKit", "~> 1.4"

end
```
注意:
1 s.version應和tag的版本一致;
2 將源代碼放置在固定的文件夾下,同時修改s.source;
3 驗證podspec文件;

$ pod lib lint
$ pod lib lint WLCommonKit.podspec --allow-warnings --use-libraries

4 推送podspec文件 到pods的specs首先push修改后的podspec文件到本地工程的git遠程倉庫

$ git commit -a -m "Add WLCommonKit.podspec文件"
$ git push origin master
  • 3、提交 spec文件(私有庫 提交到自己的specs 文件夾中,公有庫使用 trunk 提交到Cocoapods的官方specs 中)

$ cd /Users/liuwu/WorkSpace/WLCommonKit

把當前的.podspec文件推送到WLPodsrepo私有庫當中

$ pod repo push WLProjectPodsRepos WLCommonKit.podspec
or

如有警告使用下面

$ pod repo push WLProjectPodsRepos WLCommonKit.podspec --allow-warnings
--use-libraries

不出意外 用pod搜索就能搜到
  ```
$ pod search WLCommonKit
  • 4、如何使用私有庫
    我們這里創(chuàng)建一個 test555的工程,按照截圖中的方法進行編輯 Podfile文件,就可以正常使用了

    platform :ios, '8.0'
    

公有庫

source 'https://github.com/CocoaPods/Specs.git'

私有庫

source 'https://git.coding.net/welian_wu/WLProjectPodsRepos.git'
use_frameworks!
def shared_pods
pod 'WLCommonKit'
pod 'Masonry'
...
end
target 'WLCommonKit' do
shared_pods
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.0'
end
end
end
```
執(zhí)行 pod install 安裝成功

其他人怎么使用遠程私有庫
  • 將遠程的私有Specs倉庫Clone到本地
    $ pod repo add  [SpecRepoName]  [GitAddress]
    # clone 到 .cocoaPods 目錄下 添加私有倉庫到本地
    $ pod repo add WLProjectPodsRepos https://git.coding.net/welian_wu/WLProjectPodsRepos.git
    # 更新私有庫資源
    $ pod repo update WLProjectPodsRepos
    

查詢本地是否有對應的項目庫

$ pod search WLCommonKit

最后是 編輯項目Podfile文件 pod install;

#####私有庫依賴私有庫
參考上面,創(chuàng)建私有倉庫,添加.podspec文件。修改spec文件 比如:
![屏幕快照 2016-12-14 下午10.57.58.png](http://upload-images.jianshu.io/upload_images/1825863-5ffdd6a6de38b52c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

提交私有項目庫內(nèi)容到遠程Git倉庫。然后,使用以下方式驗證.podspec文件。
![屏幕快照 2016-12-14 下午11.00.23.png](http://upload-images.jianshu.io/upload_images/1825863-e183c1711a1e68a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#####出現(xiàn)錯誤處理
1、[!] Unable to find a pod with name, author, summary, or description matching WLKit

解決 : rm ~/Library/Caches/CocoaPods/search_index.json

2、Setting up CocoaPods master repo
親 ,動一動 谷歌下
3
![屏幕快照 2016-12-14 下午11.04.22.png](http://upload-images.jianshu.io/upload_images/1825863-d60e5ad7cc60b217.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容