Skip to content

Commit

Permalink
Merge pull request #14 from kolosovpetro/updates
Browse files Browse the repository at this point in the history
editor config | readme | end of lines and encoding
  • Loading branch information
kolosovpetro committed Nov 18, 2023
2 parents ed2d418 + 56691dc commit be6caff
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf

[*.{xml,yml}]
indent_size = 2
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,3 @@ Compiled document looks like as follows
<img src="img/template_example.PNG" alt="template_example"/>
<img src="img/template_example2.PNG" alt="template_example"/>
</p>

## License

This work is licensed under [Attribution-NonCommercial-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode) license
71 changes: 71 additions & 0 deletions scripts/verify-encoding.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<#
.SYNOPSIS
This script will verify that there's no UTF-8 BOM or CRLF line endings in the files inside of the project.
#>
param (
# Path to the repository root. All text files under the root will be checked for UTF-8 BOM and CRLF.
$SourceRoot = "$PSScriptRoot/..",

# Makes the script to perform file modifications to bring them to the standard.
[switch] $Autofix
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# For PowerShell to properly process the UTF-8 output from git ls-tree we need to set up the output encoding:
[Console]::OutputEncoding = [Text.Encoding]::UTF8

$allFiles = git -c core.quotepath=off ls-tree -r HEAD --name-only
Write-Output "Total files in the repository: $($allFiles.Length)"

# https://stackoverflow.com/questions/6119956/how-to-determine-if-git-handles-a-file-as-binary-or-as-text#comment15281840_6134127
$nullHash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
$textFiles = git -c core.quotepath=off diff --numstat $nullHash HEAD -- @allFiles |
Where-Object { -not $_.StartsWith('-') } |
ForEach-Object { [Regex]::Unescape($_.Split("`t", 3)[2]) }
Write-Output "Text files in the repository: $($textFiles.Length)"

$bom = @(0xEF, 0xBB, 0xBF)
$bomErrors = @()
$lineEndingErrors = @()

try {
Push-Location $SourceRoot
foreach ($file in $textFiles) {
if ([IO.Path]::GetExtension($file) -eq '.DotSettings' -or $file.EndsWith('.verified.cs')) {
continue
}

$fullPath = Resolve-Path -LiteralPath $file
$bytes = [IO.File]::ReadAllBytes($fullPath) | Select-Object -First $bom.Length
$bytesEqualsBom = @(Compare-Object $bytes $bom -SyncWindow 0).Length -eq 0
if ($bytesEqualsBom -and $Autofix) {
$fullContent = [IO.File]::ReadAllBytes($fullPath)
$newContent = $fullContent | Select-Object -Skip $bom.Length
[IO.File]::WriteAllBytes($fullPath, $newContent)
Write-Output "Removed UTF-8 BOM from file $file"
} elseif ($bytesEqualsBom) {
$bomErrors += @($file)
}

$text = [IO.File]::ReadAllText($fullPath)
$hasWrongLineEndings = $text.Contains("`r`n")
if ($hasWrongLineEndings -and $Autofix) {
$newText = $text -replace "`r`n", "`n"
[IO.File]::WriteAllText($fullPath, $newText)
Write-Output "Fixed the line endings for file $file"
} elseif ($hasWrongLineEndings) {
$lineEndingErrors += @($file)
}
}

if ($bomErrors.Length) {
throw "The following $($bomErrors.Length) files have UTF-8 BOM:`n" + ($bomErrors -join "`n")
}
if ($lineEndingErrors.Length) {
throw "The following $($lineEndingErrors.Length) files have CRLF instead of LF:`n" + ($lineEndingErrors -join "`n")
}
} finally {
Pop-Location
}

0 comments on commit be6caff

Please sign in to comment.