From 3aac4b82d7bf9d4fd0e9347cb6d3eeeb61e6b53d Mon Sep 17 00:00:00 2001 From: he3als <65787561+he3als@users.noreply.github.com> Date: Sat, 8 Jun 2024 14:27:08 +0100 Subject: [PATCH] feat: rewrite path setting --- src/playbook/Configuration/atlas/start.yml | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/playbook/Configuration/atlas/start.yml b/src/playbook/Configuration/atlas/start.yml index 3c6ae4ae51..a493d5b030 100644 --- a/src/playbook/Configuration/atlas/start.yml +++ b/src/playbook/Configuration/atlas/start.yml @@ -6,19 +6,27 @@ actions: - !powerShell: wait: true command: >- - $modulesPath = """$([Environment]::GetFolderPath('Windows'))\AtlasModules"""; + function Add-ToPath ([array]$Paths) { + $machine = [System.EnvironmentVariableTarget]::Machine; + $seperator = [IO.Path]::PathSeparator; + + $Paths | ForEach-Object { + if (!(Test-Path $_ -PathType Container)) { + Write-Error """Path '$_' doesn't exist as a container.""" + } + }; - function AddToVar($env, $value) { - $a = [IO.Path]::PathSeparator; - $b = [System.EnvironmentVariableTarget]::Machine; - $c = [Environment]::GetEnvironmentVariables($b); + $newPath = @(); + $newPath += [Environment]::GetEnvironmentVariable('PATH', $machine) -split $seperator; + if ($newPath.Count -eq 0) { Write-Warning 'The PATH variable is currently empty.' }; + $newPath += $Paths; + $newPath = $newPath | Where-Object { $_ } | Select-Object -Unique; - [Environment]::SetEnvironmentVariable($env, - ("""$($c.'Path');$value""" -replace ';',"""$a"""), - $b); + [Environment]::SetEnvironmentVariable('PATH', """$($newPath -join $seperator)""", $machine); }; - AddToVar 'Path' """$modulesPath;$modulesPath\Apps;$modulesPath\Other;$modulesPath\Tools;$modulesPath\Scripts"""; + $modulesPath = """$([Environment]::GetFolderPath('Windows'))\AtlasModules"""; + Add-ToPath """$modulesPath""", """$modulesPath\Apps""", """$modulesPath\Other""", """$modulesPath\Tools""", """$modulesPath\Scripts"""; ################ NO LOCAL BUILD ################