Last active
January 13, 2020 10:25
-
-
Save mkht/69c7b71f0c5e966f3fe3117ecb87e71d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Compress-ScriptBlock { | |
[CmdletBinding()] | |
[Alias('minify')] | |
[OutputType([scriptblock])] | |
param ( | |
[Parameter(Mandatory, Position = 0, ValueFromPipeline)] | |
[ValidateNotNullOrEmpty()] | |
[scriptblock] | |
$ScriptBlock | |
) | |
$StringBuilder = New-Object System.Text.StringBuilder | |
$StrSb = $ScriptBlock.ToString() | |
$AliasList = Get-Alias | |
$Tokens = [System.Management.Automation.PSParser]::Tokenize($ScriptBlock, [ref]$null) | |
foreach ($Token in $Tokens) { | |
switch ($Token.Type) { | |
'Comment' { | |
break | |
} | |
'NewLine' { | |
if ($StringBuilder.Length -eq 0) { } | |
elseif ($StringBuilder.Chars($StringBuilder.Length - 1) -in ("`n", ';', '{', '(')) { } | |
else { | |
[void]$StringBuilder.Append("`n") | |
} | |
break | |
} | |
'GroupEnd' { | |
if (($StringBuilder.Length -ne 0) -and ($StringBuilder.Chars($StringBuilder.Length - 1) -in ("`n", ';'))) { | |
[void]$StringBuilder.Remove($StringBuilder.Length - 1, 1) | |
} | |
[void]$StringBuilder.Append($Token.Content) | |
break | |
} | |
'Variable' { | |
[void]$StringBuilder.Append($StrSb.Substring($Token.Start, $Token.Length)) | |
break | |
} | |
'Keyword' { | |
switch ($Token.Content) { | |
{ $_ -in ('catch', 'finally', 'elseif', 'else', 'until') } { | |
if (($StringBuilder.Length -ne 0) -and ($StringBuilder.Chars($StringBuilder.Length - 1) -in ("`n", ';'))) { | |
[void]$StringBuilder.Remove($StringBuilder.Length - 1, 1) | |
} | |
[void]$StringBuilder.Append($Token.Content) | |
break | |
} | |
'while' { | |
if (($StringBuilder.Length -ne 0) -and ($StringBuilder.Chars($StringBuilder.Length - 1) -in ("`n", ';'))) { | |
[void]$StringBuilder.Remove($StringBuilder.Length - 1, 1) | |
[void]$StringBuilder.Append("`n") | |
} | |
[void]$StringBuilder.Append($Token.Content) | |
break | |
} | |
'in' { | |
[void]$StringBuilder.Append(' ') | |
[void]$StringBuilder.Append($Token.Content) | |
[void]$StringBuilder.Append(' ') | |
break | |
} | |
{ $_ -in ('return', 'exit') } { | |
[void]$StringBuilder.Append($Token.Content) | |
[void]$StringBuilder.Append(' ') | |
break | |
} | |
Default { | |
[void]$StringBuilder.Append($Token.Content) | |
} | |
} | |
} | |
'Command' { | |
if ($Alias = $AliasList | Where-Object { $_.Definition -eq $Token.Content }) { | |
$ShortestAlias = $Alias | Select-Object -ExpandProperty Name | Sort-Object { $_.Length } | Select-Object -First 1 | |
[void]$StringBuilder.Append($ShortestAlias) | |
} | |
else { | |
[void]$StringBuilder.Append($Token.Content) | |
} | |
[void]$StringBuilder.Append(' ') | |
break | |
} | |
'CommandArgument' { | |
[void]$StringBuilder.Append(' ') | |
[void]$StringBuilder.Append($Token.Content) | |
[void]$StringBuilder.Append(' ') | |
break | |
} | |
'CommandParameter' { | |
[void]$StringBuilder.Append(' ') | |
$Content = switch ($Token.Content) { | |
'-Debug' { '-db'; break } | |
'-ErrorAction' { '-ea'; break } | |
'-ErrorVariable' { '-ev'; break } | |
'-InformationAction' { '-infa'; break } | |
'-InformationVariable' { '-iv'; break } | |
'-OutVariable' { '-ov'; break } | |
'-OutBuffer' { '-ob'; break } | |
'-PipelineVariable' { '-pv'; break } | |
'-Verbose' { '-vb'; break } | |
'-WarningAction' { '-wa'; break } | |
'-WarningVariable' { '-wv'; break } | |
Default { $Token.Content } | |
} | |
[void]$StringBuilder.Append($Content) | |
if ($Content[-1] -ne ':') { | |
[void]$StringBuilder.Append(' ') | |
} | |
break | |
} | |
'Operator' { | |
if ($Token.Content -eq '-not') { | |
[void]$StringBuilder.Append('!') | |
} | |
else { | |
[void]$StringBuilder.Append($Token.Content) | |
} | |
break | |
} | |
'String' { | |
[void]$StringBuilder.Append($StrSb.Substring($Token.Start, $Token.Length)) | |
break | |
} | |
'LineContinuation' { | |
break | |
} | |
Default { | |
[void]$StringBuilder.Append($Token.Content) | |
} | |
} | |
} | |
if ($StringBuilder.Length -ne 0) { | |
if ($StringBuilder.Chars($StringBuilder.Length - 1) -in ("`n", ';')) { | |
[void]$StringBuilder.Remove($StringBuilder.Length - 1, 1) | |
} | |
} | |
[scriptblock]::Create($StringBuilder.ToString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment