17 September 2013

Ruby Gem Creation Cheatsheet using Bundler and Github

There are some good tutorials out there, but this is my cheatsheet for how I go about creating a gem template and then populating it with my favorite things. I want to use github for the gem repo and use bundler for the gem structure. I used to use jeweler in the past, but I think bundler makes it simpler. The examples I'm using are for the xsrf-token gem I'm creating. All of my projects exist in the directory "projectspace" on my linux workstation.

Step 1: Create the github repo

I go direct to github.com website and use the wizard there to create a public repository.
  • choose a name:
    xsrftoken
  • write a description:
    XSRF token generation and validation gem with accompanying faraday-middleware
  • select the license:
    MIT
  • select the .gitignore option:
    none -- i'll clone from an existing project
  • create repository

Step 2: Clone the new repo

  • copy the new repo SSH url at github.com
    (look on the repo page, it'll be somewhere; currently on mid-right side)
  • clone the new repo onto the workstation (snippet & output below)
$ cd projectspace
$ git clone git@github.com:dsaronin/xsrftoken.git
Cloning into 'xsrftoken'...
remote: Counting objects: 4, done
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4/4), done.

Step 3: Bundler creates skeleton gem

  • copy over typical gitignore and rvm ruby specifiers
$ cp kinokero/.gitignore xsrftoken/
$ cp kinokero/.ruby-version xsrftoken/
  • have bundler generate gem skeleton; skip creating any cloned github/rvm/gitignore files
$ bundle gem xsrftoken
 create xsrftoken/Gemfile
 create xsrftoken/Rakefile
 create xsrftoken/LICENSE.txt
 conflict xsrftoken/README.md
Overwrite /projectspace/xsrftoken/README.md? (enter "h" for help) [Ynaqdh] n
 skip xsrftoken/README.md
 conflict xsrftoken/.gitignore
Overwrite /projectspace/xsrftoken/.gitignore? (enter "h" for help) [Ynaqdh] n
 skip xsrftoken/.gitignore
 create xsrftoken/xsrftoken.gemspec
 create xsrftoken/lib/xsrf/token.rb
 create xsrftoken/lib/xsrf/token/version.rb
Initializating git repo in /projectspace/xsrftoken

Step 4: Copy in existing content; tweak template files; commit

I like to tweak the .ruby-version, .gitignore, readme, etc as needed. If I have any exisiting content, I'll copy that over.

$ git add .
$ git commit -am 'gem skeleton initiating commit'
$ git push origin master -u

Step 5: Build and publish

To build a local copy of the gem (not distribute publicly):

$ gem build xsrftoken.gemspec

To add to RubyGems.org. After creating an account on the site, visit your profile. You will find a command you need to run to authorize your computer. For example:

$ curl -u cloud8421 https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials

After running that, then you can publish to RubyGems by (assuming version 0.0.1):

$ gem push xsrftoken-0.0.1.gem

references

http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/