Skip to content

How to package your app in a gem

royw edited this page Aug 16, 2012 · 3 revisions

First, use your favorite gem building helper. I like to use jeweler to create my app's file structure and Rakefile.

Create app using jeweler

➤ jeweler --summary "The Greatest App Ever" --description "The world's greatest app ever developed" the-greatest-app-ever
	create	.gitignore
	create	Rakefile
	create	Gemfile
	create	LICENSE.txt
	create	README.rdoc
	create	.document
	create	lib
	create	lib/the-greatest-app-ever.rb
	create	test
	create	test/helper.rb
	create	test/test_the-greatest-app-ever.rb
Jeweler has prepared your gem in ./the-greatest-app-ever

Add your .rvmrc

➤ echo "rvm use 1.8.7@the-greatest-app-ever --create" >the-greatest-app-ever/.rvmrc
➤ cd the-greatest-app-ever
====================================================================================
= NOTICE                                                                           =
====================================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory       =
= This is a shell script and therefore may contain any shell commands.             =
=                                                                                  =
= Examine the contents of this file carefully to be sure the contents are          =
= safe before trusting it! ( Choose v[iew] below to view the contents )            =
====================================================================================
Do you wish to trust this .rvmrc file? (/Users/roy/Projects/github/mine/the-greatest-app-ever/.rvmrc)
y[es], n[o], v[iew], c[ancel]> y

Edit your Gemfile

 cat Gemfile
source "http://rubygems.org"
gem 'ramaze'
group :development do
  gem "rdoc"
  gem "bundler"
  gem "jeweler"
  gem "rcov", ">= 0"
end

Install gems

➤ bundle install
Fetching gem metadata from http://rubygems.org/......
Fetching gem metadata from http://rubygems.org/..
Using rake (0.9.2.2) 
Using bundler (1.1.5) 
Installing git (1.2.5) 
Installing rack (1.4.1) 
Installing innate (2012.03) 
Installing json (1.7.4) with native extensions 
Installing rdoc (3.12) 
Installing jeweler (1.8.4) 
Installing ramaze (2012.04.14) 
Installing rcov (1.0.0) with native extensions 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

➤ gem install rdoc-data; rdoc-data --install
Fetching: rdoc-data-3.12.gem (100%)
rdoc-data is only required for C ruby 1.8.7 or 1.9.1.

rdoc-data is required for JRuby.

To install ri data for RDoc 2.5+ run:

  rdoc-data --install

Successfully installed rdoc-data-3.12
1 gem installed

Create ramaze skeleton in lib directory

➤ cd lib
➤ ramaze create the-greatest-app-ever
The application has been generated and saved in the-greatest-app-ever

add script to start your web app

➤ cd ..
➤ mkdir bin
➤ cd bin
➤ touch the-greatest-app-ever

And inside your app start script something like:

 cat the-greatest-app-ever 
#!/usr/bin/env ruby

require File.expand_path("../lib/the-greatest-app-ever/app", File.dirname(__FILE__))

Ramaze.start(:adapter => :webrick, 
             :port => 7000, 
             :file => File.expand_path("../lib/the-greatest-app-ever/start.rb", File.dirname(__FILE__)))

Write the greatest app ever!

Note you may not have write access to where your gem is installed on the target system so your app should locate write files under /tmp or $HOME ($HOME/.the-greatest-app-ever is a good location).

Package as gem

Using your gem building helper to build and package your gem.

Install gem

Run

To start your application, simply invoke the startup script:

➤ the-greatest-app-ever
Clone this wiki locally