Minimal Scaffolding for Visual Studio

Here is an article that explains the setup.

However to get started in four lines:

New Project
From NuGet Console:
Install-Package t4scaffolding
Scaffold CustomScaffolder MinimalDemo

Then you can simply use:
scaffold MinimalDemo -Project MinimalScaffolding

I will experiment with performing database and filessytem queries.

The demo project creates a MinimalDemo.ps1 and a minimal demo t4 template.

The demo looks like:

[T4Scaffolding.Scaffolder(Description = “Enter a description of MinimalDemo here”)][CmdletBinding()]
param(
[string]$Project,
[string]$CodeLanguage,
[string[]]$TemplateFolders,
[switch]$Force = $false,
[string]$Filename = “ExampleOutput”
)

$outputPath = $FilenameĀ  # The filename extension will be added based on the template’s <#@ Output Extension=”…” #> directive
$namespace = (Get-Project $Project).Properties.Item(“DefaultNamespace”).Value

Add-ProjectItemViaTemplate $outputPath -Template MinimalDemoTemplate `
-Model @{ Namespace = $namespace; ExampleValue = “Hello, world!” } `
-SuccessMessage “Added MinimalDemo output at {0}” `
-TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force

Note in this instance I have edited the param list to allow the filename to be specified on the command line.

Since data is passed into the template via the model parameter it is trivial to perform database or filesystem queries to enrich the data being passed to the item generated.

Combine this with version control and you can overwrite the existing version and merge back existing customisations.

We have MVC style one-shot code generation (with the force option to overwrite).

This is incredibly powerful. A few of these templates could save hours of work. Especially across a team.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s