From 64572662eb17469e6d207cc0a5b91ad852a493df Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 3 Sep 2024 12:02:32 +0200 Subject: [PATCH] Add a backup script for Visual Studio --- .../Windows/Software/VisualStudio/Manage.ps1 | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/scripts/Windows/Software/VisualStudio/Manage.ps1 b/scripts/Windows/Software/VisualStudio/Manage.ps1 index 53ea48c9..1dd4ee88 100644 --- a/scripts/Windows/Software/VisualStudio/Manage.ps1 +++ b/scripts/Windows/Software/VisualStudio/Manage.ps1 @@ -6,6 +6,8 @@ param( & { param($parameters) + . "$PSScriptRoot/../../Scripts/Restoration.ps1"; + . "$PSScriptRoot/../../../Common/Scripts/SoftwareManagement.ps1"; . "$PSScriptRoot/../../../Common/Scripts/BrowserAutomation.ps1"; . "$PSScriptRoot/../../../Common/Scripts/Software.ps1"; . "$PSScriptRoot/../../../Common/Scripts/System.ps1"; @@ -16,19 +18,65 @@ param( [System.Tuple]::Create("visualstudio2022enterprise", "VisualStudio.17.Release", "Microsoft.VisualStudio.Product.Enterprise"), [System.Tuple]::Create("visualstudio2022community", "VisualStudio.17.Release", "Microsoft.VisualStudio.Product.Community") ); + + <# + .SYNOPSIS + Gets the path to the Visual Studio Installer. + #> + function Get-InstallerPath { + return "$((Get-KnownFolder ProgramFilesX86).Path)/Microsoft Visual Studio/Installer/vs_installer.exe"; + } + + <# + .SYNOPSIS + Gets the path to the configuration file in the backup archive. + + .PARAMETER PackageName + The name of the package to get the configuration for. + #> + function Get-ConfigPath { + param( + [string] $PackageName + ) + + return Join-Path "Visual Studio" "$PackageName.vsconfig"; + } Start-SoftwareInstaller @parameters ` + -Backup { + foreach ($version in $versions) { + if (Test-ChocoPackage $version[0]) { + Write-Host "Backing up ``$($version[0])…"; + $configFile = New-TemporaryFile; + + Start-Process -FilePath (Get-InstallerPath) -Wait ` + -ArgumentList @( + "export", + "--channelId", $version[1], + "--productId", $version[2], + "--config", $configFile, + "--quiet" + ); + + Add-BackupArtifacts -Source $configFile -Path (Get-ConfigPath $version[0]); + Remove-Item $configFile; + } + } + } ` -Installer { foreach ($version in $versions) { $packageName = $version[0]; - Write-Host "Installing ``$packageName``…"; - Install-ChocoPackage $packageName; + $file = New-TemporaryFile; + Remove-Item $file; + Expand-BackupArtifacts -Path (Get-ConfigPath $packageName) -Target $file; + + if (Test-Path $file) { + Write-Host "Restoring ``$packageName``…"; + Install-ChocoPackage $packageName -ArgumentList "--params","--config `"$file`""; + } } Remove-DesktopIcon "CocosCreator*"; Remove-DesktopIcon "Unity Hub*"; }; - - # ToDo: Add restoration - # Only restore version if it has been backed up } $PSBoundParameters;