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.
We need to create a folder for our Generator and then, let’s go ahead open it.
In the new folder, we’ll create the file where our Generator will be written.
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.tttouch 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.
- Define our Generator Root File.
- Create our Gem folder.
- Create the
gem.gemspecfile in our gem's folder (using the proper template). - Create the
libfolder inside our gem's folder. - Create the
gem.rbfile insidelib, which will be our gem's main file.
Now, it is time to define those tasks in newgem.thor
Let’s test this! We can see if it works by executing thor newgem hello in our terminal.
helloIt 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 helloThis 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!






