ScriptBrowser.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
#requires -Version 3

#Import Localized Data
Import-LocalizedData -BindingVariable Messages

# If Script Browser is installed by using standalone setup package,
# then this module is conflicted with the module in "$HOME\Documents\WindowsPowerShell".
# Please uninstall Script Browser first if you want to use PSGet to get the latest version of Script Browser.
if ($env:PROCESSOR_ARCHITECTURE -eq "X86")
{
    $installDir = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Script Browser\" -PSProperty "Install Directory" -ErrorAction:SilentlyContinue
}
else
{
    $installDir = Get-ItemProperty -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Script Browser\" -PSProperty "Install Directory" -ErrorAction:SilentlyContinue
}

if ($installDir)
{
    $errorMsg = $Messages.UninstallScriptBrowser
    throw $errorMsg        
}


# Adds Script Browser to Windows PowerShell ISE.
function Enable-ScriptBrowser
{
    [CmdletBinding()]
    Param()

    Process
    {
        # This function is only available in Windows PowerShell ISE console pane.
        TestWindowsPowerShellISE -FunctionName "Enable-ScriptBrowser"

        # Get script root.
        $scriptRoot = $PSCmdlet.MyInvocation.MyCommand.Module.ModuleBase

        try
        {
            Add-Type -Path "$scriptRoot\bin\System.Windows.Interactivity.dll" -ErrorAction:Stop
            Add-Type -Path "$scriptRoot\bin\ScriptBrowser.dll" -ErrorAction:Stop
            $scriptBrowser = $psISE.CurrentPowerShellTab.VerticalAddOnTools.Add('Script Browser', [ScriptExplorer.Views.MainView], $true)
            $psISE.CurrentPowerShellTab.VisibleVerticalAddOnTools.SelectedAddOnTool = $scriptBrowser
        }
        catch
        {
            Write-Error -ErrorRecord $PSItem
        }
    }
} # end Enable-ScriptBrowser.

# Removes Script Browser from Windows PowerShell ISE.
function Disable-ScriptBrowser
{
    [CmdletBinding()]
    Param()

    Process
    {
        # This function is only available in Windows PowerShell ISE console pane.
        TestWindowsPowerShellISE -FunctionName "Disable-ScriptBrowser"

        try
        {
            $scriptBrowser = $psISE.CurrentPowerShellTab.VerticalAddOnTools | `
                Where-Object -FilterScript {$PSItem.Control.ToString() -eq "ScriptExplorer.Views.MainView"}

            if ($scriptBrowser -ne $null)
            {
                [void]$psISE.CurrentPowerShellTab.VerticalAddOnTools.Remove($scriptBrowser)
            }
        }
        catch
        {
            Write-Error -ErrorRecord $PSItem
        }
    }
} # end Disable-ScriptBrowser.

# Adds Script Analyzer to Windows PowerShell ISE.
function Enable-ScriptAnalyzer
{
    [CmdletBinding()]
    Param()

    Process
    {
        # This function is only available in Windows PowerShell ISE console pane.
        TestWindowsPowerShellISE -FunctionName "Enable-ScriptAnalyzer"

        # Get script root.
        $scriptRoot = $PSCmdlet.MyInvocation.MyCommand.Module.ModuleBase

        try
        {
            Add-Type -Path "$scriptRoot\bin\System.Windows.Interactivity.dll" -ErrorAction:Stop
            Add-Type -Path "$scriptRoot\bin\BestPractices.dll" -ErrorAction:Stop
            $scriptAnalyzer = $psISE.CurrentPowerShellTab.VerticalAddOnTools.Add('Script Analyzer', [BestPractices.Views.BestPracticesView], $true)
            $psISE.CurrentPowerShellTab.VisibleVerticalAddOnTools.SelectedAddOnTool = $scriptAnalyzer
        }
        catch
        {
            Write-Error -ErrorRecord $PSItem
        }
    }
} # end Enable-ScriptAnalyzer.

# Removes Script Analyzer from Windows PowerShell ISE.
function Disable-ScriptAnalyzer
{
    [CmdletBinding()]
    Param()

    Process
    {
        # This function is only available in Windows PowerShell ISE console pane.
        TestWindowsPowerShellISE -FunctionName "Disable-ScriptAnalyzer"

        try
        {
            $scriptAnalyzer = $psISE.CurrentPowerShellTab.VerticalAddOnTools | `
                Where-Object -FilterScript {$PSItem.Control.ToString() -eq "BestPractices.Views.BestPracticesView"}

            if ($scriptAnalyzer -ne $null)
            {
                [void]$psISE.CurrentPowerShellTab.VerticalAddOnTools.Remove($scriptAnalyzer)
            }
        }
        catch
        {
            Write-Error -ErrorRecord $PSItem
        }
    }
} # end Disable-ScriptAnalyzer.

# Start Script Browser desktop application.
function Start-ScriptBrowserDesktop
{
    [CmdletBinding()]
    Param()

    Process
    {
        # Get script root.
        $scriptRoot = $PSCmdlet.MyInvocation.MyCommand.Module.ModuleBase

        try
        {
            Start-Process -FilePath "$scriptRoot\bin\ScriptBrowserDesktop.exe" -ErrorAction:Stop
        }
        catch
        {
            Write-Error -ErrorRecord $PSItem
        }
    }
} # end Start-ScriptBrowserApp.

#region Helper function(s)

# Some functions are only available in Windows PowerShell ISE console pane.
function TestWindowsPowerShellISE
{
    param ([string]$FunctionName)

    if ($Host.Name -ne "Windows PowerShell ISE Host")
    {
        $errorMsg = $Messages.RequiresISE -f $FunctionName
        throw $errorMsg
    }
}

#endregion

Export-ModuleMember -Function "Enable-ScriptBrowser", "Disable-ScriptBrowser", "Enable-ScriptAnalyzer", "Disable-ScriptAnalyzer", "Start-ScriptBrowserDesktop"