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

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

Related

Creating Ruby gems with same name executables?

I want to have a Ruby Gem that will have the same executable as another Gem.
When called with command args it will either do something, or pass the command on to the other Gem.
The first problem I have is that it isn't able to run two same named executables. I get this error:
Bundler is using a binstub that was created for a different gem. This is deprecated, in future versions you may need to bundle binstub yourgem to work around a system/bundle conflict.
How can I have Gems with the same named executables and ensure that the target one executes?
You cannot rely on Bundler or Rubygems to manage this for you. All it does it copy an executable that you specified in your gem spec to its bin/ directory.
The first problem you'll have is that the executable that runs may be dependent on the order in which the gems were installed which you can't guarantee.
Another problem that you'll have is that you cannot execute code on gem installation so you will be unable to run code that would try to automate this set up for people who install your gem.
I believe your gem should provide a non-conflicting executable. You can then supply post install instructions in your gem spec that are displayed to a user installing the gem, in the README, in a blog post, etc. you can tell the user that they need to set up an alias that points to your executable. In all shells that I'm aware of aliases will be executed before filesystem executables.
For the times when people want to bypass your alias and execute the original executable you can tell people to escape the command, e.g. \original-gem. That bypasses alias and function lookup in most shells and will allow users to have your super awesome version as the default (thru the alias) and a way to easily access the original.

How to automatically download .gem files, including dependencies, and save .gem's to one folder?

I work behind a firewall and I need some gems to automate some processes at work.
Problem:
Rubygems cannot automatically download gem dependencies so I have to manually download each gem from rubygems.org and install using the local copies. As you can imagine, this could take a very long time to find each gem dependency and manually download.
Ideal Solution:
I would like to automatically download all of these gems on another computer not behind a firewall, package the entire collection into one folder (no need to install, just download .gem files), and make that folder available to myself to download at work. The file just needs to contain every .gem file including dependencies.
Notes:
location: A solution in Ruby would be ideal! Remember, I can use gems on the secondary computer to create the solution for the primary computer where I cannot download gems remotely.
attempts: I have poured over documentation at rubygems.org, google searched, and more, but cannot find a solution. I can't seem to access the downloaded .gem files, they are unpacked and installed before I have a chance to incercept the file and save them elsewhere.
Bundler has a great way to do this: bundle package.
http://bundler.io/v1.2/bundle_package.html
The package command will put all your gems in the bundle into ./vendor/cache. You can then do whatever you want with that directory, such as copying it to another machine, or checking it into version control, or torrenting it. etc.

Packaging Compiled Binary /w a Ruby Gem

I am creating a small daemon, written in Ruby, which relies in part on a small binary utility compiled from C code. I want to package this as a gem and include this dependency along with it.
Essentially, this daemon will need to run commands such as ip addr add ... without requiring sudo, so I created a small C program to proxy those commands which must be compiled, chowned to root, and have the setuid bit set.
I would like to have the gem compile and install this dependency along with the daemon, but I am unsure how to do so. I understand extensions can be compiled via extconf.rb, but that is specifically meant for managing Ruby extensions, right? Would it be an ugly hack to have this compile and install a binary to /usr/local/bin or similar?
Does anyone know of an existing gem which does a similar thing which I can study as an example?
Here's a gem that packages the pdftk binary.
https://github.com/charliemaffitt/pdftk-heroku

how do I repackage a ruby gem with native extensions

I need to install a number of ruby gems (all with C extensions) on a production server which does not have any dev tools installed. I'd like to build gems on a dev server first and then repackage and install resulting native gems on production server.
However, there seems to be no standard methods to package gem with native extensions for redistribution. I am aware of rake-compiler, but none of the gems in concern works with it out of the box. Specifically, I am working with json-1.7.5, rb-inotify-0.8.8 and ffi-1.2.1 gems.
Any pointers on how to do this task or documentations on the subject are appreciated.
Using Jordan Sissel's fpm you can take various input archives (including gems) and compile and package them as (among others) DEBs or RPMs.
An example to compile the json gem into a deb package follows:
cd /tmp
fpm -s gem -t deb json
This will download the latest version of the json gem and create a rubygem-json-1.5.7-1.amd64.deb archive in /tmp which you can install on your server. Note that the compile box and the final server need to be rather identical. At least the distribution and bitness, the ruby version and its file layout, and the available loadable libraries should be the same. Basically all the constraints your upstream distribution deals with...
That said, in the long term I found it much easier to just install a compiler on the target servers and use rbenv or rvm on the server. For most small and mid-size installations, it's much easier to handle as you don't need to pre-compile and ship everything to your servers.
Hi You could do it with: gem-compiler
You need to tell RubyGems the filename of the gem you want to compile:
$ gem compile yajl-ruby-1.1.0.gem
The above command will unpack, compile any existing extensions found and repackage everything as a binary gem:
Unpacking gem: 'yajl-ruby-1.1.0' in temporary directory...
Building native extensions. This could take a while...
Successfully built RubyGem
Name: yajl-ruby
Version: 1.1.0
File: yajl-ruby-1.1.0-x86-mingw32.gem
It have well written documentations here:
https://github.com/luislavena/gem-compiler.
I am using it as well as we have own gem server. Just have to be careful with distribution because some gems compiled on wheezy won't work on jessie and so on.
You're going to have to build them on a system that's almost exactly the same for this to work. If you're linking against shared libraries that are in a different location or have slightly different versions it may not work at all. Sometimes you have some slack, where it will work with a range of versions, but this cannot be assured.
There's no way to package with native extensions for this very reason, there's just too many possible combinations of libraries.
You'll also need to make sure you're using the same architecture, including 32-bit or 64-bit as required.
Sometimes you'll get lucky and there's a package for your OS that can install these for you, but these won't work with rvm or rbenv.

Ruby cannot find sqlite3 driver on windows

I am trying to set up Ruby on Rails on windows. I am using the Flash Rails distribution that looks pretty good, but there is an issue with sqlite3. I found the threads telling me to install version 1.2.3, which installed fine. I'm using ruby 1.9.0, and every time I try and run a script (e.g. rake db:create) that uses the database I get an error message "no driver for sqlite3 found".
This apparently is a missing sqlite3.dll, but I have the dll in my %PATH%, and I have also tried copying it into the directory where I am running the script from, the directory where the sqlite3 ruby code lives.
Does anyone have any ideas? If possible I want all teh ruby stuff to be self contained so I can use it from a pen drive.
EDIT: To clarify, I already used gem install to install the ruby-sqlite3 gem - it is just non functional as it cannot find the sqlite3.dll (even though it is actually present in a directory on my %PATH%)
EDIT PART 2: After doing some more digging, the problem appears that ruby will not load the sqlite3_api.dll. I have copied it all over my filesystem, I just get a failure to read file. Other dll libraries in the same directory (e.g. zlib.dll) work fine!
I tried installing the dlls into system32, and that did not work either.
The problem put simply is that sqlite3-ruby 1.2.3 is not compatible with ruby 1.9. This is caused because ruby 1.9 does not use .dll files for c libraries it uses .so files instead. Additionally, since sqlite3_api.dll is written against msvcrt-ruby18.dll. This means that it specifically only will support ruby 1.8.*.
The good news is that there is a fat binary version that will support both ruby 1.8 and ruby 1.9. Uninstalling all former versions of sqlite3-ruby and then installing this one. (You may have to manually delete some versions the gem after uninstalling.) in order to install it use
install sqlite3-ruby --source http://gems.rubyinstaller.org
for more information see this website
Try installing the sqlite3-ruby gem:
gem install sqlite3-ruby
Something similar happened to me recently so I thought I'd update my answer.
For reference there's a sqlite3_api.dll file located in the gem's lib directory. Also the sqlite3.dll file needs to be reachable on the path. They are different files, the first is required by the gem to interface Ruby to C code, while the second contains the actual Sqlite implementation.
It's best to get the second file from the sqlite website and extract it to the Ruby\bin directory (as you shouldn't manually put DLL's into the windows or windows\system directories any more).
So for reference "sqlite3_api.dll" needs to be in:
Ruby\lib\ruby\gems\1.8\gems\sqlite3-ruby-1.2.3-x86-mswin32\lib
and "sqlite3.dll" needs to be on the path, possibly in:
Ruby\bin
As for the "driver not found" problem I would suggest trying the easy things first and making sure gems is installed correctly, up to date, and that the RUBYLIB and PATH environment variables are set appropriately. (System restart may be required to propagate the changes fully.)
Re this link
Download sqlitedll-3_6_10.zip and extract into ruby/bin!
Try going to sqlite.org download page and get the zipped up dll. Then put that in your c:\windows\system32 folder, that should allow Ruby to find it.
Restart your machine after running install sqlite3-ruby
To clarify, which gem are you using? sqlite-ruby or sqlite3-ruby?
They're part of the same project, but different releases. The key is that sqlite3 appears to have driver code included.
I assume you're attempting to use the first, since it's giving me the same error. If so, try switching.
Also.. How literal do you mean by this?
but I have the dll in my %PATH%
PATH=...;C:\sqlite\sqlite3.dll
PATH=...;C:\sqlite
The first will attempt to find C:\sqlite\sqlite3.dll\sqlite3.dll, AFAIK.
I use Ruby 1.8.7 (works with 1.9.1 too)
OS is WindowsXP SP3
Go to
http://www.sqlite.org/download.html
and Download file
sqlitedll-3_7_0_1.zip (265.19 KiB)
and unzip then we will get
sqlite3.dll
Copy sqlite3.dll to your bin folder
as C:\Ruby191\bin or C:\Ruby187\bin
then it works

Resources