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

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?

Related

How do I execute a ruby script when gem install is called?

I'm making a Ruby wrapper for a C Library that doesn't really have a system wide installation(the libxxx.a linkable library is distributed). During development, I was able to just keep this .a file in a local folder and refer to it using the C extension code. However this would be cumbersome for anyone who just wants to type gem install into their terminal. Since there is no system wide installation, I would need to execute an intermediate script that downloads the right distribution and then puts it in a generic folder within the gem directory itself.
To be clear, when someone types gem install, a tarball would get downloaded from an ftp, unpacked into the gem folder, and then gem install would proceed normally. If it helps, the C library in question is CSPICE

Pack gem with app

So I have a simple app/script wich depends on a single gem and what I want to do is to pack it with the app so there is no need to install it each time on every new machine it needs to run.
I tried with bundler pack command but the problem is that this way I still need to have bundler installed which I can't assure.
So what a made was grab the code I need from the gem and then used it.
My question is:
How can I pack the gem with my app without any dependency and is my current solution is polite?
Cheers
from setup.rb(or wherever the entry point of your application) add this line:
Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib| require lib }

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/

How execute a self contained rake build?

We have an application which is compiled using Rake (on windows). We have a new requirement that one of our clients needs to compile the source code in their own environment using a bat file.
So I need to find a way to execute a rake build without installing anything on the host environment (i.e. everything required to do the build needs to be in the source directory, ruby, gems, etc...) from a batch file.
Anyone have any clues how I could get started with this?
Download and install ruby to a folder inside your project (do not add it to your PATH). After go to this folder and delete any "uninstall" file. Go to the folder again with the console (cmd and then use cd path\to\ruby\folder) and run gem install ... to install everything you need. After add a .bat file to run your app. Something like:
#echo off
rubyfolder\bin\ruby.exe myscript.rb
This is a fully portable ruby installation, you can put it in any computer and it will work as well. (I use it as a portable ruby in my pendrive to let me play everywhere with ruby!)
PS.: rake is a script from bin, you can open it with:
rubyfolder\bin\ruby.exe rubyfolder\bin\rake

Ruby gems in lib - spare tire principle

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

Resources