PowerShell utility functions for manipulating environment paths.

#
# path-utils.psm1
# Source: https://github.com/appveyor/ci/blob/master/scripts/path-utils.psm1
#

function Get-Path
{
    ([Environment]::GetEnvironmentVariable("path", "machine")).Split(";") | Sort-Object
}

function Add-Path([string]$item,[switch]$before)
{
    $item = (Get-SanitizedPath $item)
    $pathItemsArray = ([Environment]::GetEnvironmentVariable("path", "machine")).Split(";")
    $pathItems = New-Object System.Collections.ArrayList($null)
    $pathItems.AddRange($pathItemsArray)

    # add folder
    $index = -1
    for($i = 0; $i -lt $pathItems.Count; $i++) {
        if((Get-SanitizedPath $pathItems[$i]) -eq $item) {
            $index = $i;
            break
        }
    }

    if($index -eq -1) {
        # item not found - add it
        if ($before) {
            $pathItems.Insert(0, $item)
        } else {
            $pathItems.Add($item) | Out-null
        }

        # update PATH variable
        $updatedPath = $pathItems -join ';'
        [Environment]::SetEnvironmentVariable("path", $updatedPath, "machine")
    }
}

function Remove-Path([string]$item)
{
    $item = (Get-SanitizedPath $item)
    $pathItemsArray = ([Environment]::GetEnvironmentVariable("path", "machine")).Split(";")
    $pathItems = New-Object System.Collections.ArrayList($null)
    $pathItems.AddRange($pathItemsArray)

    $index = -1
    for($i = 0; $i -lt $pathItems.Count; $i++) {
        if((Get-SanitizedPath $pathItems[$i]) -eq $item) {
            $index = $i;
            break
        }
    }

    if($index -ne -1) {
        # remove folder
        $pathItems.RemoveAt($index) | Out-null

        # update PATH variable
        $updatedPath = $pathItems -join ';'
        [Environment]::SetEnvironmentVariable("path", $updatedPath, "machine")
    }
}

function Get-SanitizedPath([string]$path) {
    return $path.Replace('/', '\').Trim('\')
}

function Add-SessionPath([string]$path) {
    $sanitizedPath = Get-SanitizedPath $path
    foreach($item in $env:path.Split(";")) {
        if($sanitizedPath -eq (Get-SanitizedPath $item)) {
            return # already added
        }
    }
    $env:path = "$sanitizedPath;$env:path"
}

# export module members
Export-ModuleMember -Function Get-Path, Add-Path, Remove-Path, Add-SessionPath

#
# install_path_utils.ps1
# https://github.com/appveyor/ci/blob/master/scripts/enterprise/install_path_utils.ps1
#

$pathUtilsPath = "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules\path-utils"
New-Item $pathUtilsPath -ItemType Directory -Force
(New-Object Net.WebClient).DownloadFile('https://raw.githubusercontent.com/appveyor/ci/master/scripts/path-utils.psm1', "$pathUtilsPath\path-utils.psm1")
Remove-Module path-utils -ErrorAction SilentlyContinue
Import-Module path-utils

$UserModulesPath = "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules"
$PSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
if(-not $PSModulePath.contains($UserModulesPath)) {
    [Environment]::SetEnvironmentVariable('PSModulePath', "$PSModulePath;$UserModulesPath", 'Machine')
}