-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.ps1
165 lines (137 loc) · 5.17 KB
/
action.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
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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, HelpMessage = 'Specify the path(s) to process')]
[ValidateNotNullOrEmpty()]
[string[]]
$Path,
[Parameter(HelpMessage = 'Specify the token style to use')]
[ValidateSet('mustache', 'handlebars', 'envsubst', 'make', ErrorMessage = 'Unknown token style', IgnoreCase = $true)]
[string]
$Style = 'mustache',
[Parameter(HelpMessage = 'Specify a filter for the files to process')]
[string]
$Filter,
[Parameter(HelpMessage = 'Recurse into subdirectories')]
[switch]
$Recurse,
[Parameter(HelpMessage = 'Specify the depth of recursion')]
[int]
$Depth,
[Parameter(HelpMessage = 'Follow symbolic links')]
[switch]
$FollowSymlinks,
[Parameter(HelpMessage = 'Specify the file encoding')]
[ValidateSet('utf8', 'utf-8', 'utf8NoBOM', 'utf8BOM', 'ascii', 'ansi', 'bigendianunicode', 'bigendianutf32', 'oem', 'unicode', 'utf32', ErrorMessage = 'Unknown encoding', IgnoreCase = $true)]
[string]
$Encoding = 'utf8',
[Parameter(HelpMessage = 'Do not add a newline at the end of the file')]
[switch]
$NoNewline,
[Parameter(HelpMessage = 'Specify files or directories to exclude')]
[string[]]
$Exclude,
[Parameter(HelpMessage = 'Run in dry-run mode (do not modify files)')]
[Alias('WhatIf')]
[switch]
$DryRun
)
# Initialize tracking variables
$script:filesReplaced = New-Object System.Collections.Generic.HashSet[string]
$script:tokensReplaced = 0
$script:tokensSkipped = 0
# Define token patterns with validation for environment variable names
$mustachePattern = '\{\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}' # handlebars/mustache pattern, e.g. {{VARIABLE}}
$envsubstPattern = '\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}' # envsubst template pattern, e.g. ${VARIABLE}
$makePattern = '\$\(([a-zA-Z_][a-zA-Z0-9_]*)\)' # make pattern, e.g. $(VARIABLE)
# Determine the token pattern based on the style
$tokenPattern = switch ($Style.ToLower())
{
'mustache' { $mustachePattern }
'handlebars' { $mustachePattern }
'envsubst' { $envsubstPattern }
'make' { $makePattern }
default { throw "Unknown token style: $Style" }
}
# Normalize utf8 (no bom) encoding
$fileEncoding = switch ($Encoding.ToLower())
{
'utf8' { 'utf8NoBOM' }
'utf-8' { 'utf8NoBOM' }
'utf8nobom' { 'utf8NoBOM' }
default { $Encoding }
}
# Function to replace tokens in a file
function ReplaceTokens([string] $File, [string] $Pattern, [string] $FileEncoding, [bool] $NoNewline)
{
try
{
$content = Get-Content -Path $File -Raw -Encoding $FileEncoding -ErrorAction Stop
$originalContent = $content
$tokensInFile = 0
$skippedInFile = 0
# Replace tokens using a regex evaluator
$content = [Regex]::Replace($content, $Pattern, {
param ($match)
$varName = $match.Groups[1].Value
if (-not (Test-Path -LiteralPath "Env:$varName"))
{
Write-Warning "[$File] Environment variable '$varName' not found - token will not be replaced"
$script:tokensSkipped++
$skippedInFile++
return $match.Value
}
$replacement = (Get-Item -LiteralPath "Env:$varName" -ErrorAction Continue).Value
if ([string]::IsNullOrWhiteSpace($replacement))
{
Write-Warning "[$File] Environment variable '$varName' exists but has empty value - token will not be replaced"
$script:tokensSkipped++
$skippedInFile++
return $match.Value
}
$script:tokensReplaced++
$tokensInFile++
return $replacement
})
if ($content -ne $originalContent)
{
if (-not $DryRun)
{
Set-Content -Path $File -Value $content -Encoding $FileEncoding -NoNewline:$NoNewline -Force -ErrorAction Stop
}
$script:filesReplaced.Add($File) | Out-Null
Write-Verbose "[$File] Replaced $tokensInFile token(s) (skipped $skippedInFile)"
}
}
catch
{
Write-Error "Failed to process file ${File}: $_"
}
}
# Build parameters for Get-ChildItem
$params = @{
Path = $Path
File = $true
ErrorAction = 'Continue'
}
if (-not [string]::IsNullOrWhiteSpace($Filter)) { $params.Add('Filter', $Filter) }
if ($Recurse) { $params.Add('Recurse', $true) }
if ($Depth -gt 0) { $params.Add('Depth', $Depth) }
if ($FollowSymlinks) { $params.Add('FollowSymlink', $true) }
if ($Exclude) { $params.Add('Exclude', $Exclude) }
# Get files to process
$files = Get-ChildItem @params | Where-Object { -not $_.PSIsContainer }
# Process each file
foreach ($file in $files)
{
ReplaceTokens -File $file.FullName -Pattern $tokenPattern -FileEncoding $fileEncoding -NoNewline $NoNewline
}
# Output results
if ($DryRun)
{
Write-Host "DRY RUN: Would replace $($script:tokensReplaced) token(s) in $($script:filesReplaced.Count) file(s)"
}
else
{
Write-Verbose "Replaced $($script:tokensReplaced) token(s) in $($script:filesReplaced.Count) file(s)"
}
Write-Output $script:filesReplaced