From f85d9ad03a261c696029cbb0832f58d843546996 Mon Sep 17 00:00:00 2001 From: sandrews Date: Mon, 15 Sep 2025 12:35:45 -0500 Subject: [PATCH] Refactor Update-Scripts function to improve file comparison logic and user prompts for applying updates --- 1_Install.ps1 | 97 +++++++++++++++++++++++++++++++------- 2_ConfigUpdate.ps1 | 97 +++++++++++++++++++++++++++++++------- 3_ConfigAfterNextcloud.ps1 | 97 +++++++++++++++++++++++++++++++------- 3 files changed, 243 insertions(+), 48 deletions(-) diff --git a/1_Install.ps1 b/1_Install.ps1 index 9d82684..2418e69 100644 --- a/1_Install.ps1 +++ b/1_Install.ps1 @@ -71,28 +71,93 @@ function Update-Scripts { $extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1 if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) { - # Copy all files except .git directory to current location - Write-Host "Installing updates from $($extractedDir.FullName)..." -ForegroundColor Cyan + Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan - # Copy files from the extracted directory directly - Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object { - $destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name - Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force + # Function to get file hash + function Get-FileHashQuick($filePath) { + if (Test-Path $filePath -PathType Leaf) { + return (Get-FileHash -Path $filePath -Algorithm MD5).Hash + } + return $null } - # Record the update time + $differentFiles = @() + $newFiles = @() + $unchangedFiles = @() + + # Compare files from the extracted directory with existing files + Get-ChildItem -Path $extractedDir.FullName -Recurse | Where-Object { !$_.PSIsContainer -and $_.Name -ne ".git" } | ForEach-Object { + $relativePath = $_.FullName.Substring($extractedDir.FullName.Length + 1) + $currentFilePath = Join-Path -Path $updateScriptDir -ChildPath $relativePath + + if (Test-Path $currentFilePath) { + $newHash = Get-FileHashQuick $_.FullName + $currentHash = Get-FileHashQuick $currentFilePath + + if ($newHash -ne $currentHash) { + $differentFiles += $relativePath + } else { + $unchangedFiles += $relativePath + } + } else { + $newFiles += $relativePath + } + } + + # Display the results + Write-Host "`nUpdate analysis complete!" -ForegroundColor Green + + if ($differentFiles.Count -gt 0) { + Write-Host "`nFiles with changes ($($differentFiles.Count)):" -ForegroundColor Yellow + foreach ($file in $differentFiles) { + Write-Host " - $file" -ForegroundColor Yellow + } + } + + if ($newFiles.Count -gt 0) { + Write-Host "`nNew files ($($newFiles.Count)):" -ForegroundColor Cyan + foreach ($file in $newFiles) { + Write-Host " - $file" -ForegroundColor Cyan + } + } + + if ($unchangedFiles.Count -gt 0) { + Write-Host "`nUnchanged files: $($unchangedFiles.Count)" -ForegroundColor DarkGray + } + + # Record the update check time Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force - Write-Host "Update completed successfully!" -ForegroundColor Green - - # Suggest restarting the script with the updated version - $restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)" - if ($restartChoice -eq "Y" -or $restartChoice -eq "y") { - Write-Host "Restarting script..." -ForegroundColor Cyan - Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" - exit + # Only ask to apply changes if there are differences + if ($differentFiles.Count -gt 0 -or $newFiles.Count -gt 0) { + $applyChoice = Read-Host "`nWould you like to apply these changes? (Y/N)" + if ($applyChoice -eq "Y" -or $applyChoice -eq "y") { + Write-Host "Installing updates..." -ForegroundColor Cyan + + # Copy files from the extracted directory directly + Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object { + $destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name + Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force + } + + Write-Host "Update completed successfully!" -ForegroundColor Green + + # Suggest restarting the script with the updated version + $restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)" + if ($restartChoice -eq "Y" -or $restartChoice -eq "y") { + Write-Host "Restarting script..." -ForegroundColor Cyan + Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" + exit + } + return $true # Update performed successfully + } else { + Write-Host "Update cancelled. No changes were made." -ForegroundColor Yellow + return $false + } + } else { + Write-Host "`nNo changes detected. Your installation is up to date!" -ForegroundColor Green + return $false # No changes to apply } - return $true # Update performed successfully } else { Write-Host "Could not find extracted update directory." -ForegroundColor Yellow } diff --git a/2_ConfigUpdate.ps1 b/2_ConfigUpdate.ps1 index 1b5c5ff..3487ba2 100644 --- a/2_ConfigUpdate.ps1 +++ b/2_ConfigUpdate.ps1 @@ -71,28 +71,93 @@ function Update-Scripts { $extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1 if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) { - # Copy all files except .git directory to current location - Write-Host "Installing updates from $($extractedDir.FullName)..." -ForegroundColor Cyan + Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan - # Copy files from the extracted directory directly - Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object { - $destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name - Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force + # Function to get file hash + function Get-FileHashQuick($filePath) { + if (Test-Path $filePath -PathType Leaf) { + return (Get-FileHash -Path $filePath -Algorithm MD5).Hash + } + return $null } - # Record the update time + $differentFiles = @() + $newFiles = @() + $unchangedFiles = @() + + # Compare files from the extracted directory with existing files + Get-ChildItem -Path $extractedDir.FullName -Recurse | Where-Object { !$_.PSIsContainer -and $_.Name -ne ".git" } | ForEach-Object { + $relativePath = $_.FullName.Substring($extractedDir.FullName.Length + 1) + $currentFilePath = Join-Path -Path $updateScriptDir -ChildPath $relativePath + + if (Test-Path $currentFilePath) { + $newHash = Get-FileHashQuick $_.FullName + $currentHash = Get-FileHashQuick $currentFilePath + + if ($newHash -ne $currentHash) { + $differentFiles += $relativePath + } else { + $unchangedFiles += $relativePath + } + } else { + $newFiles += $relativePath + } + } + + # Display the results + Write-Host "`nUpdate analysis complete!" -ForegroundColor Green + + if ($differentFiles.Count -gt 0) { + Write-Host "`nFiles with changes ($($differentFiles.Count)):" -ForegroundColor Yellow + foreach ($file in $differentFiles) { + Write-Host " - $file" -ForegroundColor Yellow + } + } + + if ($newFiles.Count -gt 0) { + Write-Host "`nNew files ($($newFiles.Count)):" -ForegroundColor Cyan + foreach ($file in $newFiles) { + Write-Host " - $file" -ForegroundColor Cyan + } + } + + if ($unchangedFiles.Count -gt 0) { + Write-Host "`nUnchanged files: $($unchangedFiles.Count)" -ForegroundColor DarkGray + } + + # Record the update check time Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force - Write-Host "Update completed successfully!" -ForegroundColor Green - - # Suggest restarting the script with the updated version - $restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)" - if ($restartChoice -eq "Y" -or $restartChoice -eq "y") { - Write-Host "Restarting script..." -ForegroundColor Cyan - Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" - exit + # Only ask to apply changes if there are differences + if ($differentFiles.Count -gt 0 -or $newFiles.Count -gt 0) { + $applyChoice = Read-Host "`nWould you like to apply these changes? (Y/N)" + if ($applyChoice -eq "Y" -or $applyChoice -eq "y") { + Write-Host "Installing updates..." -ForegroundColor Cyan + + # Copy files from the extracted directory directly + Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object { + $destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name + Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force + } + + Write-Host "Update completed successfully!" -ForegroundColor Green + + # Suggest restarting the script with the updated version + $restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)" + if ($restartChoice -eq "Y" -or $restartChoice -eq "y") { + Write-Host "Restarting script..." -ForegroundColor Cyan + Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" + exit + } + return $true # Update performed successfully + } else { + Write-Host "Update cancelled. No changes were made." -ForegroundColor Yellow + return $false + } + } else { + Write-Host "`nNo changes detected. Your installation is up to date!" -ForegroundColor Green + return $false # No changes to apply } - return $true # Update performed successfully } else { Write-Host "Could not find extracted update directory." -ForegroundColor Yellow } diff --git a/3_ConfigAfterNextcloud.ps1 b/3_ConfigAfterNextcloud.ps1 index 1e67d96..9bb264d 100644 --- a/3_ConfigAfterNextcloud.ps1 +++ b/3_ConfigAfterNextcloud.ps1 @@ -71,28 +71,93 @@ function Update-Scripts { $extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1 if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) { - # Copy all files except .git directory to current location - Write-Host "Installing updates from $($extractedDir.FullName)..." -ForegroundColor Cyan + Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan - # Copy files from the extracted directory directly - Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object { - $destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name - Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force + # Function to get file hash + function Get-FileHashQuick($filePath) { + if (Test-Path $filePath -PathType Leaf) { + return (Get-FileHash -Path $filePath -Algorithm MD5).Hash + } + return $null } - # Record the update time + $differentFiles = @() + $newFiles = @() + $unchangedFiles = @() + + # Compare files from the extracted directory with existing files + Get-ChildItem -Path $extractedDir.FullName -Recurse | Where-Object { !$_.PSIsContainer -and $_.Name -ne ".git" } | ForEach-Object { + $relativePath = $_.FullName.Substring($extractedDir.FullName.Length + 1) + $currentFilePath = Join-Path -Path $updateScriptDir -ChildPath $relativePath + + if (Test-Path $currentFilePath) { + $newHash = Get-FileHashQuick $_.FullName + $currentHash = Get-FileHashQuick $currentFilePath + + if ($newHash -ne $currentHash) { + $differentFiles += $relativePath + } else { + $unchangedFiles += $relativePath + } + } else { + $newFiles += $relativePath + } + } + + # Display the results + Write-Host "`nUpdate analysis complete!" -ForegroundColor Green + + if ($differentFiles.Count -gt 0) { + Write-Host "`nFiles with changes ($($differentFiles.Count)):" -ForegroundColor Yellow + foreach ($file in $differentFiles) { + Write-Host " - $file" -ForegroundColor Yellow + } + } + + if ($newFiles.Count -gt 0) { + Write-Host "`nNew files ($($newFiles.Count)):" -ForegroundColor Cyan + foreach ($file in $newFiles) { + Write-Host " - $file" -ForegroundColor Cyan + } + } + + if ($unchangedFiles.Count -gt 0) { + Write-Host "`nUnchanged files: $($unchangedFiles.Count)" -ForegroundColor DarkGray + } + + # Record the update check time Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force - Write-Host "Update completed successfully!" -ForegroundColor Green - - # Suggest restarting the script with the updated version - $restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)" - if ($restartChoice -eq "Y" -or $restartChoice -eq "y") { - Write-Host "Restarting script..." -ForegroundColor Cyan - Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" - exit + # Only ask to apply changes if there are differences + if ($differentFiles.Count -gt 0 -or $newFiles.Count -gt 0) { + $applyChoice = Read-Host "`nWould you like to apply these changes? (Y/N)" + if ($applyChoice -eq "Y" -or $applyChoice -eq "y") { + Write-Host "Installing updates..." -ForegroundColor Cyan + + # Copy files from the extracted directory directly + Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object { + $destPath = Join-Path -Path $updateScriptDir -ChildPath $_.Name + Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force + } + + Write-Host "Update completed successfully!" -ForegroundColor Green + + # Suggest restarting the script with the updated version + $restartChoice = Read-Host "The script has been updated. Would you like to restart the script to use the updated version? (Y/N)" + if ($restartChoice -eq "Y" -or $restartChoice -eq "y") { + Write-Host "Restarting script..." -ForegroundColor Cyan + Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" + exit + } + return $true # Update performed successfully + } else { + Write-Host "Update cancelled. No changes were made." -ForegroundColor Yellow + return $false + } + } else { + Write-Host "`nNo changes detected. Your installation is up to date!" -ForegroundColor Green + return $false # No changes to apply } - return $true # Update performed successfully } else { Write-Host "Could not find extracted update directory." -ForegroundColor Yellow }