mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-02-17 16:06:59 +00:00
Refactor code structure for improved readability and maintainability (#473)
* Add ToolTips to Tweaks
This commit is contained in:
44
Scripts/FileIO/LoadAppsDetailsFromJson.ps1
Normal file
44
Scripts/FileIO/LoadAppsDetailsFromJson.ps1
Normal file
@@ -0,0 +1,44 @@
|
||||
# Read Apps.json and return list of app objects with optional filtering
|
||||
function LoadAppsDetailsFromJson {
|
||||
param (
|
||||
[switch]$OnlyInstalled,
|
||||
[string]$InstalledList = "",
|
||||
[switch]$InitialCheckedFromJson
|
||||
)
|
||||
|
||||
$apps = @()
|
||||
try {
|
||||
$jsonContent = Get-Content -Path $script:AppsListFilePath -Raw | ConvertFrom-Json
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to read Apps.json: $_"
|
||||
return $apps
|
||||
}
|
||||
|
||||
foreach ($appData in $jsonContent.Apps) {
|
||||
$appId = $appData.AppId.Trim()
|
||||
if ($appId.length -eq 0) { continue }
|
||||
|
||||
if ($OnlyInstalled) {
|
||||
if (-not ($InstalledList -like ("*$appId*")) -and -not (Get-AppxPackage -Name $appId)) {
|
||||
continue
|
||||
}
|
||||
if (($appId -eq "Microsoft.Edge") -and -not ($InstalledList -like "* Microsoft.Edge *")) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
$displayName = if ($appData.FriendlyName) { "$($appData.FriendlyName) ($appId)" } else { $appId }
|
||||
$isChecked = if ($InitialCheckedFromJson) { $appData.SelectedByDefault } else { $false }
|
||||
|
||||
$apps += [PSCustomObject]@{
|
||||
AppId = $appId
|
||||
DisplayName = $displayName
|
||||
IsChecked = $isChecked
|
||||
Description = $appData.Description
|
||||
SelectedByDefault = $appData.SelectedByDefault
|
||||
}
|
||||
}
|
||||
|
||||
return $apps
|
||||
}
|
||||
45
Scripts/FileIO/LoadAppsFromFile.ps1
Normal file
45
Scripts/FileIO/LoadAppsFromFile.ps1
Normal file
@@ -0,0 +1,45 @@
|
||||
# Returns list of apps from the specified file, it trims the app names and removes any comments
|
||||
function LoadAppsFromFile {
|
||||
param (
|
||||
$appsFilePath
|
||||
)
|
||||
|
||||
$appsList = @()
|
||||
|
||||
if (-not (Test-Path $appsFilePath)) {
|
||||
return $appsList
|
||||
}
|
||||
|
||||
try {
|
||||
# Check if file is JSON or text format
|
||||
if ($appsFilePath -like "*.json") {
|
||||
# JSON file format
|
||||
$jsonContent = Get-Content -Path $appsFilePath -Raw | ConvertFrom-Json
|
||||
Foreach ($appData in $jsonContent.Apps) {
|
||||
$appId = $appData.AppId.Trim()
|
||||
$selectedByDefault = $appData.SelectedByDefault
|
||||
if ($selectedByDefault -and $appId.length -gt 0) {
|
||||
$appsList += $appId
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
# Legacy text file format
|
||||
Foreach ($app in (Get-Content -Path $appsFilePath | Where-Object { $_ -notmatch '^#.*' -and $_ -notmatch '^\s*$' } )) {
|
||||
if (-not ($app.IndexOf('#') -eq -1)) {
|
||||
$app = $app.Substring(0, $app.IndexOf('#'))
|
||||
}
|
||||
|
||||
$app = $app.Trim()
|
||||
$appString = $app.Trim('*')
|
||||
$appsList += $appString
|
||||
}
|
||||
}
|
||||
|
||||
return $appsList
|
||||
}
|
||||
catch {
|
||||
Write-Error "Unable to read apps list from file: $appsFilePath"
|
||||
AwaitKeyToExit
|
||||
}
|
||||
}
|
||||
32
Scripts/FileIO/LoadJsonFile.ps1
Normal file
32
Scripts/FileIO/LoadJsonFile.ps1
Normal file
@@ -0,0 +1,32 @@
|
||||
# Loads a JSON file from the specified path and returns the parsed object
|
||||
# Returns $null if the file doesn't exist or if parsing fails
|
||||
function LoadJsonFile {
|
||||
param (
|
||||
[string]$filePath,
|
||||
[string]$expectedVersion = $null,
|
||||
[switch]$optionalFile
|
||||
)
|
||||
|
||||
if (-not (Test-Path $filePath)) {
|
||||
if (-not $optionalFile) {
|
||||
Write-Error "File not found: $filePath"
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
try {
|
||||
$jsonContent = Get-Content -Path $filePath -Raw | ConvertFrom-Json
|
||||
|
||||
# Validate version if specified
|
||||
if ($expectedVersion -and $jsonContent.Version -and $jsonContent.Version -ne $expectedVersion) {
|
||||
Write-Error "$(Split-Path $filePath -Leaf) version mismatch (expected $expectedVersion, found $($jsonContent.Version))"
|
||||
return $null
|
||||
}
|
||||
|
||||
return $jsonContent
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to parse JSON file: $filePath"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
31
Scripts/FileIO/LoadSettings.ps1
Normal file
31
Scripts/FileIO/LoadSettings.ps1
Normal file
@@ -0,0 +1,31 @@
|
||||
# Loads settings from a JSON file and adds them to script params
|
||||
function LoadSettings {
|
||||
param (
|
||||
[string]$filePath,
|
||||
[string]$expectedVersion = "1.0"
|
||||
)
|
||||
|
||||
$settingsJson = LoadJsonFile -filePath $filePath -expectedVersion $expectedVersion
|
||||
|
||||
if (-not $settingsJson -or -not $settingsJson.Settings) {
|
||||
throw "Failed to load settings from $(Split-Path $filePath -Leaf)"
|
||||
}
|
||||
|
||||
# Get current Windows build version
|
||||
$WinVersion = Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' CurrentBuild
|
||||
|
||||
foreach ($setting in $settingsJson.Settings) {
|
||||
if ($setting.Value -eq $false) {
|
||||
continue
|
||||
}
|
||||
|
||||
$feature = $script:Features[$setting.Name]
|
||||
|
||||
# Check version and feature compatibility using Features.json
|
||||
if (($feature.MinVersion -and $WinVersion -lt $feature.MinVersion) -or ($feature.MaxVersion -and $WinVersion -gt $feature.MaxVersion) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) {
|
||||
continue
|
||||
}
|
||||
|
||||
AddParameter $setting.Name $setting.Value
|
||||
}
|
||||
}
|
||||
15
Scripts/FileIO/SaveCustomAppsListToFile.ps1
Normal file
15
Scripts/FileIO/SaveCustomAppsListToFile.ps1
Normal file
@@ -0,0 +1,15 @@
|
||||
# Saves the provided appsList to the CustomAppsList file
|
||||
function SaveCustomAppsListToFile {
|
||||
param (
|
||||
$appsList
|
||||
)
|
||||
|
||||
$script:SelectedApps = $appsList
|
||||
|
||||
# Create file that stores selected apps if it doesn't exist
|
||||
if (-not (Test-Path $script:CustomAppsListFilePath)) {
|
||||
$null = New-Item $script:CustomAppsListFilePath -ItemType File
|
||||
}
|
||||
|
||||
Set-Content -Path $script:CustomAppsListFilePath -Value $script:SelectedApps
|
||||
}
|
||||
26
Scripts/FileIO/SaveSettings.ps1
Normal file
26
Scripts/FileIO/SaveSettings.ps1
Normal file
@@ -0,0 +1,26 @@
|
||||
# Saves the current settings, excluding control parameters, to 'LastUsedSettings.json' file
|
||||
function SaveSettings {
|
||||
$settings = @{
|
||||
"Version" = "1.0"
|
||||
"Settings" = @()
|
||||
}
|
||||
|
||||
foreach ($param in $script:Params.Keys) {
|
||||
if ($script:ControlParams -notcontains $param) {
|
||||
$value = $script:Params[$param]
|
||||
|
||||
$settings.Settings += @{
|
||||
"Name" = $param
|
||||
"Value" = $value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$settings | ConvertTo-Json -Depth 10 | Set-Content $script:SavedSettingsFilePath
|
||||
}
|
||||
catch {
|
||||
Write-Output ""
|
||||
Write-Host "Error: Failed to save settings to LastUsedSettings.json file" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
24
Scripts/FileIO/ValidateAppslist.ps1
Normal file
24
Scripts/FileIO/ValidateAppslist.ps1
Normal file
@@ -0,0 +1,24 @@
|
||||
# Returns a validated list of apps based on the provided appsList and the supported apps from Apps.json
|
||||
function ValidateAppslist {
|
||||
param (
|
||||
$appsList
|
||||
)
|
||||
|
||||
$supportedAppsList = (LoadAppsDetailsFromJson | ForEach-Object { $_.AppId })
|
||||
$validatedAppsList = @()
|
||||
|
||||
# Validate provided appsList against supportedAppsList
|
||||
Foreach ($app in $appsList) {
|
||||
$app = $app.Trim()
|
||||
$appString = $app.Trim('*')
|
||||
|
||||
if ($supportedAppsList -notcontains $appString) {
|
||||
Write-Host "Removal of app '$appString' is not supported and will be skipped" -ForegroundColor Yellow
|
||||
continue
|
||||
}
|
||||
|
||||
$validatedAppsList += $appString
|
||||
}
|
||||
|
||||
return $validatedAppsList
|
||||
}
|
||||
Reference in New Issue
Block a user