ModuleCreation.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function Test-PartOfTmpDirectory([string] $Destination, [switch] $ShowError)
{
    <#
    .SYNOPSIS
    Check if the given folder is part of the configured temp folder for environment modules.
    .PARAMETER Destination
    The folder to check.
    .OUTPUTS
    True if the folder is part of the temporary directory.
    #>

    $tmpDirectory = (Resolve-Path $script:tmpEnvironmentRootPath)

    if($Destination.StartsWith($tmpDirectory)) {
        if($ShowError) {
            Write-Error "The target destination is part of the temporary directory. Please specify another directory or set the force parameter."
        }
        return $true
    }

    return $false
}

function New-EnvironmentModule
{
    <#
    .SYNOPSIS
    Create a new environment module
    .DESCRIPTION
    This function will create a new environment module with the given parameter.
    .PARAMETER Name
    The name of the environment module to generate.
    .PARAMETER Author
    The author of the environment module. If this value is not specified, the system user name is used.
    .PARAMETER Description
    An optional description for the module.
    .OUTPUTS
    No outputs are returned.
    #>

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    Param(
        [String] $Name,
        [String] $Author = $null,
        [String] $Company = $null,
        [String] $Description = "",
        [String] $Version = "",
        [String] $Architecture = "",
        [String[]] $RequiredFiles = @(),
        [String[]] $SearchPaths = @(),
        [String[]] $Dependencies = @(),
        [hashtable] $Parameters = @{},
        [String] $Path = $null,
        [Switch] $Force
    )

    process {
        if([string]::IsNullOrEmpty($Name)) {
            Write-Error('A module name must be specified')
            return
        }
        if([string]::IsNullOrEmpty($Author)) {
            $Author = [Environment]::UserName
        }
        if([string]::IsNullOrEmpty($Company)) {
            $Company = ""
        }
        if([string]::IsNullOrEmpty($Description)) {
            $Description = ""
        }
        if([string]::IsNullOrEmpty($Version)) {
            $Version = ""
        }
        if([string]::IsNullOrEmpty($Architecture)) {
            $Version = ""
        }

        $environmentModulePath = Resolve-Path (Join-Path $script:moduleFileLocation "..")

        if([string]::IsNullOrEmpty($Path)) {
            $Path = Select-ModulePath
        }

        if($null -eq $Path) {
            return
        }

        if((-not $Force) -and (Test-PartOfTmpDirectory $Path)) {
            throw "The Path is Part of the tmp directory. Specifiy the Force parameter."
        }

        $requiredItemsList = [System.Collections.Generic.List[EnvironmentModuleCore.RequiredItem]]::new()
        foreach($requiredFile in $RequiredFiles) {
            $requiredItemsList.Add([EnvironmentModuleCore.RequiredItem]::new([EnvironmentModuleCore.RequiredItem]::TYPE_FILE, $requiredFile))
        }

        $searchPathsList = [System.Collections.Generic.List[EnvironmentModuleCore.SearchPath]]::new()
        foreach($searchPath in $SearchPaths) {
            $priority = $script:searchPathTypes[[EnvironmentModuleCore.SearchPath]::TYPE_DIRECTORY].Item2 + 20 # Get the default priority of the type and increase it because it is custom
            $searchPathsList.Add([EnvironmentModuleCore.SearchPath]::new($searchPath, [EnvironmentModuleCore.SearchPath]::TYPE_DIRECTORY, $priority, "", $true))
        }

        $dependenciesList = [System.Collections.Generic.List[EnvironmentModuleCore.DependencyInfo]]::new()
        foreach($dependency in $Dependencies) {
            $dependenciesList.Add([EnvironmentModuleCore.DependencyInfo]::new($dependency, $false))
        }

        $parametersDictionary = [System.Collections.Generic.Dictionary[string, string]]::new()
        foreach($key in $Parameters.Keys) {
            $parametersDictionary[$key] = $Parameters[$key]
        }

        [EnvironmentModuleCore.ModuleCreator]::CreateEnvironmentModule($Name, $Path, $Description, $environmentModulePath, $Author, $Company, $Version, $Architecture, $requiredItemsList.ToArray(), $searchPathsList.ToArray(), $dependenciesList.ToArray(), $parametersDictionary)
        Update-EnvironmentModuleCache
    }
}

function Copy-EnvironmentModule
{
    <#
    .SYNOPSIS
    Copy the given environment module under the given name and generate a new GUID.
    .DESCRIPTION
    This function will clone the given module and will specify a new GUID for it. If required, the module search path is adapted.
    .PARAMETER ModuleFullName
    The module to copy.
    .PARAMETER NewModuleFullName
    The new name of the module.
    .PARAMETER Path
    The target directory for the created module files. If this parameter is empty, a selection dialogue is displayed.
    .PARAMETER Force
    If this flag is set, the module can be created in a temp file location as well.
    .PARAMETER SkipCacheUpdate
    If this flag is set, the Update-EnvironmentModule function is not called.
    .OUTPUTS
    No outputs are returned.
    #>

    [CmdletBinding()]
    Param(
        [Switch] $Force,
        [Switch] $SkipCacheUpdate,
        [Switch] $AskForPathSelection
    )
    DynamicParam {
        $runtimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        $moduleSet =  Get-NonTempEnvironmentModules | Select-Object -ExpandProperty FullName
        Add-DynamicParameter 'ModuleFullName' String $runtimeParameterDictionary -Mandatory $True -Position 0 -ValidateSet $moduleSet

        Add-DynamicParameter 'NewModuleFullName' String $runtimeParameterDictionary -Mandatory $True -Position 1
        Add-DynamicParameter 'Path' String $runtimeParameterDictionary -Mandatory $False -Position 2

        return $runtimeParameterDictionary
    }

    begin {
        # Bind the parameter to a friendly variable
        $ModuleFullName = $PsBoundParameters['ModuleFullName']
        $NewModuleFullName = $PsBoundParameters['NewModuleFullName']
        $Path = $PsBoundParameters['Path']
    }

    process {
        $matchingModules = (Get-Module -ListAvailable $ModuleFullName)
        if($matchingModules.Length -lt 1) {
            Write-Error "Unable to find module matching name '$ModuleFullName'"
            return
        }
        if($matchingModules.Length -gt 1) {
            Write-Warning "Found multiple modules matching the name '$ModuleFullName'"
        }

        $moduleFolder = ($matchingModules[0]).ModuleBase
        $destination = Resolve-Path (Join-Path $moduleFolder '..')

        if($Path) {
            $destination = $Path
        }
        else {
            if($AskForPathSelection) {
                $selectedPath = Select-ModulePath

                if($null -eq $selectedPath) {
                    return
                }

                $destination = $selectedPath
            }
        }

        $destination = Join-Path $destination $NewModuleFullName

        if((-not $Force) -and (Test-PartOfTmpDirectory $selectedPath -ShowError)) {
            return
        }

        if((Test-Path $destination) -and (-not $Force)) {
            Write-Error "The folder $destination does already exist"
            return
        }

        New-Item -ItemType directory $destination -Force | Out-Null

        Write-Verbose "Cloning module $ModuleFullName to $destination"

        $filesToCopy = Get-ChildItem -File $moduleFolder
        $directoriesToCopy = Get-ChildItem -Directory $moduleFolder

        Write-Verbose "Found $($filesToCopy.Length) files to copy"

        foreach($file in $filesToCopy) {
            Write-Verbose "Handling file $file"
            $length = $file.Name.Length - $file.Extension.Length
            $shortName = $file.Name.Substring(0, $length)
            $newFileName = $file.Name

            Write-Verbose "Checking if `"$shortName`" matches `"$ModuleFullName`""
            if("$shortName" -match "$ModuleFullName") {
                $newFileName = "$($NewModuleFullName)$($file.Extension)"
            }

            $newFullName = Join-Path $destination $newFileName
            Copy-Item -Path "$($file.FullName)" -Destination "$newFullName"

            Write-Verbose "Handling file with extension $($file.Extension.ToUpper())"
            $fileContent = (Get-Content $newFullName)
            $newId = [Guid]::NewGuid()

            switch ($file.Extension.ToUpper()) {
                ".PSD1" {
                    $fileContent = $fileContent -replace "GUID[ ]*=[ ]*'[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}'", "GUID = '$newId'"
                }
                Default {}
            }

            ($fileContent.Replace("$ModuleFullName", "$NewModuleFullName")) > "$newFullName"
        }

        foreach($directory in $directoriesToCopy) {
            Write-Verbose "Handling directory $directory"
            Copy-Item -Recurse -Force -Path $directory.FullName -Destination $destination
        }

        if(-not $SkipCacheUpdate) {
            Update-EnvironmentModuleCache
        }
    }
}

# This argument completer is used by Copy-EnvironmentModule for the file filter
Register-ArgumentCompleter -CommandName Copy-EnvironmentModule -ParameterName Path -ScriptBlock {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
    $commandName | Out-Null; $parameterName | Out-Null; $wordToComplete | Out-Null; $commandAst | Out-Null; $fakeBoundParameter | Out-Null

    $env:PSModulePath.Split([IO.Path]::PathSeparator) | Where-Object {(Test-Path $_) -and -not (Test-PartOfTmpDirectory $_)} | Select-Object -Unique
}

function Edit-EnvironmentModule
{
    <#
    .SYNOPSIS
    Edit the environment module files.
    .DESCRIPTION
    This function will open the environment module files with the default editor.
    .PARAMETER Name
    The name of the environment module.
    .PARAMETER FileFilter
    The files of the module to edit. Default is "*.pse1".
    .OUTPUTS
    No outputs are returned.
    #>

    [CmdletBinding()]
    Param()
    DynamicParam {
        $runtimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        $moduleSet = Get-AllEnvironmentModules | Select-Object -ExpandProperty FullName
        Add-DynamicParameter 'ModuleFullName' String $runtimeParameterDictionary -Mandatory $True -Position 0 -ValidateSet $moduleSet

        Add-DynamicParameter 'FileFilter' String $runtimeParameterDictionary -Mandatory $False -Position 1
        return $runtimeParameterDictionary
    }

    begin {
        # Bind the parameter to a friendly variable
        $ModuleFullName = $PsBoundParameters['ModuleFullName']
        $FileFilter = $PsBoundParameters['FileFilter']

        if(-not $FileFilter) {
            $FileFilter = '*.pse1'
        }
    }

    process {
        $modules = Get-Module -ListAvailable $ModuleFullName

        if(($null -eq $modules) -or ($modules.Count -eq 0)) {
            Write-Error "The module was not found"
            return
        }

        Get-ChildItem ($modules[0].ModuleBase) | Where-Object {$_ -like $FileFilter} | Invoke-Item
    }
}

# This argument completer is used by Edit-EnvironmentModule for the file filter
Register-ArgumentCompleter -CommandName Edit-EnvironmentModule -ParameterName FileFilter -ScriptBlock {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $commandName | Out-Null; $parameterName | Out-Null; $wordToComplete | Out-Null; $commandAst | Out-Null
    $module = Get-EnvironmentModule -ListAvailable $fakeBoundParameter["ModuleFullName"]

    if($module) {
        Get-ChildItem $module.ModuleBase
    }
}

function Select-ModulePath
{
    <#
    .SYNOPSIS
    Show a selection dialog for module paths.
    .DESCRIPTION
    This function will an input selection for all module paths that are defined.
    .OUTPUTS
    The selected module path or $null if no path was selected.
    #>

    $pathPossibilities = $env:PSModulePath.Split([IO.Path]::PathSeparator) | Where-Object {(Test-Path $_) -and -not (Test-PartOfTmpDirectory $_)} | Select-Object -Unique
    return Show-SelectDialogue $pathPossibilities "Select the target directory for the module"
}