Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions tools/install-powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,56 @@ Function Remove-Destination([string] $Destination) {
}
}

<#
.SYNOPSIS
Validation for Add-PathTToSettingsToSettings.
.DESCRIPTION
Validates that the parameter being validated:
- is not null
- is a folder and exists
- and that it does not exist in settings where settings is:
= the process PATH for Linux/OSX
- the registry PATHs for Windows
#>
function Test-PathNotInSettings($Path) {
if ([string]::IsNullOrWhiteSpace($Path)) {
return $false
}

# Remove ending DirectorySeparatorChar for comparison purposes
$Path = [System.Environment]::ExpandEnvironmentVariables($Path.TrimEnd([System.IO.Path]::DirectorySeparatorChar));

if (-not [System.IO.Directory]::Exists($Path)) {
return $false
}

# [System.Environment]::GetEnvironmentVariable automatically expands all variables
[System.Array] $InstalledPaths = @()
if ([System.Environment]::OSVersion.Platform -eq "Win32NT") {
$InstalledPaths += @(([System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)).Split([System.IO.Path]::PathSeparator))
$InstalledPaths += @(([System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)).Split([System.IO.Path]::PathSeparator))
} else {
$InstalledPaths += @(([System.Environment]::GetEnvironmentVariable('PATH'), [System.EnvironmentVariableTarget]::Process).Split([System.IO.Path]::PathSeparator))
}

# Remove ending DirectorySeparatorChar in all items of array for comparison purposes
$InstalledPaths = $InstalledPaths.ForEach( { $_.TrimEnd([System.IO.Path]::DirectorySeparatorChar) } )

# if $InstalledPaths is in setting return false
if ($InstalledPaths -icontains $Path) {
return $false
}

return $true
}

<#
.Synopsis
Adds a Path to settings (Supports Windows Only)
.DESCRIPTION
Adds the target path to the target registry.
.Parameter Path
The path to add to the registry. It is validated with ValidatePathNotInSettings which ensures that:
The path to add to the registry. It is validated with Test-PathNotInSettings which ensures that:
-The path exists
-Is a directory
-Is not in the registry (HKCU or HKLM)
Expand All @@ -138,7 +181,7 @@ Function Add-PathTToSettings {
param(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[ValidatePathNotInSettings()]
[ValidateScript({Test-PathNotInSettings $_})]
[string] $Path,

[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
Expand Down