Gemfile gem installation and gemspec dependencies - ruby-on-rails

I have an app whose Gemfile requires a gem that is also dependent on another gem that is currently found on github.
So
app/Gemfile reads
gem "my-gem", :git => "git://github.com/MyCompany/my-gem.git"
my-gem/Gemfile reads
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
my-gem/my-gem.gemspec reads
spec.add_dependency "my-gem-2"
When I run bundle inside of app I get an error that it can't find gem my-gem-2 which is required by my-gem; however, if I put the following line
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
inside of app/Gemfile then it works fine.
This practice seems redundant as I wouldn't think I'd have to add gem dependencies of another gem into my parent app. Is there something I'm doing wrong here that myapp can't find my-gem-2?

This is just the way it goes - Gemfile dependencies within gems are just for when you're developing that gem. Only the gemspec gets evaluated when the gem is used elsewhere (and gemspecs have no concept of git dependencies), hence only dependencies in the gemspec apply.
So: you will need to have both git references within your app's Gemfile.

As stated in the gem specification, the list of gems that you provide through add_dependency will be use to make sure those are already installed in the system during the installation process (i.e gem install). So this line:
my-gem/my-gem.gemspec reads spec.add_dependency "my-gem-2"
Will trigger the verification of whether or not the gem is installed in the system, but it will not trigger any automatic installation of such gem, as Bundler would do.
This other line (inside of your gem):
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
Specify that a gem should come from a git repository with a .gemspec at its root.
For more details: Gems from git repositories

Related

What kind of dependencies must be in Gemfile and in gemspec?

Cant figure out where rails gem must specify its dependencies? In Gemfile on in gemspec? The generated Gemfile has these description:
# Declare your gem's dependencies in malibu.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.
But I still can understand. Can you help me? My thanks
If you are developing a new gem, then you'll want to declare all of your production-ready gems in the .gemspec using add_dependency.
As for the Gemfile itself, as the comment states, it is used for adding dependencies which are still in development (i.e. not released). For example, if you want to use the latest edge version of Rails, you'd have to specify that dependency with the git or github option (e.g. gem "rails", github: "rails/rails"). These options are only available in the Gemfile, not the .gemspec.
In general you want to always put your dependencies in the .gemspec and only use the Gemfile if you need to.

Path to dependency of a gem in Gemfile

In a rails project, I have a Gemfile which specifies a gem dependency as follows:
gem 'darshan', '1.1.4'
To be compiled, this particular gem requires the location of the corresponding C library, which may not be in a "standard" location such as /usr/lib. Manually, one would install this gem as follows, for example:
gem install darshan -- --with-darshan-dir=$HOME/local
How can I add this "with-darshan-dir..." to the gem command in the Gemfile, and more importantly, how can I make it such that the user himself provides this location as optional parameter when calling bundle install?
I checked the gemfiles manual (http://bundler.io/man/gemfile.5.html) but couldn't find anything to do that.
Thanks

Gem located on github don't require gems from rubygems

I have this line in my Gemfile:
gem 'gem_name', '1.0.27', :git => "git#github.com:company/gem_name.git"
gem_name is a gem that I built stored on github, and in one of the gem code files I wrote:
require 'cssmin'
(I have more gem requirements in the gem code files).
When I run bundle in my app, everything works fine and gem_name is installed properly
But when I run rails s I get this error:
cannot load such file -- cssmin
Do you know why I cant require gems from rubygems in my gem code files that are stored on github?
When writing your own gem, you should include all its requirements in a .gemspec file. That way, the gem program can make sure it installs all the dependencies when the user runs gem install gem_name, and your code won't crash when it runs the require 'my_dependency' line.
If you want to use other libraries in your code, you need both parts. The Gemspec file makes sure dependencies are installed; the require statement loads it when your code runs.
In this case, you should use the add_runtime_dependency method in your Gemspec. Something like this:
Gem::Specification.new do |s|
# Required attributes omitted
s.add_runtime_dependency('cssmin') # If you don't care which version is installed
s.add_runtime_dependency('nokogiri', '>= 1.2') # If you want a version greater than or equal to 1.2
s.add_runtime_dependency('kaminari', '~> 1.1') # If you want a version between 1.1 and 1.9.
end
You can read more about declaring dependencies in the Rubygems documentation.

How to make edits to a gem and have that take affect on heroku?

I have a gem to which I want to make a few edits.
I have opened the gem using
bundle open gem_name
Made the edits and they work fine on development.
What do I need to do in order to make the changes take affect on heroku.
There is nothing to commit locally.
Your local gem is modified, but during deployment the one on github gets installed.
What you could do is to find the gem project on github, and fork it.
Then you can apply all your custom mods and commit into your gem repo.
Once you're done, remember to point to the right repo in your Gemfile:
gem your_gem, , :git => "https://github.com/your_user_name/your_gem.git"
You can vendorize the gem like this:-
gem unpack my_private_gem --target vendor/gems
and then in your Gemfile
gem 'my_private_gem', :path => "vendor/gems/my_private_gem-VERSION"

how to include all project gems into the gemfile?

I work as a developer supporting several apps built in Rails, often I need to install gems that are not included into the gem file.
Last day I build a project and when I try to run it on another computer I experienced some issues with missing gems, a lot actually and I didn't know where to get a list of all the missing gems or how to install them.
The question is, is there a way to include all the gems that the project needs into the gem file so next time someone try to run it on another computer it will be enough to use the comand bundle install.
You need to include the Gems in your Gemfile, and then run bundle install on ANY new machine in order to install those Gems and their dependencies. For example:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'jquery-rails'
gem 'mongoid'
gem 'devise'
gem 'cancan'
With this example, all dependencies of rails such as Active Record, Action Pack and so on will be installed when bundler installs rails. The same for the remaining gems and their dependencies.
If you are planning, and it appears that you are, to spend much time with rails, you should really read up on Bundler.

Resources