Private/Utils/DiffTools/New-DiffTool.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
function New-DiffTool
{
    param
    (
        [PSObject]$DiffToolConfig,
        [string]$UserPath
    )

    $tool = $null

    # If user has supplied a path
    if ($UserPath)
    {
        $tool = Get-Command $UserPath -ErrorAction SilentlyContinue
    }
    else
    {
        if ((Get-OperatingSystem) -ieq 'Windows')
        {
            $executable = $DiffToolConfig.ExecutableName + ".exe"
            $searchPaths = $DiffToolConfig.WindowsPaths
        }
        else
        {
            $executable = $DiffToolConfig.ExecutableName
            $searchPaths = $DiffToolConfig.NonWindowsPaths
        }

        if ($DiffToolConfig.SearchInPath)
        {
            $tool = Get-Command $executable -ErrorAction SilentlyContinue
        }

        if (-not $tool)
        {
            foreach ($path in $searchPaths)
            {
                $tool = Get-Command (Join-Path $ExecutionContext.InvokeCommand.ExpandString($path) $executable) -ErrorAction SilentlyContinue

                if ($tool)
                {
                    break
                }
            }

            if (-not $tool)
            {
                Write-Warning "Cannot locate $($DiffToolConfig.DisplayName). Try specifying a path to the executable with -Path"
                return $null
            }
        }

        New-Object PSObject -Property @{
            Name = $DiffToolConfig.DisplayName
            Path = $tool.Path
            Arguments = $DiffToolConfig.Arguments
            IsGUI = $DiffToolConfig.IsGUI
        }
    }
}