Skip to content

Commit

Permalink
better $minifyer Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed May 30, 2024
1 parent 46aac5e commit 1d4699a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ps12exe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ $ParamList = $MyInvocation.MyCommand.Parameters
$Params.Remove('Content') | Out-Null #防止回滚覆盖

function bytesOfString($str) {
[system.Text.Encoding]::UTF8.GetBytes($str).Count
if ($str) { [system.Text.Encoding]::UTF8.GetBytes($str).Count } else { 0 }
}
#_if PSScript #在PSEXE中主机永远是winpwsh,所以不会内嵌
if (!$nested) {
Expand Down Expand Up @@ -238,7 +238,12 @@ if (!$nested) {
if ($minifyer) {
Write-Host "Minifying script..."
try {
$MinifyedContent = $Content | ForEach-Object $minifyer
# get caller's stackframe
$Stack = Get-PSCallStack
$Frame = $Stack[1]
$Variables = $Frame.GetFrameVariables()
$Variables._ = [System.Management.Automation.PSVariable]::New('_', $Content)
$MinifyedContent = $minifyer.InvokeWithContext(@{}, $Variables.Values, $Variables.args.Value)
RollUp
Write-Host "Minifyed script -> $(bytesOfString $MinifyedContent) bytes"
}
Expand Down
9 changes: 7 additions & 2 deletions ps12exe.psm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
function LoadFileAsFunction($File, $FunctionName) {
([System.Management.Automation.Language.Parser]::ParseInput("
$errors = $null
$result = [System.Management.Automation.Language.Parser]::ParseInput("
function $FunctionName {
$(Get-Content -Path $File -Raw)
}
", $File, [ref]$null, [ref]$null)).GetScriptBlock()
", $File, [ref]$null, [ref]$errors)
$errors | ForEach-Object {
Write-Warning $_
}
$result.GetScriptBlock()
}

. $(LoadFileAsFunction $PSScriptRoot/ps12exe.ps1 ps12exe)
Expand Down

0 comments on commit 1d4699a

Please sign in to comment.