草庐IT

CocoaPods - 搭建私有库

北風 2023-10-14 原文

一、前言

公司项目繁多,为了框架的统一和更好维护,需要将自己的业务,封装为私有库,上传到公司私有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$ cd /Users/xxx/.cocoapods/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 push -f origin master
<!-- 添加tag -->
git tag -a '0.0.1'  -m '描述'
<!-- 查看tag -->
git tag 
<!-- 将本地创建的tag推到远程库 -->
git push --tags
<!-- 删除tag -->
git tag -d '0.0.1'

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

5. 校验私有库

注:当代码里有警告时会验证失败,可通过参数
参数解释
--verbose 输出详情
--allow-warnings 允许警告
--use-libraries 允许使用静态库
--skip-import-validation 跳过验证

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. 发布私有库

<!-- 跳转到本地仓路径提交本地仓内容 -->
cd /Users/xxx/.cocoapods/repos/[Private Repo Name]
<!-- 允许空提交 -->
git commit --allow-empty
<!-- 将本地库的代码推到远程库 -->
git push -f origin master
<!-- 创建库 -->
pod lib create [Private Repo Name]
<!-- 更新库 -->
<!--[Private Repo Name] 远端仓库名
[Private Lib Name].podspec 要上传的podspec 注意[Private Lib Name].podspec所在的路径,我这里使用的是相对路径,你也可以使用绝对路径比如:/Users/xxx/Documents/[Private Lib Name].podspec
--sources是索引库对应的远端仓库地址 -->
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://xxx/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 - 搭建私有库的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. Ruby - 如何处理子类意外覆盖父类(super class)私有(private)字段的问题? - 2

    假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案

  4. ruby - 从另一个私有(private)方法中使用 self.xxx() 调用私有(private)方法 xxx,导致错误 "private method ` xxx' called” - 2

    我正在尝试获得良好的Ruby编码风格。为防止意外调用具有相同名称的局部变量,我总是在适当的地方使用self.。但是现在我偶然发现了这个:classMyClass上面的代码导致错误privatemethodsanitize_namecalled但是当删除self.并仅使用sanitize_name时,它会起作用。这是为什么? 最佳答案 发生这种情况是因为无法使用显式接收器调用私有(private)方法,并且说self.sanitize_name是显式指定应该接收sanitize_name的对象(self),而不是依赖于隐式接收器(也是

  5. ruby - 如何在 Ruby 中实现私有(private)内部类 - 2

    来自Java,我正在尝试在Ruby中实现LinkedList。我在Java中实现它的通常方法是有一个名为LinkedList的类和一个名为Node的私有(private)内部类,其中LinkedList的每个对象都作为Node对象。classLinkedListprivateclassNodeattr_accessor:val,:nextendend我不想将Node类暴露给外部世界。然而,通过Ruby中的这个设置,我可以使用这个访问LinkedList类之外的私有(private)Node类对象-node=LinkedList::Node.new我知道,在Ruby1.9中,我们可以使用

  6. ruby 私有(private)类方法助手 - 2

    您好,我正在尝试创建一个帮助程序,用于将ruby​​方法大量定义为私有(private)类方法。通常,可以通过使用private_class_method键工作将方法定义为私有(private)类方法。但我想创建一个以下样式的助手:classPersondefine_private_class_methodsdodefmethod_oneenddefmethod_twoendendend我计划通过以下方式动态定义它,但根本不起作用:classObjectdefself.define_private_class_methods&blockinstance_evaldoprivate&bl

  7. ruby - 使实例方法在运行时私有(private) - 2

    在另一个对象中注册该对象后,我需要将一些实例方法设为私有(private)。我不想卡住对象,因为它必须保持可编辑状态,只是功能较少。而且我不想取消定义这些方法,因为它们是在内部使用的。我需要的是这样的:classMyClassdefmy_methodputs"Hello"endenda=MyClass.newb=MyClass.newa.my_method#=>"Hello"a.private_instance_method(:my_method)a.my_method#=>NoMethodErrorb.my_method#=>"Hello"有什么想法吗?

  8. ruby - Ruby 导入的方法总是私有(private)的吗? - 2

    最好用一个例子来解释:文件1.rb:deffooputs123end文件2.rb:classArequire'file1'endA.new.foo将给出错误“':调用了私有(private)方法'foo'”。我可以通过执行A.new.send("foo")来解决这个问题,但是有没有办法公开导入的方法?编辑:澄清一下,我没有混淆include和require。另外,我不能使用正常包含的原因(正如许多人正确指出的那样)是因为这是元编程设置的一部分。我需要允许用户在运行时添加功能;例如,他可以说“run-this-app--includefile1.rb”,应用程序的行为将根据他在file1

  9. ruby-on-rails - 私有(private) gem 没有安装在 docker 中 - 2

    我正在尝试使用docker运行一个Rails应用程序。通过github的sshurl安装的gem很少,如下所示:Gemfilegem'swagger-docs',:git=>'git@github.com:xyz/swagger-docs.git',:branch=>'my_branch'我在docker中添加了keys,它能够克隆所需的repo并从git安装gem。DockerfileRUNmkdir-p/root/.sshCOPY./id_rsa/root/.ssh/id_rsaRUNchmod700/root/.ssh/id_rsaRUNssh-keygen-f/root/.ss

  10. ruby - 从私有(private)实例方法调用私有(private)类方法 - 2

    我是Ruby新手,来自C#世界。在C#中,这样做是合法的:publicclassTest{publicvoidMethod(){PrivateMethod();}privatevoidPrivateMethod(){PrivateStaticMethod();}privatestaticvoidPrivateStaticMethod(){}}是否可以在Ruby中做类似的事情?一些背景知识:我有一个Rails应用程序...其中一个模型有一个私有(private)方法来设置一些依赖项。有一个类方法可以创建模型的初始化实例。由于遗留原因,模型的某些实例未正确初始化。我添加了一个实例方法来初始

随机推荐