Skip to content

Commit 7e6eb88

Browse files
author
FriedrichWeinmann
committed
Added custom WhatIf parameter
1 parent b298a8b commit 7e6eb88

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

PSModuleDevelopment/functions/refactor/Rename-PSMDParameter.ps1

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
.PARAMETER NoAlias
4141
Avoid creating an alias for the old parameter name.
4242
This may cause a breaking change!
43+
44+
.PARAMETER WhatIf
45+
Prevents the command from updating the files.
46+
Instead it will return the strings of all its changes.
4347
4448
.PARAMETER EnableException
4549
Replaces user friendly yellow warnings with bloody red exceptions of doom!
@@ -76,6 +80,9 @@
7680
[switch]
7781
$NoAlias,
7882

83+
[switch]
84+
$WhatIf,
85+
7986
[switch]
8087
$EnableException,
8188

@@ -240,7 +247,18 @@
240247
{
241248
Add-FileReplacement -Path $Ast.Extent.File -Start $Ast.Extent.StartOffset -Length ($Ast.Extent.EndOffset - $Ast.Extent.StartOffset) -NewContent "`$$NewName"
242249
}
243-
250+
}
251+
"System.Management.Automation.Language.IfStatementAst"
252+
{
253+
foreach ($clause in $Ast.Clauses)
254+
{
255+
Invoke-AstWalk -Ast $clause.Item1 -Command $Command -Name $Name -NewName $NewName -IsCommand $IsCommand -NoAlias $NoAlias
256+
Invoke-AstWalk -Ast $clause.Item2 -Command $Command -Name $Name -NewName $NewName -IsCommand $IsCommand -NoAlias $NoAlias
257+
}
258+
if ($Ast.ElseClause)
259+
{
260+
Invoke-AstWalk -Ast $Ast.ElseClause -Command $Command -Name $Name -NewName $NewName -IsCommand $IsCommand -NoAlias $NoAlias
261+
}
244262
}
245263
default
246264
{
@@ -390,7 +408,8 @@
390408
{
391409
[CmdletBinding()]
392410
Param (
393-
411+
[bool]
412+
$WhatIf
394413
)
395414

396415
foreach ($key in $globalFunctionHash.Keys)
@@ -410,8 +429,8 @@
410429

411430
$newString += $content.SubString($currentIndex)
412431

413-
[System.IO.File]::WriteAllText($key, $newString)
414-
#$newString
432+
if ($WhatIf) { $newString }
433+
else { [System.IO.File]::WriteAllText($key, $newString) }
415434
}
416435
}
417436

@@ -478,6 +497,6 @@
478497
}
479498

480499
Set-PSFResultCache -InputObject $issues -DisableCache $DisableCache
481-
Apply-FileReplacement
500+
Apply-FileReplacement -WhatIf $WhatIf
482501
$issues
483502
}

0 commit comments

Comments
 (0)