Bundle install runs from incorrect directory - ruby

I'm building a simple thor based generator for some internal projects, and can't seem to get bundle install running from the correct directory.
As I run the new [APP_NAME] function, it should create the directories and files, then run bundle install to install the gems required for the application.
The source of the generator function:
def create
puts "Creating application #{name}"
directory 'application', "#{name}"
Dir.chdir("#{Dir.pwd}/#{name}") do
puts `bundle install`
end
end
And the console output from running the command that calls this create method:
$ bundle exec bin/my_gem new test_app
Creating application test_app
create test_app
create test_app/Gemfile
create test_app/Guardfile
create test_app/README.md
create test_app/app/controllers
create test_app/app/helpers
create test_app/app/models
create test_app/app/views
Using thor (0.14.6)
Using my_gem (0.0.1)
Using bundler (1.1.3)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
As you can see, it is running bundle install but it's running it in my current directory (thor, bundler, my_gem), as opposed to the test_app directory (guard, guard-coffeescript, guard-less, and others) .
Running other commands such as ls or pwd give the expected results:
Gemfile
Guardfile
README.md
app
and
/Users/davidlumley/Development/Gems/my_gem/test_app
Not sure if it makes any difference, but I use RVM for managing my rubies.

Sounds like your app is already using bundler and you have a bundler-inside-bundler problem. Try this:
Bundler.with_clean_env do
puts `bundle install`
end
I'm guessing what's happening is that your outer bundler sets the BUNDLE_GEMFILE env variable to your app's Gemfile, and then your inner bundler ends up inheriting it.

Please try this i will sure helps you.
rvm gemset create app_name
rvm use ruby-1.9.2#app_name
Ruby version what else you used.
then install bundler gem
gem install bundler.
Then bundle install and run server..

Related

Ruby - Cannot use locally installed gem

I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. I have a Gemfile, the content of it is this:
gem 'password_generator', path: '~/workspace/gems/password_generator'
I installed it locally, like this:
bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
It looks like it's installed locally:
bundle info password_generator
* password_generator (0.1.0)
Summary: Simple password generator
Homepage: https://github.com/jedrekdomanski/password_generator
Path: /home/jedrek/workspace/gems/password_generator
When I try to use it
~/workspace/rubysamples/app/password_reset.rb
PasswordGenerator.generate
I get an error
uninitialized constant PasswordGenerator (NameError)
What am I doing wrong? Am I missing anything?
Here's my gem repo: https://github.com/jedrekdomanski/password_generator
I also tried pointing to my repo and branch in the Gemfile
gem 'password_generator', git: 'git#github.com:jedrekdomanski/password_generator.git', branch: 'master'
but I get the same error message uninitialized constant PasswordGenerator (NameError)
There are potentially two issues. The first is how you are starting Ruby and the second is how you are requiring your module.
First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile. The Gemfile is only used when you're using bundler, so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb. This causes bundler to read your Gemfile and execute Ruby in that context.
Second, you're not properly including your module in your Ruby file. Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; it just makes the module available for use. You might wonder, "Why don't I have to do that in Rails?" Because Rails does that for you automatically via config/application.rb.
Given these two issues, the correct way to accomplish your goal is to configure your app as follows:
First, create your Gemfile:
# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'
Second, create your password_reset.rb file:
# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate
Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock:
⇒ bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Fourth, run bundle exec ruby password_reset.rb and observe the output:
⇒ bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK
Everything works because:
Ruby is started with Bundler
Bundler reads your Gemfile and makes the gems available to Ruby
Your app requires the module from the gem before attempting to use the module

How to install a gem to the current folder

I want to use a Ruby gem locally (not install it for the entire machine) for use in a single script. I know how to install gems with Bundler with a Gemfile and bundle install. But for a simple script, this seems overkill to set up bundler.
Is there a way to install a gem to a subfolder of my script and use it, similar to the way npm installs Node.js packages in node_modules?
Here's what I have tried so far.
gem install -i ruby plist installs the plist gem in ruby/gems/plist-3.1.0
I tried to require it in my script extract.rb by doing require './ruby/gems/plist-3.1.0/lib/plist but that fails with require: cannot load such file: plist/generator (plist/generator.rb is required by lib/plist.rb).
Ruby 2.0 on OSX
You can bundle install to a different location with the --path option, for example:
bundle install --path vendor/bundle
Also see http://bundler.io/v1.1/bundle_install.html
If you don't want to involve Bundler, just install your gems locally as in your example and then set the GEM_PATH env in your script before your require, e.g.:
#!/usr/bin/env ruby
ROOT = File.expand_path('..', __FILE__)
ENV['GEM_PATH'] = File.join(ROOT, 'ruby')
# or to just append to
# ENV['GEM_PATH'] += ":#{ File.join(ROOT, 'ruby') }"
require 'plist'
assuming your script is in the same folder as the ruby folder (otherwise adjust the filepath accordingly).
You can do it by creating gemset for particular application. follow these steps for that -
$ rvm gemset create <gemset_name>
It will create a gemset for currently selected ruby version.
you can check currently selected ruby version by this command -
$ rvm list
Then navigate to your app directory by cd into it.
now execute this command -
$ rvm use #<gemset_name>
Now whenever you install any gem it will be installed in current gemset which is being used not for the entire machine.
Make sure - you run gem install bundler in newly created gemset so it will not raise error when you will run bundle install.

Make ruby script use local gems, instead of common

I'm deploying my rails project to production server. There is only 1.9.3 version of ruby (I developed on 2.1.2) so there is few compatibility problems in gems versions. More over, I downloaded one of gems to vendor/gem_name and made necessary fixes in its sources, so I need to use exactly my version of that gem and, as you understand, It's not possible to update it.
in Gemfile
require 'gem_name', :path => 'vendor/gem_name'
So after cloning project to server I run
bundle install --path vendor/bundle
and it created bundle directory in vendor folder with gems versions, needed to me, inside it.
After that I tried to run fetching script to fill db with some data by command
ruby *_fetch.rb
inside *_fetch.rb:
require 'gem_name'
And it fails with error
You have already activated gem_name older_version, but your Gemfile requires
gem_name newest_version. Using bundle exec may solve this. (Gem::LoadError)
from /usr/local/rvm/gems/ruby-1.9.3-p374/gems/bundler-1.3.5/lib/bundler/runtime.rb:19:in `setup'
So how could I specify script to require my edited local gem?
Run it with bundle exec That's exactly what bundle exec is for.

Bundler does not install from stash private repo, but reports that it does

My bundle file doesn't appear to be pulling down a gem from a private repository properly.
Inside of my Gemfile, I have:
group :internal do
gem 'private', git: 'ssh://git#internalserver.org:<port>/gems/private.git'
end
This runs, and verbose logging produces:
Updating ssh://git#internalserver.org:<port>/gems/private.git
Cloning into '/Users/<username>/.rvm/gems/ruby-2.0.0-p247/bundler/gems/private-ddec73caf50f'...
done.
When I navigate to /Users/<username>/.rvm/gems/ruby-2.0.0-p247/bundler/gems/, I see the correct repository cloned properly, with a gemspec with the correct name.
When bundler is finished running, gem list does not show the private gem. It produces an error when I attempt to require it.
I tried deleting the Gemfile.lock file in the repository and rerunning, and that did not work. All of the public gems in the Gemfile install correctly.
Relevant version numbers / software:
Bundler version 1.3.5
rvm 1.23.14
ruby 2.0.0p247
Atlassan Stash
Git gems are a Bundler-specific extension to Rubygems. The gem command does not know about these, so they're not listed by gem list. You can run bundle show to see the list of gems that are recognised by Bundler, which will include git gems.
To require the gem, you'll need to be sure that the load path is set up correctly by Bundler. There are three ways to do this:
Call require 'bundler/setup' in your app. This is typical for Rails apps. More on Bundler.setup
Call bundle exec <command> to run the command. This is more common when running commands from a gem, such as rake or rspec. More on bundle exec
Create binstubs for commands that you run frequently.
See http://bundler.io/v1.5/git.html for more information on git gems.

Bundling local gem (that I'm developing) does not seem to include lib directory (using rvm)

I'm trying to develop a gem locally, and have installed it with Bundler.
My Gemfile looks like this:
source "http://rubygems.org"
gemspec
And my gemspec is a standard gemspec file.
I can install the gem with 'bundle install' in the directory, and i see the local gem and all it's dependencies install:
bundle install
Using rack (1.3.4)
Using tilt (1.3.3)
Using sinatra (1.3.1)
Using {my gem} (0.0.2) from source at .
Using bundler (1.0.21)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
However, when I do a 'gem list', my gem is not included in the list of gems - which is my guess as to why my bin directory does not appear in the path. Is there a way to test a local gem and include it in the list of installed gems using bundler, so that the bin directory properly works?
Easiest way to get rid of bundler: command not found: {your bin executable}:
git add bin/* # git-ls-files will now list your bin executables.
bundle install
# No git-commit necessary.
bundle exec <MY_BIN_EXECUTABLE>
gem list shows your system installed gems, not the gems in your Bundle (this are often the same but not always--as in this case). When you're using Bundler, you should always execute gem executables with bundle exec so that Bundler can set up the environment for you. So, if you have a binary called, for example, mygem, you should use bundle exec mygem.
See more info at Bundler's site or in the manpage.
[Edit]
Also be sure that your gemspec includes a bin directory! Common convention is to create a directory called bin at the same level as your lib directory, put your binaries in there, and then add this as the directory in your gemspec. If you don't do this, Bundler won't expose your binaries!
I had this problem too.
Make sure the executables and default_executable lines don't contain 'bin/'. Then:
git add add . # You can be more precice if you want.
git commit -m "My lousy commit message."
bundle install
bundle exec <binaryname>

Resources