iOS组件化之搭建私有库(Gitlab+Cocoapods)

搭建私有库(PrivateSpec)

前言:

多项目多工程组件化之路,之前搭建了公司内部私有库,有空整理了下资料

1.名词解释

1.1 索引库 repo

2nxNNR.png

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": "[email protected]"
  },
  "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' => '[email protected]' }
  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、创建索引库

1)在gitlab上创建一个新的库,这个库用来保存私有库的podspec文件,索引库取名为PrivateSpec
2)创建本地索引库文件,然后将其于刚才创建的远程索引库相关联。

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

pod repo add 本地索引库名称 远程索引库的git地址 
> 注意!!!此时的远程索引库是空的!有master分支,可以添加一个readme.md文件。

3、创建本地私有库

  • 手动添加
  • 使用pod创建

1)创建本地私有库

cd 本地私有库文件夹目录

pod lib create WLTestTool

pod lib create 私有库名称

2)根据提示一步一步配置,如图

2uDlRO.png

3)配置完成后会生成一些内容,层级如图

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

TKfXGj.png

4)查看并编写podspec文件里面的内容


#
# 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版本对应
  s.summary          = '这个是测试库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           = { '园丁云' => '[email protected]' }
  s.source           = { :git => 'https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git', :tag => s.version.to_s }  #仓库地址
  # s.social_media_url = 'https://twitter.com/'

  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文件删除掉,替换成你要上传私有库的代码。

TK5zQA.png

4.验证podspec文件合法性

两种验证方式,建议先本地校验,代码本地库push到远程仓库再使用远程验证。

1)本地验证,验证文件格式、语法等

pod lib lint podName.podspec

2)远程验证,远程仓库地址、验证文件格式、语法等

pod spec lint podName.podspec

3)可选参数
--allow-warnings:允许警告
--sources=‘master,privateSpecs':指定源,比如你的私有pod同时依赖了公有库和私有库,你必须指定源才行,因为默认只会去在公有源中查找对应的依赖
--use-libraries:如果使用了静态库,记得加上它

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到远程仓库

1)在Gitlab上创建远程私有库WLTestTool。

2)将代码本地库推送到远程私有库。

3)本地验证之后,打tag。然后进行远程验证。

注意这里添加的tag要跟刚才在spec文件里面写的版本号一致

6.将代码库中的.podspec文件push到私有索引库

这里也会做一遍第四点验证合法性,也可以使用

pod repo push PrivateSpecName podName.podspec

pod repo push PrivateSpec WLTestTool.podspec

7.更新本地索引库

先更新pod库,不然找不到你刚上传的私有库。(此操作也会更新公共索引库)

pod repo update (此操作也会更新公共索引库)

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

你可能感兴趣的:(iOS组件化之搭建私有库(Gitlab+Cocoapods))