Ruby gems in lib - spare tire principle - ruby

I'm working on a console ruby application (not rails!) I will be installing this application on several machines. I was wondering if there is a way i can build it so i dont have to install the gems i'm using for the app on each machine. I'd like to be able to just copy the directory to each machine and run it. Ideally, i'd like to put the gems in the lib folder or something and reference them from there, so i don't have to even install them on my dev machine. Is there a way to do this?
In .net, we call this the "spare tire" principle.
thanks,
Craig

How about using bundler?
Then you can include a Gemfile that specifies all the necssary gems and just run "bundle install" on each machine to pull them down.
If you really want to bundle them with the app run "bundle package" and the gems will be stored in vendor/cache.

You could take the same approach as rails allows and "vendor" your gems. This involves creating a new directory (rails uses vendor/gems) and unpack the gem into this directory, using gem unpack.
You then configure your load path to include all of the sub-folders below that.
Edit
You can configure your load path by doing something like this
Dir.glob(File.join("vendor", "gems", "*", "lib")).each do |lib|
$LOAD_PATH.unshift(File.expand_path(lib))
end

Related

How can I make a gem perform an action on install?

Update:
Mainly, I need a .bat file to be placed in RUBY_HOME/bin, similar to how rails.bat gets placed in bin when the Rails gem is installed
I'm building a gem that can be used for generating automation test frameworks for testing web application written in any language, with any framework.
I need one file within my gem to be run every time the gem is installed. This file generates either a .bat or .sh file.
I have already built this script and it works great, I just need it be run each time the gem is installed using bundler. Is this possible?

How to develop a Ruby GEM without having to install it first?

I'm developing a GEM that I've forked and I'm trying to modify it slightly for my app.
I'm finding it difficult and time consuming because for every change I make I have to
uninstall
build
re-install
run the app
Is there an easier way of which doesn't require repeating all steps above?
To use it in some app using bundler
If what you mean is for using it in a app to test it / use it, you can just specify a path for your gem or even point to a git repo in the Gemfile http://gembundler.com/gemfile.html
Like
gem "mygem", :path => "~/code/gems/mygem"
To use it as a standalone gem. i.e: like rspec or rake that can run outside of an app.
Just specify the path to your gem binary when running the gem command, like:
$ ~/path_to_my_gem/bin/mygem some args
If you can execute inside your gem directory (i.e: the command does not create files in the current directory, or needs any specific files from the current directory), just do this:
$ ./bin/mygem some args
Note that this last one is just for future reference, I think it's not applicable in the OP context.
use require_relative to include your files:
require_relative 'yourgem/yourclass'
This is the documentation for the function.

Self contained ruby "binary"?

[Ruby Noob]
I have a small (command line) utility written in Ruby, which requires a few gems. Is there a way to create a self contained bundle of my program such that I can run it on another machine that has Ruby installed (but not necessarily the gems)?
FWIW, the target machine runs Linux/Ubuntu.
You can use the gem bundle http://gembundler.com/
With bundle you create a Gemfile in your project root - a text that contains all your dependencies, very similar to Maven concept
In order to fetch all your dependencies simply tun
bundle install
The only issue is that you need to have the bundle gem itself installed, so you are back with the chicken-or-Egg problem :-)
I've used:
http://www.erikveen.dds.nl/rubyscript2exe/
before, but it was a while ago. Seemed to work okay for simple programs.
You can download it here:
http://rubyforge.org/projects/rubyscript2exe/

Pain-free private gems without bundler?

I want to distribute a sysadmin utility inside our company that depends on not-yet-released gems (say, the github master for fog). We have a private GitHub organization account already, and we all have ssh keys for it.
But: While Bundler can install gems with a simple
gem "sysadmin", :git => "git://github.com/ourorg/sysadmin.git"`
there's no way I can find to do that with bare RubyGems. And people are going to want to install other gems on their own machine, so I wouldn't want to force everyone to use Bundler for their home directory.
I can think of a few solutions, none ideal:
Make a "sysadmin" project that has a Gemfile, have everyone clone it, and have them cd into that project dir to run the utility.
Have them manually download and build the gem on their system every time it changes.
Use a third-party gem-hosting repo, and count on security-by-obscurity to prevent outsiders from finding it. Ick.
Set up a secure internal-only server to run gem server or the like. We currently don't have one (everything's in the cloud), and I'd like to avoid setting one up just for this.
There must be a better way.. what is it? Is there a way to set up an old-style gem source in a github repository?
Use a third-party gem-hosting repo, and count on security-by-obscurity to prevent outsiders > from finding it. Ick.
I wanted to suggest Gemfury, but it sounds like you're not interested in 3rd-party gem-hosting solutions. Can you tell me more about your security concerns?
Disclaimer: I work on Gemfury
there's no way I can find to do that with bare RubyGems
This is covered in the documentation. Use the :path option in your Gemfile:
gem "nokogiri", :path => "~/sw/gems/nokogiri"
And there's no reason this would interfere with their home directory or their own gems. They're not all going to want to put your utilities in their home directory anyway. Let them put the files in whatever directory they want, then either tell them to add it to their $PATH or write a Rake task that automatically creates symlinks in e.g. /usr/bin for them.

How to develop a gem in staging environment?

I am trying to hack through a forked gem (buildr). As such I cloned it from github and began to butcher the code. The official gem is installed on my system (under /usr/lib/ruby.../gems/buildr...). There is an executable which I need to use in my dev process - buildr.
Now I want the buildr executable and the library to point to my forked repo and not the default gem installation. This would be for this gem only. As such, the changes I make against the forked repo is usable directly for testing and so forth.
I would guess I need to load my library prior to the system gem loading. Can somebody recommend the best way to do so?
I did something similar for work when the Spreadsheet gem broke backward compatibility. I put the previous versions code in it's own module and just renamed the gem my-spreadsheet and installed that (I really wanted some of the features of the new gem but I also didn't want to rewrite all my previous code at that point).
If it's just a binary you want to override you could always do some PATH magic, setting the directory of your binary first and thus make sure you always override. But personally I'd prefer making my own copy with a new name and installing that.
you could bump the version in the gemspec for your fork. Then when you install your version of the gem, it will use your (newer) version by default.
change buildr.gemspec
#...
spec.version = '1.3.4.dev'
#...
Then
$ gem build buildr.gemspec
$ sudo gem install buildr-1.3.4.dev.gem
and it should work.

Resources