From 5d383875f11860de8b7e8d50bad38b13f020109f Mon Sep 17 00:00:00 2001 From: steve02081504 Date: Tue, 25 Jun 2024 10:43:01 +0800 Subject: [PATCH] fixup streams --- .github/workflows/CI.ps1 | 14 ++++++------- ps12exe.ps1 | 38 ++++++++++++++---------------------- src/AstAnalyze.ps1 | 8 ++++---- src/GUI/ContextMenuAdder.ps1 | 2 +- src/GUI/Functions.ps1 | 11 ----------- src/GUI/Main.ps1 | 2 +- src/PSObjectToString.ps1 | 3 +++ src/WebServer/main.ps1 | 2 +- 8 files changed, 32 insertions(+), 48 deletions(-) diff --git a/.github/workflows/CI.ps1 b/.github/workflows/CI.ps1 index 7387e3e4..7086043a 100644 --- a/.github/workflows/CI.ps1 +++ b/.github/workflows/CI.ps1 @@ -3,13 +3,13 @@ $error.clear() $repoPath = "$PSScriptRoot/../.." try{ mkdir $repoPath/build -ErrorAction Ignore | Out-Null - Import-Module $repoPath -Force | Out-Host + Import-Module $repoPath -Force | Write-Host Set-ps12exeContextMenu 1 - & $repoPath/ps12exe.ps1 $repoPath/ps12exe.ps1 $repoPath/build/ps12exe.exe -verbose | Out-Host - & $repoPath/build/ps12exe.exe $repoPath/ps12exe.ps1 -verbose -noConsole -title 'lol' | Out-Host - & $repoPath/build/ps12exe.exe $repoPath/ps12exe.ps1 $repoPath/build/ps12exe2.exe -verbose | Out-Host - "'Hello World!'" | ps12exe -outputFile $repoPath/build/hello.exe -verbose | Out-Host - & $repoPath/build/ps12exe2.exe -Content '$PSEXEpath;$PSScriptRoot' -outputFile $repoPath/build/pathtest.exe | Out-Host + & $repoPath/ps12exe.ps1 $repoPath/ps12exe.ps1 $repoPath/build/ps12exe.exe -verbose | Write-Host + & $repoPath/build/ps12exe.exe $repoPath/ps12exe.ps1 -verbose -noConsole -title 'lol' | Write-Host + & $repoPath/build/ps12exe.exe $repoPath/ps12exe.ps1 $repoPath/build/ps12exe2.exe -verbose | Write-Host + "'Hello World!'" | ps12exe -outputFile $repoPath/build/hello.exe -verbose | Write-Host + & $repoPath/build/ps12exe2.exe -Content '$PSEXEpath;$PSScriptRoot' -outputFile $repoPath/build/pathtest.exe | Write-Host $pathresult=. $repoPath/build/pathtest.exe $pathresultshouldbe=@("$repoPath/build/pathtest.exe","$repoPath/build") # 在路径层面比较 $pathresult 和 $pathresultshouldbe @@ -23,7 +23,7 @@ try{ Write-Error "$path1 -ne $path2" } } - & $repoPath/build/hello.exe | Out-Host + & $repoPath/build/hello.exe | Write-Host Set-ps12exeContextMenu 0 Remove-Item $repoPath/build -Recurse -Force }catch{} diff --git a/ps12exe.ps1 b/ps12exe.ps1 index e4276287..9e94c053 100644 --- a/ps12exe.ps1 +++ b/ps12exe.ps1 @@ -184,16 +184,17 @@ Param( $Verbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent function RollUp { param ($num = 1, [switch]$InVerbose) - $CousorPos = $Host.UI.RawUI.CursorPosition - try { - if (-not $Verbose -or $InVerbose) { - if ($nested) { throw } - $CousorPos.Y = $CousorPos.Y - $num - $Host.UI.RawUI.CursorPosition = $CousorPos + if (-not $Verbose -or $InVerbose) { + if ($Host.UI.SupportsVirtualTerminal) { + Write-Host $([char]27 + '[' + $num + 'A') -NoNewline + } + elseif (-not $nested) { + $CousorPos = $Host.UI.RawUI.CursorPosition + try { + $CousorPos.Y = $CousorPos.Y - $num + $Host.UI.RawUI.CursorPosition = $CousorPos + } catch { $Error.RemoveAt(0) } } - } - catch { - Write-Host $([char]27 + '[' + $num + 'A') -NoNewline } } function Show-Help { @@ -207,11 +208,11 @@ function Show-Help { . $PSScriptRoot\src\HelpShower.ps1 -HelpData $MyHelp } if ($help) { - Show-Help | Out-Host + Show-Help | Write-Host return } if (-not ($inputFile -or $Content)) { - Show-Help | Out-Host + Show-Help | Write-Host Write-Host Write-Error "Input not specified!" return @@ -385,6 +386,7 @@ else { } } #_if PSScript #在PSEXE中主机永远是winpwsh,可省略该部分 +. $PSScriptRoot/src/PSObjectToString.ps1 function UsingWinPowershell($Boundparameters) { # starting Windows Powershell $Params = ([hashtable]$Boundparameters).Clone() @@ -408,21 +410,11 @@ function UsingWinPowershell($Boundparameters) { if ($iconFile) { $Params['iconFile'] = $iconFile } - $CallParam = foreach ($Param in $Params.GetEnumerator()) { - if ($Param.Value -is [Switch]) { - "-$($Param.Key):`$$([bool]$Param.Value)" - } - elseif ($Param.Value -is [string] -and $Param.Value) { - "-$($Param.Key):'$(($Param.Value).Replace("'", "''"))'" - } - else { - "-$($Param.Key):$($Param.Value)" - } - } + $CallParam = Get-ArgsString $Params Write-Verbose "Starting WinPowershell ps12exe with parameters: $CallParam" - powershell -noprofile -Command "&'$PSScriptRoot\ps12exe.ps1' $CallParam -nested" | Out-Host + powershell -noprofile -Command "&'$PSScriptRoot\ps12exe.ps1' $CallParam -nested" | Write-Host return } if (!$nested -and ($PSVersionTable.PSEdition -eq "Core") -and $UseWindowsPowerShell -and (Get-Command powershell -ErrorAction Ignore)) { diff --git a/src/AstAnalyze.ps1 b/src/AstAnalyze.ps1 index 4bed884e..436195b5 100644 --- a/src/AstAnalyze.ps1 +++ b/src/AstAnalyze.ps1 @@ -1,8 +1,8 @@ <# function ShowAst($Ast) { function Mapper($Ast) { - $Ast.GetType().Name | Out-Host - $Ast | Out-Host + $Ast.GetType().Name | Write-Host + $Ast | Write-Host return $false } $Ast.Find($function:Mapper, $true) @@ -17,8 +17,8 @@ AI prompt: 用你所知的最刁钻的方式和最花哨的技术写一段powers 我需要测试一段评估程序是不是常量程序的程序 #> function AstAnalyze($Ast) { - $script:ConstCommands = @('Write-Host', 'echo', 'Write-Output', 'Write-Debug', 'Write-Information', 'ConvertFrom-Json', 'ConvertTo-Json', 'Out-Host') - $script:ConstVariables = @('?', '^', '$', 'Error', 'false', 'IsCoreCLR', 'IsLinux', 'IsMacOS', 'IsWindows', 'null', 'true', 'PSEXEScript', 'Out-Host') + $script:ConstCommands = @('Write-Host', 'echo', 'Write-Output', 'Write-Debug', 'Write-Information', 'ConvertFrom-Json', 'ConvertTo-Json', 'Write-Host') + $script:ConstVariables = @('?', '^', '$', 'Error', 'false', 'IsCoreCLR', 'IsLinux', 'IsMacOS', 'IsWindows', 'null', 'true', 'PSEXEScript', 'Write-Host') $script:ConstTypes = @('Boolean', 'Char', 'DateTime', 'Decimal', 'Double', 'Int16', 'Int32', 'Int64', 'Int8', 'Int', 'Single', 'String', 'UInt16', 'UInt32', 'UInt64', 'UInt8', 'Void', 'Regex', 'System.Text.RegularExpressions.RegexOptions', 'HashTable', 'OrderedDictionary', 'PSObject', 'PSVariable', 'PSNoteProperty', 'PSMemberInfo', 'PSCustomObject', 'Math', 'Array', 'ref', 'Guid') $script:EffectVariables = @('ConfirmPreference', 'DebugPreference', 'EnabledExperimentalFeatures', 'ErrorActionPreference', 'ErrorView', 'ExecutionContext', 'FormatEnumerationLimit', 'HOME', 'Host', 'InformationPreference', 'input', 'MaximumHistoryCount', 'MyInvocation', 'NestedPromptLevel', 'OutputEncoding', 'PID', 'PROFILE', 'ProgressPreference', 'PSBoundParameters', 'PSCommandPath', 'PSCulture', 'PSDefaultParameterValues', 'PSEdition', 'PSEmailServer', 'PSGetAPI', 'PSHOME', 'PSNativeCommandArgumentPassing', 'PSNativeCommandUseErrorActionPreference', 'PSScriptRoot', 'PSSessionApplicationName', 'PSSessionConfigurationName', 'PSSessionOption', 'PSStyle', 'PSUICulture', 'PSVersionTable', 'PWD', 'ShellId', 'StackTrace', 'VerbosePreference', 'WarningPreference', 'WhatIfPreference') $script:AnalyzeResult = @{ diff --git a/src/GUI/ContextMenuAdder.ps1 b/src/GUI/ContextMenuAdder.ps1 index 55422145..7371f3b3 100644 --- a/src/GUI/ContextMenuAdder.ps1 +++ b/src/GUI/ContextMenuAdder.ps1 @@ -41,7 +41,7 @@ $LocalizeData = . $PSScriptRoot\..\LocaleLoader.ps1 -Localize $Localize if ($help) { $MyHelp = $LocalizeData.SetContextMenuHelpData - . $PSScriptRoot\..\HelpShower.ps1 -HelpData $MyHelp | Out-Host + . $PSScriptRoot\..\HelpShower.ps1 -HelpData $MyHelp | Write-Host return } diff --git a/src/GUI/Functions.ps1 b/src/GUI/Functions.ps1 index 0991c395..decc40ea 100644 --- a/src/GUI/Functions.ps1 +++ b/src/GUI/Functions.ps1 @@ -76,17 +76,6 @@ function Set-UIData { $Script:refs.LongPathSupportCheckBox.Checked = $UIData.longPaths } -. $PSScriptRoot/../PSObjectToString.ps1 - -function Get-ps12exeArgsString { - param( - [hashtable]$Params - ) - $result = 'ps12exe' - $Params.GetEnumerator() | ForEach-Object { $result += " -$($_.Key):$(PSObjectToString $_.Value -OneLine)" } - $result -} - function Get-ps12exeArgs { $UIData = Get-UIData $result = $UIData.Clone() diff --git a/src/GUI/Main.ps1 b/src/GUI/Main.ps1 index abdbef09..df7ca2a6 100644 --- a/src/GUI/Main.ps1 +++ b/src/GUI/Main.ps1 @@ -44,7 +44,7 @@ param( if ($help) { $LocalizeData = . $PSScriptRoot\..\LocaleLoader.ps1 -Localize $Localize $MyHelp = $LocalizeData.GUIHelpData - . $PSScriptRoot\..\HelpShower.ps1 -HelpData $MyHelp | Out-Host + . $PSScriptRoot\..\HelpShower.ps1 -HelpData $MyHelp | Write-Host return } diff --git a/src/PSObjectToString.ps1 b/src/PSObjectToString.ps1 index 5494146a..cacfa1ce 100644 --- a/src/PSObjectToString.ps1 +++ b/src/PSObjectToString.ps1 @@ -38,3 +38,6 @@ function PSObjectToString($obj, [Switch]$OneLine = $false) { elseif ($obj -is [bool]) { "`$$obj" } else { "$obj" } } +function Get-ArgsString([hashtable]$Params) { + $Params.GetEnumerator() | ForEach-Object { "-$($_.Key):$(PSObjectToString $_.Value -OneLine)" } +} diff --git a/src/WebServer/main.ps1 b/src/WebServer/main.ps1 index b2f0c0e0..9c756689 100644 --- a/src/WebServer/main.ps1 +++ b/src/WebServer/main.ps1 @@ -51,7 +51,7 @@ $LocalizeData = . $PSScriptRoot\..\LocaleLoader.ps1 -Localize $Localize if ($help) { $MyHelp = $LocalizeData.WebServerHelpData - . $PSScriptRoot\..\HelpShower.ps1 -HelpData $MyHelp | Out-Host + . $PSScriptRoot\..\HelpShower.ps1 -HelpData $MyHelp | Write-Host return }