Introduction
skema is a command line utility for expanding templates. It can be used in a number of ways. Examples include:- fast prototyping applications in frameworks that require a lot of boilerplate code;
- adding copyright statements to source files;
- automating repetitive coding tasks.
Of course, there’s nothing in skema that you can’t find in your favourite text editor’s templating facility; skema just makes it easier if you happen to like the command line.
skema uses the ERB templating library included with ruby, but you don’t need to know ruby or ERB to use it. See the ‘Creating templates’ section below for more details on how to add your own templates.
Installing
sudo gem install skema skema -i skemaconfig . filename:.skemarc
and answer the questions.
Tutorial
skema comes with a couple of predefined templates. For example, the ‘gpl’ template adds a copyright statement at the beginning of a C++ file. To try it out, pick a source file test.cpp and run:
$ skema gpl test.cpp -> ./test.cppNow take a look at test.cpp and you’ll notice that a nice GPL header with your name and email has been added. But what if you want to use a different name and/or email for the license? No problem, just set some arguments on the command line:
$ skema gpl test.cpp "name:John Doe" email:john.doe@example.com
The ‘gpl’ template, however, is somewhat peculiar in that it acts on a single, already existing file. Most of the times, you want to expand a template on a new directory, populating it with files. Let’s take the kapp template, that expands to an empty KDE4 application:
$ skema kapp myapp ERROR: Missing argument title
What happened? Well, skema complains because it requires an argument and it has no way to infer it without asking.
$ skema kapp myapp title:MyApp ERROR: Missing argument name
Now the name is missing:
$ skema kapp myapp title:MyApp name:myapp -> CMakeLists.txt -> main.cpp -> ./myappui.rc
Ok. It worked! Note that skema has an interactive mode, useful when you don’t know (or don’t remember) the required args:
$ skema -i kapp myapp email [p.capriotti@gmail.com]: version [0.1]: title: MyApp name: myapp author [Paolo Capriotti]: -> CMakeLists.txt -> main.cpp -> ./myappui.rc
Just press enter at the prompt when you want to use the default.
Creating templates
Let’s create a class template, that expands to a C++ class file.
- Create a
classdirectory in$HOME/.skema - Inside the
classdirectory, create a file calledclass.rbwith something like:template :class => nil
- Create a ‘templates’ directory inside ‘class’
- Create a file called ‘h’ with the following content:
<%= filename @class.downcase + ".cpp" %> #ifndef <%= @class.upcase %>_H #define <%= @class.upcase %>_H class <%= @class %> { public: <%= @class %>(); virtual ~<%= @class %>(); }; #endif - Create a file called
cpp. Content left as an exercise :) - Try out your new template with
$ skema class . class:Test
you should then get two files:test.handtest.cpp.
How to submit patches
Fork my repository at github.
License
This code is free to use under the terms of the MIT license.
Contact
Please send comments, complaints, new templates, bug reports and feature requests to Paolo Capriotti. skema does not have a bug tracker, yet, sorry.