A yeoman generator, ruby-cmd

Starting a project usually means (at least for me) firing the same commands over and over, usually checking documentations that are quite easy to forget, all while feeling quite miserable about not being able to do it at the first shot.

Researching on the topic, you sure can't miss Yeoman. It's the standard tool when it comes to scaffolding apps (not always web related): I searched for nice generators multiple times, but never found one that suited my needs and didn't require some tweaking.

In my opinion, even when very complete and well written, many Yeoman generators published tend to be "overloaded" with dependencies that limit the choice of the end user and lack some of the features that I deem essential.

Let's take a very common Ruby generator as an example.

Take a look at generator-ruby-gem: while with it you can easily create a Ruby gem with rspec support, you can at the same time disable them and go a bit against the language conventions.

If you need a command line program to be exposed by the gem, this generator will include thor, to allow creation of self documenting binaries.

Although the Thor gem really is awesome, it should be responsibility of the end user to choose for such an option: and what really boggles me even more is that there is nothing there that will test for the correct execution of such a program.

With this in mind, and having coded a couple of private gems lately, I decided to try to write my own small generator to speed up my future development.

My generator

The generator I wrote is called ruby-cmd, and has the following features:

  • Sets up unit testing with rspec
  • Sets up binary feature testing with Cucumber and Aruba
  • Sets up coverage with simplecov
  • Gem is bundled and exposes rake tasks to launch tests

All of these dependencies are development ones, and only provide what should be a common ground to start development: any other decision is left for the user.

By the way, if you never used Aruba to test your binaries, give it a look:

My generator provides 3 sample scenariors. Select your binary and type in your input:

Feature: provided sample
  Scenario: Typing your input
    Given I run `bin/gem` interactively
    When I type "Name"
    Then I close the stdin stream
    Then output should contain exactly:
    """
    0.1.0
    Hey, let us know your name:
    Hello, Name!
    """

Or pass your input as argument when running your binary:

  Scenario: File passed as argument
    Given a file named "file.txt" with:
    """
    Name
    """
    When I run `bin/gem file.txt`
    Then output should contain exactly:
    """
    0.1.0
    Hey, let us know your name:
    Hello, Name!
    """

Or you can pipe it in throuh STDIN:

  Scenario: File passed via STDIN
    Given a file named "file.txt" with:
    """
    Name
    """
    When I run `bin/gem` interactively
    And I pipe in the file "file.txt"
    Then output should contain exactly:
    """
    0.1.0
    Hey, let us know your name:
    Hello, Name!
    """

So, if you need to bootstrap a gem, and you also want a nice testing platform, install this generator with:

npm install -g generator-ruby-cmd

and then run it:

yo ruby-cmd

Happy coding!