Skip to content

Commit

Permalink
i2n update
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jul 16, 2024
1 parent 8e17bf8 commit 2f81cd7
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 121 deletions.
194 changes: 108 additions & 86 deletions ps12exe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,54 @@ function RollUp {
}
}
}
$LocalizeData =
#_if PSScript
. $PSScriptRoot\src\LocaleLoader.ps1 -Localize $Localize
#_else
#_include "$PSScriptRoot/src/locale/en-UK.ps1"
#_endif
#_if PSScript
if (!$LocalizeData.CompilingI18nData) { $LocalizeData.CompilingI18nData = @{} }
#_endif
function Show-Help {
$LocalizeData =
#_if PSScript
. $PSScriptRoot\src\LocaleLoader.ps1 -Localize $Localize
#_else
#_include "$PSScriptRoot/src/locale/en-UK.ps1"
#_endif
$MyHelp = $LocalizeData.ConsoleHelpData
. $PSScriptRoot\src\HelpShower.ps1 -HelpData $MyHelp
. $PSScriptRoot\src\HelpShower.ps1 -HelpData $LocalizeData.ConsoleHelpData | Write-Host
}
function Write-I18n(
[ValidateSet('Info', 'Warning', 'Error', 'Debug', 'Verbose', 'Host')]
$PipeLineType,
$Mid,
$FormatArgs,
$ErrorId = $Mid,
[System.Management.Automation.ErrorCategory]$Category = 'NotSpecified',
$TargetObject,
$Exception,
$ForegroundColor = $Host.UI.RawUI.ForegroundColor
) {
$value = $LocalizeData.CompilingI18nData[$Mid] -f $FormatArgs
if (!$value) { $value = "fatal error: No i18n data for $Mid" }
switch ($PipeLineType) {
'Info' { Write-Information $value }
'Warning' { Write-Warning $value }
'Error' {
$Host.UI.RawUI.ForegroundColor = "Red"
$Host.UI.WriteErrorLine($value)
$Host.UI.RawUI.ForegroundColor = $ForegroundColor
if (!$Exception) { $Exception = New-Object System.Exception }
Write-Error -Exception $Exception -Message $value -Category $Category -ErrorId $ErrorId -TargetObject $TargetObject -ErrorAction SilentlyContinue
}
'Debug' { Write-Debug $value }
'Host' { Write-Host $value -ForegroundColor $ForegroundColor }
'Verbose' { Write-Verbose $value }
}
}
if ($help) {
Show-Help | Write-Host
Show-Help
return
}
if (-not ($inputFile -or $Content)) {
Show-Help | Write-Host
Show-Help
Write-Host
Write-Error "Input not specified!"
Write-I18n Error NoneInput -Category InvalidArgument
return
}

Expand All @@ -225,35 +255,40 @@ $ParamList = $MyInvocation.MyCommand.Parameters
$Params.Remove('Content') | Out-Null #防止回滚覆盖
$Params.Remove('DllExportList') | Out-Null

function bytesOfString($str) {
function bytesOfString([string]$str) {
if ($str) { [system.Text.Encoding]::UTF8.GetBytes($str).Count } else { 0 }
}
#_if PSScript #在PSEXE中主机永远是winpwsh,所以不会内嵌
if (!$nested) {
#_endif
[System.Collections.ArrayList]$DllExportList = @()
if ($inputFile -and $Content) {
Write-Error "Input file and content cannot be used at the same time!"
Write-I18n Error BothInputAndContentSpecified -Category InvalidArgument
return
}
. $PSScriptRoot\src\ReadScriptFile.ps1
if ($inputFile) {
$Content = ReadScriptFile $inputFile
if (!$Content) { return }
if ((bytesOfString $Content) -ne (Get-Item $inputFile -ErrorAction Ignore).Length) {
Write-Host "Preprocessed script -> $(bytesOfString $Content) bytes"
try {
if ($inputFile) {
$Content = ReadScriptFile $inputFile
if (!$Content) { return }
if ((bytesOfString $Content) -ne (Get-Item $inputFile -ErrorAction Ignore).Length) {
Write-I18n Host PreprocessedScriptSize $(bytesOfString $Content)
}
}
}
else {
$NewContent = Preprocessor ($Content -split '\r?\n') "$PWD\a.ps1"
Write-Verbose "Done preprocess input script"
if ((bytesOfString $NewContent) -ne (bytesOfString $Content)) {
Write-Host "Preprocessed script -> $(bytesOfString $NewContent) bytes"
else {
$NewContent = Preprocessor ($Content -split '\r?\n') "$PWD\a.ps1"
Write-I18n Verbose PreprocessDone
if ((bytesOfString $NewContent) -ne (bytesOfString $Content)) {
Write-I18n Host PreprocessedScriptSize $(bytesOfString $NewContent)
}
$Content = $NewContent
}
$Content = $NewContent
}
catch {
return
}
if ($minifyer) {
Write-Host "Minifying script..."
Write-I18n Host MinifyingScript
try {
# get caller's stackframe
$Stack = Get-PSCallStack
Expand All @@ -262,13 +297,13 @@ if (!$nested) {
$Variables._ = [System.Management.Automation.PSVariable]::New('_', $Content)
$MinifyedContent = $minifyer.InvokeWithContext(@{}, $Variables.Values, $Variables.args.Value)
RollUp
Write-Host "Minifyed script -> $(bytesOfString $MinifyedContent) bytes"
Write-I18n Host MinifyedScriptSize $(bytesOfString $MinifyedContent)
}
catch {
Write-Error "Minifyer failed: $_"
Write-I18n Error MinifyerError $_ -Exception $_.Exception
}
if (-not $MinifyedContent) {
Write-Warning "Minifyer failed, using original script."
if (-not $MinifyedContent -and $Content) {
Write-I18n Warning MinifyerFailedUsingOriginalScript
}
else {
$Content = $MinifyedContent
Expand All @@ -279,7 +314,7 @@ if (!$nested) {
else {
$Content = Get-Content -Raw -LiteralPath $inputFile -Encoding UTF8 -ErrorAction SilentlyContinue
if (!$Content) {
Write-Error "Temp file $inputfile not found!"
Write-I18n Error TempFileMissing $inputFile -Category ResourceUnavailable
return
}
if (!$TempDir) {
Expand All @@ -298,33 +333,27 @@ $Params.GetEnumerator() | ForEach-Object {

# 处理兼容旧版参数列表
if ($x86 -and $x64) {
Write-Error "-x86 cannot be combined with -x64"
Write-I18n Error CombinedArg_x86_x64 -Category InvalidArgument
return
}
if ($x86) { $architecture = 'x86' }
if ($x64) { $architecture = 'x64' }
$Params.architecture = $architecture
[void]$Params.Remove("x86"); [void]$Params.Remove("x64")
if ($runtime20) {
if ($runtime40) {
Write-Error "You cannot use switches -runtime20 and -runtime40 at the same time!"
return
}
if ($longPaths) {
Write-Error "Long paths are only available with .Net 4 or above"
return
}
if ($winFormsDPIAware) {
Write-Error "DPI awareness of Windows Forms is only available with .Net 4 or above"
return
foreach ($_ in @("runtime40", "longPaths", "winFormsDPIAware")) {
if ($Params[$_]) {
Write-I18n Error "CombinedArg_Runtime20_$_" -Category InvalidArgument
return
}
}
}
if ($runtime20) { $targetRuntime = 'Framework2.0' }
if ($runtime40) { $targetRuntime = 'Framework4.0' }
$Params.targetRuntime = $targetRuntime
[void]$Params.Remove("runtime20"); [void]$Params.Remove("runtime40")
if ($STA -and $MTA) {
Write-Error "-STA cannot be combined with -MTA"
Write-I18n Error CombinedArg_STA_MTA -Category InvalidArgument
return
}
if ($STA) { $threadingModel = 'STA' }
Expand All @@ -340,11 +369,11 @@ $resourceParamKeys | ForEach-Object {
}
$resourceParams.GetEnumerator() | ForEach-Object {
if (-not $resourceParamKeys.Contains($_.Key)) {
Write-Warning "Parameter -resourceParams has an invalid key: $($_.Key)"
Write-I18n Warning InvalidResourceParam $_.Key
}
}
if ($configFile -and $noConfigFile) {
Write-Error "-configFile cannot be combined with -noConfigFile"
Write-I18n Error CombinedArg_ConfigFileYes_No -Category InvalidArgument
return
}
if ($noConfigFile) { $configFile = $FALSE }
Expand All @@ -367,7 +396,7 @@ if (!$SyntaxErrors) {
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$Tokens, [ref]$SyntaxErrors)
}
if ($SyntaxErrors) {
Write-Error "Syntax error in script: $SyntaxErrors" -Category 'ParserError' -ErrorId 'ParseError' -TargetObject $SyntaxErrors
Write-I18n Error -Category ParserError -TargetObject $SyntaxErrors InputSyntaxError
return
}

Expand Down Expand Up @@ -418,7 +447,7 @@ function UsingWinPowershell($Boundparameters) {
if ($DllExportList.Length) { $Params.DllExportList = ConvertTo-Json -depth 7 -Compress -InputObject $DllExportList }
$CallParam = Get-ArgsString $Params

Write-Verbose "Starting WinPowershell ps12exe with parameters: $CallParam"
Write-Debug "Starting WinPowershell ps12exe with parameters: $CallParam"

powershell -noprofile -Command "&'$PSScriptRoot\ps12exe.ps1' $CallParam -nested" | Write-Host
return
Expand All @@ -430,7 +459,7 @@ if (!$nested -and ($PSVersionTable.PSEdition -eq "Core") -and $UseWindowsPowerSh
#_endif

if ($inputFile -eq $outputFile) {
Write-Error "Input file is identical to output file!"
Write-I18n Error IdenticalInputOutput -Category InvalidArgument
return
}

Expand All @@ -439,28 +468,21 @@ if ($winFormsDPIAware) {
}

if ($virtualize) {
$checkList = @("requireAdmin", "supportOS", "longPaths")
foreach ($_ in $checkList) {
foreach ($_ in @("requireAdmin", "supportOS", "longPaths")) {
if ($Params[$_]) {
Write-Error "-virtualize cannot be combined with -$_"
Write-I18n Error "CombinedArg_Virtualize_$_" -Category InvalidArgument
return
}
}
}
if ($longPaths -and $virtualize) {
Write-Error "-longPaths cannot be combined with -virtualize"
return
}

$CFGFILE = [bool]$configFile
if (!$CFGFILE -and $longPaths) {
Write-Warning "Forcing generation of a config file, since the option -longPaths requires this"
$CFGFILE = $TRUE
}

if (!$CFGFILE -and $winFormsDPIAware) {
Write-Warning "Forcing generation of a config file, since the option -winFormsDPIAware requires this"
$CFGFILE = $TRUE
if(!$configFile) {
foreach ($_ in @("longPaths", "winFormsDPIAware")) {
if ($Params[$_]) {
Write-I18n Warning "CombinedArg_NoConfigFile_$_" -Category InvalidArgument
$configFile = $true
}
}
}

# escape escape sequences in version info
Expand All @@ -472,7 +494,7 @@ $resourceParamKeys | ForEach-Object {

. $PSScriptRoot\src\AstAnalyze.ps1
$AstAnalyzeResult = AstAnalyze $Ast
Write-Verbose "AstAnalyzeResult: $(($AstAnalyzeResult|ConvertTo-Json) -split "\r?\n" -ne '' -join "`n")"
Write-Debug "AstAnalyzeResult: $(($AstAnalyzeResult|ConvertTo-Json) -split "\r?\n" -ne '' -join "`n")"
$CommandNames = (Get-Command).Name + (Get-Alias).Name
$FindedCmdlets = @()
$NotFindedCmdlets = @()
Expand All @@ -489,33 +511,33 @@ $AstAnalyzeResult.UsedNonConstFunctions | ForEach-Object {
}
}
if ($FindedCmdlets) {
Write-Warning "Cmdlets $($FindedCmdlets -join '') used but may not available in runtime, make sure you've checked it!"
Write-I18n Warning SomeCmdletsMayNotAvailable $($FindedCmdlets -join '')
}
if ($NotFindedCmdlets) {
Write-Warning "Unknown functions $($NotFindedCmdlets -join '') used"
Write-I18n Warning SomeNotFindedCmdlets $($NotFindedCmdlets -join '')
}
try {
. $PSScriptRoot\src\InitCompileThings.ps1
#_if PSScript
if (-not $noConsole -and $AstAnalyzeResult.IsConst -and -not $iconFile) {
# TODO: GUI(MassageBoxW)、icon
Write-Verbose "Const result, trying TinySharp Compiler..."
Write-Host "Compiling file..."
Write-I18n Verbose TryingTinySharpCompile
Write-I18n Host CompilingFile

try {
. $PSScriptRoot\src\TinySharpCompiler.ps1
$TinySharpSuccess = $TRUE
}
catch {
RollUp
Write-Verbose "TinySharp Compiler error, fail back to normal program frame"
Write-I18n Verbose TinySharpFailedFallback
Write-Error $_
}
}
#_endif
try {
if (!$TinySharpSuccess) {
Write-Host "Compiling file..."
Write-I18n Host CompilingFile
}
if ($TinySharpSuccess) {}
else {
Expand All @@ -539,12 +561,13 @@ try {
}
catch {
RollUp
Write-Host "Compilation failed!" -ForegroundColor Red
Write-I18n Host CompilationFailed -ForegroundColor Red
throw $_
}

if (!(Test-Path $outputFile)) {
Write-Error "Output file $outputFile not written" -ErrorAction Stop
Write-I18n Error OutputFileNotWritten -Category WriteError
return
}
else {
#_if PSScript
Expand All @@ -554,16 +577,16 @@ try {
) -removeVersionInfo:$($resourceParams.Count -eq 0)
}
#_endif
Write-Host "Compiled file written -> $((Get-Item $outputFile).Length) bytes"
Write-Verbose "Path: $outputFile"
if ($CFGFILE) {
Write-I18n Host CompiledFileSize $((Get-Item $outputFile).Length)
Write-I18n Verbose OutputPath $outputFile
if ($configFile) {
$configFileForEXE3 | Set-Content ($outputFile + ".config") -Encoding UTF8
Write-Host "Config file for EXE created"
Write-I18n Host ConfigFileCreated
}
if ($prepareDebug) {
$cr.TempFiles | Where-Object { $_ -ilike "*.cs" } | Select-Object -First 1 | ForEach-Object {
$dstSrc = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($outputFile), [System.IO.Path]::GetFileNameWithoutExtension($outputFile) + ".cs"))
Write-Host "Source file name for debug copied: $dstSrc"
Write-I18n Host SourceFileCopied $dstSrc
Copy-Item -Path $_ -Destination $dstSrc -Force
}
$cr.TempFiles | Remove-Item -Verbose:$FALSE -Force -ErrorAction SilentlyContinue
Expand All @@ -574,13 +597,12 @@ catch {
if (Test-Path $outputFile) {
Remove-Item $outputFile -Verbose:$FALSE
}
$_ | Write-Error -ErrorAction Continue
if ($PSVersionTable.PSEdition -eq "Core" -and (Get-Command powershell -ErrorAction Ignore)) {
$_ | Write-Error
Write-Host "Roslyn CodeAnalysis failed`nFalling back to Use Windows Powershell with CodeDom...`nYou may want to add -UseWindowsPowerShell to args to skip this fallback in future.`n...or submit a PR to ps12exe repo to fix this!" -ForegroundColor Yellow
Write-I18n Host RoslynFailedFallback -ForegroundColor Yellow
UsingWinPowershell $Params
}
else {
$_ | Write-Error
elseif(!$GuestMode) {
$githubfeedback = "https://github.com/steve02081504/ps12exe/issues/new?assignees=steve02081504&labels=bug&projects=&template=bug-report.yaml"
$urlParams = @{
title = "$_"
Expand All @@ -601,14 +623,14 @@ $($_ | Format-List | Out-String)
foreach ($key in $urlParams.Keys) {
$githubfeedback += "&$key=$([system.uri]::EscapeDataString($urlParams[$key]))"
}
Write-Host "Opps, something went wrong." -ForegroundColor Yellow
Write-I18n Host OppsSomethingWentWrong -ForegroundColor Yellow
$versionNow = Get-Module -ListAvailable ps12exe | Sort-Object -Property Version -Descending | Select-Object -First 1
$versionOnline = Find-Module ps12exe | Sort-Object -Property Version -Descending | Select-Object -First 1
if ("$($versionNow.Version)" -ne "$($versionOnline.Version)") {
Write-Host "Latest version is $($versionOnline.Version), try upgrading to it?" -ForegroundColor Yellow
Write-I18n Host TryUpgrade $($versionOnline.Version) -ForegroundColor Yellow
}
else {
Write-Host "For help, please submit an issue by pressing Enter." -ForegroundColor Yellow
Write-I18n Host EnterToSubmitIssue -ForegroundColor Yellow
Read-Host | Out-Null
Start-Process $githubfeedback
}
Expand Down
Loading

0 comments on commit 2f81cd7

Please sign in to comment.