美文网首页React Native
CocoaPods - 搭建私有库

CocoaPods - 搭建私有库

作者: 北風 | 来源:发表于2021-03-11 14:08 被阅读0次

    一、前言

    公司项目繁多,为了框架的统一和更好维护,需要将自己的业务,封装为私有库,上传到公司私有git上,利用cocoapods统一管理。

    分解需求:

    • 1、创建私有spec repo(相当于cocoapods私有库资源中心), 所有的私有库上传记录在私有spec中
    • 2、工程封装为私有库,上传到私有spec repo中
    • 3、项目工程集成私有库

    二、创建私有索引库Spec Repo

    1. 什么是spec repo?

    它是所有的Pods的一个索引,就是一个容器,所有公开的Pods都在这个里面,它实际是一个Git仓库remote端在GitHub上。

    在Podfile中,我们通过

    pod 'AFNetworking'
    
    

    它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。

    我们可以通过命令,

    pod repo list
    
    

    查看mac 上的repo列表:

    master
    - Type: git (master)
    - URL:  https://github.com/CocoaPods/Specs.git
    - Path: /Users/louielee/.cocoapods/repos/master
    
    

    Spec Repo了,目录的结构如下:

    .
    ├── YTestPod
        └── 0.0.3
            └── YTestPod.podspec
    
    

    2. 创建远端私有索引库

    在git服务器上创建一个私有仓库,私有Spec Repo取名为YTestSpec。

    pod repo add [Private Repo Name] [Git HTTPS clone URL]
    
    

    示例

    pod repo add YTestSpec https://e.coding.net/LouieLee/lypodtest/YTestSpec.git
    
    

    完成后使用pod repo list查看如下:

    MacBook-Pro$ pod repo list
    
    master
    - Type: git (master)
    - URL:  https://github.com/CocoaPods/Specs.git
    - Path: /Users/louielee/.cocoapods/repos/master
    
    YTestSpec
    - Type: git (master)
    - URL:  https://e.coding.net/LouieLee/lypodtest/YTestSpec.git
    - Path: /Users/louielee/.cocoapods/repos/YTestSpec
    
     ...
    
    

    查看本地pod repos路径如下:

    MacBook-Pro$ ls
    
     Spec_Lock  YTestSpec  master  trunk
    
    

    此时可以看到YTestSpec已经在本地创建。

    注:这一步是在本地添加私有库source,其他开发人员Podfile需要集成私有spec上的私有库,都需要首先通过pod repo add命令,本地clone spec repo。

    附上其他常用命令:

    更新本地[Private Repo Name]的spec repo。私有库podspec文件更改并push到spec

    pod repo update [Private Repo Name]
    
    

    本地删除spec repo

    pod repo remove [Private Repo Name]
    
    

    三、创建私有库

    1. 什么是私有库?

    包含具体功能的项目代码,使用Git进行代码管理,被加入私有仓库来实现cocoapod版本管理和安装使用。

    2. 创建私有库

    创建私有库的标准格式,会使用git-template默认的模板创建私有库

    pod lib create [Private Lib name]
    
    

    示例

    pod lib create YTestPod
    
    

    通过--template-url参数,指定私有库模板地址

    pod lib create --verbose --template-url=[template URL] [Private pod name]
    
    

    pod-template模板来生成私有库,创建过程中需要确定以下问题:

    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?
     > Y
    
    Running pod install on your new library.
    
    ...
    
    

    注意:在clone pod-template最好使用代理,否则可能会失败

    export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
    
    

    成功后我们看到的目录结构如下:

    MacBook-Pro$ tree -L 2
    
    .
    ├── Example
    │   ├── YTestPod
    │   ├── YTestPod.xcodeproj
    │   ├── YTestPod.xcworkspace
    │   ├── Podfile
    │   ├── Podfile.lock
    │   ├── Pods
    │   └── Tests
    ├── YTestPod
    │   ├── Assets
    │   └── Classes
    ├── YTestPod.podspec
    ├── LICENSE
    ├── README.md
    └── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
    
    10 directories, 5 files
    
    

    注:tree需要自行安装

    • YTestPod.podspec是私有库的配置文件
    • YTestPod是私有库代码文件,Assets是放资源,Classes下是编译的源文件
    • Example是自动生成的测试工程。

    3. 编辑私有库的配置文件

    如有疑问可参照官方文档

    #
    # Be sure to run `pod lib lint YTestPod.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|
      # 名称,pod search搜索的关键词,注意这里一定要和.podspec一致
      s.name             = 'YTestPod'
      # 版本号,每一个版本对应一个tag
      s.version          = '0.1.0'
      # 私有库摘要
      s.summary          = 'A short description of HTDNetworkManage.'
    
    # 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://github.com/Louie/YTestPod'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
      # 许可证
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      # 作者
      s.author           = { 'Louie' => 'louie@126.com' }
      # 项目仓库地址(重要)
      # 私有库的remote地址和tag。也可以:branch => 'master'设置分支。
      s.source           = { :git => 'https://github.com/Louie/YTestPod.git', :tag => s.version.to_s }
      # 个人主页网址
      # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    
      s.ios.deployment_target = '9.0'
    
      # 源文件路径
      s.source_files = 'YTestPod/Classes/**/*'
      # * 表示匹配所有文件
      # ** 表示匹配所有子目录
      # 如果有多个目录下则用逗号分开如['','','']形式
    
      # 资源文件路径
      # s.resource_bundles = {
      #   'YTestPod' => ['YTestPod/Assets/*.png']
      # }
    
      # 公开的头文件地址
      # s.public_header_files = 'Pod/Classes/**/*.h'
    
      # 所需的framework,多个用逗号隔开
      # s.frameworks = 'UIKit', 'MapKit'
    
      # 依赖关系,该项目所依赖的其他库,
      # s.dependency 'AFNetworking', '~> 2.3'
      # 如果有多个需要填写多个s.dependency。cocoapods会自动将依赖的其他库集成到工程中。
    end
    
    

    3. 编写私有库代码

    私有库源码放在前面说的/YTestPod/Classes中

    4. 提交私有库代码到git

    可以使用自己平时习惯的方式提交,以下我使用命令提交。
    附上git命令:

    <!-- 查看状态 -->
    git status//
    <!-- 添加文件到缓冲区 -->
    git add .
    <!-- 从缓冲区提交代码到仓库 -->
    git commit -m "描述"
    <!-- 关联本地仓库和远程仓库 -->
    git remote add origin https://github.com/xxx.git
    <!-- 添加tag -->
    git tag -a '0.0.1'  -m '描述'
    <!-- 查看tag -->
    git tag 
    <!-- 删除tag -->
    git tag -d '0.0.1'
    <!-- 将本地库的代码推到远程库 -->
    git push -f origin master
    <!-- 将本地创建的tag推到远程库 -->
    git push --tags
    <!-- 删除tag -->
    git push origin :0.0.1
    
    

    注:podspec配置文件中 s.source设置了:tag => s.version.to_s,version是0.1.0,所以需要打tag,tag必须version保持一致

    5. 校验私有库

    注:当代码里有警告时会验证失败,可通过参数--allow-warnings允许存在警告。

    5.1. 验证本地podspec文件

    本地验证不会验证 s.source 中的tag

    MacBook-Pro$ pod lib lint
    
     -> YTestPod (0.0.1)
        - WARN  | summary: The summary is not meaningful.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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 and one is not being generated automatically. (in target 'App' from project 'App')
    
    YTestPod passed validation.
    
    

    注:pod spec lint只验证一个本地仓库

    5.2. 验证远程podspec

    远程验证会验证 s.source 中的tag,如果此时没有打上相应的标签则会报错

    MacBook-Pro$ pod spec lint
    
     -> YTestPod (0.0.1)
        - WARN  | summary: The summary is not meaningful.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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 and one is not being generated automatically. (in target 'App' from project 'App')
    
    Analyzed 1 podspec.
    
    YTestPod passed validation.
    
    

    注:pod spec lint更为精确,会同时验证本地仓库和远程仓库。

    6. 发布私有库

    pod repo push [Private Repo Name] [Private Lib Name].podspec --sources='[Private Repo clone HTTPS URL]'
    
    

    注:如果有警告导致发布失败,需要使用 --verbose --allow-warnings忽略警告

    示例

    MacBook-Pro$ pod repo push YTestSpec YTestPod.podspec --sources='https://e.coding.net/LouieLee/lypodtest/YTestSpec.git'
    
    Validating spec
     -> YTestPod (0.0.1)
        - WARN  | summary: The summary is not meaningful.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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 and one is not being generated automatically. (in target 'App' from project 'App')
    
    Updating the `YTestSpec' repo
    
    Adding the spec to the `YTestSpec' repo
    
     - [Add] YTestPod (0.0.1)
    
    Pushing the `YTestSpec' repo
    
    

    注: 如果设置代理可能会出现发布失败需要关闭

    关闭代理

    unset http_proxy
    unset https_proxy
    
    

    发布成功可以通过pod search查到该私有库

    MacBook-Pro$ pod search YTestPod
    
    -> YTestPod (0.0.3)
       A short description of YTestPod.
       pod 'YTestPod', '~> 0.0.3'
       - Homepage: https://louielee.coding.net/p/lypodtest/d/YTestPod/git
       - Source:   https://e.coding.net/LouieLee/lypodtest/YTestPod.git
       - Versions: 0.0.3 [YTestSpec repo]
    
    

    四、集成测试

    在Podfile文件最顶部添加如下描述,然后执行pod install

    # Uncomment the next line to define a global platform for your project
    platform :ios, '9.0'
    
    source 'http://171.16.46.5/liya-dev/HTDNetworkManageSpec.git'
    
    target 'YBRouterAndDecouplingDemo' do
      # Comment the next line if you don't want to use dynamic frameworks
      use_frameworks!
    
      pod 'HTDNetworkManage'
    
      # Pods for YBRouterAndDecouplingDemo
    
    end
    
    

    私有索引库和私有库是一个git地址。会导致以下问题

    An unexpected version directory ‘Classes’ was encountered for the /Users/name/.cocoapods/repos/** Pod in the xxx repository.
    
    [!] Unable to find a pod with name, author, summary, or description matching 'xxx'
    
    

    私有库完成。

    相关文章

      网友评论

        本文标题:CocoaPods - 搭建私有库

        本文链接:https://www.haomeiwen.com/subject/cwonqltx.html