feat: refactor file paths to use %LOCALAPPDATA% for user data and logs (#829)

This commit is contained in:
Jeffrey
2026-10-17 00:17:44 +02:00
committed by GitHub
parent 25c3999be5
commit dd1f934174
6 changed files with 21 additions and 15 deletions
+2 -7
View File
@@ -4,7 +4,7 @@
.DESCRIPTION .DESCRIPTION
Resolves selected and undo features from the provided keys, captures their Resolves selected and undo features from the provided keys, captures their
registry state, and saves the result as a JSON file in the Backups/ folder. registry state, and saves the result under %LOCALAPPDATA%\Win11Debloat\Backups.
Returns the file path on success, $null if no registry-backed features exist. Returns the file path on success, $null if no registry-backed features exist.
.PARAMETER ActionableKeys .PARAMETER ActionableKeys
@@ -28,13 +28,8 @@ function New-RegistrySettingsBackup {
} }
$timestamp = Get-Date $timestamp = Get-Date
$backupDirectory = $script:RegistryBackupsPath
if (-not (Test-Path $backupDirectory)) {
New-Item -ItemType Directory -Path $backupDirectory -Force | Out-Null
}
$backupFileName = 'Win11Debloat-RegistryBackup-{0}.json' -f $timestamp.ToString('yyyyMMdd_HHmmss') $backupFileName = 'Win11Debloat-RegistryBackup-{0}.json' -f $timestamp.ToString('yyyyMMdd_HHmmss')
$backupFilePath = Join-Path $backupDirectory $backupFileName $backupFilePath = Join-Path $script:RegistryBackupsPath $backupFileName
$backupConfig = Get-RegistryBackupPayload -SelectedFeatures $selectedFeatures -UndoFeatures $undoFeatures -CreatedAt $timestamp $backupConfig = Get-RegistryBackupPayload -SelectedFeatures $selectedFeatures -UndoFeatures $undoFeatures -CreatedAt $timestamp
if (-not (Save-ToFile -Config $backupConfig -FilePath $backupFilePath -MaxDepth 25)) { if (-not (Save-ToFile -Config $backupConfig -FilePath $backupFilePath -MaxDepth 25)) {
+2
View File
@@ -27,6 +27,8 @@ function Save-ToFile {
) )
try { try {
$directory = Split-Path -Parent $FilePath
if ($directory) { [void][System.IO.Directory]::CreateDirectory($directory) }
$Config | ConvertTo-Json -Depth $MaxDepth | Set-Content -Path $FilePath -Encoding UTF8 -ErrorAction Stop $Config | ConvertTo-Json -Depth $MaxDepth | Set-Content -Path $FilePath -Encoding UTF8 -ErrorAction Stop
return $true return $true
} }
+1 -1
View File
@@ -98,7 +98,7 @@ function Show-MainWindow {
$menuReportBug.Add_Click({ Start-Process "https://github.com/Raphire/Win11Debloat/issues" }) $menuReportBug.Add_Click({ Start-Process "https://github.com/Raphire/Win11Debloat/issues" })
$menuLogs.Add_Click({ $menuLogs.Add_Click({
$logsFolder = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) 'Logs' $logsFolder = Split-Path -Parent $script:DefaultLogPath
if (Test-Path $logsFolder) { if (Test-Path $logsFolder) {
Start-Process "explorer.exe" -ArgumentList $logsFolder Start-Process "explorer.exe" -ArgumentList $logsFolder
} }
+2 -2
View File
@@ -69,11 +69,11 @@ Describe 'New-RegistrySettingsBackup' {
$script:RegistryBackupsPath = Join-Path $TestDrive 'Backups' $script:RegistryBackupsPath = Join-Path $TestDrive 'Backups'
$script:Features = @{ Feature = [PSCustomObject]@{ FeatureId = 'Feature'; RegistryKey = 'feature.reg' } } $script:Features = @{ Feature = [PSCustomObject]@{ FeatureId = 'Feature'; RegistryKey = 'feature.reg' } }
Mock Get-RegistryBackupPayload { @{ Version = '1.0' } } Mock Get-RegistryBackupPayload { @{ Version = '1.0' } }
Mock Save-ToFile { $true }
Mock Write-Host {} Mock Write-Host {}
} }
It 'returns null and does not write when no selected feature has registry data' { It 'returns null and does not write when no selected feature has registry data' {
Mock Save-ToFile { $true }
$script:Features.Feature.RegistryKey = '' $script:Features.Feature.RegistryKey = ''
New-RegistrySettingsBackup -ActionableKeys @('Feature') | Should -BeNullOrEmpty New-RegistrySettingsBackup -ActionableKeys @('Feature') | Should -BeNullOrEmpty
@@ -85,7 +85,7 @@ Describe 'New-RegistrySettingsBackup' {
$result | Should -Match 'Win11Debloat-RegistryBackup-\d{8}_\d{6}\.json$' $result | Should -Match 'Win11Debloat-RegistryBackup-\d{8}_\d{6}\.json$'
Test-Path -LiteralPath $script:RegistryBackupsPath | Should -BeTrue Test-Path -LiteralPath $script:RegistryBackupsPath | Should -BeTrue
Should -Invoke Save-ToFile -Times 1 -Exactly -ParameterFilter { $FilePath -eq $result -and $MaxDepth -eq 25 } (Get-Content -LiteralPath $result -Raw | ConvertFrom-Json).Version | Should -Be '1.0'
} }
It 'throws when persistence reports failure' { It 'throws when persistence reports failure' {
+8
View File
@@ -18,6 +18,14 @@ BeforeAll {
} }
Describe 'Save-ToFile' { Describe 'Save-ToFile' {
It 'creates the destination directory on first use' {
$path = Join-Path $TestDrive 'UserData\LastUsedSettings.json'
Save-ToFile -Config @{ Setting = $true } -FilePath $path | Should -BeTrue
(Get-Content -LiteralPath $path -Raw | ConvertFrom-Json).Setting | Should -BeTrue
}
It 'reports the write error and returns false when persistence fails' { It 'reports the write error and returns false when persistence fails' {
Mock Set-Content { throw [System.IO.IOException]::new('Disk is full') } Mock Set-Content { throw [System.IO.IOException]::new('Disk is full') }
Mock Write-Error {} Mock Write-Error {}
+6 -5
View File
@@ -174,20 +174,21 @@ if (-not $isAdmin) {
# Define script-level variables & paths # Define script-level variables & paths
$script:Version = "2026.08.24" $script:Version = "2026.08.24"
$configPath = Join-Path $PSScriptRoot 'Config' $configPath = Join-Path $PSScriptRoot 'Config'
$logsPath = Join-Path $PSScriptRoot 'Logs' $script:UserDataPath = Join-Path $env:LOCALAPPDATA 'Win11Debloat'
$script:LogsPath = Join-Path $script:UserDataPath 'Logs'
$schemasPath = Join-Path $PSScriptRoot 'Schemas' $schemasPath = Join-Path $PSScriptRoot 'Schemas'
$scriptsPath = Join-Path $PSScriptRoot 'Scripts' $scriptsPath = Join-Path $PSScriptRoot 'Scripts'
$script:AppsListFilePath = Join-Path $configPath 'Apps.json' $script:AppsListFilePath = Join-Path $configPath 'Apps.json'
$script:DefaultSettingsFilePath = Join-Path $configPath 'DefaultSettings.json' $script:DefaultSettingsFilePath = Join-Path $configPath 'DefaultSettings.json'
$script:FeaturesFilePath = Join-Path $configPath 'Features.json' $script:FeaturesFilePath = Join-Path $configPath 'Features.json'
$script:SavedSettingsFilePath = Join-Path $configPath 'LastUsedSettings.json' $script:SavedSettingsFilePath = Join-Path $script:UserDataPath 'LastUsedSettings.json'
$script:UiPreferencesFilePath = Join-Path $env:LOCALAPPDATA 'Win11Debloat\Preferences.json' $script:UiPreferencesFilePath = Join-Path $script:UserDataPath 'Preferences.json'
$script:LanguagesPath = Join-Path $configPath 'Languages' $script:LanguagesPath = Join-Path $configPath 'Languages'
$script:DefaultLanguagePath = Join-Path $script:LanguagesPath 'en-US' $script:DefaultLanguagePath = Join-Path $script:LanguagesPath 'en-US'
$script:DefaultLogPath = Join-Path $logsPath 'Win11Debloat.log' $script:DefaultLogPath = Join-Path $script:LogsPath 'Win11Debloat.log'
$script:RegfilesPath = Join-Path $PSScriptRoot 'Regfiles' $script:RegfilesPath = Join-Path $PSScriptRoot 'Regfiles'
$script:RegistryBackupsPath = Join-Path $PSScriptRoot 'Backups' $script:RegistryBackupsPath = Join-Path $script:UserDataPath 'Backups'
$script:AssetsPath = Join-Path $PSScriptRoot 'Assets' $script:AssetsPath = Join-Path $PSScriptRoot 'Assets'
$script:AppSelectionSchema = Join-Path $schemasPath 'AppSelectionWindow.xaml' $script:AppSelectionSchema = Join-Path $schemasPath 'AppSelectionWindow.xaml'
$script:MainWindowSchema = Join-Path $schemasPath 'MainWindow.xaml' $script:MainWindowSchema = Join-Path $schemasPath 'MainWindow.xaml'