How to add dependency of a local gem to a rails plugin/engine, in .gemspec file
Asked Answered
E

3

35

I had tried in this way:

 s.add_dependency 'gem', :path => '../gem'

like add gem in the gemfile, but it doesn't work, and will cause this error:

/Users/chenqh/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/requirement.rb:81:in `parse': Illformed requirement 
Eleni answered 3/12, 2012 at 7:31 Comment(0)
K
25

It's likely not possible to add local dependencies because other users will not be able to access the gem as it is local dependent and hence of no use after publish. Instead of that, Add remote dependency in your own plugin's gemspec.

Steps -

1) Open gemspec file of your own plugin in vendor/plugins/my_plugin/ and add before the keyword end:

s.add_dependency('will_paginate', '~> 3.0.pre2')

(In this example I have used will_paginate required dependency of my_plugin)

2) Now go in your rails app and edit Gemfile, add:

gem 'my_plugin', path: 'vendor/plugins/my_plugin'

3) Now in rails app root do:

bundle install

And dependency of my_plugin (will_paginate in this case) is installed.

Kayleen answered 3/12, 2012 at 12:52 Comment(2)
thanks. So, it's impossible to add a gem write by myself, only if i publish this gem. Actually, i want to overwrite part a published gem, and make it work with my plugin. Maybe i could put the gem in the vendor folder of my_plugin.Eleni
where is will paginate obtained from?Wayland
G
52

While developing 2 gems, gem1 and gem2, requiring that gem1 locally depends on gem2 is quite handy.

You can't do this in your gemspec, however, you can do so in your gem's Gemfile!

# Gemfile
source "https://rubygems.org"

gem 'gem2', :path => '../gem2'

# Specify your gem's dependencies in gem1.gemspec
gemspec

And then in your gemspec, require your gem like you would normally if that gem was already published:

# gem1.gemspec
spec.add_runtime_dependency "gem2"

Just make sure you don't accidentally push that Gemfile change!

Gravure answered 14/10, 2013 at 15:17 Comment(4)
I'm having trouble with this. If 'gem2' isn't installed with the gem command, I get require': cannot load such file -- gem2 when doing require 'gem2' in gem1's code.Selfassertion
Make sure that the path to gem2 in gem1's Gemfile is correct. Use an absolute path if necessary.Gravure
Thanks for the msg and answer. I took another whack at it and needed to add rake as a dependency.Selfassertion
Thanks for the comment - this is really dumb IMO from the Ruby developers. If I gem install locally my own gem, other projects I'm locally developing should not have to specify that it is a local gem - it should scan the local cache first before reaching out to a gemserver.Windgall
K
25

It's likely not possible to add local dependencies because other users will not be able to access the gem as it is local dependent and hence of no use after publish. Instead of that, Add remote dependency in your own plugin's gemspec.

Steps -

1) Open gemspec file of your own plugin in vendor/plugins/my_plugin/ and add before the keyword end:

s.add_dependency('will_paginate', '~> 3.0.pre2')

(In this example I have used will_paginate required dependency of my_plugin)

2) Now go in your rails app and edit Gemfile, add:

gem 'my_plugin', path: 'vendor/plugins/my_plugin'

3) Now in rails app root do:

bundle install

And dependency of my_plugin (will_paginate in this case) is installed.

Kayleen answered 3/12, 2012 at 12:52 Comment(2)
thanks. So, it's impossible to add a gem write by myself, only if i publish this gem. Actually, i want to overwrite part a published gem, and make it work with my plugin. Maybe i could put the gem in the vendor folder of my_plugin.Eleni
where is will paginate obtained from?Wayland
W
5

Sometimes you want to embed one gem into another gem, nevermind why. You can reference one gempec from another to completely encapsulate a local gem.

require "rubygems"

embedded_gemspec = Gem::Specification::load("path/to/internal.gemspec")

Gem::Specification.new do |spec|
  spec.name          = "gem_that_contains_another_gem"
  # spec.whatever,   = whole bunch of other info

  #                     our files               + other gem's files
  spec.files         = ['file1.rb', 'file2.rb'] + embedded_gemspec.files

  # our dependencies
  spec.add_dependency 'nokogiri'
  # other gem's dependencies
  embedded_gemspec.runtime_dependencies.each { |d| spec.add_dependency d }

end

EDIT: this appears to only work locally. If you try to install this gemspec from, say, a git repo, it won't know where to get embedded_gemspec (even though embedded_gemspec's dependencies come in just fine).

Wild answered 16/11, 2016 at 20:13 Comment(1)
Do we have a way to add complete gem instead of dependencies of internal gemspec.Welcher

© 2022 - 2024 — McMap. All rights reserved.