-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset-line-endings.ps1
More file actions
123 lines (97 loc) · 3.14 KB
/
set-line-endings.ps1
File metadata and controls
123 lines (97 loc) · 3.14 KB
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
<#
.SYNOPSIS
Converts line endings in files to LF or CRLF.
.DESCRIPTION
Updates the line endings in one or more files to use either LF (Unix-style)
or CRLF (Windows-style) line endings.
.PARAMETER Path
Path to a file or directory to process.
.PARAMETER Recurse
When Path is a directory, recursively process all files in subdirectories.
.PARAMETER lf
Use LF (Unix-style) line endings.
.PARAMETER crlf
Use CRLF (Windows-style) line endings.
.EXAMPLE
.\set-line-endings.ps1 myfile.txt -lf
Converts myfile.txt to use LF line endings.
.EXAMPLE
.\set-line-endings.ps1 .\src -Recurse -crlf
Converts all files in the src directory and subdirectories to use CRLF line endings.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript({ Test-Path $_ -PathType Any })]
[string]$Path,
[Parameter(Mandatory = $false)]
[switch]$Recurse,
[Parameter(Mandatory = $false)]
[switch]$lf,
[Parameter(Mandatory = $false)]
[switch]$crlf
)
# Validate that exactly one ending type is specified
if (-not $lf -and -not $crlf) {
throw "You must specify either -lf or -crlf"
}
if ($lf -and $crlf) {
throw "You cannot specify both -lf and -crlf"
}
# Determine the ending type
$EndingType = if ($lf) { 'lf' } else { 'crlf' }
function Convert-LineEndings {
param(
[string]$FilePath,
[string]$Ending
)
try {
# Read the file as a single string preserving line breaks
$content = Get-Content -Path $FilePath -Raw
if ($null -eq $content) {
Write-Verbose "Skipping empty file: $FilePath"
return
}
# Normalize to LF first (remove all CR characters)
$normalized = $content -replace "`r`n", "`n" -replace "`r", "`n"
# Apply the desired line ending
if ($Ending -eq 'crlf') {
$converted = $normalized -replace "`n", "`r`n"
} else {
$converted = $normalized
}
# Only write if content changed
if ($content -ne $converted) {
# Write without adding extra newline at end
[System.IO.File]::WriteAllText($FilePath, $converted)
Write-Host "Converted: $FilePath" -ForegroundColor Green
} else {
Write-Verbose "No change needed: $FilePath"
}
} catch {
Write-Warning "Failed to process $FilePath : $_"
}
}
# Main script logic
$item = Get-Item -Path $Path
if ($item.PSIsContainer) {
# It's a directory
$files = if ($Recurse) {
Get-ChildItem -Path $Path -File -Recurse
} else {
Get-ChildItem -Path $Path -File
}
if ($files.Count -eq 0) {
Write-Host "No files found in the specified path." -ForegroundColor Yellow
exit 0
}
Write-Host "Processing $($files.Count) file(s)..." -ForegroundColor Cyan
foreach ($file in $files) {
Convert-LineEndings -FilePath $file.FullName -Ending $EndingType
}
} else {
# It's a file
Write-Host "Processing single file..." -ForegroundColor Cyan
Convert-LineEndings -FilePath $item.FullName -Ending $EndingType
}
Write-Host "Done." -ForegroundColor Cyan