元件化第一步,建立私有Pod

Wrappers發表於2019-01-25

更好的閱讀體驗,歡迎訪問我的部落格

關於元件化,目前比較流行的方案大概有三種:Router, Protocol, Target-Action。 不論是選擇哪一種方案,都需要通過建立私有Pod來管理專案。本文希望通過建立一個元件化中常用的Base庫能將這個事情講清楚。

建立Pod

建立Base庫的podspec

選擇合適的本地路徑建立modularization資料夾 其中的podspec包含了這個庫的資訊(包括名稱,版本和描述等).
下面是官方定義:

A specification describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.
一個描述各個版本Pod庫的規範。它包括關於應該從何處獲取原始碼、使用到的檔案、應用構建設定以及其他通用後設資料(如其名稱、版本和描述)的詳細資訊。

cd Base
pod lib create Base
複製程式碼

建立時會需要回答幾個問題

元件化第一步,建立私有Pod

編輯podspec

用編輯器開啟Base.podspec檔案,把我們庫的資訊填寫到podspec中去。
務必每一項都要填寫正確,如s.homepage,s.source中的連結要能夠正常訪問

Pod::Spec.new do |s|
  s.name             = 'Base'
  s.version          = '0.0.1' # 這裡的version要與git操作中的tag相一致,因為目前Base庫中還沒有程式碼,故此我把version設為0.0.1
  s.summary          = 'iOS元件化中Base元件:主要存放專案中無關業務的基礎元件'

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/Wrapperss/Base.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Wrappers' => 'zhanglive@outlook.com' }
  s.source           = { :git => 'https://github.com/Wrapperss/Base.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'Base/Classes/**/*'
  
  # s.resource_bundles = {
  #   'Base' => ['Base/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  s.frameworks = 'UIKit', 'MapKit'
  s.dependency 'AFNetworking', '~> 2.3' #依賴的其他庫
end
複製程式碼

測試podspec是否正確

cd Base
pod lib lint
複製程式碼

建立倉庫(Repository)

私有庫也需要單獨一個Repository來儲存我們的程式碼,就像RxSwfit
此處用Github的私有庫為例,一般建立在公司的GitLab上。

元件化第一步,建立私有Pod

關聯本地與遠端,提交程式碼

通過以下命令將遠端倉庫與原生程式碼關聯起來

git remote add origin https://github.com/Wrapperss/Base.git
git push -u origin master
複製程式碼

提交程式碼,並將打上Tag

注意:Tag必須與之前在podspec中填寫的version一致,這裡我們是0.0.1

元件化第一步,建立私有Pod

建立私有的Spces

建立一個私有的倉庫

元件化第一步,建立私有Pod

將私有Spces建立到本地

pod repo add WSpecs https://github.com/Wrapperss/WSpecs.git
複製程式碼

檢視本地中open ~/.cocoapods/repos
repos中兩個資料夾(Master,WSpces),其中中WSpces就是我們新建立的。

將Pod推到到WSpces

pod repo push WSpecs Base.podspec
複製程式碼

使用

Podfile中加入

source 'https://github.com/CocoaPods/Specs.git' #公開的
source 'https://github.com/Wrapperss/WSpecs.git' #私有的

pod 'Base', :git=>'https://github.com/Wrapperss/Base.git' #可以使用私有庫了!
複製程式碼

相關文章