<#
.Synopsis
Install-P4VTools.ps1 installs custom tools for P4V on to local Windows machine
.Description
Installs into c:\Users\<username>\P4V_Tools and also updates the P4V CustomTools settings.
These are located in c:\Users\<username>\.p4qt\customtools.xml
It installs the following tools located in the same directory as this script:
- open_in_swarm.ps1
- freeze_stream.ps1
- stream_report.ps1
This script is called by Installer.ps1 which "sources it" and then executes the
main function: Install-P4VTools
.Parameter <none>
#>
$installpath = "$HOME\P4V_TOOLS"
function Update-CustomTool ([xml]$xml, [xml] $ToolDef, [string]$curName, [ref]$cnt) {
$exists = 0
# Check if the CurrentToolDef already exists
foreach ($CustomToolDef in $xml.CustomToolDefList.CustomToolDef | Where-Object {$_.Definition.Name -match $curName}) {
Write-Host "Warning: CustomToolDef '$curName' already exists in the CustomToolDefList and will not be appended!"
$exists = 1
}
# Append CustomToolDef only if it is not already existing
if ($exists -eq 0){
$xml.CustomToolDefList.AppendChild($xml.ImportNode($ToolDef.DocumentElement, $true))
Write-Host "Info: CustomToolDef '$curName' was appended to CustomToolDefList of Perforce!"
$cnt.value += 1
}
}
function Get-Toolpath ([ref]$toolpath) {
# Only put quotes when required - powershell can't cope if present and no
# spaces in path!
}
function Update-XML ([string]$xmlstring, [string]$xmlpath, [ref]$cnt) {
# This function adds the custom tool definitions for perforce to the xml-file
# Check if the current xml-file ist empty
if ($xmlstring) {
# if false: then append the current content of the xml-file
$xml = [xml] $xmlstring
} else {
# if true: then fill the empty xml-file with the CustomToolDefList
$xml = [xml] '
<!--perforce-xml-version=1.0-->
<CustomToolDefList varName="customtooldeflist">
<Separator/>
</CustomToolDefList>'
Write-Host "Info: CustomToolDefList was appended to customtools.xml file, because it was empty!"
}
# Install first tool
$toolpath = "${InstallPath}\open_in_swarm.ps1"
if ($toolpath -match " ") {
$toolpath = "`"$toolpath`""
}
$options = @("Swarm")
# Allow for multiple options
foreach ($opt in $options) {
# Generate CustomToolDef
[xml] $ToolDef = @"
<CustomToolDef>
<Definition>
<Name>Open in ${opt}</Name>
<Command>powershell.exe</Command>
<Arguments>-f ${toolpath} `$p `$u %d</Arguments>
<Shortcut></Shortcut>
</Definition>
<Console>
<CloseOnExit>true</CloseOnExit>
</Console>
<AddToContext>true</AddToContext>
</CustomToolDef>
"@
$exists = 0
$curName = "Open in " + $opt
Update-CustomTool $xml $ToolDef $curName $cnt
}
# --------------------------------------------
# Install second tool
$toolpath = "${InstallPath}\freeze_stream.ps1"
if ($toolpath -match " ") {
$toolpath = "`"$toolpath`""
}
[xml] $ToolDef = @"
<CustomToolDef>
<Definition>
<Name>Freeze Stream</Name>
<Command>powershell.exe</Command>
<Arguments>-f ${toolpath} `$p `$u %t freeze `$D</Arguments>
<Shortcut></Shortcut>
</Definition>
<Prompt>
<PromptText>Freeze date/time (p4 format: yyyy/mm/dd:hh:mm:ss). Leave blank for latest</PromptText>
</Prompt>
<Console>
<CloseOnExit>false</CloseOnExit>
</Console>
<AddToContext>true</AddToContext>
</CustomToolDef>
"@
$exists = 0
$curName = "Freeze Stream"
Update-CustomTool $xml $ToolDef $curName $cnt
# --------------------------------------------
[xml] $ToolDef = @"
<CustomToolDef>
<Definition>
<Name>Unfreeze Stream</Name>
<Command>powershell.exe</Command>
<Arguments>-f ${toolpath} `$p `$u %t unfreeze</Arguments>
<Shortcut></Shortcut>
</Definition>
<Console>
<CloseOnExit>false</CloseOnExit>
</Console>
<AddToContext>true</AddToContext>
</CustomToolDef>
"@
$exists = 0
$curName = "Unfreeze Stream"
Update-CustomTool $xml $ToolDef $curName $cnt
# --------------------------------------------
$toolpath = "${InstallPath}\stream_report.ps1"
if ($toolpath -match " ") {
$toolpath = "`"$toolpath`""
}
[xml] $ToolDef = @"
<CustomToolDef>
<Definition>
<Name>Stream Report</Name>
<Command>powershell.exe</Command>
<Arguments>-f ${toolpath} `$p `$u %t</Arguments>
<Shortcut></Shortcut>
</Definition>
<Console>
<CloseOnExit>false</CloseOnExit>
</Console>
<AddToContext>true</AddToContext>
</CustomToolDef>
"@
$exists = 0
$curName = "Stream Report"
Update-CustomTool $xml $ToolDef $curName $cnt
$xml.save($xmlpath)
}
function Install-P4VTools {
# Function that does the work
try
{
# Load assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create local directory if it doesn't exist
if (!(test-path $installpath -pathtype container)) {
New-Item -itemType Directory -Force -Path $installpath
Write-Host "Info: Directory $installpath was created!"
}
$tools = @("open_in_swarm.ps1", "freeze_stream.ps1", "stream_report.ps1")
foreach ($tool in $tools) {
$scriptpath = Split-Path -parent $script:MyInvocation.MyCommand.Path | Join-Path -childpath "$tool"
copy-item $scriptpath $installpath -force
Write-Host "Info: '$tool' was copied into Directory $installpath!"
}
$customtoolsfile = $env:USERPROFILE, ".p4qt", "customtools.xml" -join "\"
$xmltext = get-content $customtoolsfile
[int]$cnt = 0
update-XML $xmltext $customtoolsfile ([ref]$cnt)
[System.Windows.Forms.MessageBox]::Show("The installation of $cnt custom Peforce tools was finished successfully!","PV4 Custom Tool Installation", 0, [System.Windows.Forms.MessageBoxIcon]::Information)
}
catch
{
# Show Error Messages
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host -ForegroundColor Red "$ErrorMessage $FailedItem"
# Show Error Message Box
[System.Windows.Forms.MessageBox]::Show("The installation of the custom Peforce tools failed!","PV4 Custom Tool Installation", 0, [System.Windows.Forms.MessageBoxIcon]::Error)
}
}