Created
July 8, 2022 18:34
-
-
Save JustinGrote/2abd4ef73888e9eec3a0a062761ca3cb to your computer and use it in GitHub Desktop.
Implementing a Microsoft .NET Template Engine Package Provider as a Powershell Class - Just because you can doesn't mean you should
This file contains 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
using namespace System | |
using namespace System.Collections.Generic | |
using namespace System.Threading | |
using namespace System.Threading.Tasks | |
using namespace Microsoft.TemplateEngine.Abstractions | |
using namespace Microsoft.TemplateEngine.Abstractions.TemplatePackage | |
class PowerShellModuleTemplatePackageProvider : ITemplatePackageProvider { | |
[ITemplatePackageProviderFactory]$Factory | |
PowerShellModuleTemplatePackageProvider( | |
[ITemplatePackageProviderFactory]$Factory | |
) { | |
$this.Factory = $Factory | |
} | |
#TODO: We could potentially rig a filewatcher up to this or some way to detect module changes | |
[void] add_TemplatePackagesChanged([Action] $value) { | |
#Nothing needs to happen here, maybe a log? | |
} | |
[void] remove_TemplatePackagesChanged([Action] $value) { | |
#Nothing needs to happen here, maybe a log? | |
} | |
[Task[IReadOnlyList[ITemplatePackage]]] GetAllTemplatePackagesAsync([CancellationToken] $cancellationToken) { | |
[Console]::WriteLine('GetMortarPackages') | |
$folder = Get-Item 'C:\Users\JGrote\Projects\Mortar\Mortar.Templates\01-simple-module' | |
[List[TemplatePackage]]$result = @( | |
[TemplatePackage]::new($this, $folder.FullName, $folder.LastWriteTimeUtc) | |
) | |
return [Task]::FromResult[IReadOnlyList[ITemplatePackage]]($result) | |
} | |
} | |
class PowerShellModuleTemplatePackageProviderFactory : ITemplatePackageProviderFactory { | |
#Used FakeFactory as reference: https://github.dev/dotnet/templating/blob/4a50caebcb5b2db834bc7ea51be5f36bdc126876/test/Microsoft.TemplateEngine.Edge.UnitTests/TemplatePackageManagerTests.cs#L309-L310 | |
[guid]$Id = 'fdd52a92-307a-4a93-ad0c-23107f40add1' | |
[string]$DisplayName = 'Mortar - PowerShell Module Templates' | |
#We may need this in order to trigger events later on | |
[List[PowerShellModuleTemplatePackageProvider]]$allCreatedProviders = @() | |
[ITemplatePackageProvider] CreateProvider([IEngineEnvironmentSettings] $settings) { | |
$provider = [PowerShellModuleTemplatePackageProvider]::new($this) | |
$this.allCreatedProviders.Add($provider) | |
return $provider | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment