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.
Related
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
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.
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
In a Rails application, if one of the gems included in a Gemfile depends on another one that I want to use, do I need to include the second one in a Gemfile?
Say, I use a cucumber-rails gem. By running bundle dependency cucumber-rails, I can see that it depends on 'database_cleaner' and factory_girl. Do I need to include the former two into the Gemfile or not?
Thanks!
No, you don't need to explicitly require them in your Gemfile. Bundler will detect the dependencies, include them in your Gemfile.lock and install them.
If you use bundler then it will install any dependency when you install a gem
you can see the dependencies in gemfile.lock
but note that having a dependency installed is not always good to assume that the dependency installed is the gem version you want. The dependency installed is the version required for the gem to work. If you need factory-girl on the latest version (or any other version), then it is best to include it in the gemfile
I had a look around and apparently you should not use the bundle dependency cucumber-rails install function:
https://makandracards.com/makandra/12741-don-t-update-gems-with-bundle-update-unless-you-re-feeling-lucky
Whenever developing gems, I don't see any reasons why Gemfile is not directly inspected for dependencies.
Indeed, why use a .gemspec file in order to list them ? Is there a real benefit ?
Well that's because the Gemfile isn't a file from Rubygems, but a file from Bundler. So the Rubygem developers would have to extend their used files in order to support Gemfile. Since there already is the .gemspec file, there is no valid reason why they should. (there are enough gems which do well without a Gemfile)
In fact, it is recommended to use this as the only contents of the Gemfile of gems:
source 'https://rubygems.org'
gemspec
It will instruct bundler to use the .gemspec file as the authorative source of gems.