CocoaPods 深入浅出 - 命令概览

栏目: IOS · 发布时间: 4年前

内容简介:在 iOS 开发中,大家用到注意:本节所有操作都是基于 CocoaPods v1.6.1,可用 pod --version 来查看当前 CocoaPods版本.

 

CocoaPods 深入浅出 - 命令概览

前言

在 iOS 开发中,大家用到 CocoaPods 基本上都是  pod install 或者  pod update 这两条指令,很难对  CocoaPods 有一个全局的把控,本节从全局的角度去了解  CocoaPods 的所有命令操作,并在后续章节逐一介绍各命令的使用方式,提高大家对  CocoaPods 的认知

注意:本节所有操作都是基于 CocoaPods v1.6.1,可用 pod --version 来查看当前 CocoaPods版本.

命令概览

浏览 CocoaPods 全部指令,可以在命令行输入 pod --help ,然后输出如下

Usage:

    $ pod COMMAND

      CocoaPods, the Cocoa library package manager.

Commands:

    + cache         Manipulate the CocoaPods cache
    + deintegrate   Deintegrate CocoaPods from your project
    + env           Display pod environment
    + init          Generate a Podfile for the current directory
    + install       Install project dependencies according to versions from a
                    Podfile.lock
    + ipc           Inter-process communication
    + lib           Develop pods
    + list          List pods
    + outdated      Show outdated project dependencies
    + plugins       Show available CocoaPods plugins
    + repo          Manage spec-repositories
    + search        Search for pods
    + setup         Setup the CocoaPods environment
    + spec          Manage pod specs
    + trunk         Interact with the CocoaPods API (e.g. publishing new specs)
    + try           Try a Pod!
    + update        Update outdated project dependencies and create new Podfile.lock

Options:

    --silent        Show nothing
    --version       Show the version of the tool
    --verbose       Show more debugging information
    --no-ansi       Show output without ANSI codes
    --help          Show help banner of specified command

命令分类

通过上述输出的帮助信息可以看到 Cocoapods 总共有 17 个官方的命令,这里将这些命令分为四大类:

  • 全局环境相关

    • setup - 构建 CocoaPods 运行环境

    • env - 展示当前版本的 CocoaPods 环境

    • cache - 管理 CocoaPods 在本地的缓存

    • repo - 管理本地针对 Podspec 描述文件的存储仓库

    • list - 列出所有可用的依赖库

    • search - 搜索依赖库

  • 项目依赖相关

    • init - 为当前目录生成一个 Podfile 文件

    • install - 根据 Podfile.lock 指定版本,安装工程依赖

    • deintegrate - 让已经用 CocoaPods 管理的项目,解除 CocoaPods 的配置

    • outdated - 检查当前工程过期的依赖库

    • update - 更新依赖库,并创建新的  Podfile.lock

  • 组件管理相关

    • lib - 开发自定义 Pod 依赖库

    • spec - 针对 Podspec 的处理操作

    • trunk - 与 CocoaPods 官方 API 交互(例如:发布新的组件库到官方仓库)

    • try - 快速尝试使用一个组件库

  • 插件机制相关

    • ipc - 分析 CocoaPods 内部的通信机制

    • plugins - 插件相关操作

命令实战

根据上述分类,本节主要先针对 全局环境 环境这一类的命令以示例的方式讲解下使用场景以及一些注意事项。

1.  setup 命令

setup 命令一般是在第一次刚安装好  CocoaPods ,官方 repo (~/.cocapods/repos/master) 还没有获取过的时候使用,主要用来初始化官方 repo 仓库,可以通过  pod setup --help 查看命令的详细介绍,如下:

Usage:

    $ pod setup

      Creates a directory at `/Users/xxx/.cocoapods/repos` which will hold your
      spec-repos. This is where it will create a clone of the public `master`
      spec-repo from:

          https://github.com/CocoaPods/Specs

      If the clone already exists, it will ensure that it is up-to-date.

Options:

    --silent    Show nothing
    --verbose   Show more debugging information
    --no-ansi   Show output without ANSI codes
    --help      Show help banner of specified command

通过上述描述介绍,可以看出,运行该命令后会将 https://github.com/CocoaPods/Specs 仓库克隆到 ~/.cocapods/repos/master 目录下,如果该 repo 已经存在,则会更新官方 repo 仓库到本地,运行效果如下:

Setting up CocoaPods master repo
  $ /usr/bin/git -C /Users/coderafi/.cocoapods/repos/master fetch origin --progress
remote: Enumerating objects: 15502, done.
  remote: Counting objects: 100% (15502/15502), done.
  remote: Compressing objects: 100% (593/593), done.
  remote: Total 43571 (delta 15052), reused 15183 (delta 14867), pack-reused 28069
  Receiving objects: 100% (43571/43571), 6.17 MiB | 701.00 KiB/s, done.
  Resolving deltas: 100% (26098/26098), completed with 4994 local objects.
  From https://github.com/CocoaPods/Specs
     664206c4b51..8b2049a1dfb  master     -> origin/master
   + 54c47cbe77f...02443419999 indexes    -> origin/indexes  (forced update)
   * [new branch]              netlify    -> origin/netlify
  Checking out files: 100% (3785/3785), done.
$ /usr/bin/git -C /Users/coderafi/.cocoapods/repos/master rev-parse --abbrev-ref HEAD
  master
  $ /usr/bin/git -C /Users/coderafi/.cocoapods/repos/master reset --hard origin/master
  HEAD is now at 8b2049a1dfb [Add] FritzBase 3.5.2

CocoaPods 1.7.1 is available.
To update use: `gem install cocoapods`

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.7.1

Setup completed

注意:在运行结果中可能会出现 warning: inexact rename detection was skipped due to too many files.提示,原因是自从上次更新 repo 仓库到现在有太多的文件变动,导致 git 直接跳过了文件重命名保护策略,解决方法可以用git config merge.renameLimit 999999增大阀值,然后重新执行 pod setup,完成后再用 git config --unset merge.renameLimit进行还原,在执行 pod repo update master的时候也可能出现同样的问题

2.  env 命令

env 命令能够详细的列出  CocoaPods 在本机环境信息,可以帮助团队快速定位  CocoaPods 环境配置的不一致问题 运行效果如下:

### Stack

```
   CocoaPods : 1.7.0
        Ruby : ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
    RubyGems : 2.7.9
        Host : Mac OS X 10.15 (19A471t)
       Xcode : 11.0 (11M336w)
         Git : git version 2.21.0 (Apple Git-120)
Ruby lib dir : /Users/coderafi/.rvm/rubies/ruby-2.5.3/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ 8b2049a1dfbcd2cd2070c79e232507bbbfedb988
               vdian-specs - ssh://git@gitlab.vdian.net:60022/mobile/Specs.git @ 902dea8dd74a549640985c08b6d977db0cbf9762
               vdian-wdspecs - ssh://git@gitlab.vdian.net:60022/mobile/wdspecs.git @ 1fe1f41bfb4f525ef6240db98e5b20028f66f717
```

### Installation Source

```
Executable Path: /Users/coderafi/.rvm/gems/ruby-2.5.3/bin/pod
```

### Plugins

```
cocoapods-deintegrate : 1.0.3
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.0
cocoapods-stats       : 1.1.0
cocoapods-trunk       : 1.3.1
cocoapods-try         : 1.1.0
```

3.  cache 命令

CocoaPods 在处理依赖库时,会根据 podspec 去下载代码或二进制文件(在原理篇会详细讲解),并缓存到 ~/Library/Caches/CocoaPods/Pods/Release 下,方便下次使用时从该缓存中直接拷贝,  cache 命令则可以对这些缓存文件进行操作,一般是在发现安装的依赖库跟实际想要用的不一致时用该命令清下缓存,命令介绍如下:

Usage:

    $ pod cache COMMAND

      Manipulate the download cache for pods, like printing the cache content or
      cleaning the pods cache.

Commands:

    + clean     Remove the cache for pods
    + list      List the paths of pod caches for each known pod

Options:

    --silent    Show nothing
    --verbose   Show more debugging information
    --no-ansi   Show output without ANSI codes
    --help      Show help banner of specified command

示例操作:

pod cache list # 显示所有本地缓存

pod cache list AFNetworking # 显示 AFNetworking 的本地缓存

pod cache clean AFNetworking # 清除 AFNetworking 本地缓存,如果存在多个版本会让你选择需要清除的版本

pod cache clean --all # 清空所有本地缓存,谨慎使用

注意:CocoaPods提供的缓存机制主要是为了提升依赖安装的速度,帮你快速构建开发环境,但如果本地有多个 CocoaPods 版本要切换,每次切换的时候都会将 ~/Library/Caches/CocoaPods 清空重新进行缓存,也就是说缓存机制是与 CocoaPods版本对应的,这里我觉得缓存应该按照版本保留或者不同版本共用一套缓存会更好,或许 CocoaPods官方不想做的那么复杂吧,:grin:

4.  repo 命令

repo 一般是在多套私有 repo 镜像仓库共存时使用,那么可能有人要问了,说了这么多 repo 仓库,有官方的还有私有的,里面都放了些什么东西呢?其实 repo 仓库中并不是存放真实的依赖库代码,而是存放依赖库各版本的版本描述文件,也就是 podspec 或 podspec.json 详细的命令介绍如下,可以看出有 6 个子命令:

Usage:

    $ pod repo [COMMAND]

      Manage spec-repositories

Commands:

    + add       Add a spec repo
    + add-cdn   Add a spec repo backed by a CDN
    + lint      Validates all specs in a repo
    > list      List repos
    + push      Push new specifications to a spec-repo
    + remove    Remove a spec repo
    + update    Update a spec repo

Options:

    --silent    Show nothing
    --verbose   Show more debugging information
    --no-ansi   Show output without ANSI codes
    --help      Show help banner of specified command

下面我们用 GitHub - artsy/Specs: The Artsy CocoaPods Specs 开源 Specs 镜像作为示例来看下 repo 仓库的 add、list、update、remove、lint 操作

pod repo add artsy-specs git@github.com:artsy/Specs.git # 添加一个 spec repo 仓库

pod repo list # 列出所有本地 repo 仓库

pod repo update artsy-specs # 更新 repo 仓库

pod repo lint artsy-specs # 校验 repo 中 podspec 文件的合法性

pod repo remove artsy-specs # 移除 artsy-specs repo 仓库

注意:在添加 repo 仓库建议用 pod repo add的方式来操作,不建议直接用 git clone xxx ~/.cocoapods/repos/master 的方式,包括更新的时候也是一样的,虽然直接采用 git 命令的目前效果是一样的,但如果将来 CocoaPod 更换了 repo 仓库拉取策略(在 1.7.0 例已经支持了 CDN repo source 的方式),就会容导致要重新更换 repo 操作命令,带来不必要的麻烦有时候在执行 pod install 或者 pod upate的时候,会出现本地不存在此版本的依赖库,需要用 pod repo update xxx来更新pod repo push命令会在自定义组件开发篇中介绍

5.  list 命令

list 命令主要是列出所有可用的 pod 依赖库,可以携带  --update 参数以达到先更新 repo 仓库的目的,由于该命令要便利所有的依赖库,操作时间比较久,所以一般使用的比较少,大家可以用  pod list 尝试下,这里给大概的运行结果,了解即可。

...

ABOnboarding 1.1.1
ABOtpView 0.1.0
ABPOD 0.1.1
ABPadLockScreen 3.4.2
ABPasscode 0.1
ABPicker 0.1.0
ABPickerDialog 0.1.4
ABPopups 0.0.1
ABReleaseNotesViewController 0.1.1
ABRequestManager 0.0.2
ABReviewReminder 1.0
ABRouter 0.1
ABSDK 2.0.0
ABSNetwork 1.0.1
ABSQLite 1.2.0
ABSQLiteManager 0.3
ABSegmentedViewController 0.4.0
ABSensorSDK 0.1
ABSignatureView 0.1.1

...

6.  search 命令

search 命令由于首先建立索引,然后再全局查询,操作时间也比较久,也没太作用,一般大家查找组件库都会直接用谷歌或者 github 来查,官方给出的命令介绍如下:

Usage:

    $ pod search QUERY

      Searches for pods, ignoring case, whose name, summary, description, or authors
      match `QUERY`. If the `--simple` option is specified, this will only search in
      the names of the pods.

Options:

    --regex      Interpret the `QUERY` as a regular expression
    --simple     Search only by name
    --stats      Show additional stats (like GitHub watchers and forks)
    --web        Searches on cocoapods.org
    --ios        Restricts the search to Pods supported on iOS
    --osx        Restricts the search to Pods supported on macOS
    --watchos    Restricts the search to Pods supported on watchOS
    --tvos       Restricts the search to Pods supported on tvOS
    --no-pager   Do not pipe search results into a pager
    --verbose    Show more debugging information
    --no-ansi    Show output without ANSI codes
    --help       Show help banner of specified command

这里给出一个示例,运行 pod search Alamofire --ios ,结果如下:

-> Alamofire (5.0.0-beta.6)
   Elegant HTTP Networking in Swift
   pod 'Alamofire', '~> 5.0.0-beta.6'
   - Homepage: https://github.com/Alamofire/Alamofire
   - Source:   https://github.com/Alamofire/Alamofire.git
   - Versions: 5.0.0-beta.6, 5.0.0-beta.5, 5.0.0-beta.4, 5.0.0-beta.3, 5.0.0-beta.2, 5.0.0.beta.1, 4.8.2, 4.8.1, 4.8.0, 4.7.3, 4.7.2, 4.7.1, 4.7.0, 4.6.0, 4.5.1, 4.5.0, 4.4.0, 4.3.0, 4.2.0, 4.1.0, 4.0.1, 4.0.0, 3.5.1, 3.5.0, 3.4.2, 3.4.1, 3.4.0, 3.3.1, 3.3.0, 3.2.1, 3.2.0, 3.1.5, 3.1.4, 3.1.3, 3.1.2, 3.1.1, 3.1.0, 3.0.1, 3.0.0, 3.0.0-beta.3, 3.0.0-beta.2, 3.0.0-beta.1, 2.0.2, 2.0.1, 2.0.0, 2.0.0-beta.4, 2.0.0-beta.3, 2.0.0-beta.2, 2.0.0-beta.1, 1.3.1, 1.3.0, 1.2.3, 1.2.2, 1.2.1, 1.2.0, 1.1.5, 1.1.4, 1.1.3 [master repo]

-> Alamofire+Result (3.0.0-karumi)
   Result<Elegant> HTTP Networking in Swift
   pod 'Alamofire+Result', '~> 3.0.0-karumi'
   - Homepage: https://github.com/Karumi/Alamofire
   - Source:   https://github.com/Karumi/Alamofire.git
   - Versions: 3.0.0-karumi [master repo]

-> Alamofire-Decodable (3.0.0)
   A trivial method added to alamofire to automagically decode responses using Decodable
   pod 'Alamofire-Decodable', '~> 3.0.0'
   - Homepage: https://github.com/deanWombourne/Alamofire-Decodable
   - Source:   https://github.com/deanWombourne/Alamofire-Decodable.git
   - Versions: 3.0.0, 2.0.1, 2.0.0, 1.0.1, 1.0.0 [master repo]

总结

本节主要从全局的角度,全面的描述了 CocoaPods 的所有命令,主要内容包含如下三部分:

  • CocoaPods 命令概览、简介

  • CocoaPods 命令分类

  • CocoaPods 全局环境命令详解

CocoaPods 深入浅出 - 命令概览

I n


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

移动Web手册

移动Web手册

Peter-Paul Koch(彼得-保罗 科赫) / 奇舞团 / 电子工业出版社 / 2015-4 / 65.00元

《移动Web手册(双色)》主要讲解了移动Web开发和传统PC网站开发的不同之处。作者首先对移动互联网相关的运营商、设备、操作系统和软件进行了简单的介绍,让读者理解移动开发的复杂之处。接下来对移动设备上的各种浏览器进行了详细介绍,以及这些浏览器的市场占有率、特性支持等。《移动Web手册(双色)》为那些想进入移动Web开发领域的人提供了一些指导性的建议并对移动Web开发的未来进行了展望。 《移动......一起来看看 《移动Web手册》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具