-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyProfile.psm1
167 lines (141 loc) · 5.32 KB
/
MyProfile.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
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
$moduleSw = [System.Diagnostics.Stopwatch]::StartNew()
$script:timers = @()
########
# Debug Mode
########
if (("Desktop" -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSVersion.Major -ge 7)) {
# Detect Shift Key
try {
# Check SHIFT state ASAP at startup so I can use that to control verbosity :)
Add-Type -Assembly PresentationCore, WindowsBase
if ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::LeftShift) -OR
[System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::RightShift)) {
$ProfileDebugMode = "true"
}
} catch {
# If that didn't work ... oh well.
}
}
if ($env:MyProfileDebugMode) {
$ProfileDebugMode = "true"
}
########
# Critical Variables
########
$moduleNamespace = "MyPro"
$messagePrefix = "$($PSStyle.Foreground.BrightBlack)${moduleNamespace}$($PSStyle.Reset): "
########
# Critical Functions
########
function Write-MyProDebug() {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[string]$Message
)
if ($ProfileDebugMode) {
$space = " " * $script:VerboseDepth
Write-Information "${messagePrefix}$($PSStyle.Foreground.Cyan)DEBUG$($PSStyle.Reset): ${space}${Message}" -Tags @('MyPro', 'Debug') -InformationAction "Continue"
}
}
function Write-MyProError() {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[string]$Message
)
$space = " " * $script:VerboseDepth
Write-Information "${messagePrefix}$($PSStyle.Foreground.Red)ERROR$($PSStyle.Reset): ${space}${Message}" -Tags @('MyPro', 'Error') -InformationAction "Continue"
}
if ($ProfileDebugMode) {
Write-MyProDebug ">>> MyProfile Runing in DEBUG Mode <<<"
}
$script:VerboseDepth = 0
$script:VerboseBlockName = [System.Collections.Stack]::new()
Function VerboseBlock {
param(
[Parameter(Mandatory=$true)]
$Name,
[Parameter(Mandatory=$true)]
[scriptblock]$Scriptblock
)
if ($ProfileDebugMode) {
Write-MyProDebug "🔽 '$($PSStyle.Foreground.BrightYellow)$Name$($PSStyle.Reset)'"
}
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$script:VerboseDepth++
$script:VerboseBlockName.Push($Name)
$Scriptblock.Invoke()
$sw.Stop
$nameArray = $script:VerboseBlockName.ToArray()
[array]::Reverse($nameArray)
$fullName = [string]::Join( " ", $nameArray)
$script:timers += [PSCustomObject]@{ Name = $fullName; Timer = $sw.ElapsedMilliseconds }
$null = $script:VerboseBlockName.Pop()
$script:VerboseDepth--
if ($ProfileDebugMode) {
Write-MyProDebug "🔼 '$($PSStyle.Foreground.BrightYellow)$Name$($PSStyle.Reset)' $($sw.ElapsedMilliseconds)ms"
}
}
########
# Setup Profile
########
$script:abort = $false
VerboseBlock "Checking Modules" {
$requiredModules = @(
@{ Name = "PSReadline"; MinimumVersion = "2.2.0" }
@{ Name = "Terminal-Icons" }
@{ Name = "posh-git" }
)
$requiredModules | ForEach-Object {
$_.Found = (Get-Module $_.Name -ListAvailable | Select-Object -First 1).Version
Write-MyProDebug "🎯 $($_.Name) $($_.MinimumVersion) 🔎 $($_.Found)"
if ($null -eq $_.Found) {
$script:abort = $true
Write-MyProError "${prefix}Missing module $($_.Name)"
} elseif ($null -ne $_.MinimumVersion -and [Version]::new($_.MinimumVersion) -gt [Version]::new($_.Found)) {
$script:abort = $true
Write-MyProError "${prefix}Expected module $($_.Name) to >= $($_.MinimumVersion). Found $($_.Found)"
}
}
}
if ($script:abort) {
Write-MyProError "Aborting Profile Import."
throw "Aborting Profile Import."
}
VerboseBlock "Functions" {
$script:publicFunctions = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$privateFunctions = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )
foreach($import in @($publicFunctions + $privateFunctions))
{
try
{
Write-MyProDebug "📥 $($import.BaseName)"
. $import.FullName
}
catch
{
Write-MyProError -Message "Failed to import function $($import.FullName): $_"
}
}
}
VerboseBlock "Exports" {
foreach ($item in $publicFunctions) {
Write-MyProDebug "📤 $($item.BaseName)"
Export-ModuleMember -Function $item.BaseName
}
}
VerboseBlock "Config Blocks" {
$configs = @(Get-ChildItem -Path $PSScriptRoot\config\*.ps1 -ErrorAction SilentlyContinue ) | Sort-Object -Property Name
foreach ($item in $configs) {
Write-MyProDebug "⚙️ $($item.BaseName)"
. $item.FullName
}
}
$moduleSw.Stop()
$script:timers += @{ Name = 'Total'; Timer = $moduleSw.ElapsedMilliseconds }
$longest = ($script:timers | Sort-Object -Property @{ Expression = { $_.Name.Length } } -Descending | Select-Object -First 1).Name.Length
$script:timers | ForEach-Object {
Write-MyProDebug ("⌛ {0,-$longest} {1,5} ms" -f $_.Name, $_.Timer)
}
Write-Information "⏲️ $($PSStyle.Foreground.BrightYellow)MyProfile$($PSStyle.Foreground.BrightBlue) processed in $($PSStyle.Foreground.BrightYellow)$($moduleSw.ElapsedMilliseconds)$($PSStyle.Foreground.BrightBlue) ms.$($PSStyle.Reset)" -InformationAction 'Continue' -Tags @($moduleNamespace)