forked from DBremen/PowerShellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-ScriptHelpISEAddOn.ps1
More file actions
376 lines (347 loc) · 17 KB
/
Add-ScriptHelpISEAddOn.ps1
File metadata and controls
376 lines (347 loc) · 17 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#Requires -Modules ShowUI
Import-Module ShowUI
#region ConvertTo-ISEAddon
#based on function from James Brundage
function ConvertTo-ISEAddOn
{
[CmdletBinding(DefaultParameterSetName="CreateOnly")]
param(
[Parameter(Mandatory=$true,
ParameterSetName="DisplayNow")]
[string]$DisplayName,
[Parameter(Mandatory=$true,
ParameterSetName="CreateOnly")]
[Parameter(Mandatory=$true,
ParameterSetName="DisplayNow")]
[ScriptBlock[]]
$ScriptBlock,
[Parameter(Mandatory=$true,
ParameterSetName="CreateOnly")]
$DLLPath,
[Parameter(Mandatory=$true,
ParameterSetName="CreateOnly")]
$Namespace,
[Parameter(Mandatory=$true,
ParameterSetName="CreateOnly")]
[string[]]$Class,
[Parameter(ParameterSetName="DisplayNow")]
[switch]
$AddVertically,
[Parameter(ParameterSetName="DisplayNow")]
[switch]
$AddHorizontally,
[Parameter(Mandatory=$true,
ParameterSetName="DisplayNow")]
[switch]
$Visible,
[Parameter(Mandatory=$true,
ParameterSetName="DisplayNow")]
[switch]
$addMenu
)
begin {
if ($psVersionTable.PSVersion -lt "3.0") {
Write-Warning "Ise Window Add ons were not added until version 3.0."
return
}
}
process {
$Class = @($Class)
$addOnNumber = Get-Random
$addOnTypeStart =@"
$(if ($PSCmdlet.PARAMETEReterSetName -eq 'CreateOnly'){
"namespace $Namespace"
}else{
"namespace ISEAddOn"
})
{
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.PowerShell.Host.ISE;
using System.Collections.Generic;
using System.Windows.Input;
using System.Text;
"@
$index=0
$(foreach ($currSB in $ScriptBlock){
$addOnTypeMiddle+=@"
$(if ($PSCmdlet.PARAMETEReterSetName -eq 'CreateOnly'){
"public class $($Class[$index]) : UserControl, IAddOnToolHostObject"
}else{
"public class IseAddOn${addOnNumber} : UserControl, IAddOnToolHostObject"
})
{
ObjectModelRoot hostObject;
#region IAddOnToolHostObject Members
public ObjectModelRoot HostObject
{
get
{
return this.hostObject;
}
set
{
this.hostObject = value;
this.hostObject.CurrentPowerShellTab.PropertyChanged += new PropertyChangedEventHandler(CurrentPowerShellTab_PropertyChanged);
Run();
}
}
private void CurrentPowerShellTab_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "CanInvoke" && this.hostObject.CurrentPowerShellTab.CanInvoke)
{
if (this.Content != null && this.Content is UIElement ) {
(this.Content as UIElement).IsEnabled = true;
}
} else {
if (this.Content != null && this.Content is UIElement) {
(this.Content as UIElement).IsEnabled = true;
}
}
}
$(if ($PSCmdlet.PARAMETEReterSetName -eq 'CreateOnly'){
"public $($Class[$index])() { }"
}else{
"public IseAddOn${addOnNumber}() { }"
})
public void Run() {
if (Runspace.DefaultRunspace == null ||
Runspace.DefaultRunspace.ApartmentState != System.Threading.ApartmentState.STA ||
Runspace.DefaultRunspace.ThreadOptions != PSThreadOptions.UseCurrentThread) {
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "ShowUI" });
Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.ApartmentState = System.Threading.ApartmentState.STA;
rs.ThreadOptions = PSThreadOptions.UseCurrentThread;
rs.Open();
rs.SessionStateProxy.SetVariable("psise", HostObject);
Runspace.DefaultRunspace = rs;
}
PowerShell psCmd = PowerShell.Create().AddScript(@"
$($currSB.ToString().Replace('"','""'))
");
psCmd.Runspace = Runspace.DefaultRunspace;
try {
this.Content = psCmd.Invoke<UIElement>()[0];
} catch {
}
}
#endregion
}
"@
$index++
})
$addOnType=$addOnTypeStart + $addOnTypeMiddle + "}"
$presentationFramework = [System.Windows.Window].Assembly.FullName
$presentationCore = [System.Windows.UIElement].Assembly.FullName
$windowsBase=[System.Windows.DependencyObject].Assembly.FullName
$gPowerShell=[Microsoft.PowerShell.Host.ISE.PowerShellTab].Assembly.FullName
$systemXaml=[system.xaml.xamlreader].Assembly.FullName
$systemManagementAutomation=[psobject].Assembly.FullName
if ($PSCmdlet.PARAMETEReterSetName -eq 'CreateOnly'){
Add-Type -TypeDefinition $addOnType -ReferencedAssemblies $systemManagementAutomation,$presentationFramework,$presentationCore,$windowsBase,$gPowerShell,$systemXaml -ignorewarnings -OutputAssembly $DLLPath -OutputType Library
return
}
else{
$t = add-type -TypeDefinition $addOnType -ReferencedAssemblies $systemManagementAutomation,$presentationFramework,$presentationCore,$windowsBase,$gPowerShell,$systemXaml -ignorewarnings -PassThru
if ($AddHorizontally) {
$psISE.CurrentPowerShellTab.HorizontalAddOnTools.Add("$displayName",$t,$true)
}
elseif ($addVertically) {
$psISE.CurrentPowerShellTab.VerticalAddOnTools.Add("$displayName",$t,$true)
}
if($addMenu){
$name="ISEAddon$addonNumber"
$sb = [scriptblock]::Create('$psISE.CurrentPowerShellTab.VerticalAddOnTools.Add("' + $name + '",[ISEAddon.ISEAddon' + $addonNumber + '],$true);($psISE.CurrentPowerShellTab.VerticalAddOnTools | where {$_.Name -eq "' + $name + '"}).IsVisible=$true')
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add($name, $sb, $null)
}
}
}
}
#endregion
#for testing
#ConvertTo-ISEAddOn -ScriptBlock $addScriptHelp -AddVertically -Visible -DisplayName "Add-ScriptHelp" -addMenu
#compile to dl
function Add-ScriptHelpISEAddOn {
<#
.SYNOPSIS
Function to create an ISE Add-On that will generate comment based help for functions. The functions requires the Show-UI module.
.DESCRIPTION
Creates an ISE Add-On that integrates itself graphically in a similar way as the built-in Show-Command Add-On (using the VerticalAddOnTools pane)
without having to use Visual Studio and writing the code in C#.
.PARAMETER DLLPath
Path of the dll file that the function either generates (using -GenerateDll switch) or loads (see example).
Defaults to "$env:USERPROFILE\Desktop\AddScriptHelp.dll".
.PARAMETER GenerateDll
Switch to indicate whether the function should Generate the dll and run the ISE Add-On or just run the ISE Add-On.(see example)
.EXAMPLE
#Generate the dll (needed only once)
Add-ScriptHelpISEAddOn -Dll-GenerateDll
#optionally add the following to your profile in order to load the script automaticaly on ISE startup and add an entry in the Add-ons menu
if ($host.Name -eq 'Windows PowerShell ISE Host'){
Add-ScriptHelpISEAddOn -DllPath <PATH TO GENERATED DLL>
}
.LINK
https://powershellone.wordpress.com/2015/09/28/create-an-integrated-wpf-based-ise-add-on-with-powershell/
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$DllPath = "$env:USERPROFILE\Desktop\AddScriptHelp.dll",
[Parameter(Mandatory)]
[switch]$GenerateDll
)
if ($GenerateDll){
#region AddScriptHelp scriptBlock
$addScriptHelp = {
#get the parameters of the enclosing function at the current cursor position if any
$lineNumber = $psISE.CurrentPowerShellTab.Files.SelectedFile.Editor.CaretLine
$code = $psISE.CurrentPowerShellTab.Files.SelectedFile.Editor.Text
$Errors = $Tokens = $null
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Code, [ref]$Tokens, [ref]$Errors)
$functions = $AST.FindAll({ $args[0].GetType().Name -like "*FunctionDefinition*Ast" }, $true )
$enclosingFunctionParamNames = -1
foreach ($function in $functions){
if ($function.Extent.StartLineNumber -le $lineNumber -and $function.Extent.EndLineNumber -ge $lineNumber){
$enclosingFunctionParamNames = $function.PARAMETEReters.Name.VariablePath.UserPath
break
}
}
$dynamicParams = $false
if ($enclosingFunctionParamNames -ne -1){
$dynamicParams = $true
}
New-StackPanel {
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "Synopsis"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtSynopsis"
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "Description"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtDescription"
if ($dynamicParams){
foreach ($paramName in $enclosingFunctionParamNames){
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "Parameter description: $paramName"
New-TextBox -Margin "7, 5, 7, 5" -Name ("txt$paramName" + 'Desc')
}
}
else{
New-TextBox -Margin "7, 5, 7, 5" -Name "txtFirstParamName"
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "1. Param Description"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtFirstParamDesc"
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "2. Param"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtSecondParamName"
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "2. Param Description"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtSecondParamDesc"
}
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "Link"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtLink"
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "1. Example"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtFirstExample"
New-TextBlock -FontSize 17 -Margin "24 2 0 3" -FontWeight Bold -Text "2. Example"
New-TextBox -Margin "7, 5, 7, 5" -Name "txtSecondExample"
New-CheckBox -Margin "5, 5, 2, 0" -Name "chkOutput" {
New-StackPanel -Margin "3,-5,0,0" {
New-TextBlock -Name "OutputText" -FontSize 16 -FontWeight Bold -Text "Copy to clipboard"
New-TextBlock -FontSize 14 TextWrapping Wrap
}
}
New-Button -HorizontalAlignment Stretch -Margin 7 {
New-TextBlock -FontSize 17 -FontWeight Bold -Text "Add to ISE"
} -On_Click{
$txtSynopsis = ($this.Parent.Children | Where-Object {$_.Name -eq "txtSynopsis"}).Text
$txtDescription = ($this.Parent.Children | Where-Object {$_.Name -eq "txtDescription"}).Text
if ($dynamicParams){
foreach ($paramName in $enclosingFunctionParamNames){
Set-Variable ("txt$paramName" + 'Desc') -Value ($this.Parent.Children |
Where-Object {$_.Name -eq ("txt$paramName" + 'Desc')}).Text
}
}
else{
$txtFirstParamName = ($this.Parent.Children | Where-Object {$_.Name -eq "txtFirstParamName"}).Text
$txtFirstParamDesc = ($this.Parent.Children | Where-Object {$_.Name -eq "txtFirstParamDesc"}).Text
$txtSecondParamName = ($this.Parent.Children | Where-Object {$_.Name -eq "txtSecondParamName"}).Text
$txtSecondParamDesc = ($this.Parent.Children | Where-Object {$_.Name -eq "txtSecondParamDesc"}).Text
}
$txtLink = ($this.Parent.Children | Where-Object {$_.Name -eq "txtLink"}).Text
$txtFirstExample = ($this.Parent.Children | Where-Object {$_.Name -eq "txtFirstExample"}).Text
$txtSecondExample = ($this.Parent.Children | Where-Object {$_.Name -eq "txtSecondExample"}).Text
$chkOutput = ($this.Parent.Children | Where-Object {$_.Name -eq "chkOutput"}).isChecked
$helptext=@"
<#
.SYNOPSIS
$txtSynopsis
.DESCRIPTION
$txtDescription
"@
if ($dynamicParams){
foreach ($paramName in $enclosingFunctionParamNames){
$txtParamDesc = Get-Variable -Name ("txt$paramName" + 'Desc') -ValueOnly
$helpText+="`n`t.PARAMETERETER $paramName`n`t`t$txtParamDesc"
}
}
else{
if ($txtFirstParamName) {
$helpText+="`n`t.PARAMETERETER $txtFirstParamName`n`t`t$txtFirstParamDesc"
}
if ($txtSecondParamName) {
$helpText+="`n`t.PARAMETERETER $txtSecondParamName`n`t`t$txtSecondParamDesc"
}
}
if ($txtFirstExample) {
$helpText+="`n`t.EXAMPLE`n`t`t$txtFirstExample"
}
if ($txtSecondExample) {
$helpText+="`n`t.EXAMPLE`n`t`t$txtSecondExample"
}
if ($txtLink) {
$helpText+="`n`t.LINK`n`t`t$txtLink"
}
$helpText+="`n" + @"
.NOTES
CREATED: $((Get-Date).ToShortDateString())
AUTHOR : $env:USERNAME
Changelog:
----------------------------------------------------------------------------------
Name Date Description
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
"@.TrimEnd() + "`n`t#>"
if ($chkOutput) {
$helptext | clip
}
$psise.CurrentPowerShellTab.Files.SelectedFile.Editor.InsertText($helpText)
}
}
}
#endregion
ConvertTo-ISEAddOn -ScriptBlock $addScriptHelp -NameSpace ISEUtils -DLLPath $DllPath -class AddScriptHelp
$message = @"
The dll has been compiled. You can add...:
if (`$host.Name -eq 'Windows PowerShell ISE Host'){
Add-ScriptHelpISEAddOn -DllPath <PATH TO GENERATED DLL>
}
... to your profile in order to have it loaded every time you start the ISE
"@
Write-Host $message
}
Add-Type -Path $DllPath
$addScriptHelp = {
#check if the AddOn if loaded if yes unload and re-load it
$currentNameIndex = -1
$name = 'Add-ScriptHelp'
$currentNames = $psISE.CurrentPowerShellTab.VerticalAddOnTools.Name
if ($currentNames){
$currentNameIndex = $currentNames.IndexOf($name)
if ($currentNameIndex -ne -1){
$psISE.CurrentPowerShellTab.VerticalAddOnTools.RemoveAt($currentNameIndex)
}
}
$psISE.CurrentPowerShellTab.VerticalAddOnTools.Add($name,[ISEUtils.AddScriptHelp],$true)
($psISE.CurrentPowerShellTab.VerticalAddOnTools | Where-Object {$_.Name -eq $name}).IsVisible=$true
}
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Add-ScriptHelp', $addScriptHelp, $null)
}