Sitemap
Able

We are a product acceleration studio—a new model, built to design, develop, and deploy the right technology faster.

Press enter or click to view image in full size

Creating a Gem Generator

4 min readMar 1, 2018

--

Hi! In this tutorial, we’ll be creating a Gem Generator. What in the world is that? Well, it is a series of tasks that in the end will generate the scaffolding expected for a RubyGem to be functional.

As we move through this tutorial, we will be using Thor, a handy toolkit for building command-line interfaces.

Thor improves Rake’s functionality by making it easier to add arguments and options to command line tools. The generators that ship with Rails use Thor since Rails 3.0.

Keeping this wiki at hand will allow you to find Thor reference materials faster.

Let’s get to it.

First, we need to make sure we have thethor gem installed.

gem install thor

We need to create a folder for our Generator and then, let’s go ahead open it.

mkdir newgem — cd newgem

In the new folder, we’ll create the file where our Generator will be written.

touch newgem.thor

We'll be using a combination of Thor::Group which allows us to create a sequence of tasks and Thor::Actions so we can use predetermined actions on our tasks.

Next, we create a folder for the needed Gem templates, and in it, create said templates.

mkdir templates — cd templates — touch template.gemspec.tt — touch main.rb.tt

touch template.gemspec.tt — will be the Gemspec template
touch main.rb.tt — will be the Main Gem file template

Get Mariella Miranda’s stories in your inbox

Join Medium for free to get updates from this writer.

Ok, so we have our files, now we’ll define what tasks we need to generate Gems on demand.

  1. Define our Generator Root File.
  2. Create our Gem folder.
  3. Create the gem.gemspec file in our gem's folder (using the proper template).
  4. Create the lib folder inside our gem's folder.
  5. Create the gem.rb file inside lib, which will be our gem's main file.

Now, it is time to define those tasks in newgem.thor

This will be our basic structure of tasks.

Let’s test this! We can see if it works by executing thor newgem hello in our terminal.

This should generate an empty folder with name hello

It works! So, now let’s keep building our tasks. Our next task to be defined is the gemspec. We’ll start with the modification of the template.gemspec.tt file, to have the information needed for a Gem to be recognized as such.

Now that our template file has the information we need, let’s go back to newgem.thor and finish defining our tasks. We’ll start with create_gemspec:

Next, let’s move on to the create_lib_folder task

Then finish defining the tasks with the create_main_rb one.

We must modify our template main.rb.tt, so it works with our Generator and add the following to our file.

We can retest our Generator, but hold on… first, we need to erase the previous Gem we generated.

rm -fr hello (removes old hello folder) — thor newgem hello

This should create our new Gem folder with the Gemspec file and the Lib folder with the Gem file. At this point, we’ve already fulfilled the initial premise for this exercise. Now, let’s spruce it up.

So, one of the things that will help us in the future with our Gems, is that they’re Git Repositories. That's why we’ll add a task to our generator. In newgem.thor add the following:

We can add an option for user license. Again, in newgem.thor we add the following before initializing git in our folder.

The cool thing is we can also make an executable command for our Gem. To do so, we’ll add this line at the start of our newgem.thor file #!/usr/bin/env ruby. Then, we’ll need to initialize our class with Newgem.start at the end of our file.

Also, we need to change the name of the file just to newgem, for that we'll carry out the following in the terminal: mv newgem.thor newgem

To initiate our Gem, first we’ll make sure the system knows it’s an executable file with chmod +x newgem in the terminal. After that, we can run our executable as many times as we want to with ./newgem nameofthegem.

So, that’s it for this first post. Stay tuned!

--

--

Mariella Miranda
Mariella Miranda

Written by Mariella Miranda

Web designer. Web developer. Mom. DIY enthusiast. Crazy cat lady.

Able
Able

Published in Able

We are a product acceleration studio—a new model, built to design, develop, and deploy the right technology faster.