RubyGemのコマンドプラグイン機能の実装方法
Rubygemsのプラグインで独自のRubyGemsコマンドをカスタマイズ
あるいは、あなたも知りたい
独自のGemパッケージの作成方法
本文:
最近RubyGemsの1.3.2のバージョンでは、独自のカスタムGemsコマンドをサポートするホットスポット機能がリリースされています.ここでは、Rubygemsのプラグインを使用して、独自のGemsコマンドを生成する方法について説明します.
Gemsカスタムコマンドを作成する主な手順は、次のとおりです.
1. rubygems_の作成plugin.rb 2. あなたのコマンドを登録します.コマンドファイル4を生成する.カスタムコマンドの説明
手順1:rubygems_を作成するplugin.rb
既存のgemパッケージソースファイル、または新しいgemソースファイルディレクトリの下にlib/rubygems_を作成します.plugin.rb.graphのコマンド生成を参照できます.
手順2:コマンドの登録
あなたが作成したrubygems_plugin.rbファイルでは、コマンドを登録し、ロードする必要があります.
実際のrubygems_plugin.rbファイルは次のようになります.
ステップ3.コマンドファイルの生成
RubyGemsがコマンドを自動的にロードするには、lib/rubygems/commands/ファイルディレクトリの下にコマンドファイルを置く必要があります.また、Camel Caseの名前に従って、コマンドファイル名をマッピングします.たとえば、上のGem::Command::GrepCommandコマンドの場合、lib/rubygems/commands/grep_を作成する必要があります.command.rb. もちろん、ファイルを分離したくない場合は、rubygems_にコマンドコードを書くこともできます.plugin.rb里.
ステップ4.カスタムコマンドの説明
次に、コマンドのカスタマイズ方法を例に挙げます.たとえば、new、execute、arguments、defaults_というコマンドを生成します.str, description and usage.
new()
このコマンドに対応する記述クラスでは、コマンドの名前、プロンプト、パラメータオプションを定義する必要があります.具体的なコマンドは次のとおりです.
説明:
*通常、作成コマンドの多くはGem::Commandのサブクラスです.例でGem::Command::QueryCommandを継承するのは,このメソッド自体がqueryタイプであるためである.
*super()を使用して、コマンド名、プロンプト情報、およびデフォルトパラメータを定義します.しかし、この例ではデフォルトの親クラスが使用されていないため、これらの情報を自分でカスタマイズする必要があります.
* add_optionとremove_optionは、コマンドパラメータを追加および削除するために使用されます.パラメータはOptionParserで定義されます.
execute()
コマンドの機能部分は通常ここで完了しますが、この例ではexecute()はdelegatesによって他の場所で宣言されているため、あまり論理的ではありません.
arguments(), defaults_str(), description() and usage()
これらのセクションは、コマンドのhelpなどの情報を記述するためにオプションです.以下に説明します.
*arguments():コマンドパラメータ
* defaults_str():デフォルトパラメータを指定します.
*description:コマンドの詳細を指定します.
*usage():コマンドの順序を説明します.
たとえば
:
あるいは、あなたも知りたい
独自のGemパッケージの作成方法
本文:
最近RubyGemsの1.3.2のバージョンでは、独自のカスタムGemsコマンドをサポートするホットスポット機能がリリースされています.ここでは、Rubygemsのプラグインを使用して、独自のGemsコマンドを生成する方法について説明します.
Gemsカスタムコマンドを作成する主な手順は、次のとおりです.
1. rubygems_の作成plugin.rb 2. あなたのコマンドを登録します.コマンドファイル4を生成する.カスタムコマンドの説明
手順1:rubygems_を作成するplugin.rb
既存のgemパッケージソースファイル、または新しいgemソースファイルディレクトリの下にlib/rubygems_を作成します.plugin.rb.graphのコマンド生成を参照できます.
手順2:コマンドの登録
あなたが作成したrubygems_plugin.rbファイルでは、コマンドを登録し、ロードする必要があります.
require Gem::CommandManager
実際のrubygems_plugin.rbファイルは次のようになります.
require 'rubygems/command_manager'
# dependencies specific to my command
require 'rubygems/commands/query_command'
require 'rubygems/super_search'
require 'hirb'
Gem::CommandManager.instance.register_command :grep
ステップ3.コマンドファイルの生成
RubyGemsがコマンドを自動的にロードするには、lib/rubygems/commands/ファイルディレクトリの下にコマンドファイルを置く必要があります.また、Camel Caseの名前に従って、コマンドファイル名をマッピングします.たとえば、上のGem::Command::GrepCommandコマンドの場合、lib/rubygems/commands/grep_を作成する必要があります.command.rb. もちろん、ファイルを分離したくない場合は、rubygems_にコマンドコードを書くこともできます.plugin.rb里.
ステップ4.カスタムコマンドの説明
次に、コマンドのカスタマイズ方法を例に挙げます.たとえば、new、execute、arguments、defaults_というコマンドを生成します.str, description and usage.
new()
このコマンドに対応する記述クラスでは、コマンドの名前、プロンプト、パラメータオプションを定義する必要があります.具体的なコマンドは次のとおりです.
class Gem::Commands::GrepCommand < Gem::Commands::QueryCommand
def initialize
super 'grep', "Enhances search command by providing extra search options and displaying results as a table"
defaults.merge!(:columns=>[:name,:summary,:author])
add_option('-c', '--columns STRING', 'Gemspec columns/attributes to display per gem') do |value, options|
options[:columns] = value.split(/\s*,\s*/).map {|e|
self.class.valid_gemspec_columns.detect {|c| c =~ /^#{e}/ }
}.compact.map {|e| e.to_sym}
end
add_option('-f', '--fields STRING', 'Gemspec fields/attributes to search (only for local gems)') do |value, options|
options[:fields] = value.split(/\s*,\s*/).map {|e|
self.class.valid_gemspec_columns.detect {|c| c =~ /^#{e}/ }
}.compact
end
remove_option '--name-matches'
remove_option '-d'
remove_option '--versions'
end
説明:
*通常、作成コマンドの多くはGem::Commandのサブクラスです.例でGem::Command::QueryCommandを継承するのは,このメソッド自体がqueryタイプであるためである.
*super()を使用して、コマンド名、プロンプト情報、およびデフォルトパラメータを定義します.しかし、この例ではデフォルトの親クラスが使用されていないため、これらの情報を自分でカスタマイズする必要があります.
* add_optionとremove_optionは、コマンドパラメータを追加および削除するために使用されます.パラメータはOptionParserで定義されます.
execute()
コマンドの機能部分は通常ここで完了しますが、この例ではexecute()はdelegatesによって他の場所で宣言されているため、あまり論理的ではありません.
def execute
string = get_one_optional_argument
options[:name] = /#{string}/i
Gem.source_index.extend Gem::SuperSearch
super
end
arguments(), defaults_str(), description() and usage()
これらのセクションは、コマンドのhelpなどの情報を記述するためにオプションです.以下に説明します.
*arguments():コマンドパラメータ
* defaults_str():デフォルトパラメータを指定します.
*description:コマンドの詳細を指定します.
*usage():コマンドの順序を説明します.
たとえば
:
def arguments # :nodoc:
"REGEXP regular expression string to search specified gemspec attributes"
end
def usage # :nodoc:
"#{program_name} [REGEXP]"
end
def defaults_str # :nodoc:
"--local --columns name,summary,author --fields name --no-installed"
end
def description # :nodoc:
'Enhances search command by providing options to search (--fields) and display (--columns) ' +
'gemspec attributes. Results are displayed in an ascii table. Gemspec attributes can be specified '+
'by the first unique string that it starts with i.e. "su" for "summary". To specify multiple gemspec attributes, delimit ' +
"them with commas. Gemspec attributes available to options are: #{self.class.valid_gemspec_columns.join(', ')}."
end