[关闭]
@qidiandasheng 2022-01-01T15:43:55.000000Z 字数 3577 阅读 731

CocoaPods源码及插件调试

Cocoapods


插件

创建插件

  1. pod plugins create githooks

修改cocoapods-githooks.gemspec文件:

  1. spec.files = `git ls-files`.split($/)
  2. 修改为
  3. spec.files = Dir['lib/**/*']

修改lib/cocoapods_plugin.rb

  1. require 'cocoapods-githooks/command'
  2. module CocoapodsGitHooks
  3. Pod::HooksManager.register('cocoapods-githooks', :post_install) do |context|
  4. p "hello world!"
  5. end
  6. Pod::HooksManager.register('cocoapods-githooks', :post_update) do |context|
  7. p "hello world!"
  8. end
  9. end

编译安装

编译:

  1. sudo gem build cocoapods-githooks.gemspec

安装:

  1. sudo gem install cocoapods-githooks-0.0.1.gem

显示已安装插件:

  1. pod plugins installed

调试

创建调试环境

直接复制创建的插件源码目录cocoapods-githooksCocoaPods源码同一级目录下

截屏2021-08-26 下午4.10.24.png-52.7kB

修改CocoaPods目录下的Gemfile文件:

  1. gem 'cocoapods-githooks', path: '../cocoapods-githooks'

CocoaPods目录下执行以下命令进行安装:

  1. bundle install

bundle exec

我们bundle install之后其实是指定了一个ruby的运行环境,运行环境中就包含我们bundle install安装的一些gem。

正常我们运行pod install的时候,如果没安装过cocoapods-githooks插件,则会报以下错误,因为这样运行的时候其实是使用的系统的ruby环境。

  1. [!] Your Podfile requires that the plugin `cocoapods-githooks` be installed. Please install it and try installation again.

所以我们使用bundle exec pod install就能使用bundle install安装的ruby环境了,我们之前指定过cocoapods-githooks的源码目录,这时候也会去调用对应的源码。

但是我们还是没办法断点调试,这时候我们就需要用到对应的IDE进行断点调试了。

VSCode调试准备

在之前的Gemfile文件里加入以下两个gem,用于debug调试:

  1. gem "ruby-debug-ide"
  2. gem "debase"

运行bundle install

最后我们的Gemfile文件如下所示:

  1. SKIP_UNRELEASED_VERSIONS = false
  2. # 它是用于方便开发和调试,当 **SKIP_UNRELEASED_VERSIONS** 为 false && path 为 true 时会使用与本地的 CocoaPods 项目同级目录下的 git 仓库,否则会使用对应的项目直接通过 Gem 加载。
  3. def cp_gem(name, repo_name, branch = 'master', path: false)
  4. return gem name if SKIP_UNRELEASED_VERSIONS
  5. opts = if path
  6. { :path => "../#{repo_name}" }
  7. else
  8. url = "https://github.com/CocoaPods/#{repo_name}.git"
  9. { :git => url, :branch => branch }
  10. end
  11. gem name, opts
  12. end
  13. source 'https://rubygems.org'
  14. gemspec
  15. group :development do
  16. cp_gem 'claide', 'CLAide', path: '../CLAide'
  17. cp_gem 'cocoapods-core', 'Core', path: '../Core'
  18. cp_gem 'cocoapods-deintegrate', 'cocoapods-deintegrate'
  19. cp_gem 'cocoapods-downloader', 'cocoapods-downloader', path: '../cocoapods-downloader'
  20. cp_gem 'cocoapods-plugins', 'cocoapods-plugins'
  21. cp_gem 'cocoapods-search', 'cocoapods-search'
  22. cp_gem 'cocoapods-trunk', 'cocoapods-trunk'
  23. cp_gem 'cocoapods-try', 'cocoapods-try'
  24. cp_gem 'molinillo', 'Molinillo', path: '../Molinillo'
  25. cp_gem 'xcodeproj', 'Xcodeproj', path: '../Xcodeproj'
  26. gem 'cocoapods-dependencies', '~> 1.0.beta.1'
  27. gem 'nanaimo'
  28. gem 'activesupport', '> 5', '< 6' # Pinned < 6 because 6 requires Ruby 2.5.0
  29. gem 'bacon', :git => 'https://github.com/leahneukirchen/bacon.git'
  30. gem 'mocha', '< 1.5'
  31. gem 'mocha-on-bacon'
  32. gem 'netrc'
  33. gem 'prettybacon'
  34. gem 'typhoeus'
  35. gem 'webmock'
  36. # 依赖的插件
  37. gem 'cocoapods-githooks', path: '../cocoapods-githooks'
  38. # 用于debug的gem
  39. gem "ruby-debug-ide"
  40. gem "debase"
  41. gem 'xcpretty'
  42. gem 'bigdecimal', '~> 1.3.0'
  43. gem 'public_suffix'
  44. gem 'ruby-graphviz', '< 1.2.5'
  45. # Integration tests
  46. gem 'diffy'
  47. gem 'clintegracon', :git => 'https://github.com/mrackwitz/CLIntegracon.git'
  48. # Code Quality
  49. gem 'inch_by_inch'
  50. gem 'rubocop', '0.50.0'
  51. gem 'simplecov', '< 0.18'
  52. gem 'danger', '~> 5.3'
  53. end
  54. group :debugging do
  55. gem 'cocoapods_debug'
  56. gem 'rb-fsevent'
  57. gem 'kicker'
  58. gem 'awesome_print'
  59. gem 'ruby-prof', :platforms => [:ruby]
  60. end

VSCode断点调试

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

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "type": "Ruby",
  9. "request": "launch",
  10. "name": "Launch File",
  11. "cwd": "${workspaceFolder}/CocoaPods/examples/helloworld/Example",
  12. "program": "${workspaceFolder}/CocoaPods/bin/pod",
  13. "args": ["install"],
  14. "useBundler": true
  15. }
  16. ]
  17. }

打开VSCode,这里我们在installer.rbinstall!方法上面打上断点,点击运行程序,就可以看到如下的页面了:

截屏2021-12-03 下午8.17.34.png-567.8kB

参考

CocoaPods源码与插件断点调试
CocoaPods - 如何调试

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注