-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaFixers.psm1
More file actions
172 lines (139 loc) · 4.2 KB
/
Copy pathMetaFixers.psm1
File metadata and controls
172 lines (139 loc) · 4.2 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
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
# Taken with love from https://github.com/PowerShell/DscResource.Tests/blob/master/MetaFixers.psm1
<#
This module helps fix problems, found by Meta.Tests.ps1
#>
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'
function ConvertTo-UTF8 {
<#
.SYNOPSIS
Converts a file to UTF-8 encoding.
.DESCRIPTION
Reads the raw content of a file and writes it back to the file in UTF-8 encoding.
.PARAMETER FileInfo
Specifies the path to the file to convert.
.EXAMPLE
ConvertTo-UTF8 -FileInfo 'C:\path\to\file.txt'
This example converts the file 'C:\path\to\file.txt' to UTF-8 encoding.
#>
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
[System.IO.FileInfo]$FileInfo
)
process {
$content = Get-Content -Path $FileInfo.FullName -Encoding 'Unicode' -Raw
[System.IO.File]::WriteAllText($FileInfo.FullName, $content, [System.Text.Encoding]::UTF8)
}
}
function ConvertTo-SpaceIndentation {
<#
.SYNOPSIS
Converts tabs to spaces in a file.
.DESCRIPTION
Reads the raw content of a file and writes it back to the file with tabs replaced by spaces.
.PARAMETER FileInfo
Specifies the path to the file to convert.
.EXAMPLE
ConvertTo-SpaceIndentation -FileInfo 'C:\path\to\file.txt'
This example converts the file 'C:\path\to\file.txt' to use spaces for indentation.
#>
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
[System.IO.FileInfo]$FileInfo
)
process {
$content = (Get-Content -Raw -Path $FileInfo.FullName) -replace "`t", ' '
[System.IO.File]::WriteAllText($FileInfo.FullName, $content)
}
}
function Get-TextFilesList {
<#
.SYNOPSIS
Returns a list of text files.
.DESCRIPTION
Recursively searches for text files in a directory.
.PARAMETER Root
Specifies the root directory to search.
.EXAMPLE
Get-TextFilesList -Root 'C:\path\to\directory'
This example returns a list of text files in the directory 'C:\path\to\directory'.
#>
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string]$Root
)
begin {
$textFileExtensions = @(
'.cmd'
'.gitattributes'
'.gitignore'
'.json'
'.mof'
'.ps1'
'.psd1'
'.psm1'
'.xml'
)
}
process {
Get-ChildItem -Path $Root -File -Recurse | Where-Object {
$_.Extension -in $textFileExtensions
}
}
}
function Test-FileUnicode {
<#
.SYNOPSIS
Determines if a file is Unicode encoded.
.DESCRIPTION
Reads the raw bytes of a file and determines if it is Unicode encoded.
.PARAMETER FileInfo
Specifies the path to the file to test.
.EXAMPLE
Test-FileUnicode -FileInfo 'C:\path\to\file.txt'
This example returns $true if the file 'C:\path\to\file.txt' is Unicode encoded.
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.IO.FileInfo]$FileInfo
)
process {
$bytes = [System.IO.File]::ReadAllBytes($FileInfo.FullName)
$zeroBytes = @($bytes -eq 0)
return [bool]$zeroBytes.Length
}
}
function Get-UnicodeFilesList {
<#
.SYNOPSIS
Returns a list of Unicode encoded files.
.DESCRIPTION
Recursively searches for Unicode encoded files in a directory.
.PARAMETER Root
Specifies the root directory to search.
.EXAMPLE
Get-UnicodeFilesList -Root 'C:\path\to\directory'
This example returns a list of Unicode encoded files in the directory 'C:\path\to\directory'.
#>
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Root
)
$root | Get-TextFilesList | Where-Object {
Test-FileUnicode $_
}
}