Introduction to TSS - small tool for generating source code from templates
Sometimes, working with the code involves a lot of boilerplate snippets and source files. This often reduces creativity in development and causes developer to think more about framework API or language syntax rather than to think more about the problem they are trying to solve.
TSS is tool which enables creation and usage of various templates for the code. It enables parametrized types which means a greater scope of possible boilerplate snippets can be reused. For instance, you can create template class and parametrize its name and fields in template.
Installation
TSS can be installed by cloning this repository or via pip:
pip install tss
or, if you have downloaded source code from repository:
python setup.py install
basic usage
So, let’s take a look how to reuse existing templates. TSS supports system of multiple template sources. In such a way, you can download multiple set of existing templates from third parties and always keep track of where they come from.
There is default set of templates provided by this source code in templates directory, to add it to TSS use:
tss remote add default ./templates
first parameter after “remote add” command is name of source which can be whatever you want and second one is path to template collection.
Now, we can create simple java class from our template collection:
tss create java/class
You should get editor opened with special settings file for you to fill in. When you set all parameters, you can save the file and close the editor and you will get new java class file created in directory in which you invoked tss command.
Now, let’s investigate further file which is opened in your editor. First of all, there are variables which you can set and every variable has its type. For instance,
you can see name : string = "MyClass"
which means variable name is set to value “MyClass” and is of type string. As user of template, you should never change variable names and their types, only their values. In this manner, you are able to set parameters used in template. For instance, in our case, we have name of the class
which we can set.
There are 3 fundamental types: string, integer and boolean.
There are also complex types like dictionaries and lists. If you see type like [string]
it means this variable is list of strings. You should set it like this:
SomeParameter : [string] = ["StringA", "StringB", "StringC"]
also, we you see {a : string, b : string}
it means it is dictionary with variables a and b and both of them are strings.
Common thing to see is list of dictionaries. You can also see in our java/class example where we can define fields of the class. For defining dictionary values, you can use syntax: (a="StringA", b="StringB")
.
In our java class example, we can define fields by specifying field name and type.
Creating your own templates
All source files are written in Jinja2 template system. You can read more about Jinja2 here.
First of all, you will need to make your template collection. This is normal directory on your file system. Then, you should create directory which is name of language used and then inside it directory with name of your template. Example would be:
- mytemplates
|- java
|-|- mytemplate
Template has to contain file named params.tps
. This file has syntax you already saw while creating code from template using tss create
.
All other files are part of template content and will be used to generate code. You can reference variables using jinja2 syntax. For instance, you can see how java/class template looks like in templates directory.
As you can see, we can use all jinja2 capabilities including loops and branching as well.
You can also parametrize names of files and directories inside the template. Just insert triple underscores before and after variable name. For instance, file containing above content can be named ___name___.java
and TSS will rename it to name specified in name
variable during code generation.
If at least one variable in file name is unspecified or empty, file won’t be created.
NOTE: do not forget to add your template collection to TSS using tss remote add <NAME> <PATH>
command.
Conclusion
TSS can be used to generate code boilerplate for projects as well as for generating starting point for various documents (LaTeX, markdown etc…).