iOS組件化之搭建私有庫(Gitlab+Cocoapods)

搭建私有庫(PrivateSpec)

前言:

多項(xiàng)目多工程組件化之路,之前搭建了公司內(nèi)部私有庫,有空整理了下資料

1.名詞解釋

1.1 索引庫 repo

1.2 索引庫中的索引文件 .podspec.json

例子 AFNetworking.podspec.json

{
  "name": "AFNetworking",
  "version": "4.0.1",
  "license": "MIT",
  "summary": "A delightful networking framework for Apple platforms.",
  "homepage": "https://github.com/AFNetworking/AFNetworking",
  "social_media_url": "https://twitter.com/AFNetworking",
  "authors": {
    "Mattt Thompson": "m@mattt.me"
  },
  "source": {
    "git": "https://github.com/AFNetworking/AFNetworking.git",
    "tag": "4.0.1"
  },
  "platforms": {
    "ios": "9.0",
    "osx": "10.10",
    "watchos": "2.0",
    "tvos": "9.0"
  },
  "ios": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
    }
  },
  "osx": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
    }
  },
  "watchos": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking-watchOS"
    }
  },
  "tvos": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
    }
  },
  "source_files": "AFNetworking/AFNetworking.h",
  "subspecs": [
    {
      "name": "Serialization",
      "source_files": "AFNetworking/AFURL{Request,Response}Serialization.{h,m}"
    },
    {
      "name": "Security",
      "source_files": "AFNetworking/AFSecurityPolicy.{h,m}"
    },
    {
      "name": "Reachability",
      "platforms": {
        "ios": "9.0",
        "osx": "10.10",
        "tvos": "9.0"
      },
      "source_files": "AFNetworking/AFNetworkReachabilityManager.{h,m}"
    },
    {
      "name": "NSURLSession",
      "dependencies": {
        "AFNetworking/Serialization": [

        ],
        "AFNetworking/Security": [

        ]
      },
      "ios": {
        "dependencies": {
          "AFNetworking/Reachability": [

          ]
        }
      },
      "osx": {
        "dependencies": {
          "AFNetworking/Reachability": [

          ]
        }
      },
      "tvos": {
        "dependencies": {
          "AFNetworking/Reachability": [

          ]
        }
      },
      "source_files": [
        "AFNetworking/AF{URL,HTTP}SessionManager.{h,m}",
        "AFNetworking/AFCompatibilityMacros.h"
      ]
    },
    {
      "name": "UIKit",
      "platforms": {
        "ios": "9.0",
        "tvos": "9.0"
      },
      "dependencies": {
        "AFNetworking/NSURLSession": [

        ]
      },
      "source_files": "UIKit+AFNetworking"
    }
  ]
}


1.3 代碼庫

  • code
  • .podspec
  • Example
  • 其他文件 license readme.md

1.4 代碼庫中的索引文件 .podspec

例子 AFNetworking.podspec

Pod::Spec.new do |s|
  s.name     = 'AFNetworking'
  s.version  = '4.0.1'
  s.license  = 'MIT'
  s.summary  = 'A delightful networking framework for Apple platforms.'
  s.homepage = 'https://github.com/AFNetworking/AFNetworking'
  s.social_media_url = 'https://twitter.com/AFNetworking'
  s.authors  = { 'Mattt Thompson' => 'm@mattt.me' }
  s.source   = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => s.version }

  s.ios.deployment_target = '9.0'
  s.osx.deployment_target = '10.10'
  s.watchos.deployment_target = '2.0'
  s.tvos.deployment_target = '9.0'

  s.ios.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
  s.osx.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
  s.watchos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking-watchOS' }
  s.tvos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }

  s.source_files = 'AFNetworking/AFNetworking.h'

  s.subspec 'Serialization' do |ss|
    ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
  end

  s.subspec 'Security' do |ss|
    ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
  end

  s.subspec 'Reachability' do |ss|
    ss.ios.deployment_target = '9.0'
    ss.osx.deployment_target = '10.10'
    ss.tvos.deployment_target = '9.0'

    ss.source_files = 'AFNetworking/AFNetworkReachabilityManager.{h,m}'
  end

  s.subspec 'NSURLSession' do |ss|
    ss.dependency 'AFNetworking/Serialization'
    ss.ios.dependency 'AFNetworking/Reachability'
    ss.osx.dependency 'AFNetworking/Reachability'
    ss.tvos.dependency 'AFNetworking/Reachability'
    ss.dependency 'AFNetworking/Security'

    ss.source_files = 'AFNetworking/AF{URL,HTTP}SessionManager.{h,m}', 'AFNetworking/AFCompatibilityMacros.h'
  end

  s.subspec 'UIKit' do |ss|
    ss.ios.deployment_target = '9.0'
    ss.tvos.deployment_target = '9.0'
    ss.dependency 'AFNetworking/NSURLSession'

    ss.source_files = 'UIKit+AFNetworking'
  end
end

2、創(chuàng)建索引庫

1)在gitlab上創(chuàng)建一個(gè)新的庫,這個(gè)庫用來保存私有庫的podspec文件,索引庫取名為PrivateSpec。
2)創(chuàng)建本地索引庫文件,然后將其于剛才創(chuàng)建的遠(yuǎn)程索引庫相關(guān)聯(lián)。

pod repo add PrivateSpec https://gitlab.mydomain.com/private-cocoapods/privatespec.git 

pod repo add 本地索引庫名稱 遠(yuǎn)程索引庫的git地址 
> 注意?。?!此時(shí)的遠(yuǎn)程索引庫是空的!有master分支,可以添加一個(gè)readme.md文件。

3、創(chuàng)建本地私有庫

  • 手動(dòng)添加
  • 使用pod創(chuàng)建

1)創(chuàng)建本地私有庫

cd 本地私有庫文件夾目錄

pod lib create WLTestTool

pod lib create 私有庫名稱

2)根據(jù)提示一步一步配置,如圖

3)配置完成后會(huì)生成一些內(nèi)容,層級如圖

  • code
  • .podspec
  • Example
  • 其他文件 license readme.md

4)查看并編寫podspec文件里面的內(nèi)容


#
# Be sure to run `pod lib lint WLTestTool.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'WLTestTool'             #私有庫名稱
  s.version          = '1.0.0'                  #版本號,與下面的git tag版本對應(yīng)
  s.summary          = '這個(gè)是測試庫WLTestTool.'

# 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
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://gitlab.mydomain.com/private-cocoapods/privatespec'        #倉庫首頁地址
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '園丁云' => 'gardeneryun@foxmail.com' }
  s.source           = { :git => 'https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git', :tag => s.version.to_s }  #倉庫地址
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'

  s.source_files = 'WLTestTool/Classes/**/*'        #私有庫代碼文件目錄
  
  # s.resource_bundles = {
  #   'WLTestTool' => ['WLTestTool/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

5) 將Classes文件夾下面的ReplaceMe.m文件刪除掉,替換成你要上傳私有庫的代碼。

4.驗(yàn)證podspec文件合法性

兩種驗(yàn)證方式,建議先本地校驗(yàn),代碼本地庫push到遠(yuǎn)程倉庫再使用遠(yuǎn)程驗(yàn)證。

1)本地驗(yàn)證,驗(yàn)證文件格式、語法等

pod lib lint podName.podspec

2)遠(yuǎn)程驗(yàn)證,遠(yuǎn)程倉庫地址、驗(yàn)證文件格式、語法等

pod spec lint podName.podspec

3)可選參數(shù)
--allow-warnings:允許警告
--sources=‘master,privateSpecs':指定源,比如你的私有pod同時(shí)依賴了公有庫和私有庫,你必須指定源才行,因?yàn)槟J(rèn)只會(huì)去在公有源中查找對應(yīng)的依賴
--use-libraries:如果使用了靜態(tài)庫,記得加上它

pod lib lint WLTestTool.podspec

pod spec lint WLTestTool.podspec

pod spec lint WLTestTool.podspec --allow-warnings

pod spec lint --sources="https://github.com/CocoaPods/Specs.git,https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git"  --allow-warnings


5.代碼本地庫push到遠(yuǎn)程倉庫

1)在Gitlab上創(chuàng)建遠(yuǎn)程私有庫WLTestTool。

2)將代碼本地庫推送到遠(yuǎn)程私有庫。

3)本地驗(yàn)證之后,打tag。然后進(jìn)行遠(yuǎn)程驗(yàn)證。

注意這里添加的tag要跟剛才在spec文件里面寫的版本號一致

6.將代碼庫中的.podspec文件push到私有索引庫

這里也會(huì)做一遍第四點(diǎn)驗(yàn)證合法性,也可以使用

pod repo push PrivateSpecName podName.podspec

pod repo push PrivateSpec WLTestTool.podspec

7.更新本地索引庫

先更新pod庫,不然找不到你剛上傳的私有庫。(此操作也會(huì)更新公共索引庫)

pod repo update (此操作也會(huì)更新公共索引庫)

pod repo update PrivateSpec

8.使用私有庫

使用私有庫需要指定私有索引庫地址。

cocoapods 官方source是隱式的,一旦指定了其他source 就需要把官方的指定上。

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

[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via 'pod repo remove master'. To suppress this warning please add 'warn_for_unused_master_specs_repo => false' to your Podfile.

Podfile 示例

source 'https://github.com/CocoaPods/Specs.git'
source 'https://gitlab.mydomain.com/private-cocoapods/privatespec.git' 

use_frameworks!
platform :ios, '9.0'


target 'WLTestTool_Example' do
  pod 'WLTestTool', '1.0.0'

  target 'WLTestTool_Tests' do
    inherit! :search_paths
  end
  
end

use_frameworks!
platform :ios, '9.0'


target 'WLTestTool_Example' do
  pod 'WLTestTool', '1.0.0'
  pod 'test', :git => 'https://gitlab.mydomain.com/private-cocoapods/test.git', :tag => '1.0.0'


  target 'WLTestTool_Tests' do
    inherit! :search_paths
  end
  
end

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

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

  • 前言 隨著移動(dòng)互聯(lián)網(wǎng)的不斷發(fā)展,很多程序代碼量和業(yè)務(wù)越來越多,現(xiàn)有架構(gòu)已經(jīng)不適合公司業(yè)務(wù)的發(fā)展速度了,很多都面臨著...
    AE86閱讀 1,199評論 0 2
  • 前言:每次搭建私有庫總忘記一些步驟。這次做個(gè)筆記。同時(shí)有需要學(xué)習(xí)的小伙伴可以進(jìn)行參考。畢竟我們工作中我們還是經(jīng)常要...
    小馬哥_冬冬閱讀 1,310評論 0 3
  • 前言 2016年開始使用組件化項(xiàng)目,但是一直沒有整理創(chuàng)建私有庫的流程了,直到隔了很久自己在創(chuàng)建私有庫的時(shí)候又遇到了...
    pigLily閱讀 602評論 0 2
  • 1.創(chuàng)建本地Pods庫 2.生成本地Pods文件 3.替換ReplaceMe文件 將ReplaceMe.m替換為自...
    Singularity_Lee閱讀 2,133評論 0 6
  • 概念理解 私有索引庫 私有索引庫的作用是存放.podSpec文件的。當(dāng)你使用pod search AFN時(shí),pod...
    CoderZNB閱讀 435評論 0 0

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