Refactor Update-Scripts function to improve file comparison logic and user prompts for applying updates
This commit is contained in:
@@ -71,8 +71,68 @@ function Update-Scripts {
|
|||||||
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1
|
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1
|
||||||
|
|
||||||
if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) {
|
if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) {
|
||||||
# Copy all files except .git directory to current location
|
Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan
|
||||||
Write-Host "Installing updates from $($extractedDir.FullName)..." -ForegroundColor Cyan
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
|
$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
|
||||||
|
|
||||||
|
# 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
|
# Copy files from the extracted directory directly
|
||||||
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
|
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
|
||||||
@@ -80,9 +140,6 @@ function Update-Scripts {
|
|||||||
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
|
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
# Record the update time
|
|
||||||
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force
|
|
||||||
|
|
||||||
Write-Host "Update completed successfully!" -ForegroundColor Green
|
Write-Host "Update completed successfully!" -ForegroundColor Green
|
||||||
|
|
||||||
# Suggest restarting the script with the updated version
|
# Suggest restarting the script with the updated version
|
||||||
@@ -93,6 +150,14 @@ function Update-Scripts {
|
|||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
return $true # Update performed successfully
|
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
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
|
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,8 +71,68 @@ function Update-Scripts {
|
|||||||
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1
|
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1
|
||||||
|
|
||||||
if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) {
|
if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) {
|
||||||
# Copy all files except .git directory to current location
|
Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan
|
||||||
Write-Host "Installing updates from $($extractedDir.FullName)..." -ForegroundColor Cyan
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
|
$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
|
||||||
|
|
||||||
|
# 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
|
# Copy files from the extracted directory directly
|
||||||
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
|
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
|
||||||
@@ -80,9 +140,6 @@ function Update-Scripts {
|
|||||||
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
|
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
# Record the update time
|
|
||||||
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force
|
|
||||||
|
|
||||||
Write-Host "Update completed successfully!" -ForegroundColor Green
|
Write-Host "Update completed successfully!" -ForegroundColor Green
|
||||||
|
|
||||||
# Suggest restarting the script with the updated version
|
# Suggest restarting the script with the updated version
|
||||||
@@ -93,6 +150,14 @@ function Update-Scripts {
|
|||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
return $true # Update performed successfully
|
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
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
|
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,8 +71,68 @@ function Update-Scripts {
|
|||||||
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1
|
$extractedDir = Get-ChildItem -Path $tempDir -Directory | Where-Object { $_.Name -eq "windows-install" } | Select-Object -First 1
|
||||||
|
|
||||||
if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) {
|
if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) {
|
||||||
# Copy all files except .git directory to current location
|
Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan
|
||||||
Write-Host "Installing updates from $($extractedDir.FullName)..." -ForegroundColor Cyan
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
|
$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
|
||||||
|
|
||||||
|
# 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
|
# Copy files from the extracted directory directly
|
||||||
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
|
Get-ChildItem -Path $extractedDir.FullName | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
|
||||||
@@ -80,9 +140,6 @@ function Update-Scripts {
|
|||||||
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
|
Copy-Item -Path $_.FullName -Destination $destPath -Recurse -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
# Record the update time
|
|
||||||
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $updateCheckFile -Force
|
|
||||||
|
|
||||||
Write-Host "Update completed successfully!" -ForegroundColor Green
|
Write-Host "Update completed successfully!" -ForegroundColor Green
|
||||||
|
|
||||||
# Suggest restarting the script with the updated version
|
# Suggest restarting the script with the updated version
|
||||||
@@ -93,6 +150,14 @@ function Update-Scripts {
|
|||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
return $true # Update performed successfully
|
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
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
|
Write-Host "Could not find extracted update directory." -ForegroundColor Yellow
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user