github.com/getgauge/gauge@v1.6.9/build/install/windows/update_path.ps1 (about)

     1  <#
     2  	.DESCRIPTION
     3      This script is used to add/remove the installation path of gauge in the PATH Environment variable as part of installation/uninstallation of gauge.
     4      The script assumes that the PATH env exists before running.
     5      .PARAMETER Add
     6      This is a Switch parameter which tells the script to ADD the path supplied to the System's PATH Environment variable.
     7      .PARAMETER Remove
     8      This is a Switch parameter which tells the script to REMOVE the path supplied from the System's PATH Environment variable.
     9      .PARAMETER Path
    10      This parameter accepts a string which needs to be added/removed from the System's PATH Environment Variable.
    11  #>
    12  
    13  
    14  
    15  param(
    16      [cmdletbinding()]
    17  
    18      # This parameter dictates if the path needs to be added
    19      [Parameter(Mandatory=$false,ParameterSetName="EnvironmentVariableAddOperation")]
    20      [switch]
    21      $Add,
    22  
    23      # This parameter dictates if the path needs to be removed
    24      [Parameter(Mandatory=$false,ParameterSetName="EnvironmentVariableRemoveOperation")]
    25      [switch]
    26      $Remove,
    27  
    28      # This parameter tells us the path inside the $PATH Environment Variable for which the operation needs to be performed
    29      [Parameter(Mandatory=$true)]
    30      [string]
    31      $Path
    32  
    33  )
    34  
    35  
    36  
    37  $currentSystemPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
    38  
    39  try {
    40  
    41      if ($Add) {
    42  
    43          Write-Output "Path needs to be added."
    44  
    45          Write-Output "Checking if the given path already exists or not"
    46  
    47  
    48  
    49          if ($currentSystemPath -match [Regex]::Escape($Path)) {
    50  
    51              Write-Output "The provided path already exists in the system. Exiting now."
    52  
    53          } else {
    54  
    55              Write-Output "The given path was not found. Adding it now."
    56  
    57              if ($currentSystemPath.EndsWith(";")) {
    58  
    59                  $newSystemPath = $currentSystemPath + $Path.Trim() + ";"
    60  
    61              } else {
    62  
    63                  $newSystemPath = $currentSystemPath + ";" + $Path.Trim() + ";"
    64  
    65              }
    66  
    67              [Environment]::SetEnvironmentVariable("Path", $newSystemPath, [EnvironmentVariableTarget]::Machine)
    68  
    69              Write-Output "Path has been added successfully."
    70  
    71          }
    72  
    73      } else {
    74  
    75          Write-Output "Path needs to be added."
    76  
    77          Write-Output "Checking if the given path already exists or not"
    78  
    79  
    80  
    81          if ($currentSystemPath -match [Regex]::Escape($Path)) {
    82  
    83              Write-Output "The provided path exists in the system. Removing now."
    84  
    85              $newSystemPath = $currentSystemPath.Replace(($Path.Trim() + ";"), "")
    86  
    87              [Environment]::SetEnvironmentVariable("Path", $newSystemPath, [EnvironmentVariableTarget]::Machine)
    88  
    89          } else {
    90  
    91              Write-Output "The given path was not found. Exiting now."
    92  
    93          }
    94  
    95      }
    96  
    97  }
    98  
    99  catch {
   100  
   101      Write-Output "[Error]:: There was an error while execution. Please see the details below. Ensure that the script is running with administrator privileges."
   102  
   103      Write-Output $_
   104  
   105  }