From ae33b3535496487fc116ce6681ed6b5849d59a77 Mon Sep 17 00:00:00 2001 From: sandrews Date: Mon, 15 Sep 2025 12:42:53 -0500 Subject: [PATCH] Refactor Update-Scripts function to improve error handling and streamline file operations --- .gitattributes | 25 +++++++++++++++++++++++++ 1_Install.ps1 | 19 ++++++++++++++++++- 2_ConfigUpdate.ps1 | 19 ++++++++++++++++++- 3_ConfigAfterNextcloud.ps1 | 19 ++++++++++++++++++- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bc5dbcd --- /dev/null +++ b/.gitattributes @@ -0,0 +1,25 @@ +# Set default behavior to LF line endings +* text=auto eol=lf + +# Explicitly declare text files you want to always be normalized and converted +# to native line endings on checkout. +*.ps1 text eol=lf +*.txt text eol=lf +*.json text eol=lf +*.js text eol=lf +*.py text eol=lf +*.csv text eol=lf +*.md text eol=lf +*.ini text eol=lf +*.sh text eol=lf + +# Denote all files that are truly binary and should not be modified. +*.png binary +*.jpg binary +*.jpeg binary +*.gif binary +*.ico binary +*.ttf binary +*.zip binary +*.db binary +*.ptb binary diff --git a/1_Install.ps1 b/1_Install.ps1 index 2418e69..246a29b 100644 --- a/1_Install.ps1 +++ b/1_Install.ps1 @@ -73,9 +73,26 @@ function Update-Scripts { if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) { Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan - # Function to get file hash + # Function to get file hash that normalizes line endings function Get-FileHashQuick($filePath) { if (Test-Path $filePath -PathType Leaf) { + $fileExt = [System.IO.Path]::GetExtension($filePath).ToLower() + + # List of text file extensions that might have line ending differences + $textExtensions = @(".ps1", ".txt", ".json", ".js", ".py", ".csv", ".md", ".ini", ".sh") + + if ($textExtensions -contains $fileExt) { + # For text files, normalize line endings before computing hash + $content = Get-Content -Path $filePath -Raw + if ($content) { + # Normalize to LF + $normalizedContent = $content.Replace("`r`n", "`n") + $stream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($normalizedContent)) + return (Get-FileHash -InputStream $stream -Algorithm MD5).Hash + } + } + + # For binary files or if normalization failed return (Get-FileHash -Path $filePath -Algorithm MD5).Hash } return $null diff --git a/2_ConfigUpdate.ps1 b/2_ConfigUpdate.ps1 index 3487ba2..b43f518 100644 --- a/2_ConfigUpdate.ps1 +++ b/2_ConfigUpdate.ps1 @@ -73,9 +73,26 @@ function Update-Scripts { if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) { Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan - # Function to get file hash + # Function to get file hash that normalizes line endings function Get-FileHashQuick($filePath) { if (Test-Path $filePath -PathType Leaf) { + $fileExt = [System.IO.Path]::GetExtension($filePath).ToLower() + + # List of text file extensions that might have line ending differences + $textExtensions = @(".ps1", ".txt", ".json", ".js", ".py", ".csv", ".md", ".ini", ".sh") + + if ($textExtensions -contains $fileExt) { + # For text files, normalize line endings before computing hash + $content = Get-Content -Path $filePath -Raw + if ($content) { + # Normalize to LF + $normalizedContent = $content.Replace("`r`n", "`n") + $stream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($normalizedContent)) + return (Get-FileHash -InputStream $stream -Algorithm MD5).Hash + } + } + + # For binary files or if normalization failed return (Get-FileHash -Path $filePath -Algorithm MD5).Hash } return $null diff --git a/3_ConfigAfterNextcloud.ps1 b/3_ConfigAfterNextcloud.ps1 index 9bb264d..d2b4002 100644 --- a/3_ConfigAfterNextcloud.ps1 +++ b/3_ConfigAfterNextcloud.ps1 @@ -73,9 +73,26 @@ function Update-Scripts { if (($extractedDir) -and ((Test-Path $extractedDir.FullName))) { Write-Host "Comparing update files with current installation..." -ForegroundColor Cyan - # Function to get file hash + # Function to get file hash that normalizes line endings function Get-FileHashQuick($filePath) { if (Test-Path $filePath -PathType Leaf) { + $fileExt = [System.IO.Path]::GetExtension($filePath).ToLower() + + # List of text file extensions that might have line ending differences + $textExtensions = @(".ps1", ".txt", ".json", ".js", ".py", ".csv", ".md", ".ini", ".sh") + + if ($textExtensions -contains $fileExt) { + # For text files, normalize line endings before computing hash + $content = Get-Content -Path $filePath -Raw + if ($content) { + # Normalize to LF + $normalizedContent = $content.Replace("`r`n", "`n") + $stream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($normalizedContent)) + return (Get-FileHash -InputStream $stream -Algorithm MD5).Hash + } + } + + # For binary files or if normalization failed return (Get-FileHash -Path $filePath -Algorithm MD5).Hash } return $null