How to use bundler gem binaries in path - ruby

I just started using bundler for gem packaging in vendor/. The problem is with certain gems (like rspec and cucumber) that have binaries. The binary path that is under my_app/vendor/gems/ruby/1.8/...cucumber-0.6.2/bin/ is not in my path, therefore when I go to run cucumber i get command cannot be found.
What is the easiest way to execute the bundled gem binaries from within the app rather than adding a large number of folders to my path?
Thanks

Newer version of bundler have an "exec" action. So for cucumber it would be:
bundle exec cucumber

OK, so symlinking was in fact a daft idea. This question did get me thinking though, and I found this: http://litanyagainstfear.com/blog/2009/10/14/gem-bundler-is-the-future/
Bundler will also dump gem executables in your Rails.root/bin directory. This means you can then use bin/rake, for example.
so, from the Rails root, does bin/cucumber work?

Related

Is there a preferred tool to edit Gemfiles?

I have a project that depends on Ruby to do something. I need to tell these people to install bundler, create a Gemfile (or update an existing one) and then run bundler install.
To be very clear, these people do not care about Ruby, they don't know what Ruby is and they don't need to know what Ruby is.
Currently my documentation is:
Run this command in terminal:
gem install bundler
Create a new file name Gemfile and add these contents:
source 'https://rubygems.org'
gem 'lightning_sites'
Or if there is already a Gemfile then edit that file and add the line gem 'lightning_sites' at the bottom.
Go back to the terminal and run:
bundle install --path vendor/bundle
I would like to replace the documentation for step 2 and preferable replace it with a command line. Is there a tool that ships by default with Ruby or bundler that accomplishes this?
If you want to avoid bundler you need to force-bundle all your dependencies inside your application. This is only really practical if none of your dependencies have compiled extensions, so if they're all pure Ruby you'll be able to do it.
What you end up doing is a bundle install --path gems/ for example, then package up everything including that directory as a deployable application. You may want to make a script that performs this step and creates a .zip file of the final result for distribution purposes.
This is a heavy-handed approach, so it's best to do this only if absolutely necessary.
You don't have to use Bundler to install gems, Ruby provides the gem command for installing gems individually.
You could simply run: gem install lightning_sites --install-dir lightning_sites and in whatever Ruby script is using the gem, programatically modify your GEM_PATH using Gem.paths before the require statement to include that install directory.

Ruby program gems

I wonder whether Gemfile is a rails-tied file or not. If I have something as:
require 'json'
in my ruby file, the user tries to run it, and he doesn't have that gem, what will happen? How do I make sure that someone's computer knows what gems to install before running my script? I know bundle install exists in Rails, but what about outside Rails?
Gemfile isn't a Rails-tied file. It's the file that you can use any Ruby-based project with bundler gem installed. Once you've run bundle install command, another file Gemfile.lock is generated or updated if exists. By the way, gems you'll use are linked with locations in your computer/server you're developing.

Rakefiles and Gemfiles

So I understand the basic concepts of a Rakefile and a Gemfile (rakefiles are basically just dependency lists and shell commands to compile and link the application while gemfiles specify gem version dependencies for an application), but how should I use these in unison? More specifically: I have a project for a project at my Uni that I am going to use Ruby 1.8.7 and JRuby 1.7 on with a set of gems I have installed through RVM on my local machine.
The problem is is that my application must compile and run without error on the computer lab at my Uni and the Uni doesn't have JRuby on these machines. I can install onto my shell account, and I did that with JRuby to get my application to compile but now I am required to type in the full path to the jruby installation to compile my project (something like /usr/home/usrname/blah/jruby-1.7.0) and I feel that is annoying and I would like something simpler (and learn at the same time).
The machines at my Uni have Rake installed and I was wondering if it would be possible for me to set up a rake file to do the call to JRuby and all the gems I need (specified in the gemfile with their respective installation paths). Is this possible? Or am I thinking of rakefiles and gemfiles in the wrong way?
Thanks!
The Rakefile is used to set up dependencies for running Rake within the context of your project. You have correctly described the purpose of the Gemfile.
Rake is an independent program from JRuby. It will be included in the bin folder of your JRuby install. By the time you reach your Rakefile, you are already in the context of the wrong version of Rake (the one included with the regular Ruby install).
I think what you are looking for is just a modification to your path. Include your JRuby bin folder in your path, and that will allow you to specify which version of Rake you want to use.
For the JRuby version:
jruby -S rake your:task
Or just the regular Ruby version:
rake your:task

Making an executable GEM from a script

I started developing ruby lately and I really like it, But I feel somehow lost. I developed a script that does "whatever" and that scripts requires many gems like nokogiri and colorize. I now want to deploy the script, so after reading a while I found many people saying that deploying as a gem is the best approach. So my question is simple? Is there any tool that I can use to create a gem of my script file and include all the gem dependencies(nokogiri) in the new gem?
I am using ubuntu!
Thanks alot
Building a gem consists of basically creating a simple directory structure for your script, and a special file known as a gemspec that will list all its dependencies. That gemspec can be used with rubygems to create a gem file (*.gem), which can be installed using rubygems or uploaded to rubygems.org for public consumption.
There are several tools that automate part of this process. A relatively simple one is the Bundler gem, which will both take care of dependencies during development, and make it easy for you to package your gem. This article contains enough information to get you started with gem development using bundler.
The best way to make a gem is to use the bundler program to build a skeleton:
# bundler gem (gem_name)
bundler gem geil_gem
This will create a template gemspec file and give you the basic structure needed for your gem to have a setup command, a console and be ready to build into a working gem (both found in the bin folder of your skeleton project). From here you can add a command binary or build out the gem as a library using the lib directory or start with tests by adding rspec to the gemspec and creating a test folder.

Manually adding a Ruby Gem

I am trying to install the mechanize gem that is supposed to work with 1.9 from here: http://github.com/kemiller/mechanize but I do not know how to add it manually.
I am using Windows, I could just copy the folder to the gems directory, but how do I initialize it?
I'm not sure I understand the problem. gem install mechanize doesn't work? It produces version 0.9.3 for me, which matches the gemspec of the library you linked to.
EDIT: you're on 1.9. I knew that. Disregard my hasty post, not familiar enough with Windows to offer any help on building the extensions.
I would use the bundler gem using the command gem install bundler. This will create a file called Gemfile in your project directory where you can put your dependencies for the specific project that you are working on. In the Gemfile, you will need to specify gem mechanize. If you want a specific version include ~> VERSION after. After, run the command bundle install. This will install the gem you want and use it in your project.

Resources