NAME

Gears::Generator - Copy and transform files

SYNOPSIS

use Gears::Generator;

my $gen = Gears::Generator->new(
	base_dir => 'templates',
	name_filters => [
		sub ($name) {
			return $name =~ s{app.pl$}{my_app.pl}r;
		},
	],
	content_filters => [
		sub ($content) {
			return $content =~ s{App$}{My::App}rg;
		},
	],
);

# Generate files from template
$gen->generate('app', 'my_app');

DESCRIPTION

Gears::Generator is a simple file scaffolding tool that generates file structures from templates. It reads template directories and modifies their names and contents with custom subroutines, then writes the results to a target directory.

This tool is deliberately not very advanced. It does not use full templating engine to generate file contents. It simply copies the files and does optional filtering. This way it can be used to move things like examples into the local directory automatically. If you need more control over file names and their contents, overriding _process_file method will give you full control over the process of building final file content.

INTERFACE

Attributes

base_dir

The base directory where template directories are located. May be a string or a Path::Tiny instance. Will be turned into the latter.

Required in constructor

name_filters

An array reference of filters to use against file names. Each filter is a subroutine that takes a single value and returns the transformed value. Defaults to an empty array.

Available in constructor

content_filters

An array reference of filters to use against file contents. Each filter is a subroutine that takes a single value and returns the transformed value. Defaults to an empty array.

Available in constructor

Methods

new

$object = $class->new(%args)

A standard Mooish constructor. Consult "Attributes" section to learn what keys can be passed in %args.

get_template

$files = $gen->get_template($name)

Returns an array reference of Path::Tiny objects representing all files in the template directory specified by $name. The template directory is located under base_dir.

generate

$files = $gen->generate($template_name, $target_dir)

Generates files from the template $template_name into $target_dir. File names are processed with "name_filters", while their contents are processed by "content_filters".

Returns an array reference of Path::Tiny objects representing the generated files.