
<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Core" #> <#@ Assembly Name="System.Windows.Forms" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <# var greeting = "Hello, World!"; #> // This is the output code from your template // you only get syntax-highlighting here - not intellisense namespace MyNameSpace { class MyGeneratedClass { static void main (string[] args) { System.Console.WriteLine("<#= greeting #>"); } } } <#+ // Insert any template procedures here void foo(){} #>  // This is the output code from your template // you only get syntax-highlighting here - not intellisense namespace MyNameSpace { class MyGeneratedClass { static void main (string[] args) { System.Console.WriteLine("Hello, World!"); } } }  <#@ template language="C#" #> Hello World!  public class GeneratedTextTransform : Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { this.Write("Hello, World!"); return this.GenerationEnvironment.ToString(); } }  Hello World!  <#@ template language="C#" #> Hello World!  <# var greeting = "Hello, World!"; #>  System.Console.WriteLine("<#= greeting #>"); <#+ #> . All functions declared in this block can be called in the template. In addition, the functions themselves may contain template text.<#@ template #> allows you to set the characteristics of a conversion class from a template:<#@ template language=”C#”> - sets the class language.<#@ template debug=”true”> - allows you to debug template generation.<#@ template inherits=”MyTextTransformation”> - specifies which class should be used as the base class for the generation in the file generation procedure.<#@ output #> - sets the extension for the generated file: <#@ output extension=".cs" #> <#@ import #> - adds the use of specified namespaces in the execution procedure. The same as using add (but not to the result, but when performing text generation): <#@ import namespace="System.Collections" #> <#@ assembly #> - adds assembly declaration. The same as in VisualStudio add Reference: <#@ Assembly Name="System.Core" #> <#@ include #> - adds some other template in the place of the declaration. This is like Html. Partial (): <#@ include file="Included.tt" #> <#@ parameter #> - adds a parameter when forming a template. But its transmission is so complicated that I will not give an example. Link PM> Install-Package T4Scaffolding  [T4Scaffolding.Scaffolder(Description = "Create IRepository interface")][CmdletBinding()] param( [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$ModelType, [string]$Project, [string]$CodeLanguage, [string[]]$TemplateFolders, [switch]$Force = $false ) $foundModelType = Get-ProjectType $ModelType -Project $Project -BlockUi if (!$foundModelType) { return } # Find the IRepository interface, or create it via a template if not already present $foundIRepositoryType = Get-ProjectType IRepository -Project $Project -AllowMultiple if(!$foundIRepositoryType) { #Create IRepository $outputPath = "IRepository" $defaultNamespace = (Get-Project $Project).Properties.Item("DefaultNamespace").Value Add-ProjectItemViaTemplate $outputPath -Template IRepositoryTemplate ` -Model @{ Namespace = $defaultNamespace } ` -SuccessMessage "Added IRepository at {0}" ` -TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force $foundIRepositoryType = Get-ProjectType IRepository -Project $Project } # Add a new property on the DbContext class if ($foundIRepositoryType) { $propertyName = $foundModelType.Name $propertyNames = Get-PluralizedWord $propertyName # This *is* a DbContext, so we can freely add a new property if there isn't already one for this model Add-ClassMemberViaTemplate -Name $propertyName -CodeClass $foundIRepositoryType -Template IRepositoryItemTemplate -Model @{ EntityType = $foundModelType; EntityTypeNamePluralized = $propertyNames; } -SuccessMessage "Added '$propertyName' to interface '$($foundIRepositoryType.FullName)'" -TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage } return @{ DbContextType = $foundDbContextType }  <#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #> #region <#= ((EnvDTE.CodeType)Model.EntityType).Name #> IQueryable<<#= ((EnvDTE.CodeType)Model.EntityType).Name #>> <#= Model.EntityTypeNamePluralized #> { get; } bool Create<#= ((EnvDTE.CodeType)Model.EntityType).Name #>(<#= ((EnvDTE.CodeType)Model.EntityType).Name #> instance); bool Update<#= ((EnvDTE.CodeType)Model.EntityType).Name #>(<#= ((EnvDTE.CodeType)Model.EntityType).Name #> instance); bool Remove<#=((EnvDTE.CodeType)Model.EntityType).Name #>(int id<#= ((EnvDTE.CodeType)Model.EntityType).Name #>); #endregion  IRepositoryTemplate.cs.t4: <#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #> <#@ Output Extension="cs" #> using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace <#= Model.Namespace #> { public interface IRepository { IQueryable<T> GetTable<T>() where T : class; } } | Name | Datatype | 

 PM> Scaffold IRepository Notify Added 'Notify' to interface 'LessonProject.Model.IRepository'  [T4Scaffolding.Scaffolder(Description = "Create IRepository interface")][CmdletBinding()] param( [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$ModelType, [string]$Project, [string]$CodeLanguage, [string[]]$TemplateFolders, [switch]$Force = $false ) $ModelType - this is the name of the class, and that is what we pass on to the Scaffold IRepository Notify . The remaining parameters are either by default, as Force, or by default known as Project, CodeLanguage. $foundModelType = Get-ProjectType $ModelType -Project $Project -BlockUi if (!$foundModelType) { return }  # Find the IRepository interface, or create it via a template if not already present $foundIRepositoryType = Get-ProjectType IRepository -Project $Project -AllowMultiple if(!$foundIRepositoryType) { #Create IRepository $outputPath = "IRepository" $defaultNamespace = (Get-Project $Project).Properties.Item("DefaultNamespace").Value Add-ProjectItemViaTemplate $outputPath -Template IRepositoryTemplate ` -Model @{ Namespace = $defaultNamespace } ` -SuccessMessage "Added IRepository at {0}" ` -TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force $foundIRepositoryType = Get-ProjectType IRepository -Project $Project }  -Model @{ Namespace = $defaultNamespace } `  Get-Project $Project).Properties.Item("DefaultNamespace").Value  namespace <#= Model.Namespace #> $foundIRepositoryType ), then add to this class a few properties using the IRepositoryItemTemplate template with parameters: # Add a new property on the DbContext class if ($foundIRepositoryType) { $propertyName = $foundModelType.Name $propertyNames = Get-PluralizedWord $propertyName # This *is* a DbContext, so we can freely add a new property if there isn't already one for this model Add-ClassMemberViaTemplate -Name $propertyName -CodeClass $foundIRepositoryType -Template IRepositoryItemTemplate -Model @{ EntityType = $foundModelType; EntityTypeNamePluralized = $propertyNames; } -SuccessMessage "Added '$propertyName' to interface '$($foundIRepositoryType.FullName)'" -TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage }  -Model @{ EntityType = $foundModelType; EntityTypeNamePluralized = $propertyNames; } Get-PluralizedWord and what role it played in the created template:  IQueryable<Notify> Notifies { get; }  $class = Get-ProjectType HomeController Add-ClassMember $class "public string MyNewStringField;"  $class = Get-ProjectType HomeController Add-ClassMemberViaTemplate -CodeClass $class -Template "YourTemplateName" -Model @{ SomeParam = "SomeValue"; AnotherParam = $false } -TemplateFolders $TemplateFolders  Add-ProjectItemViaTemplate -OutputPath "Some\Folder\MyFile" -Template "YourTemplateName" -Model @{ SomeParam = "SomeValue"; AnotherParam = $false } -TemplateFolders $TemplateFolders  $result = Get-PluralizedWord Person # Sets $result to "People" $result = Get-SingularizedWord People # Sets $result to "Person"  $pk = Get-PrimaryKey StockItem  $folder = Get-ProjectFolder "Views\Shared" Write-Host "The shared views folder contains $($folder.Count) items"  $file = Get-ProjectItem "Controllers\HomeController.cs" $file.Open() $file.Activate()  $defaultProjectLanguage = Get-ProjectLanguage $otherProjectLanguage = Get-ProjectLanguage -Project SomeOtherProjectName  $class = Get-ProjectType HomeController Add-ClassMember $class "public string MyNewStringField;"  Get-RelatedEntities Product  Set-IsCheckedOut "Controllers\HomeController.cs"  public interface IWeapon { void Kill(); }  public class Bazuka : IWeapon { public void Kill() { Console.WriteLine("BIG BADABUM!"); } }  public class Sword : IWeapon { public void Kill() { Console.WriteLine("Chuk-chuck"); } }  /// <summary> /// This is LEGENDARY WARRIOR! /// </summary> public class Warrior { readonly IWeapon Weapon; public Warrior(IWeapon weapon) { this.Weapon = weapon; } public void Kill() { Weapon.Kill(); } }  Install-Package T4Scaffolding  [T4Scaffolding.Scaffolder(Description = "Print Details for class")][CmdletBinding()] param( [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$ModelType, [string]$Project, [string]$CodeLanguage, [string[]]$TemplateFolders, [switch]$Force = $false ) $foundModelType = Get-ProjectType $ModelType -Project $Project -BlockUi if (!$foundModelType) { return } $outputPath = Join-Path "Details" $ModelType Add-ProjectItemViaTemplate $outputPath -Template Details ` -Model @{ ModelType = $foundModelType } ` -SuccessMessage "Yippee-ki-yay"` -TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force  <#@ template language="C#" HostSpecific="True" Inherits="DynamicTransform" debug="true" #> <#@ assembly name="System.Data.Entity" #> <#@ import namespace="System.Linq" #> <#@ import namespace="EnvDTE" #> <#@ Output Extension="txt" #> <# var modelType = (EnvDTE.CodeType)Model.ModelType; #> FullName : <#= modelType.FullName #> Name : <#= modelType.Kind #> <#= modelType.Name #> Access : <#= modelType.Access #> Attributes : <# foreach(var codeElement in modelType.Attributes) { var attr = (EnvDTE.CodeAttribute)codeElement; #> <#= attr.Name #> <# } #> Bases : <# foreach(var codeElement in modelType.Bases) { var @base = (EnvDTE.CodeType)codeElement; #> <#= @base.Name #> <# } #> Comment : <#= modelType.Comment #> DocComment : <#= modelType.DocComment #> StartPoint : Line: <#= ((EnvDTE.TextPoint)modelType.StartPoint).Line #> EndPoint : Line : <#= ((EnvDTE.TextPoint)modelType.EndPoint).Line #> Members : <# foreach(var codeElement in modelType.Members) { var member = (EnvDTE.CodeElement)codeElement; #> <#= member.Kind #> <#= member.Name #> <# } #>  PM> Scaffold Details Warrior -Force:$true Yippee-ki-yay  Scaffold IRepository ModelName  Scaffold Proxy ModelName -Lang:$true  Scaffold SqlRepository ModelName -Lang:$true  Scaffold ProviderRepository ModelName -Lang:$true  Scaffold Model ModelName  Scaffold SelectReference City State  Scaffold Controller ModelName –Area:Admin –Paging:$true –Lang:$true  Scaffold IndexView ModelName –Area:Admin –Paging:$true –Lang:$true Scaffold EditView ModelName –Area:Admin –Paging:$true –Lang:$true Source: https://habr.com/ru/post/176097/
All Articles