How to update ruby gem dependency? Must I rebuild it? How? - ruby

I have a gem already installed locally, and I wanna add a new dependency into it. I worked with below steps:
Open the gem installation folder as below:
/usr/local/rvm/gems/ruby-2.1.0/gems/nesta-0.10.0
Add new dependency below into gemspec file (nesta.gemspec)
s.add_dependency('stacktracer', '>= 0.0.1')
Append gem 'stacktracer' into Gemfile
Add require "stacktracer" into Rakefile
Run command bundle update nesta
Run command bundle install
Check Gemfile.lock, I already found the new dependency like below:
I have next dependencies:
DEPENDENCIES
debugger
mr-sparkle (>= 0.0.2)
nesta!
rack-test (= 0.6.1)
rspec (~> 2.14.0)
**stacktracer**
test-unit (= 1.2.3)
webrat (~> 0.7.3)
But when I met two problems here:
run command gem dependency nesta, I did not find stacktracer dependency list.
I added require "stacktracer" into app.rb, then run the application, it failed due to below error:
/usr/local/rvm/gems/ruby-2.1.0/gems/nesta-0.10.0/lib/nesta/app.rb:4:in `require': cannot load such file -- stacktracer (LoadError)
Could any one help me to tackle the problems? Is there any approach that could manage to update gem dependency successfully?

You are not supposed to edit the unpacked gems under .../rvm/gems/ruby-2.1.0/gems. If you need to update the alien gem, you are to follow this approach:
Fork the gem source at github
Clone the repo to your local drive
Update whatever you want + update version
Set the dependency in your Gemfile to either use unpacked version or your git:
gem 'nesta', :path => '.../nesta.git'
gem 'nesta', :git => 'git://...'
Don’t forget to eliminate the original gem presence over your system. Hope it helps.

Related

Bundler: using shared gem when it exists, rather than downloading from gem server

Say, I have Gemfile like following.
source "GEM_REPOSITORY"
gem 'gem_A'
# gem_A has no additional dependency
gem 'gem_B'
# gem_B depends on gem_B_1 and gem_B_2
When I run bundle install, I want Bundler to do the following.
If a gem already exists in "local system-wide gems", it copies the gem from local.
If a gem doesn't exist in local, it looks for GEM_REPOSITORY.
I looked for some related articles, and found some of likely-answers like
Ruby Bundler multiple sources in Gemfile
SOURCE PRIORITY
But none of the above looks like the answer for me.
Using source repository priority does't work. Because in the example above, if dependent gem (say, gem_B_1) exits in local but the target gem (gem_B) doesn't exist in local, it'll download both of above from the remote repository.
Are there any work around for doing this?
If not, don't you guys think it's necessary considering the cost of the implementation and the effect?
This is the current behavior.
When running gem install, directly or via bundle install, gem will first build a dependency graph with all the needed gems. If the gem is found locally it will use it, otherwise it will try to download it from the specified source.
If you want, try it yourself.
bundle gem gem_a
bundle gem gem_b
cd gem_a
vim gem_a.gemspec
add
spec.add_dependency 'multi_json', '~> 1.10.1'
or any dependency you want to the gem and run bundle install.
cd ../gem_b
vim Gemfile
and add
gem 'gem_a', path: '../gem_a'
then run
bundle install --verbose
you will see that the multi_json or whatever dependency of gem_a uses the local version and does not download anything.
This is of course also true for gems from remote sources.

How do I include a commit in a ruby library that hasn't been added to the gem yet?

Specifically, this commit has been merged into mongomapper master to fix a bug that is causing my application to crash:
https://github.com/mongomapper/mongomapper/pull/572
However, it hasn't been released in a new gem. Is it possible to include it prematurely or do I have to wait until it's released? I'm on heroku with a Gemfile.lock that specifies version.
My gemfile.lock currently reads:
mongo_mapper (0.13.0)
activemodel (>= 3.0.0)
activesupport (>= 3.0)
mongo (~> 1.8)
plucky (~> 0.6.5)
I don't know about Heroku, but if you want to use a git version of a gem that is not on Rubygems yet, you must add the git path to your Gemfile.
In your Gemfile change:
gem 'mongomapper'
To:
gem 'mongomapper', :git => 'https://github.com/mongomapper/mongomapper.git'
Or if you're on a more modern ruby, you can also use:
gem 'mongomapper', github: 'mongomapper/mongomapper'
And of course after that you must run bundle install or bundle update mongomapper.
Since the Ruby code isn't compiled, if you have the notes for that patch you can always manually add those changes to the lib directory of the mongomapper gem, although I can't advise against this enough - it's undesirable to change the source code, especially underneath an installed gem.
Once you go down this road you can't really count on any support for related issues until you are updated to a newer version or reverted to the original files.

Ruby Gem not install dependency

I created a Ruby Gem and published it. I tried downloading it and I keep getting cannot require my dependent gem. The code is at https://github.com/wallerjake/toolshed and the gem in question is httparty. The error that I am getting is
~/.rvm/gems/ruby-2.0.0-p353#sullivan_cotter/bundler/gems/toolshed-46404c5af06d/lib/toolshed.rb:2:in `require': cannot load such file -- httparty (LoadError)
I have updated my https://github.com/wallerjake/toolshed/blob/master/toolshed.gemspec to use add_dependency instead but that doesn't seem to be helping. Could it be conflicting with other Gems?
The version on rubygems is still the old one with only development dependencies. Worked fine when I downloaded and built you gem from Github.
Yes sorry I solved this by adding them as a dependency not a development dependency.
spec.add_dependency "httparty"
spec.add_dependency "json"
spec.add_dependency "pivotal-tracker"
You have specified development dependency, but call the rake form binary, just put rake gem dependency into usual add_dependency, and remove ::gem call to:
bin/toolshed:
require 'rubygems'
require 'toolshed'
require 'rake'
One again question: for what do you need the binary?
The toolshed.gemspec seems correct. How do you call it? Remove the installed gem with gem uninstall toolshed including binary. Make sure that the bundle install, and then call to bundle exec bin/toolshed.rb is correct after patching the dependencies.
After that generate the gem with gem build toolshed.gemspec, and install the gem with gem install toolshed-0.0.4.gem. Make sure that binary works. Only then publish the gem.

Making a Ruby project that is not a gem (or some other kind)

I'm working on a project currently that I don't want to be a gem (or some other kind of project). How would I go about setting it up so that I can still have the same compatibility requirement abilities as a gem (e.g. Gemfile dependencies) but simultaneously not be a gem (or some other kind of project)?
You have to actually try to build a gem so this is easy!
to use bundler without Rails, a gem, whatever just create a directory
mkdir my-non-gem-project
cd my-non-gem-project
install bundler
gem install bundler
and initialize your Gemfile
bundle init
that will create a Gemfile for you and you can add to it and run bundle to install the dependencies from it
The simplest way to use bundler in your project would then be to open your main app file and add
require 'bundler/setup'
Bundler.require
This will require all of the gems you have in your Gemfile in the file this is added to. I am pretty sure that this file must be in the same directory as your Gemfile. More information here
Have fun with your Ruby project!

Installing local Ruby gem, using bundler

Our Gemfile is as follows:
source "http://rubygems.org"
gem 'langrove', :path => "/home/user/gems/"
The .gem file is in place in "/home/user/gems/ItIsHere.gem"
But when we run bundle install, the following error is received:
Could not find gem 'ItIsHere (>= 0) ruby' in source at /home/user/gems.
Source does not contain any versions of 'ItIsHere (>= 0) ruby'
Thank You
One solution is to run gem unpack on the specific .gem you want to use
Then reference the unpacked version.
I normally keep things relative to my project
eg:
gem 'awesomelib', :path => '../awesomelib'
You didn't specify the gem version.For this reason you got error.so you will change the code like this.
gem 'remotipart', '1.0.5', :path => "/home/user/gems/"
And check your folder :path also.
first
rails new app_name --skip-bundle
then
use bundle install --local
This will install gems from local.

Resources