-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scout.psm1
56 lines (42 loc) · 2.23 KB
/
Scout.psm1
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
using namespace System.Web
using namespace System.Text.RegularExpressions
$directorySeparator = [System.IO.Path]::DirectorySeparatorChar
$moduleName = $PSScriptRoot.Split($directorySeparator)[-2]
$moduleManifest = $PSScriptRoot + $directorySeparator + $moduleName + '.psd1'
$publicFunctionsPath = $PSScriptRoot + $directorySeparator + 'Public' + $directorySeparator
$privateFunctionsPath = $PSScriptRoot + $directorySeparator + 'Private' + $directorySeparator
$currentManifest = Test-ModuleManifest $moduleManifest
$aliases = @()
$publicFunctions = Get-ChildItem -Path $publicFunctionsPath | Where-Object {$_.Extension -eq '.ps1'}
$privateFunctions = Get-ChildItem -Path $privateFunctionsPath | Where-Object {$_.Extension -eq '.ps1'}
$publicFunctions | ForEach-Object { . $_.FullName }
$privateFunctions | ForEach-Object { . $_.FullName }
# Export all of the public functions from this module
$publicFunctions | ForEach-Object {
# The command has already been sourced in above. Query any defined aliases.
$alias = Get-Alias -Definition $_.BaseName -ErrorAction SilentlyContinue
if ($alias) {
$aliases += $alias
Export-ModuleMember -Function $_.BaseName -Alias $alias
}
else {
Export-ModuleMember -Function $_.BaseName
}
}
$functionsAdded = $publicFunctions | Where-Object {$_.BaseName -notin $currentManifest.ExportedFunctions.Keys}
$functionsRemoved = $currentManifest.ExportedFunctions.Keys | Where-Object {$_ -notin $publicFunctions.BaseName}
$aliasesAdded = $aliases | Where-Object {$_ -notin $currentManifest.ExportedAliases.Keys}
$aliasesRemoved = $currentManifest.ExportedAliases.Keys | Where-Object {$_ -notin $aliases}
if ($functionsAdded -or $functionsRemoved -or $aliasesAdded -or $aliasesRemoved) {
try {
$updateModuleManifestParams = @{}
$updateModuleManifestParams.Add('Path', $moduleManifest)
$updateModuleManifestParams.Add('ErrorAction', 'Stop')
if ($aliases.Count -gt 0) { $updateModuleManifestParams.Add('AliasesToExport', $aliases) }
if ($publicFunctions.Count -gt 0) { $updateModuleManifestParams.Add('FunctionsToExport', $publicFunctions.BaseName) }
Update-ModuleManifest @updateModuleManifestParams
}
catch {
$_ | Write-Error
}
}