自定义Cocoapods命令

1,730 阅读4分钟

关于cocoapods调试Ruby 调试的配置,欢迎查看文章合集 Ruby和Cocoapods文章合集 。在本文中,主要记录下自定义 Pod 命令的过程。

自定义一个play 命令,最终的效果如下:

bel@beldeMacBook-Pro cocoapods-play % pod play
#<CLAide::ARGV:0x00007fdbd13965c0>
[Cocoapods play] begin -------------
BaseketGame 初始化
进了一个三分球
[Cocoapods play] end ---------------

搭建Gem库组件

bundle gem play

在终端输入 bundle gem play 命令后,gem会提供给我们几个选项,来配置我们的代码模版,可以根据自己的需要,自主选择。

bundle gem play
......
create  cocoapods-play/Gemfile
create  cocoapods-play/lib/cocoapods/play.rb
create  cocoapods-play/lib/cocoapods/play/version.rb
create  cocoapods-play/cocoapods-play.gemspec
create  cocoapods-play/Rakefile
create  cocoapods-play/README.md
create  cocoapods-play/bin/console
create  cocoapods-play/bin/setup
create  cocoapods-play/.gitignore
create  cocoapods-play/.rspec
create  cocoapods-play/spec/spec_helper.rb
create  cocoapods-play/spec/cocoapods/play_spec.rb
create  cocoapods-play/.github/workflows/main.yml
create  cocoapods-play/LICENSE.txt
create  cocoapods-play/CHANGELOG.md

Gem 'cocoapods-play' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html

bel@beldeMacBook-Pro 自定义命令 %

gemspec

使用 VSCode 打开项目,首先来编辑下 gemspec,这个和我们平常使用 pod做组件时的podspec文件很像,我们可以在 gemspec里面添加插件说明,添加gem库依赖

1,带有Todo选项的,都需要替换掉。

2,metadata相关的选项可以删除。

3,添加必须依赖的Gem库

# frozen_string_literal: true

require_relative "lib/cocoapods/play/version"

Gem::Specification.new do |spec|
    spec.name = "cocoapods-play"
    spec.version = Cocoapods::Play::VERSION
    spec.authors = ["LY"]
    spec.email = ["xxxx@163.com"]
    spec.summary = " play 命令"
    spec.description = "play 命令"
    spec.homepage = "http://www.baidu.com"
    spec.license = "MIT"
    spec.required_ruby_version = ">= 2.4.0"
    spec.files = spec.files = Dir["lib/**/*.rb"] + %w{ README.md LICENSE.txt }
    spec.bindir = "exe"
    spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
    spec.require_paths = ["lib"]
    spec.add_development_dependency 'bundler', '~> 2.1'
    spec.add_development_dependency 'coveralls'
    spec.add_development_dependency 'rake', '~> 10.0'
    spec.add_dependency 'cocoapods', '~> 1.10'
end

编辑运行命令

我们定位到 lib/cocoapods/play.rb文件,删除默认代码,添加如下代码。


# play.rb
module PlayBall
    class BaseketGame
        def self.shotBall
            new
        end

        def initialize
            puts "BaseketGame 初始化"
            puts "进了一个三分球"
        end
    end
end
  • 1,定义了一个 PlayBall模块。
  • 2,定义了 BaseketGame 类。
  • 3,定义了 类方法: shotBall,在调用类方法的时候,会初始化一个新对象。
  • 4,在初始化方法中,输出两行文字。

编辑入口命令

cocoapods-play/bin目录下,我们将console文件的默认代码删除掉,并添加如下代码

require "bundler/setup"
require "cocoapods/play"

puts "success set up"
puts ARGV

PlayBall::BaseketGame.shotBall
  • 1,分别 输出一行文字 和 传递过来的参数。
  • 2,调用 PlayBall模块下的BasketGame对象的类方法 shotBall

创建launch文件

在根目录下,我们创建调试需要的.vscode文件夹和launch.json

{
    "configurations": [
        {
         "name": "Debug CocoaPods Plugin",
         "showDebuggerOutput": true,
         "type": "Ruby",
         "request": "launch",
         "useBundler": true,
         "cwd": "${workspaceRoot}", //命令执行的路径
        "program": "${workspaceRoot}/bin/console", // 入口
        "args": [
            "play"
        ]
       }
    ]
}

截屏2021-08-31 下午10.01.44.png

Gemfile

Gemfile中,调加调试所需的依赖库:

source "https://rubygems.org"
# Specify your gem's dependencies in cocoapods-hmap.gemspec
gemspec
group :debugging do
    gem 'ruby-debug-ide'
    gem 'debase'

end

bundle install

我们在根目录下执行 bundle install,所有的依赖都会添加到我们当前的运行环境中。

截屏2021-08-31 下午10.04.49.png

此时,Gem组件已配置完成,我们按下F5运行该Gem库,当输出如下时,就代表该组件已经成功创建

截屏2021-08-31 下午10.13.02.png

小结

1,我们在 Gemfile中,引入了 Ruby调试所依赖的三方库。在Gemspec里面,我们引入了cocoapods-play组件所依赖的三方库。

2,在launch.json我们指定了 入口文件为 当前工作目录下的bin目录下的console文件

3,在 console文件中,先输出传入参数,然后调用 BaseketGame对象的shotBall方法。

play命令

定义Play类

自定义pod命令,需要 引入cocoapodsrequire "cocoapods",引入moudle Pod

require "cocoapods"
module Pod
    class Commands
        class Play < Command // 1
            def initialize(argv)
                puts argv
                super(argv)
            end

            def run // 2
                puts "[Cocoapods play] begin -------------"
                PlayBall::BaseketGame.shotBall
                puts "[Cocoapods play] end ---------------"
            end
        end
   end
end

1,创建继承 Command的类Play

2,run方法是在 Command类中继承过来的,一定要实现,在该方法里面,实现我们命令的相应逻辑。

创建command目录

我们在 lib/cocoapods目录下,创建一个command文件夹,并将play.rb移至该目录下。

cocoapods_plugin.rb

下一步,我们需要在lib目录下,创建一个名为 cocoapods_plugin.rb为文件

# cocoapods_plugin.rb
require "cocoapods/command/play" # 引入 play.rb

最终目录结构如下

截屏2021-08-31 下午10.47.59.png

rake install:local

最后在终端执行 sudo rake install:local,安装插件到本地,

bel@beldeMacBook-Pro cocoapods-play % sudo rake install:local
cocoapods-play 0.1.0 built to pkg/cocoapods-play-0.1.0.gem.
cocoapods-play (0.1.0) installed.

我们可以通过 gem info cocoapods-play来查看该插件信息。

bel@beldeMacBook-Pro cocoapods-play % gem info cocoapods-play

*** LOCAL GEMS ***

cocoapods-play (0.1.0)
    Author: LY
    Homepage: http://www.baidu.com
    License: MIT
    Installed at: /Library/Ruby/Gems/2.6.0

    play 命令

pod play

我们在终端直接执行pod play,就能看到如下结果

截屏2021-08-31 下午10.51.55.png

这样,我们就大功告成了。

源码

关于源码工程cocoapods-play我已经上传到Github,有需要的可自行下载。