Useful T4Scaffolding cmdlets for use in custom scaffolders

Add-ClassMember

Adds a block of code to an existing class. Example:

$class = Get-ProjectType HomeController 
Add-ClassMember $class "public string MyNewStringField;"

Add-ClassMemberViaTemplate

Renders a T4 template, and adds the output as a new block of code in an existing class. Example:

$class = Get-ProjectType HomeController 
Add-ClassMemberViaTemplate -CodeClass $class -Template "YourTemplateName" -Model @{ SomeParam = "SomeValue"; AnotherParam = $false } -TemplateFolders $TemplateFolders

Note that $TemplateFolders is passed into your custom scaffolder as a parameter automatically, so you don’t have to figure out what folders your T4 templates are in.

Add-ProjectItemViaTemplate

Renders a T4 template, and adds the output as a new file in your project. Example:

Add-ProjectItemViaTemplate -OutputPath "Some\Folder\MyFile" -Template "YourTemplateName" -Model @{ SomeParam = "SomeValue"; AnotherParam = $false } -TemplateFolders $TemplateFolders

Note that if the T4 template specifies a filename extension (e.g., with the declaration <#@ Output Extension="cs" #>), this will be added to the output path automatically.

Also note that $TemplateFolders is passed into your custom scaffolder as a parameter automatically, so you don’t have to figure out what folders your T4 templates are in.

Get-PluralizedWord / Get-SingularizedWord

Uses the pluralization library (part of Entity Framework) to convert a word to its plural or singular form. Useful, for example, if you want to store items related to a Person model in a folder called People. Example:

$result = Get-PluralizedWord Person    # Sets $result to "People" 
$result = Get-SingularizedWord People    # Sets $result to "Person"

Note: The default pluralization library only supports English pluralization. For other cultures, these cmdlets will return the word unchanged.

Get-PrimaryKey

Detects and returns the name of the primary key property on a specified model class. Example:

$pk = Get-PrimaryKey StockItem

Note: Returns nothing if no primary key property was detected.

Get-ProjectFolder

Obtains an EnvDTE.ProjectItems instance representing the specified folder. You can use this to inspect or modify the contents of the folder. Example:

$folder = Get-ProjectFolder "Views\Shared" 
Write-Host "The shared views folder contains $($folder.Count) items"

Get-ProjectItem

Obtains an EnvDTE.ProjectItem instance representing the specified file. You can then read information from it, open/save/delete it, etc. Example:

$file = Get-ProjectItem "Controllers\HomeController.cs" 
$file.Open() 
$file.Activate()

Get-ProjectLanguage

Returns "cs" for C# projects, or "vb" for VB projects. Example:

$defaultProjectLanguage = Get-ProjectLanguage 
$otherProjectLanguage = Get-ProjectLanguage -Project SomeOtherProjectName

Get-ProjectType

Obtains an EnvDTE.CodeType instance representing the specific class, interface, or other type. You can then read or modify the members of the type, for example using Add-ClassMember. Example:

$class = Get-ProjectType HomeController 
Add-ClassMember $class "public string MyNewStringField;"

Get-RelatedEntities

Returns a data structure describing the 1:many relations that T4Scaffolding can detect on your model class. Example:

Get-RelatedEntities Product

Returns an empty collection if no relations were detected.

Set-IsCheckedOut

If your source control system requires you to explicitly check out files before editing them, use this to automate Visual Studio’s source control bindings to check out a file. Example:

Set-IsCheckedOut "Controllers\HomeController.cs"

Note: If your project has no source control bindings, this cmdlet does nothing.

Last edited Apr 7, 2011 at 1:33 PM by SteveSanderson, version 2

Comments

smartgrid Dec 25, 2012 at 4:27 AM 
Hi, is there a way to scaffold and entire directory of Models. I have been searching, but no go...

thanks

adam_14four Aug 8, 2011 at 8:13 PM 
There is an error in the "Get-ProjectItem" code...to activate the file you need to use "$file.Document.Activate()".