Creating templates

Creating a new template starts off easily enough. By using the cmdlet New-PlasterTemplate, you can create a new, well-formed Plaster manifest that you can add to afterwards:

# List the default (shipped) templates
Get-PlasterTemplate

# List all templates
Get-PlasterTemplate -IncludeInstalledModules

# Create new, empty template
mkdir .PlasterTemplatesFirstTemplate -Force
New-PlasterManifest -TemplateType Project -TemplateName FirstTemplate -Path .PlasterTemplatesFirstTemplateplasterManifest.xml

# The template needs to be extended first
# See the online help at https://github.com/PowerShell/Plaster/blob/master/docs/en-US/about_Plaster_CreatingAManifest.help.md
# or use Get-Help
Get-Help about_Plaster_CreatingAManifest
psedit .PlasterTemplatesFirstTemplateplasterManifest.xml

In module development and deployment, we want to create several folders and a specific structure: Pester tests, a build script, PSDepend and PSDeploy files, and Psake-tasks. A module manifest should be created, as well as our Public, Private, and Types folders, which are used in our module to control the visibility of our cmdlets, as well as to import our custom types.

In order to do this, we will first of all add some parameters to our template: the author's name and a module version, as well as the name of the module:

# Add some parameters
[xml]$content = Get-Content -Path .PlasterTemplatesFirstTemplateplasterManifest.xml

# This XML has a namespace - instanciate a namespace manager before
$nsm = [System.Xml.XmlNamespaceManager]::new($Content.NameTable)
$nsm.AddNamespace('pl', "http://www.microsoft.com/schemas/PowerShell/Plaster/v1")

$parameterNode = $content.SelectSingleNode('/pl:plasterManifest/pl:parameters', $nsm)

# Author
$node = $content.CreateElement('parameter', $nsm.LookupNamespace('pl'))
$name = $content.CreateAttribute('name')
$name.Value = 'Author'
$type = $content.CreateAttribute('type')
$type.Value = 'user-fullname'
$prompt = $content.CreateAttribute('prompt')
$prompt.Value = 'Please enter your full name.'
$node.Attributes.Append($name)
$node.Attributes.Append($type)
$node.Attributes.Append($prompt)
$parameterNode.AppendChild($node)

# Module name
$node = $content.CreateElement('parameter', $nsm.LookupNamespace('pl'))
$name = $content.CreateAttribute( 'name')
$name.Value = 'ModuleName'
$type = $content.CreateAttribute('type')
$type.Value = 'text'
$prompt = $content.CreateAttribute('prompt')
$prompt.Value = 'Please enter the module name.'
$node.Attributes.Append($name)
$node.Attributes.Append($type)
$node.Attributes.Append($prompt)
$parameterNode.AppendChild($node)

# Version
$node = $content.CreateElement('parameter', $nsm.LookupNamespace('pl'))
$name = $content.CreateAttribute('name')
$name.Value = 'Version'
$type = $content.CreateAttribute('type')
$type.Value = 'text'
$default = $content.CreateAttribute('default')
$default.Value = '0.1.0'
$prompt = $content.CreateAttribute('prompt')
$prompt.Value = 'Please enter a module version.'
$node.Attributes.Append($name)
$node.Attributes.Append($type)
$node.Attributes.Append($default)
$node.Attributes.Append($prompt)
$parameterNode.AppendChild($node)

$content.Save('.PlasterTemplatesFirstTemplateplasterManifest.xml')

After the parameters, it is time to add files and folders to the scaffolding. These settings are added to the content node inside your manifest and can be any of the following:

  • File: Plain file copy.
  • TemplateFile: File copy with replacement of variables.
  • Message: Display a message to the user.
  • Modify: Modify an existing file inside the destination folder. It can be combined with file and templateFile as well.
  • NewModuleManifest: Create a new module manifest.
  • RequireModule: Checks whether a module is installed and prompts the user if not.

In our case, we would simply like to create a couple of folders, a module manifest, and our module file. So, after you have added your parameters, you could add the following to your XML code:

<content>
<message>Creating folders</message>
<file source='' destination='Public'/>
<file source='' destination='Private'/>
<file source='' destination='Types'/>
<file source='' destination='Test'/>
<file source='' destination='TestUnit'/>
<file source='' destination='TestIntegration'/>
<message>Creating files</message>
<file source='ModuleTemplate.psm1' destination='${PLASTER_PARAM_ModuleName}.psm1' />
<newModuleManifest destination='${PLASTER_PARAM_ModuleName}.psd1'
moduleVersion='$PLASTER_PARAM_Version'
rootModule='${PLASTER_PARAM_ModuleName}.psm1'
author='$PLASTER_PARAM_FullName'
description='$PLASTER_PARAM_ModuleDesc'
encoding='UTF8-NoBOM'/>
</content>

After adding the necessary code to the content node, you can simply invoke your Plaster template with the newly created files and folders. While invoking the template, all instructions in the content node will be executed sequentially. This means that you could create or copy files and modify them afterwards:

$destination = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath MyFirstModule
$template = Invoke-Plaster -TemplatePath .PlasterTemplatesFirstTemplate -DestinationPath $destination -PassThru
Get-ChildItem -Path $template.DestinationPath
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.40.207