This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
DroneHelper/src/FileLinter/Test-FileEOL.ps1
OCram85 8fd180b776
Some checks reported errors
continuous-integration/drone/tag Build was killed
initial migration
2022-07-13 13:59:25 +02:00

62 lines
1.3 KiB
PowerShell

function Test-FileEOL {
<#
.SYNOPSIS
Returns false if EOL isn't CRLF
.DESCRIPTION
Tests given file against valid EOL. Returns true if CRLF is used.
.PARAMETER Path
Relative or full path to an existing file.
.INPUTS
[None]
.OUTPUTS
[bool]
.EXAMPLE
Test-FileEOL -Path './TestFile.txt'
.NOTES
#>
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory = $true)]
[ValidateScript(
{
Test-Path -Path $_
}
)]
[string]$Path
)
begin {
}
process {
$content = Get-Content -Path $Path -Raw -Encoding 'utf8'
$CRLFCount = ([regex]::Matches($content, "`r`n$")).Count
$LFCount = ([regex]::Matches($content, "`n$")).Count
if ($CRLFCount -eq $LFCount) {
Write-Debug -Message 'EOL: CRLFCount = LFCount -> true'
Write-Output $true
}
elseif ($CRLFCount -gt $LFCount) {
Write-Debug -Message 'EOL: CRLFCount > LFCount -> false'
Write-Output $false
}
elseif ($LFCount -gt $CRLFCount) {
Write-Debug -Message 'EOL: CRLFCount < LFCount -> false'
Write-Output $false
}
}
end {
}
}