Refactor Update-Scripts function to improve file comparison logic and user prompts for applying updates

This commit is contained in:
2025-09-15 12:35:45 -05:00
parent 18e0d633c6
commit f85d9ad03a
3 changed files with 243 additions and 48 deletions

View File

@@ -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
}