src/New-ModuleFromTemplate.ps1

<#
.Synopsis
   Creates a new PowerShell module from a Plaster template.
.DESCRIPTION
   The cmdlet is a wrapper for creating PowerShell modules with a defined structure based on a Plaster template file.
    
.EXAMPLE
   New-ModuleFromTemplate -DestinationPath .\SampleProject -ModuleName mymodule -Description "This is a test"
 
   This example shows an invocation with the mandatory parameters only.
 
   The following lines show the output generated by the Invoke-Plaster cmdlet when executing the above example.
   -----------------------------------------------------------------
   Destination path: C:\tmp\SampleProject
        Create .gitignore
        Create appveyor.yml
        Create LICENSE
        Create README.md
        Create mymodule\mymodule.psm1
        Create mymodule\src\
        Create mymodule\tests\
        Update LICENSE
        Update README.md
        Modify README.md into temp file before copying to destination
        Update README.md
        Create mymodule\mymodule.psd1
        Modify mymodule\mymodule.psd1 into temp file before copying to destination
        Update mymodule\mymodule.psd1
 
   Your new PowerShell module project 'mymodule' has been created.
   -----------------------------------------------------------------
   The invocation example from above creates the following module structur:
     
    .\SampleProject\
        |--mymodule\
            |--src\
            |--tests\
            |--mymodule.psd1
            |--mymodule.psm1
        |--.gitignore
        |--appveyor.yml
        |--LICENSE
        |--README.md
#>

function New-ModuleFromTemplate
{
    [CmdletBinding()]
    Param(
        # Specifies the path to the location of the newly created PowerShell Module.
        [Parameter(Mandatory)]
        [string]
        $DestinationPath,

        # Specifies the name of the new PowerShell Module.
        [Parameter(Mandatory)]
        [string]
        $ModuleName,

        # Specifies the description used to insert into the new PowerShell Module Manifest file.
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Description,

        # Specifies the author used to insert into the new PowerShell Module Manifest file.
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Author,

        # Specifies the version number used to insert into the new PowerShell Module Manifest file.
        [Parameter()]
        [string]
        $Version,

        # Specifies the company name used to insert into the new PowerShell Module Manifest file.
        [Parameter()]
        [string]
        $Company,

        # Specifies the minimum required PowerShell version used to insert into the new PowerShell Module Manifest file.
        [Parameter()]
        [string]
        $PowerShellVersion,

        # Specifies if the required parameters defined in the Plaster template will be prompted using the Invoke-Plaster function.
        [Parameter()]
        [switch]
        $NoPrompt
    )

    # Set all mandatory parameters
    $PlasterParameters = @{
        TemplatePath = "${PSScriptRoot}\NewModuleTemplate\"
        DestinationPath = $DestinationPath
        ModuleName = $ModuleName
        Description = $Description
    }

    # Temporarily save the passed values and the default values for every non mandatory parameter
    $nonMandatoryParmeters = @{
        Author = @{ Passed = $Author; Default = 'Unknown' }
        Version = @{ Passed = $Version; Default = '0.1.0' }
        Company = @{ Passed = $Company; Default = 'Unknown' }
        PowerShellVersion = @{ Passed = $PowerShellVersion; Default = 'None' }
    }

    # Add key/value pair with passed value, with the default value or without the key/value pair
    # dependent if the parameter and the NoPrompt switch have been specified.
    $nonMandatoryParmeters.Keys | ForEach-Object {
        $value = $nonMandatoryParmeters[$_]
        if ($value.Passed) {
            $PlasterParameters.Add($_, $value.Passed)
        }
        elseif ($NoPrompt) {
            $PlasterParameters.Add($_, $value.Default)
        }
    }

    Invoke-Plaster @PlasterParameters -Force -NoLogo
}