玩转cocoapods-plugins开发

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源码ios

  • 建立一个空目录debug-hd,而后添加Gemfilejson

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

    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.jsonbash

    {
        "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 installmarkdown

  • 在plugin里面添加断点,而后按F5运行,就能够看到进入到plugin源码了less

  • 完整目录ide

    ├── 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库oop

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 pluginpost

juejin.cn/post/690420…测试

相关文章
相关标签/搜索