Public/Utility/Invoke-ATDiffTool.ps1

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
function Invoke-ATDiffTool
{
<#
    .SYNOPSIS
        Invoke the module's configured diff tool

    .DESCRIPTION
        Invoke the module's configured diff tool.
        Provides a consistent interface to compare two files irrespective of the diff tool in use.

    .PARAMETER LeftPath
        Path to 'left' file to compare

    .PARAMETER RightPath
        Path to 'right' file to compare

    .PARAMETER LeftTitle
        Title to show for left file on tools that support this.
        Defaults to the value of LeftPath

    .PARAMETER RightTitle
        Title to show for right file on tools that support this.
        Defaults to the value of RightPath

    .PARAMETER Wait
        For tools that run as a GUI, wait for the process to exit before continuing.

    .EXAMPLE
        Invoke-ATDiffTool -LeftPath .\thisfile.txt -RightPath .\thatfile.txt -LeftTitle This -RightTitle That -Wait

        Compare files displaying custom titles and wait for GUI process to exit.

    .NOTES
        -Wait parameter currently does not work when configured diff tool is VSCode,
        as code starts a sub-process for the diff and the main process exits immediately.

    .LINK
        Set-ATConfiguratonItem
#>

    param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$LeftPath,

        [Parameter(Mandatory = $true, Position = 1)]
        [string]$RightPath,

        [Parameter(Position = 2)]
        [string]$LeftTitle,

        [Parameter(Position = 3)]
        [string]$RightTitle,

        [switch]$Wait
    )

    if (-not $script:moduleConfig.HasItem('DiffTool'))
    {
        Write-Warning "Diff tool not configured."
        Write-Warning "Run Set-ATConfigurationItem -ConfigurationItem DiffTool to configure one."
        return
    }

    $toolDefinition = $script:moduleConfig.DiffTool

    $cmd = Get-Command $toolDefinition.Path

    # Can only wait on GUI tools, i.e. not git diff
    $Wait = $Wait -and $toolDefinition.IsGUI

    if ([string]::IsNullOrEmpty($LeftTitle))
    {
        $LeftTitle = $LeftPath
    }

    if ([string]::IsNullOrEmpty($RightTitle))
    {
        $RightTitle = $RightPath
    }

    $argumentArray = $toolDefinition.Arguments -split '\s'

    for ($i = 0; $i -lt $argumentArray.Length; ++$i)
    {
        switch ($argumentArray[$i])
        {
            '{LeftTitle}'  { $argumentArray[$i] = "`"$LeftTitle`"" }
            '{RightTitle}'  { $argumentArray[$i] = "`"$RightTitle`"" }
            '{LeftPath}'  { $argumentArray[$i] = "`"$LeftPath`"" }
            '{RightPath}'  { $argumentArray[$i] = "`"$RightPath`"" }
        }
    }

    if (-not $toolDefinition.IsGUI)
    {
        # Run in current console
        & $cmd $argumentArray
    }
    else
    {
        if ($Wait)
        {
            Write-Host "Waiting for $($toolDefinition.Name)..."
        }
        else
        {
            Write-Host "Starting $($toolDefinition.Name)..."
        }

        # Start GUI process
        $proc = Start-Process -FilePath $cmd.Path -ArgumentList $argumentArray -PassThru

        if ($Wait)
        {
            $proc.WaitForExit()
        }
    }
}