-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
PSScriptTools.psm1
199 lines (176 loc) · 6.58 KB
/
PSScriptTools.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
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
#enable verbose messaging in the psm1 file
if ($MyInvocation.line -match '-verbose') {
$VerbosePreference = 'continue'
}
Write-Verbose 'Loading public functions'
#exclude files that have special requirements
Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 -Exclude 'Get-MyCounter.ps1', 'Get-FileExtensionInfo.ps1','CimMember.ps1' |
ForEach-Object -Process {
Write-Verbose $_.FullName
. $_.FullName
}
Write-Verbose 'Loading Windows-specific commands'
if ($IsWindows -OR ($PSEdition -eq 'Desktop')) {
. "$PSScriptRoot\functions\Get-MyCounter.ps1"
. "$PSScriptRoot\functions\CimMember.ps1"
}
if ($IsCoreCLR) {
Write-Verbose 'Loading PowerShell 7 specific commands'
. "$PSScriptRoot\functions\Get-FileExtensionInfo.ps1"
}
#load ANSIFile Entry format if user is not using $PSStyle
if (-Not $PSStyle.FileInfo) {
Write-Verbose "Using module PSAnsiFile features"
Write-Verbose 'Loading PSAnsiFile format files'
Update-FormatData -AppendPath "$PSScriptRoot\formats\psansifileentry.format.ps1xml"
Update-FormatData -AppendPath "$PSScriptRoot\formats\filesystem-ansi.format.ps1xml"
Write-Verbose 'Define the global PSAnsiFileMap variable'
$json = 'psansifilemap.json'
#test for user version in $HOME
$UserJSON = Join-Path -Path $HOME -ChildPath $json
$moduleJSON = Join-Path -Path $PSScriptRoot -ChildPath $json
if (Test-Path -Path $UserJSON) {
$map = $UserJSON
}
else {
#use the file from this module
$map = $moduleJSON
}
#ConvertFrom-Json doesn't write simple objects to the pipeline in Windows PowerShell so I
#need to process the results individually.
$mapData = [System.Collections.Generic.List[object]]::new()
Get-Content -Path $map | ConvertFrom-Json | ForEach-Object { $_ } | ForEach-Object {
$entry = [PSCustomObject]@{
PSTypeName = 'PSAnsiFileEntry'
Description = $_.description
Pattern = $_.pattern
Ansi = $_.ansi
}
$mapData.Add($entry)
}
Set-Variable -Name PSAnsiFileMap -Value $mapData -Scope Global
} #load PSAnsiFile features
Write-Verbose 'Define special character map'
$global:PSSpecialChar = @{
FullBlock = ([char]0x2588)
LightShade = ([char]0x2591)
MediumShade = ([char]0x2592)
DarkShade = ([char]0x2593)
BlackSquare = ([char]0x25A0)
WhiteSquare = ([char]0x25A1)
BlackSmallSquare = ([char]0x25AA)
WhiteSmallSquare = ([char]0x25AB)
UpTriangle = ([char]0x25B2)
DownTriangle = ([char]0x25BC)
Lozenge = ([char]0x25CA)
WhiteCircle = ([char]0x25CB)
BlackCircle = ([char]0x25CF)
WhiteFace = ([char]0x263A)
BlackFace = ([char]0x263B)
SixPointStar = ([char]0x2736)
Diamond = ([char]0x2666)
Club = ([char]0x2663)
Heart = ([char]0x2665)
Spade = ([char]0x2660)
Section = ([char]0x00A7)
RightPointer = ([char]0x25BA)
LeftPointer = ([char]0x25C4)
BlackRectangle = ([char]0x25AC)
}
Write-Verbose "Defining the variable `$PSSamplePath to the samples folder for this module"
$global:PSSamplePath = Join-Path -Path $PSScriptRoot -ChildPath Samples
#define a private variable with PSScriptTools data
$ToolDataPath = "$PSScriptRoot\PSScriptToolData.json"
#region editor integrations
Write-Verbose 'Add ToDo options to the ISE or VS Code'
if ($psEditor) {
Write-Verbose 'Defining VSCode additions'
$sb = {
Param($context = $psEditor.GetEditorContext() )
$prompt = 'What do you need to do?'
$title = 'To Do'
$item = Invoke-InputBox -Title $title -Prompt $prompt
$todo = "# TODO: $item [$(Get-Date)]"
$context.CurrentFile.InsertText($todo)
}
$rParams = @{
Name = 'Insert.ToDo'
DisplayName = 'Insert ToDo'
ScriptBlock = $sb
SuppressOutput = $false
}
Register-EditorCommand @rParams
Write-Verbose 'Adding Set-LocationToFile'
Function Set-LocationToFile {
#set location to directory of current file
[CmdletBinding()]
[alias('sd', 'jmp')]
[OutputType('none')]
Param ()
if ($host.name -match 'Code') {
$context = $psEditor.GetEditorContext()
$ThisPath = $context.CurrentFile.Path
$target = Split-Path -Path $ThisPath
Write-Verbose "Using $ThisPath"
Write-Verbose "Changing to $target"
Set-Location -Path $target
Clear-Host
}
else {
Write-Warning 'This command must be run in the VS Code integrated PowerShell terminal.'
}
}
} #VSCode
elseif ($psISE) {
Write-Verbose 'Defining ISE additions'
if ($psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.DisplayName -NotContains 'ToDo') {
$action = {
$prompt = 'What do you need to do?'
$title = 'To Do'
$item = Invoke-InputBox -Title $title -Prompt $prompt
$todo = "# [$(Get-Date)] TODO: $item"
$psISE.CurrentFile.Editor.InsertText($todo)
#jump cursor to the end
$psISE.CurrentFile.editor.SetCaretPosition($psISE.CurrentFile.Editor.CaretLine, $psISE.CurrentFile.Editor.CaretColumn)
}
#add the action to the Add-Ons menu
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('ToDo', $Action, 'Ctrl+Alt+2' ) | Out-Null
}
Function Set-LocationToFile {
[cmdletbinding()]
[alias('sd', 'jmp')]
[OutputType('none')]
Param()
if ($host.name -match 'ISE') {
$path = Split-Path -Path $psISE.CurrentFile.FullPath
Set-Location -Path $path
Clear-Host
}
Else {
Write-Warning 'This command must be run the the PowerShell ISE.'
}
}
}
#endregion
#define a function to open the PDF version of the README and other documentation
Function Open-PSScriptToolsHelp {
[cmdletbinding()]
Param()
Write-Verbose "Starting $($MyInvocation.MyCommand)"
$pdf = Join-Path -Path $PSScriptRoot -ChildPath PSScriptToolsManual.pdf
Write-Verbose "Testing the path $pdf"
if (Test-Path -Path $pdf) {
Try {
Write-Verbose 'Invoking the PDF'
Invoke-Item -Path $pdf -ErrorAction Stop
}
Catch {
Write-Warning "Failed to automatically open the PDF. You will need to manually open $pdf."
}
}
else {
Write-Warning "Can't find $pdf."
}
Write-Verbose "Ending $($MyInvocation.MyCommand)"
}
$VerbosePreference = 'SilentlyContinue'