Monday, January 21, 2013

Auto install required Ruby gems via script

When you are writing Ruby scripts for yourself it's cool until you need to distribute it. It might happen (as usual it is) that your scripts requires one or more gems. It means that the consumer of your script should additionally setup all required gems. But it looks ugly. There is a nice simple solution which provide simple way for automatic gem install.
Let's imagine that we are using some gem in our script, to be concrete, I've picked artii gem as a sample:
begin
  gem "artii"
rescue LoadError
  system("gem install artii")
  Gem.clear_paths
end

require 'artii'

a = Artii::Base.new
puts a.asciify('Blog!')
Let's run it:
$ jruby text.rb
Successfully installed artii-2.0.3
1 gem installed
  ____  _             _
 |  _ \| |           | |
 | |_) | | ___   __ _| |
 |  _ <| |/ _ \ / _` | |
 | |_) | | (_) | (_| |_|
 |____/|_|\___/ \__, (_)
                 __/ |
                |___/

[D:\MyProjects\temp]
$ jruby text.rb
  ____  _             _
 |  _ \| |           | |
 | |_) | | ___   __ _| |
 |  _ <| |/ _ \ / _` | |
 | |_) | | (_) | (_| |_|
 |____/|_|\___/ \__, (_)
                 __/ |
                |___/
As you can see, when we run script 1st time it installs artii gem, but when we run this script 2d time it identified that gem has been installed already.

Links:

2 comments:

  1. Greetings! Very helpful advice in this particular article!
    It's the little changes that produce the most important changes. Thanks for sharing!
    Review my homepage ; diets that Work for women

    ReplyDelete
  2. i was having trouble with distributing gems. now, thanks to you. it is going to be easier.

    ReplyDelete