玩转cocoapods-plugins开发

3,624 阅读1分钟

1 创建 Cocoapods Plugins

1.1 创建命令

pod plugins create hd

1.2 文件结构

  ├── Gemfile                      插件工程本身是用Bundler来管理项目依赖
    ├── LICENSE.txt                        
    ├── README.md
    ├── Rakefile                     rake默认执行spec目录下的spec文件执行用例自测
    ├── cocoapods-hd-0.0.1.gem
    ├── cocoapods-hd.gemspec         发布gem包需要的描述文件
    ├── lib
    │   ├── cocoapods-hd
    │   │   ├── command
    │   │   │   └── hd.rb            插件实现文件
    │   │   ├── command.rb
    │   │   └── gem_version.rb
    │   ├── cocoapods-hd.rb
    │   └── cocoapods_plugin.rb
    └── spec
        ├── command
        │   └── hd_spec.rb           默认帮你实现插件注册,可以在里面写用例测试插件
        └── spec_helper.rb

1.3 开发

module Pod
      class Command
        class Hd < Command
          self.summary = 'cocoapods-hd 功能简介'
    
          self.description = <<-DESC
            hd 插件测试功能描述
          DESC
    
          # self.arguments = 'NAME'
    
          def initialize(argv)
            @name = argv.shift_argument
            super
          end
    
          def validate!
            super
            help! 'A Pod name is required.' unless @name
          end
    
          def run
            UI.puts "Add your implementation for the cocoapods-hd plugin in #{__FILE__}"
          end
        end
      end
    end
    

2 插件操作

2.1 编译插件

sudo gem build cocoapods-hd.gemspec

2.2 安装插件

sudo gem install cocoapods-hd-0.0.1.gem

2.3 查看是否安装成功

pod plugins installed

2.4 更新

sudo gem uninstall cocoapods-hd-0.0.1.gem && sudo gem build cocoapods-hd.gemspec  && sudo gem install cocoapods-hd-0.0.1.gem

2.5 发布

gem push cocoapods-hd-0.0.1.gem
Enter your RubyGems.org credentials.
Don't have an account yet? Create one at https://rubygems.org/sign_up
   Email:   gem_author@example
Password:
Signed in.
Pushing gem to RubyGems.org...
Successfully registered gem: cocoapods-hd (0.1.0)
 

3 使用VSCode/RubyMine调试

  • 下载cocoapods源码

  • 创建一个空目录debug-hd,然后添加Gemfile

    Gemfile文件、CocoaPods、cocoapods-hd、ios工程等都要放在debug-hd目录下

    source 'https://rubygems.org'
    gem 'cocoapods', path: './CocoaPods'
    gem 'cocoapods-hd', path: './cocoapods-hd'
    group :debug do
        gem 'ruby-debug-ide'
        gem 'debase'
     end
    
  • 在.vscode文件下,创建launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "install",
                "showDebuggerOutput": true,
                "type": "Ruby",
                "request": "launch",
                "useBundler": true,
                "cwd": "${workspaceRoot}/HDPodTest",//cwd代表运行的目录,必须和gemfile一个目录。${workspaceRoot}指的是VSCode工程的目录
                "program": "~/Work/CocoaPods/bin/pod",//这里指明的是cocoapods源码里面的bin目录下的pod
                "args": [
                    "install", //这里指明是pod程序的参数 install
                    "verbose" // 其他参数
                ],
                "stopOnEntry": false
            }
        ]
    }
    
  • 执行sudo bundle install

  • 在plugin里面添加断点,然后按F5运行,就可以看到进入到plugin源码了

  • 完整目录

    ├── Gemfile
    ├── Gemfile.lock
    ├── CocoaPods        官方源码
    ├── cocoapods-hd     插件代码
    ├── HDPodTest        ios工程,包含podfile
    │── .vscode
    │   ├── launch.json
    └── bin
     
    

4 其他

4.1 How to install gems from Gemfile?

sudo gem install bundler先安装bundler,然后执行bundle init 命令安装ruby库

source "https://rubygems.org"  # where gems will be downloaded from
ruby "2.2.3"  # ruby version, change for the one you use
gem 'cocoapods'
 
group :development do   # you can make groups for test, development, production..
  gem 'mocha'
 
end

4.2 安装/卸载已经发布的cocoapods插件

sudo gem install cocoapods-hd
sudo gem uninstall cocoapods-hd

4.3 安装指定版本bundler

sudo gem uninstall bundler -v=2.2.11
sudo gem install bundler -v=2.2.11

5 参考

vscode断点调试cocoapods plugin

juejin.cn/post/690420…