Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

jordan-yee/raptorfilegenerator

Repository files navigation

Raptor File Generator

A .NET Core class library for generating custom text/code files.


Usage

1. Create text file template.

Put any text you want in a file.
template.txt:

Hello, World!

2. Embed other text files in your template via their file paths.

By default, define embedded files using lines that start with '###' followed by a file path.
template.txt:

Hello, World!
### C:\file.txt

file.txt (nested file):

This is a nested file.

3. Parameterize any text in your template.

By default, parameters are defined by surrounding a parameter name in double curly braces: {{parameter}}.
Template File:

Hello, {{name}}!
### C:\file.txt

4. Get the text of the generated templates and write it to a file, or do anything else you want with it.

C# Code:

// Define the template path
string templatePath = "C:\\template.txt";

// Define template parameters
Dictionary<string, string>[] parameters = new Dictionary<string, string>[1];
parameters[0] = new Dictionary<string, string>();
parameters[0].Add("name", "World");

TemplateData templateData = new TemplateData();
templateData.AddTemplateParameterData("template", parameters);

// Get the expanded template text
TemplateGenerator templateGenerator = new TemplateGenerator(templateData);
string templateText = templateGenerator.GenerateTemplateText(templatePath);

Result:
Hello, World!
This is a nested file.

5. Assign multiple parameter sets to a template's TemplateData to repeat that template for each parameter set.

C# Code:

// Define the template path
string templatePath = "C:\\template.txt";

// Define template parameters
Dictionary<string, string>[] parameters = new Dictionary<string, string>[2];
parameters[0] = new Dictionary<string, string>();
parameters[0].Add("name", "World");
parameters[1] = new Dictionary<string, string>();
parameters[2].Add("name", "Other World");

TemplateData templateData = new TemplateData();
templateData.AddTemplateParameterData("template", parameters);

// Get the expanded template text
TemplateGenerator templateGenerator = new TemplateGenerator(templateData);
string templateText = templateGenerator.GenerateTemplateText(templatePath);

Result:
Hello, World!
Hello, Other World!
This is a nested file.

Note: Passing an empty array or null for the parameters will result in a single instance of the template text being generated by default. An option can be set on the TemplateData object to cause nothing to be generated instead.

6. Alternatively assign template parameter values using POCOs

C# Code:

// Define the template path
string templatePath = "C:\\template.txt";

// Define template parameters
List<ParameterSets> parameters= new ParameterSet[2];
parameters.Add(new ParameterSet() { Name = "World", });
parameters.Add(new ParameterSet() { Name = "Other World" });

TemplateData templateData = new TemplateData();
// Note: The 3rd parameter is optional--whether to convert parameter objects' property names to camelCase
templateData.AddTemplateParameterData("template", parameters, true);

// Get the expanded template text
TemplateGenerator templateGenerator = new TemplateGenerator(templateData);
string templateText = templateGenerator.GenerateTemplateText(templatePath);

Result:
Hello, World!
Hello, Other World!
This is a nested file.

About

A .NET Core class library for generating custom text (code) files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages