codegen

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

codegen

GoDoc Go Report Card

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: []

Documentation

Overview

Package codegen provides interfaces to generate source files from templates and raw data. These tools can be used to automate the creation of source files that contain a lot of similar block of codes, with only small details (names, labels, values) changing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CodeGenerator

type CodeGenerator struct {
	OutputFiles []string
	// contains filtered or unexported fields
}

CodeGenerator is a struct containing references to the names and contents of the files used in the template substitution process.

func (*CodeGenerator) Generate

func (gen *CodeGenerator) Generate() error

Generate creates the files with respect to the passed templates and csv data.

func (*CodeGenerator) LoadGenerator

func (gen *CodeGenerator) LoadGenerator(templateFiles []string, dataFile string, separator rune) error

LoadGenerator initializes a CodeGenerator, parsing the tokens contained in the template files. The data file is a csv file, where each row contains one tuple of data to be generated following the template.

Directories

Path Synopsis
cmd
codegen
CLI for codegen, used to generate files from templates and data organized in a csv file.
CLI for codegen, used to generate files from templates and data organized in a csv file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL