利用Cocoapods創建基於SVN的私有庫podspec


由於項目年后要進行組件化,考慮到公司內部實現的一些私有組件,不對外公開,而又想在不同項目中使用,該怎么辦呢?由於cocoapods有了強大的功能,可以自己創建podspec,更可以設置私有的庫。那么利用cocoapods來管理公共組件就很方便了。

使用Cocoapods制作私有庫就完美的解決了這個問題。下圖就是使用私有庫帶給我們的好處:

 

Cocoapods是如何實現查找第三方庫呢?如下圖:

 

從圖中,可以看到通過.podspec可以找到我們想要的第三方庫,那么.spec是什么呢?如下:

Pod::Spec.new do |s|

s.name         = "JSPatchtest"

s.version      = "0.0.1"

s.summary      = "yoowei 測試利用SVN創建私用庫"

s.description  = <<-DESC

yoowei 測試利用SVN創建私用庫

DESC

s.homepage     = "https://www.baidu.com"

s.license      = "MIT"

s.author             = { "yoowei" => "yoowei@126.com" }

s.platform     = :ios, "7.0"

s.source       = { :svn => "https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest",:tag => "0.0.1"}

s.source_files  = 'yoowei/**/*'

end

至此,我們明白了查找到第三方庫背后的原理。但是,具體如何進行搜索的呢?

先解釋一個概念,什么是Spec Repo?它是所有的Pods的一個索引,也就是一個容器(集合),所有公開的Pods都在這個里面。實際上就是一個Git倉庫remote端,當你使用了Cocoapods,這個倉庫會被clone到本地的~/.cocoapods/repos目錄下,進入到這個文件下可以看到master文件夾,就是官方的Spec Repo了。當你pod search的時候就在master這個文件下面查找的。

 

如何制作出屬於自己的私有庫?

但是問題來了,我們公司用的是SVN ,網上的資料介紹的絕大多數是基於git的 ,怎么辦?

查找資料發現:

  #  Specify the location from where the source should be retrieved.

  #  Supports git, hg, bzr, svn and HTTP.

 結果是明確的,支持SVN制作私有庫,那么接下來就是不斷的踩坑,終於成功了,記錄下來備忘,如果能夠幫到別人更好。

 至於需要SVN賬號密碼,在接下來的步驟里面會有設計。

1、我利用JSPatch作為自己的私有庫的組件,進項測試,建立如下的目錄結構,並提前將JSPatchtest上傳到我們SVN服務器

SVN上面的文件結構(如下)

2、把svn上面對應的JSPatchtest代碼check out到本地,cd到JSPatchtest該目錄下,使用pod命令創建一個podspec文件。

$ pod spec create JSPatchtest

具體終端操作如下:

$ cd /Users/galahad/Desktop/yoowei 

$ svn co https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest

 

A    JSPatchtest/yoowei

A    JSPatchtest/yoowei/JPEngine.h

A    JSPatchtest/yoowei/JSPatch.js

A    JSPatchtest/yoowei/JPEngine.m

Checked out revision 41541.

$ cd /Users/galahad/Desktop/yoowei/JSPatchtest 

$ pod spec create JSPatchtest

 Specification created at JSPatchtest.podspec 

 

3、打開JSPatchtest.podspec文件,進行修改(不明白的可以先查資料了解)。然后將文件提交到SVN服務器

Pod::Spec.new do |s|

s.name         = "JSPatchtest"

s.version      = "0.0.1"

s.summary      = "yoowei 測試利用SVN創建私用庫"

s.description  = <<-DESC

yoowei 測試利用SVN創建私用庫

DESC

s.homepage     = "https://www.baidu.com"

s.license      = "MIT"

s.author             = { "yoowei" => "yoowei@126.com" }

s.platform     = :ios, "7.0"

s.source       = { :svn => "https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest"}

s.source_files  = 'yoowei/**/*'

end

當我們用的svn時候,

s.source       = { :svn => "https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest"}

細心的你應該發現默認的時候是有一個tags存在的,現在先刪掉,后面會敘述。

s.source_files  = "yoowei", "yoowei/**/*"

4、使用

剛巧下載了一個LXMySelf的demo,就在這里面試一下

podfile文件描述如下:

target 'LXMySelf' do

pod 'AFNetworking'

pod 'Masonry'

pod 'UITableView+FDTemplateLayoutCell'

pod 'MJRefresh'

pod 'MJExtension'

pod 'MBProgressHUD'

pod 'SDWebImage'

pod 'YYCache'

pod 'SDCycleScrollView'

pod 'JSPatchtest', :svn => 'https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest'

end

注意如果是首次的話,會讓你輸入SVN權限密碼的。(由於剛操作了一遍了,密碼已經輸入過了)

$ cd /Users/galahad/Desktop/yoowei/LXSelfProject-master 

$ pod install

Analyzing dependencies

Pre-downloading: `JSPatchtest` from `https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest`

Downloading dependencies

Using AFNetworking (3.1.0)

Installing JSPatchtest 0.0.1 (was 0.0.1)

Using MBProgressHUD (1.0.0)

Using MJExtension (3.0.13)

Using MJRefresh (3.1.12)

Using Masonry (1.0.1)

Using SDCycleScrollView (1.65)

Using SDWebImage (3.8.1)

Using UITableView+FDTemplateLayoutCell (1.4)

Using YYCache (1.0.3)

Generating Pods project

Integrating client project

Sending stats

Pod installation complete! There are 10 dependencies from the Podfile and 10 total pods installed.

檢查一下,安裝成功。

5、補充tag設置

設置tag其實還是很簡單的,我這里使用的是cornrstone管理svn代碼。需要在JSPatchtest目錄下新建一個tags文件夾,我采用的是本地創建好,然后上傳到SVN。

 

SVN目錄結構如下,選中yoowei文件夾右擊 Tag ,填寫tag和選擇tags的位置,之后創建tags.

創建完tags之后的SVN目錄結構。

 

 6、再試

podfile文件描述如下:

target 'LXMySelf' do

pod 'AFNetworking'

pod 'Masonry'

pod 'UITableView+FDTemplateLayoutCell'

pod 'MJRefresh'

pod 'MJExtension'

pod 'MBProgressHUD'

pod 'SDWebImage'

pod 'YYCache'

pod 'SDCycleScrollView'

pod 'JSPatchtest', :svn => 'https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest',:tag =>'0.0.1'

end

終端操作:

$ pod install

Analyzing dependencies

Pre-downloading: `JSPatchtest` from `https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest`, tag `0.0.1`

Downloading dependencies

Using AFNetworking (3.1.0)

Installing JSPatchtest 0.0.1 (was 0.0.1)

Using MBProgressHUD (1.0.0)

Using MJExtension (3.0.13)

Using MJRefresh (3.1.12)

Using Masonry (1.0.1)

Using SDCycleScrollView (1.65)

Using SDWebImage (3.8.1)

Using UITableView+FDTemplateLayoutCell (1.4)

Using YYCache (1.0.3)

Generating Pods project

Integrating client project

Sending stats

Pod installation complete! There are 10 dependencies from the Podfile and 10 total pods installed.

檢查安裝成功

疑問1?

當tag版本未改變,修改了本地代碼,pod install能否更新成功?

如果不能,是否需要將pod file文件中引用該庫注釋先刪除,再添加執行兩次pod install?大家可以嘗試一下。

或者直接pod update嘗試一下

疑問2?

如果我們自己的組件里面,

s.dependency  ' AFNetworking', "~> 3.0.0"

應該在工程文件podfile地方需要先pod  AFNetworking。

如果有其他的問題的話,日后慢慢改正。

解決問題的過程中踩的坑:

1、svn服務器沒有設置JSPatchtest的tags,項目podfile文件里面的來源寫上了tag 引起的錯誤

$ pod install
Analyzing dependencies
Pre-downloading: `JSPatchtest` from `https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest`, tag `0.0.1`

[!] Error installing JSPatchtest
[!] /usr/bin/svn export --non-interactive --trust-server-cert --force https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest/tags/0.0.1 /var/folders/rg/gmjp61bx0x95t4tjx2hh_3sr0000gn/T/d20170106-35146-108d7q8

svn: E170000: URL 'https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest/tags/0.0.1' doesn't exist

2、考慮到SVN上面每個人都有自己的賬號和密碼,s.source       = { :svn => "https://yoowei@yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest"}是基於我的賬號的,制作的過程中,我曾經輸過密碼。假如我將其改成:s.source       = { :svn => "https://yoowei.svn.net:443/svn/yoowei/ios/3.0/branches/JSPatchtest"} 會是怎么樣的情況呢?經過團隊其他人員的測試,不需要密碼,一切OK.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM