Private/Build/New-BrownserveInitScript.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function New-BrownserveInitScript
{
    [CmdletBinding()]
    param
    (
        # The permanent paths (i.e. those that should always exists)
        [Parameter(
            Mandatory = $true
        )]
        [InitPath[]]
        $PermanentPaths,

        # The ephemeral paths
        [Parameter(
            Mandatory = $true
        )]
        [InitPath[]]
        $EphemeralPaths,

        # Whether or not to include our custom PowerShell module loader
        [Parameter(
            Mandatory = $false
        )]
        [switch]
        $IncludeModuleLoader,

        # Any custom init steps
        [Parameter(
            Mandatory = $false
        )]
        [string]
        $CustomInitSteps
    )
    
    begin
    {
        # Import the template
        try
        {
            $InitTemplate = Get-Content (Join-Path $PSScriptRoot init.ps1.template) -Raw
        }
        catch
        {
            throw "Failed to import InitTemplate.`n$($_.Exception.Message)"
        }    
    }
    
    process
    {
        # We'll go over each permanent path and create an entry in the _init file that will resolve the path
        # to it's actual location
        $PermanentPathText = ""
        $PermanentPaths | ForEach-Object {
            # If we have a description we need to have that appear first
            if ($_.Description)
            {
                $PermanentPathText = $PermanentPathText + "# $($_.Description)`n"
            }
            # If we've got child paths we'll have to do some really fancy interpolation
            if ($_.ChildPaths)
            {
                $Path = "'$($_.Path)' " + $("'" + ($_.ChildPaths -join "','") + "'")
            }
            else
            {
                $Path = "'$($_.Path)'"
            }
            $PermanentPathText = $PermanentPathText + @"
`$Global:$($_.VariableName) = Join-Path `$global:RepoRootDirectory $Path | Convert-Path`n
"@

        }
        $InitTemplate = $InitTemplate.Replace('###PERMANENT_PATHS###', $PermanentPathText)

        # For our ephemeral paths we first need to define them as standalone variables, so we can create them
        # if they don't already exist
        $EphemeralPathText = ",`n"
        $EphemeralPaths | ForEach-Object -Process {
            # If we've got child paths we'll have to do some really fancy interpolation
            if ($_.ChildPaths)
            {
                $Path = "'$($_.Path)' " + $("'" + ($_.ChildPaths -join "','") + "'")
            }
            else
            {
                $Path = "'$($_.Path)'"
            }
            $EphemeralPathText = $EphemeralPathText + @"
    (`$$($_.VariableName) = Join-Path `$global:RepoRootDirectory $Path)
"@

            # We are building an array in the template
            # if this is the last line of the array then we don't want to add a comma!
            if ($_ -eq $EphemeralPaths[-1])
            {
                $EphemeralPathText = $EphemeralPathText + "`n"
            }
            else
            {
                $EphemeralPathText = $EphemeralPathText + ",`n"
            }
        }
        $InitTemplate = $InitTemplate.Replace('###EPHEMERAL_PATHS###', $EphemeralPathText)

        # Now we can create our global variables that reference their proper paths
        $EphemeralPathVariableText = "`n"
        $EphemeralPaths | ForEach-Object {
            # If we have a description we need to have that appear first
            if ($_.Description)
            {
                $EphemeralPathVariableText = $EphemeralPathVariableText + "# $($_.Description)`n"
            }
            $EphemeralPathVariableText = $EphemeralPathVariableText + @"
`$global:$($_.VariableName) = `$$($_.VariableName) | Convert-Path`n
"@

        }
        $InitTemplate = $InitTemplate.Replace('###EPHEMERAL_PATH_VARIABLES###', $EphemeralPathVariableText)

        # Here we set up our custom module loader for loading any Powershell modules we may have created in a given repo
        if ($IncludeModuleLoader)
        {
            $ModuleText = @'
 
# Find and load any custom PowerShell modules we've written for this repo
try
{
    Get-ChildItem $global:RepoCodeDirectory -Filter '*.psm1' -Recurse | Foreach-Object {
        Import-Module $_ -Force -Verbose:$false
    }
}
catch
{
    throw "Failed to import custom modules.`n$($_.Exception.Message)"
}
 
'@

        }
        $InitTemplate = $InitTemplate.Replace('###CUSTOM_MODULE_LOADER###', $ModuleText)

        # Finally we carry over any custom _init steps if the user has given them
        $InitTemplate = $InitTemplate.Replace('###CUSTOM_INIT_STEPS###', $CustomInitSteps)
    }   
    end
    {
        Return $InitTemplate
    }
}