关于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"
]
}
]
}
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
,所有的依赖都会添加到我们当前的运行环境中。
此时,Gem
组件已配置完成,我们按下F5
运行该Gem
库,当输出如下时,就代表该组件已经成功创建
小结
1,我们在 Gemfile
中,引入了 Ruby
调试所依赖的三方库。在Gemspec
里面,我们引入了cocoapods-play
组件所依赖的三方库。
2,在launch.json
我们指定了 入口文件为 当前工作目录下的bin目录下的console文件
。
3,在 console
文件中,先输出传入参数
,然后调用 BaseketGame
对象的shotBall
方法。
play命令
定义Play类
自定义pod
命令,需要 引入cocoapods
即require "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
最终目录结构如下
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
,就能看到如下结果
这样,我们就大功告成了。
源码
关于源码工程cocoapods-play我已经上传到Github
,有需要的可自行下载。