Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 7 commits
  • 12 files changed
  • 0 comments
  • 6 contributors
Jan 06, 2012
Gavin Brock Added "gem shell <gemame>" feature 6fcedb1
Mar 23, 2012
John Whitley jwhitley Fix multiple deprecation warnings.
"require 'rake/rdoctask'" has been deprecated in favor of 'rdoc/task'
since RDoc 2.4.2. Gem::Dependency.new no longer accepts a Regexp for the
name argument. This change does not attempt to restore matching
functionality. Correspondingly, the --exact option has been removed.
f715a78
Andre Arko Require common_options directly, so that Rubygems won't try adding an…
…ything to the LOAD_PATH.
fbfea4a
blowmage blowmage Update Launchy dependency. 1be9808
Jake Moffatt jakeonrails Added "homepage" command.
* The homepage command opens a given gem's homepage in the browser.

Conflicts:

	lib/rubygems_plugin.rb
a639b53
James Ferguson JamesFerguson Use a more forgiving regexp to search when GEMNAME doesn't match exac…
…tly. Specifically treat '-'. '_' and '' interchangeably. Includes test.

Conflicts:

	lib/open_gem/common_options.rb
46044aa
Gavin Brock Bumped version and doc 0001e9a
6 CHANGELOG.markdown
Source Rendered
... ... @@ -1,5 +1,9 @@
1 1 OpenGem
2 2 ========
  3 +1.5.0
  4 +---
  5 +* Pulled in various outstanding pull requests
  6 +
3 7 1.4.2
4 8 ---
5 9 * Respect the VISUAL environment variable above EDITOR. (cwninja)
@@ -20,4 +24,4 @@ OpenGem
20 24
21 25 1.0
22 26 ----
23   -* Initial release!
  27 +* Initial release!
6 README.markdown
Source Rendered
@@ -9,6 +9,10 @@ If you just want to read documentation you can do:
9 9
10 10 gem read activerecord
11 11
  12 +If you just want to browse the source code in a shell you can do:
  13 +
  14 + gem shell activerecord
  15 +
12 16 Installation:
13 17 ------------
14 18 OpenGem require rubygems 1.3.6 or higher. You may need
@@ -24,4 +28,4 @@ For more help:
24 28
25 29 gem open --help
26 30
27   -Adam Sanderson, netghost@gmail.com
  31 +Adam Sanderson, netghost@gmail.com
4 Rakefile
... ... @@ -1,6 +1,6 @@
1 1 require 'rake'
2 2 require 'rake/testtask'
3   -require 'rake/rdoctask'
  3 +require 'rdoc/task'
4 4
5 5 begin
6 6 require 'jeweler'
@@ -19,7 +19,7 @@ begin
19 19
20 20 # Testing
21 21 s.test_files = FileList["test/**/*_test.rb"]
22   - s.add_dependency 'launchy', '~> 0.3.5'
  22 + s.add_dependency 'launchy', '~> 0.4'
23 23 s.add_development_dependency 'mocha', '~> 0.9.5'
24 24 end
25 25
4 VERSION.yml
... ... @@ -1,4 +0,0 @@
1   ----
2   -:minor: 4
3   -:patch: 2
4   -:major: 1
15 lib/open_gem/common_options.rb
@@ -13,17 +13,10 @@ def add_latest_version_option
13 13 options[:latest] = true
14 14 end
15 15 end
16   -
17   - def add_exact_match_option
18   - add_option('-x', '--exact',
19   - 'Only list exact matches') do |value, options|
20   - options[:exact] = true
21   - end
22   - end
23   -
  16 +
24 17 def get_spec(name)
25 18 dep = Gem::Dependency.new(name, options[:version])
26   - specs = Gem.source_index.search(dep)
  19 + specs = Gem::Specification.find_all_by_name(name,options[:version])
27 20 if block_given?
28 21 specs = specs.select{|spec| yield spec}
29 22 end
@@ -31,7 +24,7 @@ def get_spec(name)
31 24 if specs.length == 0
32 25 # If we have not tried to do a pattern match yet, fall back on it.
33 26 if(!options[:exact] && !name.is_a?(Regexp))
34   - pattern = /#{Regexp.escape name}/
  27 + pattern = /#{(Regexp.escape name).gsub(/\\-|_/, '(\\-|_|)')}/
35 28 get_spec(pattern)
36 29 else
37 30 say "#{name.inspect} is not available"
@@ -49,4 +42,4 @@ def get_spec(name)
49 42 end
50 43 end
51 44 end
52   -end
  45 +end
30 lib/rubygems/commands/homepage_command.rb
... ... @@ -0,0 +1,30 @@
  1 +require 'launchy'
  2 +# HomepageCommand will open the homepage of a gem in your browser.
  3 +class Gem::Commands::HomepageCommand < Gem::Command
  4 + include OpenGem::CommonOptions
  5 + include Gem::VersionOption
  6 +
  7 + def initialize
  8 + super 'homepage', "Opens the gem's homepage.",
  9 + :command => nil,
  10 + :version=> Gem::Requirement.default,
  11 + :latest=> false
  12 +
  13 + add_command_option "Application to view homepage with"
  14 + add_latest_version_option
  15 + add_version_option
  16 + add_exact_match_option
  17 + end
  18 +
  19 + def arguments # :nodoc:
  20 + "GEMNAME gem whose homepage you want to open"
  21 + end
  22 +
  23 + def execute
  24 + name = get_one_gem_name
  25 + spec = get_spec(name)
  26 + Launchy::Browser.run(spec.homepage)
  27 + end
  28 +
  29 +end
  30 +
1  lib/rubygems/commands/open_command.rb
@@ -12,7 +12,6 @@ def initialize
12 12 add_command_option
13 13 add_latest_version_option
14 14 add_version_option
15   - add_exact_match_option
16 15 end
17 16
18 17 def arguments # :nodoc:
1  lib/rubygems/commands/read_command.rb
@@ -13,7 +13,6 @@ def initialize
13 13 add_command_option "Application to read rdoc with"
14 14 add_latest_version_option
15 15 add_version_option
16   - add_exact_match_option
17 16 end
18 17
19 18 def arguments # :nodoc:
49 lib/rubygems/commands/shell_command.rb
... ... @@ -0,0 +1,49 @@
  1 +# ShellCommand will open a shell in a gem's dir
  2 +class Gem::Commands::ShellCommand < Gem::Command
  3 + include OpenGem::CommonOptions
  4 + include Gem::VersionOption
  5 +
  6 + def initialize
  7 + super 'shell', "Opens a shell in the gem's directory",
  8 + :command => nil,
  9 + :version=> Gem::Requirement.default,
  10 + :latest=> false
  11 +
  12 + add_command_option
  13 + add_latest_version_option
  14 + add_version_option
  15 + add_exact_match_option
  16 + end
  17 +
  18 + def arguments # :nodoc:
  19 + "GEMNAME gem to read"
  20 + end
  21 +
  22 + def execute
  23 + name = get_one_gem_name
  24 + path = get_path(name)
  25 +
  26 + shell_gem(path) if path
  27 + end
  28 +
  29 + def get_path(name)
  30 + if spec = get_spec(name)
  31 + spec.full_gem_path
  32 + end
  33 + end
  34 +
  35 + def shell_gem(path)
  36 + shell = options[:command] || ENV['GEM_OPEN_SHELL'] || ENV['SHELL']
  37 + if !shell
  38 + say "Either set $GEM_OPEN_SHELL, $SHELL, or use -c <command_name>"
  39 + else
  40 + command_parts = Shellwords.shellwords(shell)
  41 + Dir.chdir(path)
  42 + say "Working directory is #{path} - type exit to leave"
  43 + success = system(*command_parts)
  44 + if !success
  45 + raise Gem::CommandLineError, "Could not run '#{shell} in #{path}', exit code: #{$?.exitstatus}"
  46 + end
  47 + end
  48 + end
  49 +end
6 lib/rubygems_plugin.rb
@@ -5,7 +5,9 @@
5 5 require 'rubygems/version_option'
6 6
7 7 require 'shellwords'
8   -require 'open_gem/common_options'
  8 +require File.expand_path('../open_gem/common_options', __FILE__)
9 9
10 10 Gem::CommandManager.instance.register_command :open
11   -Gem::CommandManager.instance.register_command :read
  11 +Gem::CommandManager.instance.register_command :read
  12 +Gem::CommandManager.instance.register_command :shell
  13 +Gem::CommandManager.instance.register_command :homepage
8 open_gem.gemspec
@@ -5,11 +5,11 @@
5 5
6 6 Gem::Specification.new do |s|
7 7 s.name = %q{open_gem}
8   - s.version = "1.4.2"
  8 + s.version = "1.5.0"
9 9
10 10 s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11   - s.authors = ["Adam Sanderson"]
12   - s.date = %q{2010-04-23}
  11 + s.authors = ["Adam Sanderson and Gavin Brock"]
  12 + s.date = %q{2012-03-23}
13 13 s.description = %q{ Open a gem's source directory with either the default editor, or a specified command.
14 14 }
15 15 s.email = %q{netghost@gmail.com}
@@ -20,10 +20,10 @@ Gem::Specification.new do |s|
20 20 "CHANGELOG.markdown",
21 21 "README.markdown",
22 22 "Rakefile",
23   - "VERSION.yml",
24 23 "lib/open_gem/common_options.rb",
25 24 "lib/rubygems/commands/open_command.rb",
26 25 "lib/rubygems/commands/read_command.rb",
  26 + "lib/rubygems/commands/shell_command.rb",
27 27 "lib/rubygems_plugin.rb",
28 28 "test/open_command_test.rb"
29 29 ]
56 test/common_options_test.rb
... ... @@ -0,0 +1,56 @@
  1 +require 'test/unit'
  2 +require 'rubygems'
  3 +require 'mocha'
  4 +require 'forwardable'
  5 +require 'ruby-debug'
  6 +
  7 +require 'open_gem/common_options'
  8 +
  9 +class Test_CommonOptions < Test::Unit::TestCase
  10 + extend Forwardable
  11 +
  12 + include OpenGem::CommonOptions
  13 + include Gem::VersionOption
  14 +
  15 + def_delegators :@command, :options
  16 +
  17 + def setup
  18 + @command = Gem::Command.new 'open'
  19 + @word_one = 'foo'
  20 + @word_two = 'bar'
  21 + end
  22 +
  23 + def test_get_spec
  24 + Gem.source_index.stubs(:all_gems).returns(mock_specs_hash(test_gem))
  25 +
  26 + choices = mock_specs_hash(test_gem).values.reverse.map{ |s| "#{s.name} #{s.version}" }
  27 + expects(:choose_from_list).with(anything, choices).returns([choices.first, 0])
  28 +
  29 + spec_chosen = get_spec(test_gem)
  30 +
  31 + assert spec_chosen.eql?(mock_specs_hash(test_gem).values.reverse.first)
  32 + end
  33 +
  34 + private
  35 +
  36 + def test_gem
  37 + "#{@word_one}_#{@word_two}"
  38 + end
  39 +
  40 + def gemset
  41 + ['', '-', '_'].map { |joiner| "#{@word_one}#{joiner}#{@word_two}" }
  42 + end
  43 +
  44 + def mock_specs_hash(testee)
  45 + @mock_specs_hash ||= gemset.inject({}) do |result, spec_name|
  46 + unless spec_name == testee
  47 + result[spec_name] = stub(
  48 + :name => spec_name,
  49 + :version => stub(:>= => true, :to_s => '0.1'),
  50 + :sort_obj => [spec_name, stub(:>= => true), -1]
  51 + )
  52 + end
  53 + result
  54 + end
  55 + end
  56 +end

No commit comments for this range

Something went wrong with that request. Please try again.