Skip to content

Commit

Permalink
#195: check whether the CopyRight information at the bottom already e…
Browse files Browse the repository at this point in the history
…xists.

If NO then append it, if YES then replace it
  • Loading branch information
TollJulian committed Jul 30, 2024
1 parent 4fa2db4 commit 57190dd
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions AzureDevops_AddCopyRight.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 57190dd

Please sign in to comment.