1

CocoaPods - 如何调试

 3 years ago
source link: http://saitjr.com/ios/cocoapods-debug.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

简单的用 RubyMine 对 CocoaPods 进行调试,当然 VSCode 也可以,大同小异。

环境信息

RubyMine 2020.3

CocoaPods 1.8.4

Ruby 2.7.1

之前写了一篇关于 CocoaPods 的博客,但感觉比较流水账,所以删掉了。后续这个系列的文章尽量短小,一篇就说清楚一件事就行。源码阅读需要专注,文章太长不利于理解性的阅读。

CocoaPods 源码

Cocoapods 的代码并不全都在 Cocoapods/Cocoapods 项目中,而是拆分为了多个 Repo。在 Cocoapods README 的最后 ,对这些项目进行了简述:

- CocoaPods : Cocoapods 命令行工具

- CocoaPods Core : 处理 space 与 podfile,比如 Podfile DSL 的定义就在这个项目中

- CocoaPods Downloader : 下载器

- Xcodeproj : 解析 .xcodeproj 文件解析

- CLAide : 命令行参数解析器

- Molinillo : 依赖分析

- CocoaPods.app : 比较像 Cocoapods 的 UI 版本,包含一些功能增强,比如 Podfile 联想

- Master repository : Spec 仓库

其中最后两个都不是源代码,可以先不去关心。剩下的仓库,可以全部 clone 下来。

环境配置

  1. 将以上仓库都 clone 下来,Cocoapods 项目作为入口,修改 Cocoapods/Gemfile ,指定每个依赖的 path

JzInmqB.jpg!mobile

组件版本问题

在 clone 过程中,注意 CocoaPods/Gemfile.lock 中依赖的组件版本。

比如在 CocoaPods 1.8.4 中,XcodeProj 依赖的是 1.13.0 这个 tag,而不是最新的 master。如果直接进行接下来的 bundle install 可能会出现版本仲裁错误。做法是,checkout XcodeProj 到 1.13.0 这个 tag。

  1. Cocoapods 目录下,执行 bundle install

Debug

在 CocoaPods/examples 下面已经包含一些 Example 工程,可以直接用来进行调试。

运行

按运行的逻辑,首先是进入某个 Example,然后 pod install ,所以步骤是:

cd CocoaPods/examples/AFNetworking\ Example
bundle exec pod install

调试

调试的逻辑和运行差不多,首先在 Cocoapods/lib/cocoapods/command.rb 中加一个断点:

yEzaqyJ.jpg!mobile

RubyMine 主界面右上角 → Add Configuration → + → Ruby,新增 Ruby 的调试配置

Zzmmeu.jpg!mobile

which bundle

保存以后,点击 :beetle: 按钮就可以了。

Ruby script

既然 CocoaPods 的源码已经拉到本地,Ruby script 也可以直接指向本地的 pod 命令:xxx/CocoaPods/bin/pod,相对的 Script arguments 为 install 即可。

Tips

断点时机

pod 命令的入口大多在 CocoaPods/lib/cocoapods/command/ 目录下。比如 pod install 对应着 install.rb 中的 run 方法。

VSCode 调试

新增 launch.json,并写入 debug 配置:

{
  "version": "0.2.0",
  "configurations": [
    {"name": "AFNetworking",
      "type": "Ruby",
      "request": "launch",
      "useBundler": true,
      "showDebuggerOutput": true,
      "cwd": "${workspaceRoot}/CocoaPods/examples/AFNetworking Example",
      "program": "${workspaceRoot}/CocoaPods/bin/pod",
      "args": ["install"],
    }
  ]
}

CocoaPods/Gemfile 需要新增两个 debug 的依赖,并重新 bundle install

gem "ruby-debug-ide"
gem "debase"

断点并执行 Debug,效果大致如下:

6riUfyQ.jpg!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK