Last active
January 2, 2019 19:22
-
-
Save jaredswarts55/5ed3679b70ffb985a45fd1dfe8109d4e to your computer and use it in GitHub Desktop.
Scaffolding Script with an example template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <#@template language="c#" hostspecific="true"#> | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using MediatR; | |
| using Serilog; | |
| using SteamChain.Common; | |
| using SteamChain.Common.Models.Functional; | |
| using SteamChain.Core.Events.<#= this.Host.ResolveParameterValue("", "", "groupName") #>.Models.Requests; | |
| using SteamChain.Core.Events.<#= this.Host.ResolveParameterValue("", "", "groupName") #>.Models.Responses; | |
| using SteamChain.Core.Models.Events.Companies; | |
| using SteamChain.Core.Models.ServiceBus; | |
| using SteamChain.Core.Models.Sql.Models; | |
| using SteamChain.Core.Services.Base; | |
| using static SteamChain.Common.Models.Functional.FunctionalExtensions; | |
| namespace SteamChain.Core.Events.<#= this.Host.ResolveParameterValue("", "", "groupName") #>.Handlers | |
| { | |
| public class <#= this.Host.ResolveParameterValue("", "", "className") #>RequestHandler : IRequestHandler<<#= this.Host.ResolveParameterValue("", "", "className") #>Request, Result<<#= this.Host.ResolveParameterValue("", "", "className") #>Response>> | |
| { | |
| private readonly IConnectionFactory _factory; | |
| private readonly ILogger _logger; | |
| private readonly IUserService _userService; | |
| private readonly IMessageQueueService _queue; | |
| public <#= this.Host.ResolveParameterValue("", "", "className") #>RequestHandler( | |
| IConnectionFactory factory, | |
| ILogger logger, | |
| IUserService userService, | |
| IMessageServiceFactory msgSvcFactory) | |
| { | |
| _factory = factory; | |
| _logger = logger; | |
| _userService = userService; | |
| _queue = msgSvcFactory.CreateQueueService(SteamChainConstants.EventStreamQueueName); | |
| } | |
| public async Task<Result<<#= this.Host.ResolveParameterValue("", "", "className") #>Response>> Handle(<#= this.Host.ResolveParameterValue("", "", "className") #>Request request, CancellationToken cancellationToken) | |
| { | |
| throw new NotImplementedException(); | |
| // return ResultValue(new <#= this.Host.ResolveParameterValue("", "", "className") #>Response()); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-CurrentScriptPath { | |
| Param ( | |
| [Parameter(Mandatory = $true)] | |
| [System.Management.Automation.InvocationInfo]$Invocation | |
| ) | |
| return split-path -parent $Invocation.MyCommand.Definition | |
| } | |
| function Get-CloudRoot { | |
| Param ( | |
| [Parameter(Mandatory = $true)] | |
| [System.Management.Automation.InvocationInfo]$Invocation | |
| ) | |
| $scriptPath = Get-CurrentScriptPath -Invocation $Invocation | |
| return (get-item $scriptPath).Parent.FullName | |
| } | |
| function Get-RepoRoot { | |
| Param ( | |
| [Parameter(Mandatory = $true)] | |
| [System.Management.Automation.InvocationInfo]$Invocation | |
| ) | |
| $cloudRoot = Get-CloudRoot -Invocation $Invocation | |
| return (get-item $cloudRoot).Parent.Parent.FullName | |
| } | |
| function Get-ScriptEnvironment { | |
| Param ( | |
| [Parameter(Mandatory = $true)] | |
| [System.Management.Automation.InvocationInfo]$Invocation | |
| ) | |
| return @{ | |
| ScriptPath = (Get-CurrentScriptPath -Invocation $Invocation); | |
| CloudRootPath = (Get-CloudRoot -Invocation $Invocation); | |
| RepoRootPath = (Get-RepoRoot -Invocation $Invocation); | |
| } | |
| } | |
| function Get-VSPaths { | |
| Param ( | |
| [int]$VSVersion = 15 | |
| ) | |
| if (!(Get-Module -ListAvailable -Name VSSetup)) { | |
| Write-Host "This scripts requires the VSSetup Powershell Module made by Microsoft. Installing..." | |
| Install-Module VSSetup -Scope CurrentUser | |
| } | |
| Import-Module VSSetup | |
| $setupInstances = Get-VSSetupInstance | ? {($_.InstallationVersion -ge (new-object System.Version ($VSVersion),0,0,0)) -and ($_.InstallationVersion -lt (new-object System.Version ($VSVersion + 1),0,0,0))} | |
| return $setupInstances; | |
| } | |
| function Check-IsAdmin { | |
| return ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| . "$(split-path -parent $MyInvocation.MyCommand.Definition)\helpers.ps1" | |
| $environment = (Get-ScriptEnvironment -Invocation $MyInvocation) | |
| function Add-FilesToProject | |
| { | |
| Param ( | |
| [Parameter(Mandatory = $true)] | |
| [string]$PathToProjectFile, | |
| [Parameter(Mandatory = $true)] | |
| [string[]]$PathsToFiles | |
| ) | |
| $proj = [xml](gc $PathToProjectFile) | |
| $fileGroup = $proj | Select-Xml "//a:ItemGroup[a:Compile]" -Namespace @{a ='http://schemas.microsoft.com/developer/msbuild/2003'} | select -first 1 | |
| foreach($fileToAdd in $PathsToFiles) | |
| { | |
| $fileCompileElement = $proj.CreateElement("Compile", $proj.Project.NamespaceURI); | |
| $projectDir = [System.IO.Path]::GetDirectoryName($PathToProjectFile); | |
| $shortPath = $fileToAdd.Replace("$projectDir\",""); | |
| $fileCompileElement.SetAttribute("Include", $shortPath); | |
| $fileCompileElement.RemoveAttribute("xmlns"); | |
| $fileGroup.Node.AppendChild($fileCompileElement); | |
| } | |
| $proj.Save($PathToProjectFile); | |
| } | |
| function Add-SteamChainGetCommand | |
| { | |
| Param ( | |
| [Parameter(Mandatory = $true)] | |
| [string]$Name, | |
| [Parameter(Mandatory = $true)] | |
| [string]$Group | |
| ) | |
| if(!$Name.ToLower().StartsWith("get") -and !$Name.ToLower().StartsWith("execute")) | |
| { | |
| Write-Output "Command does not start with 'Get' or 'Execute'. Change the command name." | |
| return; | |
| } | |
| $textTemplate = ''; | |
| $instance = $null; | |
| foreach ($vsPath in (Get-VSPaths -VSVersion 15)) { | |
| $ideFolder = "$($vsPath.InstallationPath)\Common7\IDE"; | |
| if(![System.IO.Directory]::Exists($ideFolder)){ | |
| continue; | |
| } | |
| $textTemplate = ls $ideFolder -Recurse | ? {$_.Name.ToLower() -eq 'TextTransform.exe'} | select -first 1 | |
| if($textTemplate -ne $null) { | |
| $instance = $vsPath; | |
| break; | |
| } | |
| } | |
| $prefix = "Get"; | |
| if($Name.ToLower().StartsWith("execute")) { | |
| $prefix = "Execute"; | |
| } | |
| $commandRootPaths = "$($environment.CloudRootPath)\SteamChain.Core\Events"; | |
| $simpleName = ((($Name -replace "[Rr]equest$", "") -replace "^[Gg]et", "") -replace "^[Ee]xecute", ""); | |
| $groupPath = "$($environment.CloudRootPath)\SteamChain.Core\Events\$Group"; | |
| if(![System.IO.Directory]::Exists($groupPath)) { | |
| Write-Output "Path does not exist: $groupPath" | |
| return; | |
| } | |
| # Create Diretories if they don't exist | |
| [System.IO.Directory]::CreateDirectory("$groupPath\Models\Requests") | out-null; | |
| [System.IO.Directory]::CreateDirectory("$groupPath\Models\Responses") | out-null; | |
| [System.IO.Directory]::CreateDirectory("$groupPath\Handlers") | out-null; | |
| $pathToRequestFile = "$groupPath\Models\Requests\$prefix$($simpleName)Request.cs"; | |
| $pathToResponseFile = "$groupPath\Models\Responses\$prefix$($simpleName)Response.cs"; | |
| $pathToHandlerFile = "$groupPath\Handlers\$prefix$($simpleName)RequestHandler.cs"; | |
| Write-Output "Running Templates" | |
| & ($textTemplate.FullName) "$($environment.ScriptPath)\template\commandRequest.tt" -out $pathToRequestFile -a !!className!$prefix$simpleName -a !!groupName!$Group | |
| & ($textTemplate.FullName) "$($environment.ScriptPath)\template\commandResponse.tt" -out $pathToResponseFile -a !!className!$prefix$simpleName -a !!groupName!$Group | |
| & ($textTemplate.FullName) "$($environment.ScriptPath)\template\commandHandler.tt" -out $pathToHandlerFile -a !!className!$prefix$simpleName -a !!groupName!$Group | |
| Write-Output "Adding Files To Project" | |
| Add-FilesToProject -PathToProjectFile "$($environment.CloudRootPath)\SteamChain.Core\SteamChain.Core.csproj" -PathsToFiles (($pathToRequestFile, $pathToResponseFile, $pathToHandlerFile)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment