From 57190ddaf743c7e91e0261d473fb84791e582a93 Mon Sep 17 00:00:00 2001 From: TollJulian Date: Tue, 30 Jul 2024 16:46:38 +0200 Subject: [PATCH] #195: check whether the CopyRight information at the bottom already exists. If NO then append it, if YES then replace it --- AzureDevops_AddCopyRight.ps1 | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/AzureDevops_AddCopyRight.ps1 b/AzureDevops_AddCopyRight.ps1 index ad90634..7e3a66c 100644 --- a/AzureDevops_AddCopyRight.ps1 +++ b/AzureDevops_AddCopyRight.ps1 @@ -9,28 +9,46 @@ $timestamp = (Get-Date).ToUniversalTime() $HashCodeFile = Split-Path $MyInvocation.MyCommand.Path #> -function Add-CopyRight($fileConent, $version, $buildTime) { +function Add-CopyRight($fileContent, $version, $buildTime) { $copyRight = @" #=============================================================================# # Copyright (c) coolOrange s.r.l. - All rights reserved. # -# Verson: {0} Buildtime (UTC): {1} # +# Version: {0} Buildtime (UTC): {1} # #=============================================================================# "@ -f @($version, $buildTime) - $fileConentWithCopyRight = "$($fileConent)`n`n" + $($copyRight) + $fileConentWithCopyRight = "$($fileContent)`n`n" + $($copyRight) return $fileConentWithCopyRight } - +function RemoveCopyRight { + param ($ContentLines) + $copyRightMatches = $ContentLines | Select-String -Pattern 'Copyright \(c\) coolOrange s\.r\.l\. - All rights reserved\.' + if ($null -ne $copyRightMatches) { + Write-Host "remove old copyright" + foreach ($result in $copyRightMatches) { + # remove lines including the copy right block and preceeding blank line + for ($i = $result.LineNumber - 3; $i -lt $result.LineNumber + 2; $i++) { + if ($i -lt 0 -or $i -ge $ContentLines.Count) { + # block is on top -> no blank line preceeding (for legacy script files) + continue + } + $ContentLines.Set($i,$null) + } + } + } + return $ContentLines +} $currentPath = Split-Path $MyInvocation.MyCommand.Path Get-ChildItem "$currentPath\Files" -Recurse -Include *.psm1, *.ps1 | ForEach-Object { - $fileContent = (Get-Content $_ -Raw) - + $fileContentLines = (Get-Content $_) + $fileContentLines = RemoveCopyRight -ContentLines $fileContentLines + $fileContent = ($fileContentLines -join "`r`n").TrimEnd("`r`n").TrimStart("`r`n") $version = "$($Major).$($Minor).$($Build).$($Revision)" - $newContent = Add-CopyRight -fileConent $fileContent -version $version -buildTime $Timestamp + $newContent = Add-CopyRight -fileContent $fileContent -version $version -buildTime $Timestamp Set-Content $_ -Value $newContent -Encoding UTF8 Write-Host "Add copyright to file $($_.Name)" $hash = Get-FileHash $_ (Split-Path -Path $hash.Path -Leaf) + "($($hash.Algorithm)): $($hash.Hash)" | Out-File "$HashCodeFile\PSCodeHash.txt" -Append -} +} \ No newline at end of file