Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 53 lines (44 sloc) 1.304 kb
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
1 require 'rubygems/command'
2 require 'rubygems/version_option'
3 require 'open_gem/common_options'
4
ae1a294 @adamsanderson Fixing up spec and docs.
authored
5 # OpenCommand will open a gem's source path
7218ade @adamsanderson proof of concept command.
authored
6 class Gem::Commands::OpenCommand < Gem::Command
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
7 include ::OpenGem::CommonOptions
8c6e2ae @adamsanderson You can now specify a version
authored
8 include Gem::VersionOption
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
9
f173ef9 @adamsanderson Initial commit.
authored
10 def initialize
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
11 super 'open', "Opens the gem's source directory with $GEM_OPEN_EDITOR or $EDITOR",
12 :command => nil,
e267d02 @adamsanderson option to open the latest version of the gem.
authored
13 :version=> Gem::Requirement.default,
14 :latest=> false
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
15
612ce4a @adamsanderson Refactoring common options out
authored
16 add_command_option
17 add_latest_version_option
8c6e2ae @adamsanderson You can now specify a version
authored
18 add_version_option
6a502a9 @adamsanderson Only use patten matches if there is not an exact match
authored
19 add_exact_match_option
f173ef9 @adamsanderson Initial commit.
authored
20 end
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
21
7218ade @adamsanderson proof of concept command.
authored
22 def arguments # :nodoc:
23 "GEMNAME gem to open"
24 end
f173ef9 @adamsanderson Initial commit.
authored
25
9284648 @adamsanderson Adding some tests, hooray for test later development.
authored
26 def execute
7218ade @adamsanderson proof of concept command.
authored
27 name = get_one_gem_name
9284648 @adamsanderson Adding some tests, hooray for test later development.
authored
28 path = get_path(name)
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
29
9284648 @adamsanderson Adding some tests, hooray for test later development.
authored
30 open_gem(path) if path
31 end
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
32
9284648 @adamsanderson Adding some tests, hooray for test later development.
authored
33 def get_path(name)
94470f8 @adamsanderson moving out common code
authored
34 if spec = get_spec(name)
35 spec.full_gem_path
7e82d32 @adamsanderson Ask which version to open when more than one
authored
36 end
37 end
9284648 @adamsanderson Adding some tests, hooray for test later development.
authored
38
7e82d32 @adamsanderson Ask which version to open when more than one
authored
39 def open_gem(path)
af04d84 @cwninja Respect the VISIAL environment variable
cwninja authored
40 editor = options[:command] || ENV['GEM_OPEN_EDITOR'] || ENV['VISUAL'] || ENV['EDITOR']
870c34e @adamsanderson Open gems with the default editor
authored
41 if !editor
af04d84 @cwninja Respect the VISIAL environment variable
cwninja authored
42 say "Either set $GEM_OPEN_EDITOR, $VISUAL, $EDITOR, or use -c <command_name>"
870c34e @adamsanderson Open gems with the default editor
authored
43 else
ee15a7e @adamsanderson properly handles commands with arguments.
authored
44 command_parts = Shellwords.shellwords(editor)
45 command_parts << path
46 success = system(*command_parts)
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
47 if !success
bf2b15d @adamsanderson Alert the user on failure to run a command.
authored
48 raise Gem::CommandLineError, "Could not run '#{editor} #{path}', exit code: #{$?.exitstatus}"
49 end
870c34e @adamsanderson Open gems with the default editor
authored
50 end
f173ef9 @adamsanderson Initial commit.
authored
51 end
7e0acfc @alexch RubyGems 2.0+ compatibility (and various updates)
alexch authored
52 end
Something went wrong with that request. Please try again.