玩轉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…測試

相關文章
相關標籤/搜索