iOS代碼組件化--利用cocoaPods創建私有庫
如果項目模塊多,模塊間邏輯復雜,我們發現多個人同時維護一個代碼倉庫需要十分小心,一不小心,造成沖突,解決起來很煩,相信很多人都遇到手工刪除合並的沖突的文件的經歷。
如果利用組件化思想,每個人維護自己的模塊對應的代碼庫,將會大大降低沖突的風險,而且組件化能夠很好的給工程解耦。
組件化的第一步就是創建自己的倉庫,公司的話需要搭建並維護私有庫。
1、查看本地索引庫
我們用cocoaPods 的時候,默認使用的是cocoaPods自帶的索引庫
終端中使用命令
$ pod repo
查看有哪些索引庫,這裏的master 是cocoaPods 自己的索引庫,JWSpecs是我自建的,下一步我們看一下如果和創建索引庫
wjwdeMacBook-Pro:repos wjw$ pod repo
JWSpecs
- Type: git (master)
- URL: https://gitee.com/wjwdive/JWSpecs.git
- Path: /Users/wjw/.cocoapods/repos/JWSpecs
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/wjw/.cocoapods/repos/master
2 repos
也可以 $ cd ~/.cocoaPods/specs
$list 看看有哪些庫
2、創建遠程索引庫
我們選擇國內的碼雲,當然你也可以使用github 或 coding
1、輸入倉庫名稱
2、簡單的描述一下倉庫用途
3、根據需要選擇共有還是私有
4、選擇初始化一個 REMDME
5、根據需要,選擇語言和開原協議
6、創建成功之後,復制出倉庫地址
3、創建本地索引庫
終端命令查看本地索引庫,同第一步
1、創建本地索引庫,
$pod repo add <本地索引庫名字> <遠程索引庫地址>, 索引庫名字最好和剛才在碼雲上創建的一致,遠程索引庫地址就是上一步 的第6小步拷貝的倉庫地址
2、創建成功之後,用第一步的方法查看驗證是否創建成功
4、創建遠程代碼庫
遠程代碼庫就是將來用來管理某一個組件的代碼倉庫。要註意區分和索引庫的區別
1、輸入倉庫名稱
2、簡單的描述一下倉庫用途
3、根據需要選擇共有還是私有
4、不用選擇初始化一個README,因為一會通過命令方式生成代碼模板時會生成README文件和忽略文件,這裏不再勾選了
5、根據需要,選擇語言和開原協議
6、創建成功之後,復制出倉庫地址
5、本地代碼庫
這裏就是對應的要開發的某個組價的本地代碼庫
1、選擇一個合適的目錄,進入終端,通過以下命令創建組件模板庫,組件名建議和遠程倉庫一致
\(pod lib create <組件名> ```bash wjwdeMacBook-Pro:iOSCom wjw\) pod lib create JWCategory
Cloning https://github.com/CocoaPods/pod-template.git
into JWCategory
.
Configuring JWCategory template.
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and double click links to open in a browser. )
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?
JW
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for JWCategory
from ../
Downloading dependencies
Installing JWCategory (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use JWCategory.xcworkspace
for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
Ace! you‘re ready to go!
We will start you off by opening your project in Xcode
open ‘JWCategory/Example/JWCategory.xcworkspace‘
To learn more about the template see https://github.com/CocoaPods/pod-template.git
.
To learn more about creating a new pod, see http://guides.cocoapods.org/making/making-a-cocoapod
.
```
按照提示
選擇使用平臺
選語言 swift/OC
選擇是否生成一個demo
選擇測試框架 Specta/Kiwi/None
選擇是否做基於View的測試
確定一個文件前綴
2、成功之後 會自動打工程,我們編譯一下,成功之後
我們用finder打開工程目錄,進入 JWCategory -> Classes ->
把自己的組件代碼拷貝一份過來,並刪除 ReplaceMe.m
3、cd 到 Example目錄下, pod install 一下,把剛才拖入到 classes 裏的文件pod進來。因為podfile裏面默認會導入classes裏的文件。
use_frameworks!
platform :ios, '8.0'
target 'JWCategory_Example' do
pod 'JWCategory', :path => '../'
target 'JWCategory_Tests' do
inherit! :search_paths
end
end
4、編譯一下組件,看是否報錯
沒有錯的話,在工程的
Podspec Metadata目錄下 修改 JWCategory.podspec 文件裏面幾個重要的配置項
\#概述
s.summary = 'JWCategory, some category'
\#詳細描述
s.description = <<-DESC
some category for use, you can use view.jw_x as view.frame.origin.x
DESC
\#主頁 組件的遠程倉庫對應的URL
s.homepage = 'https://gitee.com/wjwdive/JWCategory'
\#源 就是組件的遠程倉庫地址
s.source = { :git => 'https://gitee.com/wjwdive/JWCategory.git', :tag => s.version.to_s }
\# 根據需要 添加依賴
# s.dependency 'AFNetworking', '~> 2.3'
5、編譯運行一下,通過後,證明沒有問題,就可以提交代碼,並 打tag
$git add .
$git commit -m ‘init com‘
$git remote add origin <遠程代碼塊地址>
$git push origin master 或者 git push -u origin master(第一次提交用)
$git tag <版本號> / git tag -a 版本號 -m "version 版本號" 這裏的版本號必須和 podspec文件裏的一致
$git tag 驗證版本號是否提交成功
$git push --tags
6 、通過pod spec lint --allow-warnings 命令驗證podspec索引文件(pod lib lint 是檢索本地文件,pod spec lint 是本地和遠程庫提示檢索)
wjwdeMacBook-Pro:JWCategory wjw$ pod spec lint --allow-warnings
-> JWCategory (0.1.0)
- 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. (in target 'App')
Analyzed 1 podspec.
JWCategory.podspec passed validation.
7、驗證通過之後,pod repo push <本地索引庫> <索引文件名> --allow-warnings 提交索引文件到遠程索引庫
Pushing the `JWSpecs' repo
$ /usr/bin/git -C /Users/wjw/.cocoapods/repos/JWSpecs -C
/Users/wjw/.cocoapods/repos/JWSpecs push origin master
remote: Powered By Gitee.com
To https://gitee.com/wjwdive/JWSpecs.git
806c622..9b0dc93 master -> master
6、如何使用
新建一個工程
在Podfile 文件裏做引用,然後pod install即可,不要忘記把 自己的索引庫 添加到source 配置項裏
source 'https://gitee.com/wjwdive/JWSpecs.git'
source '[email protected]:CocoaPods/Specs.git'
platform :ios, '8.0'
target:'ComponentsProject' do
pod 'AFNetworking', '~> 3.1.0'
pod 'Masonry', '~>1.1.0'
pod 'MBProgressHUD', '1.1.0'
pod 'YYCache', '~> 1.0.4'
pod 'YYModel', '~> 1.0.4'
pod 'IQKeyboardManager'
pod 'BaiduMapKit', '~> 4.2.0'
pod 'BMKLocationKit'
pod 'MMDrawerController', '~> 0.6.0'
pod 'MLeaksFinder'
pod 'SDWebImage', '~> 4.3.3'
pod 'FBRetainCycleDetector', '~> 0.1.4'
pod 'FMDB', '~> 2.7.2'
pod 'MJRefresh', '~> 3.1.15.3'
pod 'FHHFPSIndicator'
pod 'CocoaLumberjack', '~> 3.4.2'
pod 'SAMKeychain', '~> 1.5.3'
pod 'WeexSDK', '0.20.1'
pod 'JWCategory', '0.1.0'
end
7、遇到的錯誤
xcrun unable to find simctl, Xcode 的命令行配置問題,配置一下就可以Xcode-> ,+command -> location ->command line tools 選中一下即可。
xcrun unable to find simctl
iOS代碼組件化--利用cocoaPods創建私有庫