Skip to content

Instantly share code, notes, and snippets.

@lzybkr
Created January 13, 2017 02:25
Show Gist options
  • Save lzybkr/f040f18d1fbfff9eaf3f4533e24126fe to your computer and use it in GitHub Desktop.
Save lzybkr/f040f18d1fbfff9eaf3f4533e24126fe to your computer and use it in GitHub Desktop.
using namespace System.Text.RegularExpressions
param([string[]]$extensions = 'ps1')
function Get-FileEncoding
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path
)
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) { Write-Output 'UTF8' }
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { Write-Output 'Unicode' }
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { Write-Output 'UTF32' }
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { Write-Output 'UTF7'}
else { Write-Output 'ASCII' }
}
function CheckFix($orig, $new)
{
$origContent = Get-Content -Encoding byte $orig
$newContent = Get-Content -Encoding byte $new
if ($origContent.Length -lt $newContent.Length)
{
Write-Error "Unexpected - original is shorter: $orig"
return $false
}
$newI = 0
$oldI = 0
while ($newI -lt $newContent.Length)
{
$o = $origContent[$oldI]
$n = $newContent[$newI]
while ($o -ne $n -and ($o -eq 32 -or $o -eq 9))
{
$oldI += 1
$o = $origContent[$oldI]
}
if ($o -ne $n)
{
$msg = "file $orig differs, encoding messed up somehow`n" +
($origContent[($oldI-6)..($oldI+9)] | fhx | Out-String) +
($newContent[($newI-6)..($newI+9)] | fhx | Out-String)
Write-Error $msg
return $false
}
$oldI += 1
$newI += 1
}
return $true
}
function FixWhiteSpace
{
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[Alias('PSPath')]
[string] $LiteralPath
)
begin {
$trailingWhiteSpace = [Regex]::new('(?m)[ \t]+\r?$', [RegexOptions]::Compiled)
$tempFile = New-TemporaryFile
}
process {
try {
$LiteralPath = (Resolve-Path $LiteralPath).ProviderPath
if (Test-path -PathType Leaf $LiteralPath) {
$Encoding = Get-FileEncoding $LiteralPath
$content = Get-Content -Raw -LiteralPath $LiteralPath
if ($null -ne $content) {
$trimmedContent = $trailingWhiteSpace.Replace($content, "`r")
if ($trimmedContent -ne $content) {
# Set-Content fails for some reason with 'Set-Content : Stream was not readable.'
[io.file]::WriteAllText($tempFile, $trimmedContent, [Text.Encoding]::$Encoding)
if (CheckFix $LiteralPath $tempFile)
{
mv $tempFile $LiteralPath -Force
}
Write-Verbose "Updated '$LiteralPath' with encoding $Encoding"
#Get-Item $LiteralPath
}
}
}
}
catch {
Write-Error -Message $_.Exception.Message -TargetObject $LiteralPath
}
}
}
$extensionRegex = ($extensions -join '$|') + '$'
(git ls-tree -r --name-only --full-name HEAD) -match $extensionRegex | Get-ChildItem | FixWhiteSpace -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment