-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormat-IndentSpacesAsTabs.ps1
88 lines (59 loc) · 1.63 KB
/
Format-IndentSpacesAsTabs.ps1
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
<#
.SYNOPSIS
Converts indentation spaces to tabs.
.DESCRIPTION
Author: @JamesDBartlett3@techhub.social (https://techhub.social/@JamesDBartlett3)
.PARAMETER InputFile
The file to format.
.PARAMETER Indentation
The number of spaces to convert to tabs. Default is 2.
.EXAMPLE
PS C:\> Format-IndentSpacesAsTabs.ps1 -InputFile ".\MyScript.ps1"
.EXAMPLE
PS C:\> Format-IndentSpacesAsTabs.ps1 -InputFile ".\*.ps1"
.EXAMPLE
PS C:\> Get-ChildItem -Path ".\*.ps1" | Format-IndentSpacesAsTabs.ps1 -Indentation 4
#>
# Parameters
Param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[ValidateNotNullOrEmpty()]
[string[]]$InputFile,
[Parameter(
Mandatory = $false
)]
[ValidateNotNullOrEmpty()]
[int]$Indentation = 2
)
# Process
Begin {
# Set the tab character
$tab = "`t"
# Set the regex pattern
$pattern = " " * $Indentation
}
Process {
# Check if InputFile is a wildcard
If ([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($InputFile)) {
# Get the files
$InputFile = Get-ChildItem -Path $InputFile -File
}
# Loop through each file
ForEach ($file in $InputFile) {
# Resolve the file path
$filePath = $file | Resolve-Path -ErrorAction SilentlyContinue
# Read the file
$content = Get-Content -LiteralPath $filePath
# Replace the spaces with tabs
$content.Replace($pattern, $tab) | Set-Content -LiteralPath $filePath -Force
# Clear the variables
Remove-Variable -Name filePath, content -ErrorAction SilentlyContinue
}
}
End {
# Clear the variables
Remove-Variable -Name tab, pattern -ErrorAction SilentlyContinue
}