templit

package module
v0.0.0-...-f0a65c5 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

README

Templit

Templit is a project template tool. You can create any file and structure you wish and create new items from that.

Templit is written in Go and uses the Handlebars template syntax. For detail instructions and examples on the syntax of Handlebars, please see Handlebars Introduction.

Installation

For now, you must have Go installed:

$ go install codeberg.org/jcowgar/templit/cmd/templit@latest

Basics

Templit looks for a master template directory in either the environment variable TEMPLIT_DIR, the command line option -template-dir or .templits in the current working directory.

The master template directory contains sub directories, of which are the names for templates you can invoke. Under these sub directories you can have any structure you wish. Templit will recreate that structure in your output directory.

Each file found in the master template's sub directory will be recreated using the template found.

Simple Example

Given the directory structure:

project-dir/
  .templits/
    controller/
	  controller.js
	  tests/test.js
	migration/
	  migrations/
	    up.sql
		  down.sql

You have two "master" templates you can invoke. 'controller' and 'migration'. Following are the content of each file. These are simple examples, not meant to be real (or working) JavaScript/SQL.

# controller/controller.js
filename: {{ lowercase(snakecase name) }}_controller.js
---
// {{ camelcase name }}Controller is a controller.
class {{ pascalcase name }}Controller {
{{#each (split routeNames ",") }}
  function handle{{ gocase . }} {
    console.log("Route {{ gocase . }} is not yet implemented")
  }
{{/each}}
}

# controller/tests/test.js
filename: {{ lowercase(snakecase name) }}_test.js
---
class {{ name }}Test {
}


# migrations/up.sql
filename: {{ now "060102150405" }}-{{ lowercase(kebabcase name) }}.up.sql
---
-- See .templits/config.yaml file for author and email variables
-- UP MIGRATION by {{ author }} <{{ email }}>

BEGIN;

-- UP MIGRATION CONTENT HERE

COMMIT;

# migrations/down.sql
filename: {{ now "060102150405" }}-{{ lowercase(kebabcase name) }}.down.sql
---

-- DOWN MIGRATION

BEGIN;

-- DOWN MIGRATION CONTENT HERE

COMMIT;

Now, in your project directory, you can:

Generate a few new controllers:

$ templit controller name=Ping routeNames=ping,pong
$ templit controller name=SayHello routeNames=hi,hello,howdy,bye,cya

This would in turn create with the appropriate content:

ping_controller.js
say_hello_controller.js
tests/
  ping_controller_test.js
  say_hello_controller_test.js

If you needed to create a new SQL migration:

$ templit migration name="Create new users table"

This would in turn create with the appropriate content:

migrations/
  22070312332943-create-new-users-table.up.sql
  22070312332943-create-new-users-table.down.sql

Use

Any name/value pair can be supplied on the command line after any options.

templit myCoolTemplate name=HelloWorld count=5 people=John,Jack,Joe

In your template, you can access {{ name }}, {{ count }} and {{ people }}. In addition to these, all environment variables are exposed to templates.

You can use functions in your templates such as:

{{#each (split people ",")}}
  echo "Hello, {{ . }}"
{{/each}}

There are "modifier" type functions:

  • lowercase lower
  • uppercase UPPER
  • snakecase snake_case
  • kebabcase kebab-case
  • camelcase camelCase
  • pascalcase PascalCase
  • gocase HTTPHandler

You can access the current date via the now function which takes a format as it's parameter.

You can split a string by some delimiter via the split function.

If you have TEMPLIT_ALLOW_SHELL=Y or use the -allow-shell parameter, you can use the shell function to include the standard output of any shell command, such as {{ shell "git rev-parse head" }}.

Filenames will be used as is unless there is a "header" block in your template that redefines the filename to use, as seen in the above examples.

Getting Help

templit is still very and will likely change. Most changes, however, will probably be enhancements. The core templating engine is using Handlebars, so it is stable and mature.

If you need help, I would suggest opening an issue here on codeberg.org/jcowgar/templit.

Helping

PRs are always welcome for new features and bug fixes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// TemplateDirName defines the path where all templates are stored.
	TemplateDirNames []string

	// OutputDirName is where generated files will be written.
	OutputDirName string

	// SystemVariables is all known system wide variables for template processing.
	SystemVariables Variables

	// DryRun causes the system to only determine what would be done, no files
	// are actually written.
	DryRun bool

	// Debug enables verbose output useful for debugging Templit and user
	// templates.
	Debug bool

	// Overwrite enables overwritting existing files instead of skipping if
	// they already exist.
	Overwrite bool

	// AllowShell enables the built in shell function in templates.
	//
	// It is disabled by default due to security concerns about a template
	// being able to execute any shell command.
	AllowShell bool
}

Config options for a templit execution.

func BaseConfig

func BaseConfig() Config

BaseConfig creates a default configuration for templit execution.

func (*Config) AddTemplateDirHead

func (c *Config) AddTemplateDirHead(dir string)

AddTemplateDirHead adds a template to the top of the list. Templates are searched for from first in the array to last.

func (*Config) AddTemplateDirTail

func (c *Config) AddTemplateDirTail(dir string)

AddTemplateDirTail adds a template to the bottom of the list. Templates are searched for from first in the array to last.

type Source

type Source struct {
	SourceFilename      string
	DestinationFilename string
	HeaderVariables     Variables
	Content             string
}

func ParseSource

func ParseSource(t *Templit, srcFname string) (Source, error)

func (*Source) Process

func (s *Source) Process(t *Templit) error

type Templit

type Templit struct {
	// MasterTemplate that will be processed.
	MasterTemplate string

	// Config for templit execution.
	Config Config

	// Templates is an array of all found templates in a given master template.
	Templates []Source
	// contains filtered or unexported fields
}

Templit represents configuration for a templit execution.

func New

func New(cfg Config, masterName string) (Templit, error)

New constructs a Templit structure.

func (*Templit) AddSource

func (t *Templit) AddSource(s Source)

AddSource adds a template to be processed.

func (*Templit) LoadConfigFile

func (t *Templit) LoadConfigFile(configFilename string) error

LoadConfigFile loads a Templit configuration file.

func (*Templit) Process

func (t *Templit) Process() error

Process will generate all templates in `templateName` substituting all values for `vars`.

type Variables

type Variables map[string]string

Variables represents variable substitutions used in the template processing.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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