adamsanderson / open_gem

Gem Command to easily open a ruby gem with the editor of your choice. (For a more flexible solution, see Qwandry)
http://endofline.wordpress.com
173 stars 23 forks source link

undefined method `source_index' for Gem:Module #17

Open alexch opened 10 years ago

alexch commented 10 years ago

In the latest RubyGems (2.2.1), this plugin fails:

ERROR:  While executing gem ... (NoMethodError)
    undefined method `source_index' for Gem:Module

This is a shame since I love using gem open (at least when I'm not using RubyMine, which opens the gem source for you with a command-click). I'm pretty sure it's because the RubyGems team refactored the internals, but it's definitely still possible to find the location of a gem since they added gem which. So all we have to do (knock wood) is look in the source to see what which is calling and update open_gem to use it instead of source_index.

adamsanderson commented 10 years ago

Hey Alex, sorry I sat on this for a while. If you happen to find a fix, I'll pull it, especially if it is backwards compatible with previous versions of RubyGems.

Personally, I would recommend trying out Qwandryhttps://github.com/adamsanderson/qwandry/, which is more general and will open files in the standard library as well as gems. That's where I'm focussing my time these days ;)

On Fri, Feb 7, 2014 at 6:04 AM, Alex Chaffee notifications@github.comwrote:

In the latest RubyGems (2.2.1), this plugin fails:

ERROR: While executing gem ... (NoMethodError) undefined method `source_index' for Gem:Module

This is a shame since I love using gem open (at least when I'm not using RubyMine, which opens the gem source for you with a command-click). I'm pretty sure it's because the RubyGems team refactored the internals, but it's definitely still possible to find the location of a gem since they added gem which. So all we have to do (knock wood) is look in the source to see what which is calling and update open_gem to use it instead of source_index.

Reply to this email directly or view it on GitHubhttps://github.com/adamsanderson/open_gem/issues/17 .

alexch commented 10 years ago

Alright, I'll take a stab at it. (Unit testing against different versions of rubygems will be a nightmare...)

Maybe a new "gem open" plugin should be part of qw? Or a new open_gem should just delegate to qw?

alexch commented 10 years ago

Proof of concept is easy: in common_options.rb:

def get_spec(name)

  # dep = Gem::Dependency.new(name, options[:version])
  # specs = Gem.source_index.search(dep)

  specs = Gem::Specification.find_all { |s|
    s.name.include? name
  }

Actually building the gem -- not to mention running tests -- remains elusive.

...and here's some (ugly) code that honors the options:

  specs = Gem::Specification.find_all do |s|
    (options[:exact] ? s.name == name : 
      (name.is_a?(Regexp) ? s.name =~ name : s.name.include?(name))) and 
     (options[:version] ? options[:version].satisfied_by?(s.version) : true)
  end
adamsanderson commented 10 years ago

Actually Alex, if we make that update, we can just bump the minimum supported version of rubygems, so don't worry about prior versions as long as the gemspec gets updated.

On Fri, Feb 21, 2014 at 6:51 AM, Alex Chaffee notifications@github.comwrote:

Proof of concept is easy: in common_options.rb:

def get_spec(name)

dep = Gem::Dependency.new(name, options[:version])

specs = Gem.source_index.search(dep)

specs = Gem::Specification.find_all { |s| s.name.include? name }

Actually building the gem -- not to mention running tests -- remains elusive.

...and here's some (ugly) code that honors the options:

specs = Gem::Specification.find_all do |s| (options[:exact] ? s.name == name : (name.is_a?(Regexp) ? s.name =~ name : s.name.include?(name))) and (options[:version] ? options[:version].satisfied_by?(s.version) : true) end

Reply to this email directly or view it on GitHubhttps://github.com/adamsanderson/open_gem/issues/17#issuecomment-35736587 .

alexch commented 10 years ago

Looks like the find_all method goes back at least as far as 1.8.0 (May 2011) so that shouldn't ruffle too many feathers. But is it even possible to check the rubygems version? I suppose we could put in a check against Gem::VERSION but I don't think rubygems itself lets you specify a minimum rubygems version...

I dusted off an old 1.9 ruby install and I'm having much better luck building it. I'll submit a PR shortly.

adamsanderson commented 10 years ago

I think you should be able to update the gemspec with s.rubygems_version = %q{1.8.0}. Barring that perhaps we can specify an explicit dependency on rubygems 1.8.0 with the add_dependency method in the gemspec. I should tear out Jeweler as well, it doesn't really add anything...

On Fri, Feb 21, 2014 at 11:03 AM, Alex Chaffee notifications@github.comwrote:

Looks like the find_all method goes back at least as far as 1.8.0 (May 2011) so that shouldn't ruffle too many feathers. But is it even possible to check the rubygems version? I suppose we could put in a check against Gem::VERSION but I don't think rubygems itself lets you specify a minimum rubygems version...

I dusted off an old 1.9 ruby install and I'm having much better luck building it. I'll submit a PR shortly.

Reply to this email directly or view it on GitHubhttps://github.com/adamsanderson/open_gem/issues/17#issuecomment-35761899 .

alexch commented 10 years ago

rubygems_version is not a check; it's just recording what version created it (or installed it? docs are vague). add_dependency wouldn't work since rubygems is not itself a gem (rubygems-update is but there's no guarantee that will still be around after an update).

Aha! Looks like there's an (ahem) undocumented required_rubygems_version attribute now that does what we want.

alexch commented 10 years ago

https://github.com/rubygems/guides/issues/78 :-)