﻿# encoding: utf-8
# api: powershell
# title: Add-on.InstallPortable.p
# description: This file was uploaded by a PowerGUI Script Editor Add-on.
# version: 3.0
# type: module
# author: Adam Driscoll                                                                                     
# license: CC0
# x-poshcode-id: 4267
# x-archived: 2013-06-29T07:03:33
# x-published: 2013-06-26T14:59:00
#
#
#######################################################################################################################
# File:             Add-on.InstallPortable.psm1                                                                       #
# Author:           Adam Driscoll                                                                                     #
# Publisher:        Quest Software                                                                                    #
# Copyright:        Â© 2011 Quest Software. All rights reserved.                                                       #
# Usage:            To load this module in your Script Editor:                                                        #
#                   1. Open the Script Editor.                                                                        #
#                   2. Select "PowerShell Libraries" from the File menu.                                              #
#                   3. Check the Add-on.InstallPortable module.                                                       #
#                   4. Click on OK to close the "PowerShell Libraries" dialog.                                        #
#                   Alternatively you can load the module from the embedded console by invoking this:                 #
#                       Import-Module -Name Add-on.InstallPortable                                                    #
#                   Please provide feedback on the PowerGUI Forums.                                                   #
#######################################################################################################################

Set-StrictMode -Version 2

#region Define the Win32WindowClass

if (-not ('PowerShellTypeExtensions.Win32Window' -as [System.Type])) {
	$cSharpCode = @'
using System;

namespace PowerShellTypeExtensions {
	public class Win32Window : System.Windows.Forms.IWin32Window
	{
		public static Win32Window CurrentWindow {
			get {
				return new Win32Window(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
			}
		}

		public Win32Window(IntPtr handle) {
			_hwnd = handle;
		}

		public IntPtr Handle {
			get {
				return _hwnd;
			}
		}

		private IntPtr _hwnd;
	}
}
'@

	Add-Type -ReferencedAssemblies System.Windows.Forms -TypeDefinition $cSharpCode
}

#endregion

#region Initialize the Script Editor Add-on.

$minimumSdkVersion = [System.Version]'1.0'

if ($Host.Name â��ne 'PowerGUIScriptEditorHost') {
	return
}

if (-not (Get-Variable -Name PGVersionTable -ErrorAction SilentlyContinue)) {
	[System.Windows.Forms.MessageBox]::Show([PowerShellTypeExtensions.Win32Window]::CurrentWindow,"The ""$(Split-Path -Path $PSScriptRoot -Leaf)"" Add-on module requires PowerGUI 3.0 or later. The current PowerGUI version is $($Host.Version).$([System.Environment]::NewLine * 2)Please upgrade to the latest version of PowerGUI and try again.",'PowerGUI 3.0 or later is required',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
	return
} elseif ($PGVersionTable.SDKVersion -lt $minimumSdkVersion) {
	[System.Windows.Forms.MessageBox]::Show([PowerShellTypeExtensions.Win32Window]::CurrentWindow,"The ""$(Split-Path -Path $PSScriptRoot -Leaf)"" Add-on module requires version $minimumSdkVersion or later of the PowerGUI Script Editor SDK. The current SDK version is $($PGVersionTable.SDKVersion).$([System.Environment]::NewLine * 2)Please upgrade to the latest version of PowerGUI and try again.","Version $minimumSdkVersion or later of the SDK is required",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
	return
}

#endregion

#region Load resources from disk.

$iconLibrary = @{
	InstallPortableIcon16 = New-Object System.Drawing.Icon -ArgumentList "$PSScriptRoot\usb.ico",16,16
	InstallPortableIcon32 = New-Object System.Drawing.Icon -ArgumentList "$PSScriptRoot\usb.ico",32,32
}
$imageLibrary = @{
	InstallPortable16 = $iconLibrary['InstallPortableIcon16'].ToBitmap()
}

#endregion

if (-not ($clearConsoleCommand = $PGSE.Commands['ToolsCommand.InstallPortableCommand'])) {
	$InstallPortableCommand = New-Object -TypeName Quest.PowerGUI.SDK.ItemCommand -ArgumentList 'ToolsCommand','InstallPortableCommand'
	$InstallPortableCommand.Text = '&Install Portable'
	$InstallPortableCommand.Image = $imageLibrary['InstallPortable16']
	$InstallPortableCommand.ScriptBlock = {
	
		#
		#	Open a FolderBrowserDialog to navigate to the folder to place PowerGUI in.
		#
		$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
		$dialog.ShowDialog()
		
		$destination = $dialog.SelectedPath
		
		if ($destination -ne $null -and $destination.LastIndexOf([system.IO.Path]::DirectorySeparatorChar) -lt 0)
		{
			$destination = "$destination$([system.IO.Path]::DirectorySeparatorChar)"
		}
		
		if (-not [String]::IsNullOrEmpty($destination))
		{
			$i = 0
			
			#
			#	Progress bar that doesn't actually report actual progress....makes you feel better about it though...
			#
			Write-Progress -Activity "Installing PowerGUI to USB Key..." -Status  "Processing..." -PercentComplete $i
			
			Start-Job -Name "InstallPortable"  -FilePath "$PSScriptRoot\InstallPortable.ps1" -ArgumentList $destination,$PGHome,"PowerGUI"
			
			#
			#	Hack because the Job returned by Start-Job always returns completed...even when it has not...
			#
			while(-not (Test-Path -Path (Join-Path $destination "PowerGUI ScriptEditor_x86.exe")))
			{
				Write-Progress -Activity "Installing PowerGUI to USB Key..." -Status  "Processing..." -PercentComplete $i
				
				#
				#	In case it takes longer than 25 seconds...
				#
				$i++
				if ($i -gt 100)
				{
					$i = 0
				}
				
				Start-Sleep -Milliseconds 250
			}
			Write-Progress -Activity "Installing PowerGUI to USB Key..." -Status "Processing..." -Completed
			
			#
			#	In case there was an error
			#
			$jobResult = Receive-Job -Name "InstallPortable" 
			
			if ($jobResult -ne $null)
			{
				throw $jobResult
			}
		}
	}

	$PGSE.Commands.Add($InstallPortableCommand)
	
	$ToolBar = $PGSE.Toolbars | Where { $_.Title -eq "Standard" } 
	$ToolBar.Items.Add($InstallPortableCommand)
}

#region Clean-up the Add-on when it is removed.

$ExecutionContext.SessionState.Module.OnRemove = {
	#region Remove the install portable toolbar button from the Standard toolbar.

	$standardToolbar = $null
	foreach ($item in $pgse.Toolbars) {
		if ($item.Title -eq 'Standard') {
			$standardToolbar = $item
			break
		}
	}
	if (($standardToolbar) -and
	    ($newAddonToolbarButton = $standardToolbar.Items['ToolsCommand.InstallPortableCommand'])) {
		$standardToolbar.Items.Remove($newAddonToolbarButton) | Out-Null
	}

	#endregion
if (($viewMenu = $PGSE.Menus['MenuBar.View']) -and
    ($installPortableCommand = $viewMenu.Items['ToolsCommand.InstallPortableCommand'])) {
	$viewMenu.Items.Remove($installPortableCommand) | Out-Null
	}
	
	if ($installPortableCommand =
		$PGSE.Commands['ToolsCommand.InstallPortableCommand']) {
		$PGSE.Commands.Remove($installPortableCommand) | Out-Null
	}
}

#endregion

# SIG # Begin signature block
# MIIPIgYJKoZIhvcNAQcCoIIPEzCCDw8CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU5AxIA2EVAIt/MCwdiKxi3AzO
# EFWgggz6MIIEDTCCAvWgAwIBAgILBAAAAAABI54PrLMwDQYJKoZIhvcNAQEFBQAw
# VzELMAkGA1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNV
# BAsTB1Jvb3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05OTAx
# MjgxMzAwMDBaFw0xNzAxMjcxMjAwMDBaMIGBMQswCQYDVQQGEwJCRTEZMBcGA1UE
# ChMQR2xvYmFsU2lnbiBudi1zYTElMCMGA1UECxMcUHJpbWFyeSBPYmplY3QgUHVi
# bGlzaGluZyBDQTEwMC4GA1UEAxMnR2xvYmFsU2lnbiBQcmltYXJ5IE9iamVjdCBQ
# dWJsaXNoaW5nIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAopt1
# KqcTuglxJBjfoQZiKRKe3J51c+jfVldplhNWT/LIvAFY7SaGcgtg9RnFVQNXm7kQ
# yaHUdED/bADo5lM3/rfaeT64UjjpgSyfDjNSps1wzkpdYvTRZ168l0oHyrzdjUex
# zfFlW4UBsEtr3s2OLvVQ6KOcnSaYazZjQQNwRPBf4iV1eVB9X6EGokYMVZVHFNOR
# RoZomecn9JTsmkFR95bUeozhRJdocQPYWG+9tBBC7g1mdUaLRJbSMAASB2P3RJsB
# cMpWb5xYlyonF4sucVJtRqunKw9/FkhkyFL6BhcAB3TXRbM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