From 346e93983ddf9306e47c7dc02c2ff9381b144911 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 28 Aug 2024 05:05:55 +0200 Subject: [PATCH] Add scripts for backing up and restoring LGHub --- scripts/Windows/Software/LGHub/Manage.ps1 | 78 +++++++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/scripts/Windows/Software/LGHub/Manage.ps1 b/scripts/Windows/Software/LGHub/Manage.ps1 index 7caefcba..31b8d55d 100644 --- a/scripts/Windows/Software/LGHub/Manage.ps1 +++ b/scripts/Windows/Software/LGHub/Manage.ps1 @@ -3,13 +3,81 @@ param( [hashtable] $Arguments ) +. "$PSScriptRoot/../../Scripts/Restoration.ps1"; . "$PSScriptRoot/../../../Common/Scripts/Software.ps1"; -. "$PSScriptRoot/../../Scripts/AppAssociations.ps1"; + +$null = New-Module { + param ($Parameters) + $configPath = "$env:LocalAppData/LGHUB"; + + Export-ModuleMember -Function @(); + + <# + .SYNOPSIS + Stops the running Logitech G Hub instance. + #> + function Stop-LGHub { + [OutputType([string])] + param() + + $hubName = "lghub_system_tray.exe"; + $lghubPath = $(Get-Process | Where-Object { $_.Path -and ((Split-Path -Leaf $_.Path) -eq $hubName) })[0].Path; + + $mainProcesses = Get-Process | Where-Object { + $_.Path -and + (@("lghub.exe", "lghub_agent.exe", $hubName) -contains (Split-Path -Leaf $_.Path)) -and + (($_.Parent.ProcessName -eq "explorer") -or ($null -eq $_.Parent)) + }; + + $null = $mainProcesses | ForEach-Object { $_.Kill($true) }; + return $lghubPath; + } + + <# + .SYNOPSIS + Edits the Logitech G Hub configuration. + #> + function Edit-LGHubConfig { + param( + [scriptblock] $Action + ) + + Write-Host "Stopping Logitech G Hub"; + $path = Stop-LGHub; + & $Action; + + if ($path) { + Write-Host "Restarting Logitech G Hub"; + Start-Process $path; + } + } + + Start-SoftwareInstaller @Parameters ` + -Installer { + Install-ChocoPackage lghub; + Remove-DesktopIcon "*G HUB*"; + } ` + -UserBackup { + param( + [hashtable] $Arguments + ) + + Edit-LGHubConfig { + Add-BackupArtifacts -User $Arguments.Name -Source $configPath -Path "LGHub" ` + -Include @("settings.db", "icon_cache") + }; + } ` + -UserConfigurator { + param( + [hashtable] $Arguments + ) + + Edit-LGHubConfig { + Expand-BackupArtifacts -User $Arguments.Name -Path "LGHub" -Target $configPath; + }; + }; +} $PSBoundParameters; Start-SoftwareInstaller @PSBoundParameters ` - -Installer { - Install-ChocoPackage lghub; - Remove-DesktopIcon "*G HUB*"; - }; # ToDo: Add restoration