From 30638e42f19cfb35385458e1a93c696433c5b393 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 7 Aug 2024 21:50:21 +0200 Subject: [PATCH] Refactor chocolatey script --- scripts/Windows/OS/Install.ps1 | 1 + .../Windows/Software/chocolatey/Manage.ps1 | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 0d6b6600..12422d5a 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -183,6 +183,7 @@ $null = New-Module { # Essentials . "$softwarePath/OpenSSH/Manage.ps1" @arguments; . "$softwarePath/PowerShell/Manage.ps1" @arguments; + . "$softwarePath/chocolatey/Manage.ps1" @arguments; } if (Test-Collection "common") { diff --git a/scripts/Windows/Software/chocolatey/Manage.ps1 b/scripts/Windows/Software/chocolatey/Manage.ps1 index 9cd6d9b9..1ca9490a 100644 --- a/scripts/Windows/Software/chocolatey/Manage.ps1 +++ b/scripts/Windows/Software/chocolatey/Manage.ps1 @@ -16,24 +16,25 @@ Start-SoftwareInstaller @PSBoundParameters ` & $Installer -Action ([InstallerAction]::Configure) } ` -Configurator { + [string] $backup = $null; $nativeProfile = powershell -c '$PROFILE'; + + if (Test-Path -PathType Leaf $nativeProfile) { + $backup = "${nativeProfile}_"; + Move-Item $nativeProfile $backup; + } + $null = New-Item -Force $nativeProfile; choco install -y --force chocolatey; - Copy-Item -Force $nativeProfile $PROFILE; - Push-Location ~; - $files = @($nativeProfile, $PROFILE) | ForEach-Object { Resolve-Path -Relative $_ }; - Pop-Location; + Add-PowerShellProfileStatement ` + -DefaultUser ` + -Category "chocolatey" ` + -Script (Get-Content $nativeProfile | Out-String) ` + -Append; - foreach ($path in $files) { - $fullName = "$env:SystemDrive/Users/Default/$path"; - $dirName = Split-Path -Parent $fullName; - - if (-not (Test-Path -PathType Container $dirName)) { - $null = New-Item -Force -ItemType Directory $dirName; - } - - Copy-Item -Force ~/"$path" $fullName; + if ($backup) { + Move-Item $backup $nativeProfile; } Import-Module "$env:ChocolateyInstall/helpers/chocolateyProfile.psm1";