EndOfLine.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
$script:SUT = $false
$script:ReportCollection
<#
.SYNOPSIS
Converts UTF-8 encoded files with CRLF end-of-line markings to LF markings

.DESCRIPTION
With the given -Path, this function will look for a .gitignore file for exclusion unless
specified by the -SkipIgnoreFile switch. With this ignore file you can add additional
items using the -Exclude parameter. There are limitations with nested items being excluded,
see Examples for more info.

If a .gitignore file is found it will output in the console before the console prompts you
to proceed with conversion. If the -WhatIf is switched, it will perform the same operations
as if it wasn't except the writing operation with the file.

After conversion operation, the report will output in the console. The -ExportReportData
switch is an alternative to the report mentioned.

.PARAMETER Path
Ideally the location of the repository (.git folder). But it can be any folder too, just
haven't tested in those circumstances much.

.PARAMETER Exclude
To exclude files or folders. This can be used in addition to this function using the .gitignore
file or without it. Exclusion of .gitignore file use -SkipIgnoreFile switch.

.PARAMETER SkipIgnoreFile
Skips seeking and import of .gitignore file.

.PARAMETER ExportReportData
For every file it opens, regardless if its written too, will be included in this output if switched

.PARAMETER WhatIf
Will do the same operations as if not switched except writing to the file.

.EXAMPLE
The path being used in this example will, assume that there is a .gitignore in it or depth or two below.

ConvertTo-CRLF C:\repos\sots -WhatIf

.EXAMPLE
If the repo has a .gitignore file, skip it and just exclude modules folder that is specified in
-Exclude parameter.

ConvertTo-CRLF C:\repos\sots -Exclude .\modules\, .\node_modules\ -SkipIgnoreFile -WhatIf

.EXAMPLE
Having the -Verbose switched it will continue the parsing a .gitignore file it there is one, and will
prompt before proceeding to convert files.

ConvertTo-CRLF C:\repos\sots -Verbose

.NOTES
General notes
#>

function ConvertTo-LF {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path,

        [Parameter(Mandatory = $false,
            ValueFromPipeline = $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Exclude,

        [switch]$SkipIgnoreFile,

        [switch]$ExportReportData,

        [switch]$WhatIf
    )
    $Path | Convert-EOL -EOL 'LF' `
        -Exclude $Exclude `
        -SkipIgnoreFile:$SkipIgnoreFile.IsPresent `
        -ExportReportData:$ExportReportData.IsPresent `
        -WhatIf:$WhatIf.IsPresent
}

<#
.SYNOPSIS
Converts UTF-8 encoded files with LF end-of-line markings to CRLF markings

.DESCRIPTION
With the given -Path, this function will look for a .gitignore file for exclusion unless
specified by the -SkipIgnoreFile switch. With this ignore file you can add additional
items using the -Exclude parameter. There are limitations with nested items being excluded,
see Examples for more info.

If a .gitignore file is found it will output in the console before the console prompts you
to proceed with conversion. If the -WhatIf is switched, it will perform the same operations
as if it wasn't except the writing operation with the file.

After conversion operation, the report will output in the console. The -ExportReportData
switch is an alternative to the report mentioned.

.PARAMETER Path
Ideally the location of the repository (.git folder). But it can be any folder too, just
haven't tested in those circumstances much.

.PARAMETER Exclude
To exclude files or folders. This can be used in addition to this function using the .gitignore
file or without it. Exclusion of .gitignore file use -SkipIgnoreFile switch.

.PARAMETER SkipIgnoreFile
Skips seeking and import of .gitignore file.

.PARAMETER ExportReportData
For every file it opens, regardless if its written too, will be included in this output if switched

.PARAMETER WhatIf
Will do the same operations as if not switched except writing to the file.

.EXAMPLE
The path being used in this example will, assume that there is a .gitignore in it or depth or two below.

ConvertTo-CRLF C:\repos\sots -WhatIf

.EXAMPLE
If the repo has a .gitignore file, skip it and just exclude modules folder that is specified in
-Exclude parameter.

ConvertTo-CRLF C:\repos\sots -Exclude .\modules\, .\node_modules\ -SkipIgnoreFile -WhatIf

.EXAMPLE
Having the -Verbose switched it will continue the parsing a .gitignore file it there is one, and will
prompt before proceeding to convert files.

ConvertTo-CRLF C:\repos\sots -Verbose

.NOTES
General notes
#>

function ConvertTo-CRLF {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path,

        [Parameter(Mandatory = $false,
            ValueFromPipeline = $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Exclude,

        [switch]$SkipIgnoreFile,

        [switch]$ExportReportData,

        [switch]$WhatIf
    )
    $Path | Convert-EOL -EOL 'CRLF' `
        -Exclude $Exclude `
        -SkipIgnoreFile:$SkipIgnoreFile.IsPresent `
        -ExportReportData:$ExportReportData.IsPresent `
        -WhatIf:$WhatIf.IsPresent
}

function Convert-EOL {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path,

        [Parameter(Mandatory = $false,
            ValueFromPipeline = $false)]
        [string[]]$Exclude,

        [ValidateSet("LF", "CRLF")]
        [string]$EOL,

        [switch]$SkipIgnoreFile,

        [switch]$ExportReportData,

        [switch]$WhatIf
    )

    if ($Path | Test-Path -PathType Container) {
        Push-Location
        if ($Path -eq '.') {
            $Path = Resolve-Path '.'
        }
        $Path | Set-Location

        if (($SkipIgnoreFile.IsPresent -eq $false) -and (!$Exclude)) {
            $GitIgnoreContents = Import-GitIgnoreFile $Path
            if ($GitIgnoreContents) {
                $IgnoreHashTable = New-IgnoreHashTable $GitIgnoreContents
            }
        }
        elseif (($SkipIgnoreFile.IsPresent -eq $false) -and ($Exclude)) {
            $GitIgnoreContents = Import-GitIgnoreFile $Path
            if ($GitIgnoreContents) {
                $ArrayList = New-Object System.Collections.ArrayList
                $ArrayList.AddRange($GitIgnoreContents)
                $ArrayList.AddRange($Exclude)
                [string[]]$GitAndExcludeContents = $ArrayList.ToArray()
                $IgnoreHashTable = New-IgnoreHashTable $GitAndExcludeContents
            }
        }
        elseif ($Exclude) {
            $IgnoreHashTable = New-IgnoreHashTable $Exclude
        }
        Pop-Location
    }
    
    $ConfirmationMessage = New-ConfirmationMessage -EOL $EOL -WhatIf:$WhatIf.IsPresent
    $Decision = Request-Confirmation -Message $ConfirmationMessage -WhatIf:$WhatIf.IsPresent

    if ($Decision -eq $true) {
        Start-ConversionProcess -Path $Path `
            -EOL $EOL `
            -IgnoreHashTable $IgnoreHashTable `
            -ExportReportData:$ExportReportData.IsPresent `
            -WhatIf:$WhatIf.IsPresent
    }
    else {
        Write-Output -InputObject "Operation has been cancelled, no files have been modified."
    }
}

function Import-GitIgnoreFile {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path
    )
    # TODO: if $Exclude contains a .gitignore file (perhaps nested module repo) it needs to be ignored in this method
    try {
        
        $IgnoreFile = Get-ChildItem -Path . -Recurse -Depth 3 -Filter ".gitignore" | Select-Object -First 1
        
        # TODO: Import-GitIgnoreFile is only called for with path being a folder. Get-ParentItem
        # will be benefical if user drills into a specific dir but still needs the repo .gitignore
        <#
        else {
            $Path | Get-ItemPropertyValue -Name DirectoryName
            $IgnoreFile = Get-ParentItem -Path $Path -Name '.gitignore' -Recurse
        }#>


        $GitIgnoreContents = $IgnoreFile | `
            Get-Content | `
            Where-Object {($_ -match '\S+') -and ($_.StartsWith('#') -ne $true)}

        if ($IgnoreFile) {
            Write-Host ("Imported and will be using the following ignore file: " + $IgnoreFile.FullName)
        }

        $GitIgnoreContents
    }
    catch {
        Write-Error "An error occurred with importing and parsing .gitignore file."
    }
}

function New-IgnoreHashTable {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Contents
    )

    # since .git folder is not listed in .gitignore, add it here
    $Contents += '.git/'
    $FolderEntries = @()
    $FileEntries = @()
    
    Write-Verbose ("Starting to parse excluded items")

    $Contents | ForEach-Object {
        # global - if matches '*.html'
        if (($_ -match '(?<=\*)[\~\-\.\w]+') -or ($_ -match '(?<!\*)[\~\-\.\w]+(?=\*)')) {
            Write-Verbose (" Determined the following is global file: " + $_)
            $FileEntries += $_
        }
        # relative - log.txt | LICENSE | .gitignore | main.test.ts
        elseif ($_ -match '(?<=^)[\~\-\.\w]+(?=$)') {
            if (Test-Path $_ -PathType Leaf -ErrorAction SilentlyContinue) {
                Write-Verbose (" Determined the following is relative file: " + $_)
                $FileEntries += $_
            }
        }
        # relative - if matches '.\index-UTF16BE-CRLF-NoBOM-NoXtraLine.html'
        elseif ($_ -match '(?<=\\)[\~\-\.\w]+[.?\w]+') {
            if (Test-Path $_ -PathType Leaf -ErrorAction SilentlyContinue) {
                Write-Verbose (" Determined the following is relative file: " + $_)
                $FileEntries += $Matches.Values
            }
            elseif (Test-Path $_ -PathType Container -ErrorAction SilentlyContinue) {
                Write-Verbose (" Determined the following is relative folder: " + $_)
                $FolderEntries += $Matches.Values.TrimEnd('/')
            }
        }
        # relative - if matches 'out/**' | '.vscode/**' | out/www/**
        # -or
        # relative - .\resources\ | .\.vscode\ | .vscode/
        elseif (($_ -match '\.?[(\\|\/)\~\-\.\w]+(?=(?:\\|\/)\*{0,2})') -or ($_ -match '(?<=\\)\.?[\~\-\.\w]+[.?\w]+(?=\\)')) {
            $EntryCandidate = $_.TrimEnd('**').TrimEnd('/')
            # is this a sub-path entry?
            if ($_ -match '[\~\-\.\w]+[\\|\/][\~\-\.\w]+') {
                # if so does it exist?
                if (Test-Path $EntryCandidate -PathType Container -ErrorAction SilentlyContinue) {
                    Write-Verbose (" Determined the following is a subpath folder: " + $_)
                    $FolderEntries += $EntryCandidate
                }
            }
            elseif (Test-Path $EntryCandidate -PathType Container -ErrorAction SilentlyContinue) {
                Write-Verbose (" Determined the following is relative folder: " + $_)
                $FolderEntries += $EntryCandidate
            }
        }
        # global - if matches '**\tests' | '**/tests'
        elseif ($_ -match '(?<=\* {2}(?:\\|\/))[\~\-\.\w]+') {
            Write-Verbose (" Determined the following is global folder: " + $_)
            $FolderEntries += $_.TrimEnd('/')
        }
        else {
            Write-Verbose (" Undetermined item: " + $_)
        }
    }
    Write-Verbose ("Finished parsing excluded items")
    
    $IgnoreHashTable = @{
        FolderEntries = $FolderEntries
        FileEntries   = $FileEntries
    }
    $IgnoreHashTable
}

function New-ConfirmationMessage {
    [CmdletBinding()]
    Param
    (
        [ValidateSet("LF", "CRLF")]
        [string]$EOL,

        [switch]$WhatIf
    )
    
    if ($WhatIf.IsPresent -eq $false) {
        $ConfirmationMessage = @"
You have requested to convert all files to ${EOL} end-of-line (EOL) characters."
"@

    }
    else {
        $ConfirmationMessage = @"
You have requested to see what files will be converted to ${EOL} end-of-line (EOL) characters."
"@

    }

    $ConfirmationMessage
}

function Request-Confirmation {
    Param
    (
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$Message,

        [switch]$WhatIf
    )
    
    if ($WhatIf.IsPresent -eq $false) {
        $Question = "Do you want to proceed in modifying file(s)?"
    }
    else {
        $Question = "Since 'WhatIf' has been switched, do you want to see what file(s) would have been modified?"
    }

    $Yes = [System.Management.Automation.Host.ChoiceDescription]::new("&Yes")
    $Yes.HelpMessage = "Executes operation"
    $No = [System.Management.Automation.Host.ChoiceDescription]::new("&No")
    $No.HelpMessage = "Aborts operation"

    if ($script:SUT -eq $false) {
        [bool]$Decision = !($Host.UI.PromptForChoice($Message, $Question, @($Yes, $No), 1))
    }
    else {
        [bool]$Decision = $true
    }

    $Decision
}

function Start-ConversionProcess {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path,

        [Parameter(Mandatory = $false)]
        [object[]]$IgnoreHashTable,

        [ValidateSet("LF", "CRLF")]
        [string]$EOL,

        [switch]$ExportReportData,

        [switch]$WhatIf
    )
    
    try {
        $script:ReportCollection = @()

        Push-Location
      
        if (($Path | Test-Path) -eq $false) {
            if ($Path | Test-Path -PathType Container -IsValid) {
                throw [System.IO.DirectoryNotFoundException]::new()
            }
            elseif ($Path | Test-Path -PathType Leaf -IsValid) {
                throw [System.IO.FileNotFoundException]::new()
            }
            else {
                throw [System.IO.IOException]::new() 
            }
        }
        else {
            if ($Path | Test-Path -PathType Container) {
                Invoke-RecurseFolders -Path $Path[0] `
                    -EOL $EOL `
                    -IgnoreHashTable $IgnoreHashTable `
                    -WhatIf:$WhatIf.IsPresent
            }
            else {
                $script:ReportCollection += Get-FileObject `
                    -FilePath $Path[0] `
                    -EOL $EOL `
                    -WhatIf:$WhatIf.IsPresent | `
                    Write-File | `
                    Out-ReportData
            }
        }

        Pop-Location
        
        if ($ExportReportData.IsPresent -eq $false) {
            Format-ReportTable -EOL $EOL -WhatIf:$WhatIf.IsPresent
        }
        else {
            $script:ReportCollection
        }
    }
    catch [System.IO.DirectoryNotFoundException] {
        Write-Error -Message ("The following directory cannot be found: $Path")
        Pop-Location
    }
    catch [System.IO.FileNotFoundException] {
        Write-Error -Message ("The following file cannot be found: $Path")
        Pop-Location
    }
    catch [System.IO.IOException] {
        Write-Error -Message ("The following is invalid: $Path")
        Pop-Location
    }
}

# NOTE: Algorithm by Brian Nadjiwon:
# https://social.technet.microsoft.com/Forums/windowsserver/en-US/71a473c7-7cee-4c48-ab02-491703aa1f5f/getchilditem-with-millions-of-files
#
function Invoke-RecurseFolders {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $false)]
        [string]$Path,
        
        [ValidateSet("LF", "CRLF")]
        [string]$EOL,

        [Parameter(Mandatory = $false)]
        [object[]]$IgnoreHashTable,

        [switch]$WhatIf
    )

    Set-Location -Path $Path
    # HACK: when -File and -Exclude are switched on Get-ChildItem, it doesnt return
    # anything; hence the piped Where-Object to determine if its a file. This
    # seems to be fixed in PowerShell Core 6.0.0
    #
    # https://github.com/PowerShell/PowerShell/issues/5699
    # https://github.com/PowerShell/PowerShell/pull/5896
    #
    [string[]]$Files = Get-ChildItem . -Exclude $IgnoreHashTable.FileEntries | Where-Object {$_.PSIsContainer -eq $false}
    ForEach ($File in $Files) {
        $script:ReportCollection += Get-FileObject `
            -FilePath $File `
            -EOL $EOL `
            -WhatIf:$WhatIf.IsPresent | `
            Write-File | `
            Out-ReportData
    }

    $RevisedFolderEntries = @()
    if ($IgnoreHashTable.FolderEntries.Length) {
        $IgnoreHashTable.FolderEntries | Foreach-Object {
            # this 'if' are for subpaths only, and check to see if we
            # are at base directory. If so, extract that base directory
            # name and add in $RevisedFolderEntries
            if ($_ -match '[\~\-\.\w]+[\\|\/][\~\-\.\w]+') {
                $($_ -match '[\~\-\.\w]+$') | Out-Null
                if (Test-Path $Matches[0]) {
                    $RevisedFolderEntries += $Matches[0]
                }
            }
            elseif (Test-Path $_) {
                $RevisedFolderEntries += $_
            }
        }
    }

    [string[]]$Folders = Get-ChildItem . -Exclude $RevisedFolderEntries | Where-Object {$_.PSIsContainer -eq $true}
    ForEach ($Folder in $Folders) {
        Invoke-RecurseFolders -Path $Folder `
            -EOL $EOL `
            -IgnoreHashTable $IgnoreHashTable `
            -WhatIf:$WhatIf.IsPresent

        Set-Location -Path '..'
    }
}

function Format-ReportTable {
    [CmdletBinding()]
    Param
    (
        [ValidateSet("LF", "CRLF")]
        [string]$EOL,

        [switch]$WhatIf
    )
    
    if ($script:ReportCollection.Count -gt 0) {
        $ModifiedCount = 0
        $script:ReportCollection | ForEach-Object {
            if ($_.Modified) {
                $ModifiedCount++
            }
        }
 
        if ($WhatIf.IsPresent) {
            $ColHeaderForModified = "Would be Modified "
            $SummaryMessage = "A total of '$ModifiedCount' files would have been modified with " + $EOL + " end-of-line (EOL) characters."
        }
        else {
            $ColHeaderForModified = "Modified "
            $SummaryMessage = "A total of '$ModifiedCount' files has been modified with " + $EOL + " end-of-line (EOL) characters."
        }

        $script:ReportCollection | `
            Sort-Object -property `
        @{Expression = "Modified"; Descending = $true}, `
        @{Expression = "FilePath"; Descending = $false} | `
            Format-Table `
        @{Label = "Name "; Expression = {($_.FilePath)}}, `
        @{Label = $ColHeaderForModified; Expression = {($_.Modified)}; Alignment = "Left"}, `
        @{Label = "Reason Not Modified "; Expression = {
                if ($_.EmptyFile) {
                    "File is empty"
                }
                elseif ($_.EncodingNotCompatiable) {
                    $mesg = "Encoding is not compatiable"
                    if ($_.FileEncoding) {
                        $mesg += " - " + $_.FileEncoding
                    }
                    $mesg
                }
                elseif ($_.SameEOLAsRequested) {
                    "Same EOL as requested"
                }
            } ; Alignment = "Left"
        } -AutoSize
    }
    else {
        if ($WhatIf.IsPresent) {
            $SummaryMessage = @"

No files would have been modified with $EOL end-of-line (EOL) characters.
"@

        }
        else {
            $SummaryMessage = @"

No files has been modified with $EOL end-of-line (EOL) characters.
"@

        }
    }
    Write-Host $SummaryMessage
}

function Get-FileObject {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]$FilePath,

        [Parameter(Mandatory = $true)]
        [ValidateSet("LF", "CRLF")]
        [string]$EOL,
        
        [switch]$WhatIf
    )

    $Data = [PsCustomObject]@{
        EOL                    = $EOL
        FilePath               = ''
        FileItem               = $null
        FileContent            = ''
        FileEOL                = ''
        FileEncoding           = $null
        EncodingNotCompatiable = $false
        SameEOLAsRequested     = $false
        EmptyFile              = $false
        EndsWithEmptyNewLine   = $false
        Modified               = $false
        WhatIf                 = $WhatIf.IsPresent
    }
    
    Write-Verbose ("Opening: " + $FilePath)
    $Data.FilePath = Resolve-Path $FilePath -Relative
    $Data.FileItem = Get-Item -Path $FilePath
    $Data.EmptyFile = $Data.FileItem.Length -eq 0

    if ($Data.EmptyFile -eq $false) {
        # unicode: U+000D | byte (decimal): 13 | html: \r\n | powershell: `r`n
        [byte]$CR = 0x0D
        # unicode: U+000A | byte (decimal): 10 | html: \n | powershell: `n
        [byte]$LF = 0x0A
        # TODO: would be nice to pipe StreamReader into Test-Encoding...
        $Data.EncodingNotCompatiable = !(Test-Encoding -Path $Data.FileItem.FullName 'utf8')

        if ($Data.EncodingNotCompatiable -eq $false) {

            $StreamReader = New-Object -TypeName System.IO.StreamReader -ArgumentList $Data.FileItem.FullName
            
            $Data.FileEncoding = $StreamReader.CurrentEncoding
            
            if ($Data.FileEncoding -is [System.Text.UTF8Encoding]) {
            
                $Data.FileContent = $StreamReader.ReadToEnd()
            
                $FileAsBytes = [System.Text.Encoding]::UTF8.GetBytes($Data.FileContent)
                $FileAsBytesLength = $FileAsBytes.Length
            }

            $IndexOfLF = $FileAsBytes.IndexOf($LF)
            if (($IndexOfLF -ne -1) -and ($FileAsBytes[$IndexOfLF - 1] -ne $CR)) {
                $Data.FileEOL = 'LF'
                if ($FileAsBytesLength) {
                    $Data.EndsWithEmptyNewLine = ($FileAsBytes.Get($FileAsBytesLength - 1) -eq $LF) -and `
                    ($FileAsBytes.Get($FileAsBytesLength - 2) -eq $LF)
                }

                $Data.SameEOLAsRequested = $Data.FileEOL -eq $Data.EOL
            }
            elseif (($IndexOfLF -ne -1) -and ($FileAsBytes[$IndexOfLF - 1] -eq $CR)) {
                $Data.FileEOL = 'CRLF'
                if ($FileAsBytesLength) {
                    $Data.EndsWithEmptyNewLine = ($FileAsBytes.Get($FileAsBytesLength - 1) -eq $LF) -and `
                    ($FileAsBytes.Get($FileAsBytesLength - 3) -eq $LF)
                }

                $Data.SameEOLAsRequested = $Data.FileEOL -eq $Data.EOL
            }
            else {
                $Data.FileEOL = 'unknown'
            }
            
            $StreamReader.Close()
        }
    }

    $Data
}

function Write-File {
    [CmdletBinding()]
    Param
    (
        [Parameter(ValueFromPipeline = $true)]
        [PSCustomObject]$Data
    )

    if (($Data.SameEOLAsRequested -eq $false) -and `
        ($Data.EmptyFile -eq $false) -and `
        ($Data.EncodingNotCompatiable -eq $false)) {
 
        if ($Data.WhatIf -eq $false) {
            New-Object -TypeName System.IO.StreamWriter -ArgumentList $Data.FileItem.FullName -OutVariable StreamWriter | Out-null

            if ($Data.EOL -eq 'LF') {
                $Data.FileContent = $Data.FileContent -replace "`r", ""
                if ($Data.EndsWithEmptyNewLine) {
                    $Data.FileContent + "`n"
                }
            }
            elseif ($Data.EOL -eq 'CRLF') {
                $Data.FileContent = $Data.FileContent -replace "`n", "`r`n"
                if ($Data.EndsWithEmptyNewLine) {
                    $Data.FileContent + "`r`n"
                }
            }

            try {
                $StreamWriter.Write($Data.FileContent)
                $StreamWriter.Flush()
                $StreamWriter.Close()
            }
            catch {
                Write-Error ("EndOfLine threw an exception when writing to: " + $Data.FileItem.FullName)
            }
        }
        $Data.Modified = $true
    }
    # free-up memory; no longer need FileContent data
    $Data.FileContent = '[removed]'
    
    $Data
}

function Out-ReportData {
    [CmdletBinding()]
    Param 
    (
        [Parameter(ValueFromPipeline = $true)]
        [PSCustomObject]$Data
    )

    if ($Data.Modified -eq $true) {
        if ($Data.WhatIf -eq $false) {
            Write-Verbose (" Modifying file")
        }
        else {
            Write-Verbose (" Would have modified file")
        }
    }
    else {
        if ($Data.EmptyFile) {
            Write-Verbose (" This file has been excluded since it is empty.") 
        }
        elseif ($Data.SameEOLAsRequested) {
            Write-Verbose (" This file has been excluded since the end-of-line characters are the same as requested to convert.") 
        }
        elseif ($Data.EncodingNotCompatiable) {
            Write-Verbose (" This file has been excluded since it is not UTF-8 encoded.")
        }
    }

    Write-Verbose ("Closing file")

    $Data
}

Export-ModuleMember -Function ConvertTo-LF
Export-ModuleMember -Function ConvertTo-CRLF