-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
EZOut.psm1
80 lines (67 loc) · 3.12 KB
/
EZOut.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Justification="Using Within the Module")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="Allowing Extensibility")]
param()
$Script:FormatModules = @{}
$script:TypeModules = @{}
$commandsPath = Join-Path $psScriptRoot "Commands"
:ToIncludeFiles foreach ($file in (Get-ChildItem -Path "$commandsPath" -Filter "*-*" -Recurse)) {
if ($file.Extension -ne '.ps1') { continue } # Skip if the extension is not .ps1
foreach ($exclusion in '\.[^\.]+\.ps1$') {
if (-not $exclusion) { continue }
if ($file.Name -match $exclusion) {
continue ToIncludeFiles # Skip excluded files
}
}
. $file.FullName
}
. $psScriptRoot\@.ps1 # Import Splatter
$myInvocation.MyCommand.ScriptBlock.Module.pstypenames.insert(0,'EZOut.RichModuleInfo')
#region Import Parts
# Parts are simple .ps1 files beneath a /Parts directory that can be used throughout the module.
$partsDirectory = $( # Because we want to be case-insensitive, and because it's fast
foreach ($dir in [IO.Directory]::GetDirectories($psScriptRoot)) { # [IO.Directory]::GetDirectories()
if ($dir -imatch "\$([IO.Path]::DirectorySeparatorChar)Parts$") { # and some Regex
[IO.DirectoryInfo]$dir;break # to find our parts directory.
}
})
if ($partsDirectory) { # If we have parts directory
foreach ($partFile in $partsDirectory.EnumerateFileSystemInfos()) { # enumerate all of the files.
if ($partFile.Extension -ne '.ps1') { continue } # Skip anything that's not a PowerShell script.
$partName = # Get the name of the script.
$partFile.Name.Substring(0, $partFile.Name.Length - $partFile.Extension.Length)
$ExecutionContext.SessionState.PSVariable.Set( # Set a variable
$partName, # named the script that points to the script (e.g. $foo = gcm .\Parts\foo.ps1)
$ExecutionContext.SessionState.InvokeCommand.GetCommand($partFile.Fullname, 'ExternalScript').ScriptBlock
)
}
}
#endregion Import Parts
Update-FormatData -PrependPath $psScriptRoot\EZOut.format.ps1xml
$myModule = $myInvocation.MyCommand.ScriptBlock.Module
$executionContext.SessionState.PSVariable.Set(
$myModule.Name,
$myModule
)
Export-ModuleMember -Function * -Alias * -Variable $myModule.Name
try {
$newDriveSplat = @{Name = $myModule.Name;Scope = 'Global';PSProvider='FileSystem';ErrorAction='Ignore'}
$newDriveSplat.Root = $myModule | Split-Path
New-PSDrive @newDriveSplat
} catch {
Write-Verbose "$_"
}
$myModule.OnRemove = {
$myModule = Get-Module EZOut
$debuggingTypeNames = $myModule.DebuggingTypeNames
if ($debuggingTypeNames) {
foreach ($typeName in $debuggingTypeNames | Select-Object -Unique) {
try {
Remove-TypeData -TypeName $typeName -Confirm:$false -ErrorAction Ignore
} catch {
Write-Verbose "$_"
}
}
}
Clear-FormatData
Clear-TypeData
}