How to create a gem
From WhyNotWiki
Aliases: How to make a gem, Packaging gems, Gem creation
Contents |
[edit] The steps
- Write your gem specification
- Build your gem with a rake task
- Test installing your gem
- Publish your gem
[edit] Test installing a gem
> sudo gem install --local pkg/OurExtensions-0.0.1.gem --rdoc # So you can test that the rdoc installs properly --test # So you can ensure that all the tests pass still --force # In case it's already installed Attempting local installation of 'pkg/OurExtensions-0.0.1.gem' Successfully installed OurExtensions, version 0.0.1 Installing RDoc documentation for OurExtensions-0.0.1... [runs tests for you...]
(Hint: You can always do --install-dir ./some_local_dir/ if you don't have sudo.)
This should huncover any installation problems that your users might have. If you specified an incorrect autorequire file in your gemspec, for example, you will find out about it here.
And if you want to clean up afterwards...
> sudo gem uninstall OurExtensions Attempting to uninstall gem 'OurExtensions' Successfully uninstalled OurExtensions version 0.0.1
[edit] Publish your gem
Put it on RubyForge!
...
[edit] Is there a way to have RubyGems execute some 'post-install' script after it installs your gem?
No, not that I know of.
What would you want your 'installer' script to do?
- Copy files to locations that RubyGems doesn't know about (like /home/tyler/.vim or /usr/share/vim/vimfiles in the case of vim-ruby)
- Add a line to the user's ~/.bash_profile or equivalent
- Create a ~/.your_gem_name_rc file
- ...
Since RubyGems can't do it for you, you'll have to either
- Instruct your users how to install/configure your gem (the steps that come after gem install)
- Provide an installer script so they only have to run one additional command after the gem install
[edit] post-install script idea 1
It looks like that's the route vim-ruby chose to go... Their installation directions go like this:
# gem install vim-ruby --remote # vim-ruby-install.rb
vim-ruby-install.rb is an executable that RubyGems installed for you and placed at /usr/bin/vim-ruby-install.rb (or equivalent) so it should be in your path already.
If you create an installer script, keep in mind:
- Give it a unique name. You don't want to conflict with/overwrite any other files in /usr/bin ...
bin/_svn_command_post_install
#!/usr/bin/env ruby
require 'fileutils'
if ARGV.include?('--dry-run')
include FileUtils::DryRun
else
include FileUtils::Verbose
end
bin_dir = File.expand_path(File.dirname(__FILE__))
chmod 0755, Dir["#{bin_dir}/*"]
path_command = "export PATH=`ls -dt --color=never /usr/lib/ruby/gems/1.8/gems/svn-command* | head -n1`/bin:$PATH"
system "grep gems/svn-command ~/.bash_profile || " +
"echo '#{path_command}' >> ~/.bash_profile"
[edit] post-install script idea 2
/usr/lib/ruby/gems/1.8/gems/railsbench-0.9.1/INSTALL
Since gem packages don't support running postinstall scripts, you will need to make the scripts executable by running sudo railsbench postinstall
[edit] Adding your gem's executable directory to user's path
/usr/lib/ruby/gems/1.8/gems/railsbench-0.9.1/INSTALL
Alternatively, add the railsbench script directory to your search
path. The exact place can be found running
railsbench base
which prints the script directory path. Another option is to run
eval `railsbench path`
> railsbench path PATH=/usr/lib/ruby/gems/1.8/gems/railsbench-0.9.1/script:$PATH > railsbench base railsbench is installed in: /usr/lib/ruby/gems/1.8/gems/railsbench-0.9.1
/usr/lib/ruby/gems/1.8/gems/railsbench-0.9.1/bin/railsbench
RAILSBENCH_BASE = File.expand_path(File.dirname(__FILE__) + '/..')
case real_cmd
when 'base'
puts "railsbench is installed in: #{RAILSBENCH_BASE}"
exit
when 'help', 'readme'
File.open("#{RAILSBENCH_BASE}/README").each_line{|l| puts l}
exit
when 'path'
puts "PATH=#{RAILSBENCH_BASE}/script:$PATH"
exit
