codegen
A small tool to automatically generate code starting from one or more template file(s), and a csv file.
Contents
Installation
go get github.com/Abathargh/codegen/cmd/codegen
Build
This is how the binaries shipped in the release section are built:
git clone https://github.com/Abathargh/codegen
cd codegen
go test ./...
go build -ldflags "-s -w" ./cmd/codegen/
Pre-compiled binaries are available from the release section.
Get it as a library
# either clone everything and build it
go get github.com/Abathargh/codegen
How To
This tool is able to substitute data from csv files into template files containing
tokens in the #n# format, where n is a number greater or equal to 1. You could also use different
separators, '#' is the default one.
If you have a template file called templ.c, and a csv called data.csv like these:
#define test_#1# #2#
test,123
test2,456
test3,testing
You may call codegen like this:
codegen -t templ.c -d data.csv
This is what will be generated:
#define test_test 123
#define test_test2 456
#define test_test3 testing
Template blocks
One or more consecutive lines containing a token make a template block. When the code generation process starts,
every line of a template block gets processed together, producing n consecutive blocks each referring to a row od the
csv file.
The following template:
package main
func main() {
test()
}
func test() {
for i := 0; i < #2#; i++ {
#1# := #2# >> 1
}###
test2()
}
func test2() {
// do stuff ...
}
will generate this source code:
package main
func main() {
test()
}
func test() {
for i := 0; i < 123; i++ {
a := 123 >> 1
}
for i := 0; i < 12; i++ {
b := 12 >> 1
}
for i := 0; i < 2; i++ {
c := 2 >> 1
}
test2()
}
func test2() {
// do stuff ...
}
Including a non-token line in a block
In the last example you can see that there was a line containing the closing brace of the for loop, that was escaped
with the ### string. This allows for a line that doesn't contain a token to be considered as if it did.
By doing so, you can include a non-token line in a template block: this comes in handy when you have to generate number
of loops, functions, ect. in languages that use braces as block delimiters.
Usage
usage: codegen [-h|--help] -t|--template "<value>" [-t|--template "<value>"
...] -d|--data "<value>" [-s|--separator "<value>"] [-o|--output
"<value>" [-o|--output "<value>" ...]]
Generates files based on templates and csv tables. Template
files contain tokens in the #n# format, where n is a number and
# is the default separator. If the passed csv has n columns, the
application will be able to substitute tokens with the
corresponding column values, up to a total of n, for each row of
the csv file.
Arguments:
-h --help Print help information
-t --template a template to process in order to generate code, it should
contain tokens in the #n# format, where n is a number and #
is the default separator: e.g. #1# #2#
-d --data a csv file that contains the data to substitute the tokens
with
-s --separator the separator to use to identify tokens. Default: #
-o --output specifies a file where to save the generated source code.
Default: []