src/scaffolds/Install-Template.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using module .\..\dynamicparams\GetPlasterTemplateVarSet.ps1
using module .\..\module\Get-MKModuleInfo.ps1

# TODO: add Path and Name parameters so that the scaffold files can be added to rootmodule
function Install-Template {
    [CmdletBinding(PositionalBinding = $true, 
        DefaultParameterSetName = "ByTemplatePath")]
    Param
    (
        [Parameter(Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $false, 
            ParameterSetName = "ByTemplatePath")]
        [string]$TemplatePath,

        [Parameter(Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $false, 
            ParameterSetName = "ByTemplateName")]
        [ValidateSet("NewScript", "NewModuleProject")]
        [string]$TemplateName
    )

    DynamicParam {
        # when platyPS calls Install-Template need to manually give it a value. This value cannot be
        # assigned to the param within Param parentheses since it has an undetermined count of
        # parameters.
        if ((-not $TemplatePath) -and (-not $TemplateName)) {
            $script:TemplatePath = 'resources\templates\NewScript\plasterManifest_en-US.xml'
        }
        elseif ($PSCmdlet.ParameterSetName -eq "ByTemplateName") {
            $script:TemplatePath = Join-Path -Path $PowerBoltPATH -ChildPath "resources\templates\$TemplateName\plasterManifest_en-US.xml"
        }
        elseif ($PSCmdlet.ParameterSetName -eq "ByTemplatePath") {
            $script:TemplatePath = $TemplatePath
        }
        
        $TemplateVarDictionary = GetPlasterTemplateVarSet -Path $script:TemplatePath -ParameterSetName $PSCmdlet.ParameterSetName
        return $TemplateVarDictionary
    }

    end {

        if ($TemplateName -eq 'NewScript') {
            try {
                $CanidateModulePath = Resolve-Path . | Select-Object -ExpandProperty Path
                $ModInfo = Get-MKModuleInfo $CanidateModulePath
                $IsModulePath = $ModInfo.IsValid
            }
            catch {
                $IsModulePath = $false
            }
        }
        else {
            $IsModulePath = $true
        }

        if ($IsModulePath) {
            $TemplateVarDictionary.GetEnumerator() | ForEach-Object {
                $Name = "PLASTER_PARAM_" + ($_.Key)
                $Value = $(($_.Value).Value)
                Set-Variable -Name $Name -Value $Value -Scope Global
            }

            if ($TemplateName -eq 'NewScript') {

                $ModuleHomeDeclarationCode = "`$script:PSCommandPath | Split-Path -Parent"
                $Depth = [regex]::Matches($PLASTER_PARAM_ScriptCongruentPath, "[\w]+").Count
                
                for ($i = 0; $i -le $Depth; $i++) {
                    $ModuleHomeDeclarationCode += " | Split-Path -Parent"
                }

                $PlasterCustomVar = @{PLASTER_ModuleHomeDeclarationCode = $ModuleHomeDeclarationCode; PLASTER_ModuleName = ($ModInfo.Name)}
                $PlasterCustomVar.GetEnumerator() | `
                    ForEach-Object {
                    $Name = $_.Key
                    $Value = $_.Value
                    Set-Variable -Name $Name -Value $Value -Scope Global
                }
            }

            $PlasterTemplateFolderPath = Split-Path -Path $script:TemplatePath -Parent

            if (-not $PSBoundParameters.DestinationPath) {
                Invoke-Plaster -TemplatePath $PlasterTemplateFolderPath -DestinationPath '.'
            }
            else {
                Invoke-Plaster -TemplatePath $PlasterTemplateFolderPath -DestinationPath $PSBoundParameters.DestinationPath
            }

            # TODO: to have this conditional avail when TemplatePath is used too
            if ($TemplateName -eq 'NewScript') {
                Update-ModuleExports 
            }
            elseif ($TemplateName -eq 'NewModuleProject') {
                $ModInfo = Get-MKModuleInfo -Path (Join-Path -Path $PSBoundParameters.DestinationPath -ChildPath $PSBoundParameters.ModuleName)
                
                Update-ModuleManifest -Path $ModInfo.ManifestFilePath -Prerelease 'alpha'
                Add-ModuleToProfile $ModInfo.Path
            }

            $TemplateVarDictionary.GetEnumerator() | ForEach-Object {
                $Name = "PLASTER_PARAM_" + ($_.Key)
                Remove-Variable -Name $Name -Scope Global
            }

            if ($PlasterCustomVar) {
                $PlasterCustomVar.GetEnumerator() | `
                    ForEach-Object {
                    $Name = $_.Key
                    Remove-Variable -Name $Name -Scope Global
                }
            }
        }
        else {
            Write-Error "Unable to find root module" -Category InvalidArgument -RecommendedAction "Set location inside a root module folder and try again."
        }
    }
}