Public/New-StuccoModule.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function New-StuccoModule {
    <#
    .SYNOPSIS
        Creates a new module based on the Stucco module template
    .DESCRIPTION
        Creates a new module based on the Stucco module template
    .EXAMPLE
        New-StuccoModule -DestinationPath ./MyModule
 
        Creates a new module called MyModule in the directory ./MyModule
    .EXAMPLE
        $template = Get-StuccoTemplate
        $params = @{
            DestinationPath = './MyAwesomeModule'
            Force = $true
            NoLogo = $true
            TemplateParameters = @{
                ModuleName = 'MyAwesomeModule'
                Description = 'My awesome module'
                Version = '0.1.0'
                FullName = 'Community'
                License = 'MIT'
                CoC = 'No'
                MkDocs = 'No'
                Classes = 'Yes'
                PlatyPS = 'Yes'
                devcontainer = 'Yes'
                CICD = 'GitHubActions'
            }
        }
        $template | New-StuccoModule @params
 
        Gets the Stucco template, then creates a hashtable containing all
        required parameters to create a new module without additional prompts.
    .LINK
        Get-StuccoTemplate
    #>

    [CmdletBinding(DefaultParameterSetName = 'notemplate')]
    param(
        # Destination path for new module
        [Parameter(Mandatory, Position = 0)]
        [string]$DestinationPath,

        # Plaster template object
        [Parameter(Mandatory, ParameterSetName = 'template', ValueFromPipeline)]
        [Alias('InputObject')]
        [PSCustomObject]$Template,

        # Template parameters to pass to Invoke-Plaster
        [hashtable]$TemplateParameters = @{},

        # Overwrite existing files in destination path
        [switch]$Force,

        # Suppress the Plaster logo
        [switch]$NoLogo,

        # Return a InvokePlasterInfo object representing the created module
        [switch]$PassThru
    )

    process {
        if ($Template) {
            $templatePath = $Template.TemplatePath
        } else {
            # Get the Plaster template included in the Stucco module
            $moduleBase   = $ExecutionContext.SessionState.Module.ModuleBase
            $t            = Get-PlasterTemplate -Path $moduleBase
            $templatePath = $t.TemplatePath
        }

        if ((Test-Path -Path $templatePath)) {
            $plasterParams = @{
                TemplatePath    = $templatePath
                DestinationPath = $DestinationPath
                Force           = $Force.IsPresent
                NoLogo          = $NoLogo.IsPresent
                PassThru        = $PassThru.IsPresent
            }
            Invoke-Plaster @plasterParams @TemplateParameters
        } else {
            # We should never see this but...belt and suspenders and all that.
            Write-Error "Unable to find Plaster template at [$templatePath]"
        }
    }
}