Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #804 from quozd/open-command

Add `gem open` subcommand
  • Loading branch information...
commit d92087e899080add934a79713e45a261bc57edc2 2 parents e85f550 + 5128f38
@drbrain drbrain authored
View
79 lib/rubygems/commands/open_command.rb
@@ -0,0 +1,79 @@
+require 'English'
+require 'rubygems/command'
+require 'rubygems/version_option'
+require 'rubygems/util'
+
+class Gem::Commands::OpenCommand < Gem::Command
+
+ include Gem::VersionOption
+
+ def initialize
+ super 'open', 'Open gem sources in editor'
+
+ add_option('-e', '--editor EDITOR', String,
+ "Opens gem sources in EDITOR") do |editor, options|
+ options[:editor] = editor || get_env_editor
+ end
+ end
+
+ def arguments # :nodoc:
+ "GEMNAME name of gem to open in editor"
+ end
+
+ def defaults_str # :nodoc:
+ "-e #{get_env_editor}"
+ end
+
+ def description # :nodoc:
+ <<-EOF
+ The open command opens gem in editor and changes current path
+ to gem's source directory. Editor can be specified with -e option,
+ otherwise rubygems will look for editor in $EDITOR, $VISUAL and
+ $GEM_EDITOR variables.
+ EOF
+ end
+
+ def usage # :nodoc:
+ "#{program_name} GEMNAME [-e EDITOR]"
+ end
+
+ def get_env_editor
+ ENV['GEM_EDITOR'] ||
+ ENV['VISUAL'] ||
+ ENV['EDITOR'] ||
+ 'vi'
+ end
+
+ def execute
+ @version = options[:version] || Gem::Requirement.default
+ @editor = options[:editor] || get_env_editor
+
+ found = open_gem(get_one_gem_name)
+
+ terminate_interaction 1 unless found
+ end
+
+ def open_gem name
+ spec = spec_for name
+ return false unless spec
+
+ open_editor(spec.full_gem_path)
+ end
+
+ def open_editor path
+ Dir.chdir(path) do
+ pid = fork do
+ args = (@editor.split(/\s+/) + [path]).join(' ')
+ exec(args)
+ end
+ end
+ end
+
+ def spec_for name
+ spec = Gem::Specification.find_all_by_name(name, @version).last
+
+ return spec if spec
+
+ say "Unable to find gem '#{name}'"
+ end
+end
View
46 test/rubygems/test_gem_commands_open_command.rb
@@ -0,0 +1,46 @@
+require 'rubygems/test_case'
+require 'rubygems/commands/open_command'
+
+class TestGemCommandsOpenCommand < Gem::TestCase
+
+ def setup
+ super
+
+ @cmd = Gem::Commands::OpenCommand.new
+ end
+
+ def gem name
+ spec = quick_gem name do |gem|
+ gem.files = %W[lib/#{name}.rb Rakefile]
+ end
+ write_file File.join(*%W[gems #{spec.full_name} lib #{name}.rb])
+ write_file File.join(*%W[gems #{spec.full_name} Rakefile])
+ end
+
+ def test_execute
+ @cmd.options[:args] = %w[foo]
+ @cmd.options[:editor] = 'echo'
+
+ gem 'foo'
+
+ use_ui @ui do
+ @cmd.execute
+ end
+
+ assert_equal "", @ui.error
+ end
+
+ def test_execute_bad_gem
+ @cmd.options[:args] = %w[foo]
+
+ assert_raises Gem::MockGemUi::TermError do
+ use_ui @ui do
+ @cmd.execute
+ end
+ end
+
+ assert_match %r|Unable to find gem 'foo'|, @ui.output
+ assert_equal "", @ui.error
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.