functions/Install-DotNetTool.ps1

# <copyright file="Install-DotNetTool.ps1" company="Endjin Limited">
# Copyright (c) Endjin Limited. All rights reserved.
# </copyright>

<#
.SYNOPSIS
    Simple wrapper to install a .Net global tool if it is not already installed.
.DESCRIPTION
    Simple wrapper to install a .Net Global Tool if it is not already installed. Any existing
    installed version will be uninstalled before installing the required version.
.EXAMPLE
    PS C:\> <example usage>
    Explanation of what the example does
.PARAMETER Name
    The name of the .Net global tool to install
.PARAMETER Version
    The version of the global tool to install.
.PARAMETER AdditionalArgs
    An array of arbitrary command-line arguments supported by 'dotnet tool install'
.PARAMETER Global
    When true (the default), the tool is installed for the current user.
#>

function Install-DotNetTool
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $Name,

        [Parameter()]
        [string] $Version,

        [Parameter()]
        [string[]] $AdditionalArgs = @(),
        
        [Parameter()]
        [bool] $Global = $true
    )

    $allInstalledTools = & dotnet tool list -g
    $existingInstall = $allInstalledTools | select-string $Name

    if ($existingInstall -and $Version) {
        # parse out the currently installed version from the whitespace delimited columns
        $existingVersion = ($existingInstall.Line -split "[\s*]" | ? { $_ })[1]
        if ($existingVersion -ne $Version) {
            # uninstall the incorrect version
            & dotnet tool uninstall -g $Name
            # force a re-install below
            $existingInstall = $null
        }
    } 

    # Install the tool, if necessary
    if (!$existingInstall) {
        if ($Version) {
            $AdditionalArgs += @(
                "--version"
                $Version
            )
        }

        Write-Verbose "cmdline: & dotnet tool install -g $Name $AdditionalArgs"
        & dotnet tool install -g $Name $AdditionalArgs
    }

    # Ensure .NET global tools are available in the PATH environment variable
    $toolsPath = Join-Path $HOME ".dotnet/tools"
    
    if ($toolsPath -notin ($env:PATH -split [IO.Path]::PathSeparator)) {
        $env:PATH = "{0}{1}{2}" -f $env:PATH, [IO.Path]::PathSeparator, $toolsPath
    }
}