-
Notifications
You must be signed in to change notification settings - Fork 3
/
ps12exe.ps1
735 lines (686 loc) · 24.6 KB
/
ps12exe.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#Requires -Version 5.0
<#
.SYNOPSIS
Converts powershell scripts to standalone executables.
.DESCRIPTION
Converts powershell scripts to standalone executables. GUI output and input is activated with one switch,
real windows executables are generated. You may use the graphical front end ps12exeGUI for convenience.
Please see Remarks on project page for topics "GUI mode output formatting", "Config files", "Password security",
"Script variables" and "Window in background in -noConsole mode".
.PARAMETER inputFile
Powershell script file path or url to convert to executable (file has to be UTF8 or UTF16 encoded)
.PARAMETER Content
The content of the PowerShell script to convert to executable
.PARAMETER outputFile
destination executable file name or folder, defaults to inputFile with extension '.exe'
.PARAMETER CompilerOptions
additional compiler options (see https://msdn.microsoft.com/en-us/library/78f4aasd.aspx)
.PARAMETER TempDir
directory for storing temporary files (default is random generated temp directory in %temp%)
.PARAMETER minifyer
scriptblock to minify the script before compiling
.PARAMETER lcid
location ID for the compiled executable. Current user culture if not specified
.PARAMETER noConsole
the resulting executable will be a Windows Forms app without a console window.
You might want to pipe your output to Out-String to prevent a message box for every line of output
(example: dir C:\ | Out-String)
.PARAMETER prepareDebug
create helpful information for debugging of generated executable. See parameter -debug there
.PARAMETER architecture
compile for specific runtime only. Possible values are 'x64' and 'x86' and 'anycpu'
.PARAMETER threadingModel
Threading model for the compiled executable. Possible values are 'STA' and 'MTA'
.PARAMETER resourceParams
A hashtable that contains resource parameters for the compiled executable. Possible keys are 'iconFile', 'title', 'description', 'company', 'product', 'copyright', 'trademark', 'version'
iconFile can be a file path or url to an icon file. All other values are strings.
see https://msdn.microsoft.com/en-us/library/system.reflection.assemblytitleattribute(v=vs.110).aspx for details
.PARAMETER UNICODEEncoding
encode output as UNICODE in console mode, useful to display special encoded chars
.PARAMETER credentialGUI
use GUI for prompting credentials in console mode instead of console input
.PARAMETER configFile
write a config file (<outputfile>.exe.config)
.PARAMETER noOutput
the resulting executable will generate no standard output (includes verbose and information channel)
.PARAMETER noError
the resulting executable will generate no error output (includes warning and debug channel)
.PARAMETER noVisualStyles
disable visual styles for a generated windows GUI application. Only applicable with parameter -noConsole
.PARAMETER exitOnCancel
exits program when Cancel or "X" is selected in a Read-Host input box. Only applicable with parameter -noConsole
.PARAMETER DPIAware
if display scaling is activated, GUI controls will be scaled if possible.
.PARAMETER winFormsDPIAware
creates an entry in the config file for WinForms to use DPI scaling. Forces -configFile and -supportOS
.PARAMETER requireAdmin
if UAC is enabled, compiled executable will run only in elevated context (UAC dialog appears if required)
.PARAMETER supportOS
use functions of newest Windows versions (execute [Environment]::OSVersion to see the difference)
.PARAMETER virtualize
application virtualization is activated (forcing x86 runtime)
.PARAMETER longPaths
enable long paths ( > 260 characters) if enabled on OS (works only with Windows 10 or up)
.PARAMETER targetRuntime
the target runtime to compile for. Possible values are 'Framework4.0' or 'Framework2.0', default is 'Framework4.0'
.PARAMETER GuestMode
Compile scripts with additional protection, prevent native files from being accessed
.PARAMETER Localize
The language code to be used for server-side logging
.PARAMETER help
Display localized help message
.EXAMPLE
ps12exe C:\Data\MyScript.ps1
Compiles C:\Data\MyScript.ps1 to C:\Data\MyScript.exe as console executable
.EXAMPLE
ps12exe -inputFile C:\Data\MyScript.ps1 -outputFile C:\Data\MyScriptGUI.exe -iconFile C:\Data\Icon.ico -noConsole -title "MyScript" -version 0.0.0.1
Compiles C:\Data\MyScript.ps1 to C:\Data\MyScriptGUI.exe as graphical executable, icon and meta data
#>
[CmdletBinding(DefaultParameterSetName = 'InputFile')]
Param(
[Parameter(ParameterSetName = 'InputFile', Position = 0)]
[ValidatePattern("^(https?|ftp)://.*|.*\.(ps1|psd1|tmp)$")]
[String]$inputFile,
[Parameter(ParameterSetName = 'Content', ValueFromPipeline = $TRUE)]
[String]$Content,
[Parameter(ParameterSetName = 'InputFile', Position = 1)]
[Parameter(ParameterSetName = 'Content', Position = 0)]
[ValidatePattern(".*\.(exe|com|scr|bin|bat|cmd)$")]
[String]$outputFile = $NULL, [String]$CompilerOptions = '/o+ /debug-', [String]$TempDir = $NULL,
[scriptblock]$minifyer = $null, [Switch]$noConsole, [Switch]$prepareDebug, [int]$lcid,
[ValidateSet('x64', 'x86', 'anycpu')]
[String]$architecture = 'anycpu',
[ValidateSet('STA', 'MTA')]
[String]$threadingModel = 'STA',
[HashTable]$resourceParams = @{},
[Switch]$UNICODEEncoding,
[Switch]$credentialGUI,
[Switch]$configFile,
[Switch]$noOutput,
[Switch]$noError,
[Switch]$noVisualStyles,
[Switch]$exitOnCancel,
[Switch]$DPIAware,
[Switch]$winFormsDPIAware,
[Switch]$requireAdmin,
[Switch]$supportOS,
[Switch]$virtualize,
[Switch]$longPaths,
[ValidateSet('Framework2.0', 'Framework4.0')]
[String]$targetRuntime = 'Framework4.0',
#_if PSScript
[ArgumentCompleter({
Param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
. "$PSScriptRoot\src\LocaleArgCompleter.ps1" @PSBoundParameters
})]
#_endif
[Switch]$GuestMode,
[string]$Localize,
[Switch]$help,
# TODO
# in dev, not support yet
[Parameter(DontShow)]
[switch]$UseWindowsPowerShell = $true,
# deprecated. use `-noConfigFile` instead.
[Parameter(DontShow)]
[Switch]$noConfigFile,
# deprecated. use `-Architecture x86` instead.
[Parameter(DontShow)]
[Switch]$x86,
# deprecated. use `-Architecture x64` instead.
[Parameter(DontShow)]
[Switch]$x64,
# deprecated. use `-ThreadModel STA` instead.
[Parameter(DontShow)]
[Switch]$STA,
# deprecated. use `-ThreadModel MTA` instead.
[Parameter(DontShow)]
[Switch]$MTA,
# deprecated. use `-resourceParams {iconFile = $iconFile}` instead.
[Parameter(DontShow)]
[String]$iconFile,
# deprecated. use `-resourceParams {title = $title}` instead.
[Parameter(DontShow)]
[String]$title,
# deprecated. use `-resourceParams {description = $description}` instead.
[Parameter(DontShow)]
[String]$description,
# deprecated. use `-resourceParams {company = $company}` instead.
[Parameter(DontShow)]
[String]$company,
# deprecated. use `-resourceParams {product = $product}` instead.
[Parameter(DontShow)]
[String]$product,
# deprecated. use `-resourceParams {copyright = $copyright}` instead.
[Parameter(DontShow)]
[String]$copyright,
# deprecated. use `-resourceParams {trademark = $trademark}` instead.
[Parameter(DontShow)]
[String]$trademark,
# deprecated. use `-resourceParams {version = $version}` instead.
[Parameter(DontShow)]
[String]$version,
# deprecated. use `-targetRuntime Framework2.0` instead.
[Parameter(DontShow)]
[Switch]$runtime20,
# deprecated. use `-targetRuntime Framework4.0` instead.
[Parameter(DontShow)]
[Switch]$runtime40,
# internal. do not use it unless you know what you are doing.
[Parameter(DontShow)]
[Switch]$nested,
# internal. do not use it unless you know what you are doing.
[Parameter(DontShow)]
[string]$DllExportList
)
$global:LastExitCode = 0
$Verbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
$Debug = $DebugPreference -ne 'SilentlyContinue'
$UICultureBackup = [cultureinfo]::CurrentUICulture
function RollUp {
param ($num = 1, [switch]$InVerbose)
if (-not ($Verbose -or $InVerbose -or $Debug)) {
if ($Host.UI.SupportsVirtualTerminal) {
Write-Host $([char]27 + '[' + $num + 'A') -NoNewline
}
elseif (-not $nested) {
if ($CousorPos = $Host.UI.RawUI.CursorPosition) {
try {
$CousorPos.Y = $CousorPos.Y - $num
$Host.UI.RawUI.CursorPosition = $CousorPos
} catch { $Error.RemoveAt(0) }
}
}
}
}
if ($Debug) { $DebugPreference = 'Continue' } # fix -debug sets it to 'Inquire'
#_if PSScript
$LocaleLoaderArg = @{ Localize = $Localize }
if ($nested) { $LocaleLoaderArg.FaildLoadLocaleData = {} }
#_endif
$LocalizeData =
#_if PSScript
. $PSScriptRoot\src\LocaleLoader.ps1 @LocaleLoaderArg
#_else
#_include "$PSScriptRoot/src/locale/en-UK.ps1"
#_endif
function Show-Help {
. $PSScriptRoot\src\HelpShower.ps1 -HelpData $LocalizeData.ConsoleHelpData | Write-Host
}
function Write-I18n(
[ValidateSet('Info', 'Warning', 'Error', 'Debug', 'Verbose', 'Host', 'Output')]
$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, Rest format args: $FormatArgs" }
if (!$ForegroundColor) { $ForegroundColor = 'White' }
switch ($PipeLineType) {
'Info' { Write-Information $value }
'Warning' { Write-Warning $value }
'Error' {
if (!$Exception) { $Exception = [System.Exception]::new($value) }
try{$Host.UI.RawUI.ForegroundColor = "Red"}catch{}
$Host.UI.WriteErrorLine($value)
try{$Host.UI.RawUI.ForegroundColor = $ForegroundColor}catch{}
Write-Error -Exception $Exception -Message $value -Category $Category -ErrorId $ErrorId -TargetObject $TargetObject -ErrorAction SilentlyContinue
}
'Debug' { Write-Debug $value }
'Host' { Write-Host $value -ForegroundColor $ForegroundColor }
'Output' { $value }
'Verbose' { Write-Verbose $value }
}
}
if ($help) {
Show-Help
return
}
if (-not ($inputFile -or $Content)) {
Show-Help
Write-Host
Write-I18n Error NoneInput -Category InvalidArgument
$global:LastExitCode = 2
return
}
$Params = $PSBoundParameters
$ParamList = $MyInvocation.MyCommand.Parameters
$Params.Remove('Content') | Out-Null #防止回滚覆盖
$Params.Remove('DllExportList') | Out-Null
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-I18n Error BothInputAndContentSpecified -Category InvalidArgument
$global:LastExitCode = 2
return
}
. $PSScriptRoot\src\ReadScriptFile.ps1
try {
if ($inputFile) {
$Content = ReadScriptFile $inputFile
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-I18n Verbose PreprocessDone
if ((bytesOfString $NewContent) -ne (bytesOfString $Content)) {
Write-I18n Host PreprocessedScriptSize $(bytesOfString $NewContent)
}
$Content = $NewContent
}
}
catch {
$global:LastExitCode = 1
return
}
if ($minifyer -is [string]) {
if (Get-Command $minifyer -ErrorAction Ignore) {
$minifyer = "$minifyer `$_"
}
$minifyer = [scriptblock]::Create($minifyer)
}
if ($minifyer) {
Write-I18n Host MinifyingScript
try {
# 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-I18n Host MinifyedScriptSize $(bytesOfString $MinifyedContent)
}
catch {
Write-I18n Error MinifyerError $_ -Exception $_.Exception
}
if (-not $MinifyedContent -and $Content) {
Write-I18n Warning MinifyerFailedUsingOriginalScript
}
else {
$Content = $MinifyedContent
}
}
#_if PSScript
}
else {
$Content = Get-Content -Raw -LiteralPath $inputFile -Encoding UTF8 -ErrorAction SilentlyContinue
if (!$Content) {
Write-I18n Error TempFileMissing $inputFile -Category ResourceUnavailable
$global:LastExitCode = 3
return
}
if (!$TempDir) {
Remove-Item $inputFile -ErrorAction SilentlyContinue
}
if ($DllExportList) {
[System.Collections.ArrayList]$DllExportList = $DllExportList | ConvertFrom-Json
}
}
#_endif
# pragma预处理命令可能会修改参数,所以现在开始参数更新
$Params.GetEnumerator() | ForEach-Object {
Set-Variable -Name $_.Key -Value $_.Value
}
# 处理兼容旧版参数列表
if ($x86 -and $x64) {
Write-I18n Error CombinedArg_x86_x64 -Category InvalidArgument
$global:LastExitCode = 2
return
}
if ($x86) { $architecture = 'x86' }
if ($x64) { $architecture = 'x64' }
$Params.architecture = $architecture
[void]$Params.Remove("x86"); [void]$Params.Remove("x64")
if ($runtime20) {
foreach ($_ in @("runtime40", "longPaths", "winFormsDPIAware")) {
if ($Params[$_]) {
Write-I18n Error "CombinedArg_Runtime20_$_" -Category InvalidArgument
$global:LastExitCode = 2
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-I18n Error CombinedArg_STA_MTA -Category InvalidArgument
$global:LastExitCode = 2
return
}
if ($STA) { $threadingModel = 'STA' }
if ($MTA) { $threadingModel = 'MTA' }
$Params.threadingModel = $threadingModel
[void]$Params.Remove("STA"); [void]$Params.Remove("MTA")
$resourceParamKeys = @('iconFile', 'title', 'description', 'company', 'product', 'copyright', 'trademark', 'version')
$resourceParamKeys | ForEach-Object {
if ($Params.ContainsKey($_)) {
$resourceParams[$_] = $Params[$_]
}
[void]$Params.Remove($_)
}
$resourceParams.GetEnumerator() | ForEach-Object {
if (-not $resourceParamKeys.Contains($_.Key)) {
Write-I18n Warning InvalidResourceParam $_.Key
}
}
if ($configFile -and $noConfigFile) {
Write-I18n Error CombinedArg_ConfigFileYes_No -Category InvalidArgument
$global:LastExitCode = 2
return
}
if ($noConfigFile) { $configFile = $FALSE }
$NoResource = -not $resourceParams.Count
# 由于其他的resourceParams参数需要转义,iconFile参数不需要转义,所以提取出来单独处理
$iconFile = $resourceParams['iconFile']
$resourceParams.Remove('iconFile')
# 语法检查
if ($targetRuntime -eq 'Framework2.0') {
#_if PSScript
$SyntaxErrors = powershell -version 2.0 -OutputFormat xml -file $PSScriptRoot/src/RuntimePwsh2.0/CodeChecker.ps1 -scriptText $Content
#_else
#_include_as_value Pwsh2CodeCheckerCodeStr $PSScriptRoot/src/RuntimePwsh2.0/CodeChecker.ps1
#_!! powershell -version 2.0 -OutputFormat xml -Command "&{$Pwsh2CodeCheckerCodeStr} -scriptText '$($Content -replace "'","''")'"
#_endif
}
else {
[cultureinfo]::CurrentUICulture = $LocalizeData.LangID
$SyntaxErrors = $Tokens = $null
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$Tokens, [ref]$SyntaxErrors)
[cultureinfo]::CurrentUICulture = $UICultureBackup
}
if ($SyntaxErrors) {
$errorData = & $PSScriptRoot/src/SyntaxErrorI18nDataBuilder.ps1 -SyntaxErrors $SyntaxErrors -CodeContent $Content -Localize:$LocalizeData.LangID
$lastFullText = $null
$ErrMessage = @()
foreach ($errinfo in $errorData) {
$fullText = ($errinfo.SpoceText + $errinfo.Text) -join "`n"
if ($fullText -ne $lastFullText) {
$lastFullText = $fullText
$ErrMessage += [ordered]@{
Split = ''
StartLine = Write-I18n Output SyntaxErrorLineStart $errinfo.SpoceText
ScriptLine = $errinfo.Text
HighlightLine = (" "*($errinfo.Spoce.Column-1) + '^'*([Math]::Max($errinfo.Spoce.ColumnEnd-$errinfo.Spoce.Column,1)))
Messages = @()
}
}
$ErrMessage[-1].Messages += $errinfo.Message
}
Write-I18n Error -Category ParserError -TargetObject @{
Errors = $SyntaxErrors
Text = ($ErrMessage | ForEach-Object{ $_.GetEnumerator() | ForEach-Object {$_.Value} }) -join "`n"
MessageTree = $ErrMessage
} InputSyntaxError
$ErrMessage | ForEach-Object{
Write-Host $_.Split
Write-Host $_.StartLine -ForegroundColor Cyan
Write-Host $_.ScriptLine
Write-Host $_.HighlightLine -ForegroundColor Red
Write-Host ($_.Messages -join "`n")
}
$global:LastExitCode = 1
return
}
elseif (!$AST) {
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$null, [ref]$null)
}
# retrieve absolute paths independent if path is given relative oder absolute
if (-not $inputFile) {
$inputFile = '.\a.ps1'
}
if ($inputFile -notmatch "^(https?|ftp)://") {
$inputFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($inputFile)
}
if (-not $outputFile) {
if ($inputFile -match "^https?://") {
$outputFile = ([System.IO.Path]::Combine($PWD, [System.IO.Path]::GetFileNameWithoutExtension($inputFile) + ".exe"))
}
else {
$outputFile = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($inputFile), [System.IO.Path]::GetFileNameWithoutExtension($inputFile) + ".exe"))
}
}
else {
$outputFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($outputFile)
if ((Test-Path $outputFile -PathType Container)) {
$outputFile = ([System.IO.Path]::Combine($outputFile, [System.IO.Path]::GetFileNameWithoutExtension($inputFile) + ".exe"))
}
}
#_if PSScript #在PSEXE中主机永远是winpwsh,可省略该部分
. $PSScriptRoot/src/PSObjectToString.ps1
function UsingWinPowershell($Boundparameters) {
# starting Windows Powershell
$Params = ([hashtable]$Boundparameters).Clone()
$Params.Remove("minifyer")
$Params.Remove("Content")
$Params.Remove("inputFile")
$Params.Remove("outputFile")
$Params.Remove("resourceParams") #使用旧版参数列表传递hashtable参数更为保险
$TempFile = if ($TempDir) {
[System.IO.Path]::Combine($TempDir, 'main.ps1')
} else { [System.IO.Path]::GetTempFileName() }
$Content | Set-Content $TempFile -Encoding UTF8 -NoNewline
$Params.Add("outputFile", $outputFile)
$Params.Add("inputFile", $TempFile)
if ($TempDir) { $Params.TempDir = $TempDir }
$resourceParamKeys | ForEach-Object {
if ($resourceParams.ContainsKey($_) -and $resourceParams[$_]) {
$Params[$_] = $resourceParams[$_]
}
}
if ($iconFile) { $Params.iconFile = $iconFile }
if ($DllExportList.Length) { $Params.DllExportList = ConvertTo-Json -depth 7 -Compress -InputObject $DllExportList }
$CallParam = Get-ArgsString $Params
Write-Debug "Starting WinPowershell ps12exe with parameters: $CallParam"
powershell -noprofile -Command "&'$PSScriptRoot\ps12exe.ps1' $CallParam -nested; exit `$LastExitCode" | Write-Host
}
if (!$nested -and ($PSVersionTable.PSEdition -eq "Core") -and $UseWindowsPowerShell -and (Get-Command powershell -ErrorAction Ignore)) {
UsingWinPowershell $Params
return
}
#_endif
if ($inputFile -eq $outputFile) {
Write-I18n Error IdenticalInputOutput -Category InvalidArgument
$global:LastExitCode = 2
return
}
if ($winFormsDPIAware) {
$supportOS = $TRUE
}
if ($virtualize) {
foreach ($_ in @("requireAdmin", "supportOS", "longPaths")) {
if ($Params[$_]) {
Write-I18n Error "CombinedArg_Virtualize_$_" -Category InvalidArgument
$global:LastExitCode = 2
return
}
}
}
if (!$configFile) {
foreach ($_ in @("longPaths", "winFormsDPIAware")) {
if ($Params[$_]) {
Write-I18n Warning "CombinedArg_NoConfigFile_$_" -Category InvalidArgument
$configFile = $true
}
}
}
# escape escape sequences in version info
$resourceParamKeys | ForEach-Object {
if ($resourceParams.ContainsKey($_)) {
$resourceParams[$_] = $resourceParams[$_] -replace "\\", "\\"
}
}
. $PSScriptRoot\src\AstAnalyze.ps1
$AstAnalyzeResult = AstAnalyze $Ast
Write-Debug "AstAnalyzeResult: $(($AstAnalyzeResult|ConvertTo-Json) -split "\r?\n" -ne '' -join "`n")"
$CommandNames = (Get-Command).Name + (Get-Alias).Name
$FindedCmdlets = @()
$NotFindedCmdlets = @()
$AstAnalyzeResult.UsedNonConstFunctions | ForEach-Object {
if ($_ -match '\$' -or -not $_) { return }
if ($CommandNames -notcontains $_) {
if ($_ -match '^[\w\-_]+$' -and (Get-Command $_ -ErrorAction Ignore)) {
$FindedCmdlets += $_
}
# 跳过成员函数,因为解析Add-Type太过复杂
elseif (-not $_.Contains(']::')) {
$NotFindedCmdlets += $_
}
}
}
if ($AST.ParamBlock) { $AstAnalyzeResult.IsConst = $false }
$NotFindedTypes = @()
$AstAnalyzeResult.UsedNonConstTypes | ForEach-Object {
if (!($_ -as [Type])) {
$NotFindedTypes += $_
}
}
if ($FindedCmdlets) {
Write-I18n Warning SomeCmdletsMayNotAvailable $($FindedCmdlets -join '、')
}
if ($NotFindedCmdlets) {
Write-I18n Warning SomeNotFindedCmdlets $($NotFindedCmdlets -join '、')
}
if ($NotFindedTypes) {
Write-I18n Warning SomeTypesMayNotAvailable $($NotFindedTypes -join '、')
}
try {
. $PSScriptRoot\src\InitCompileThings.ps1
#_if PSScript
if (-not $noConsole -and $AstAnalyzeResult.IsConst -and -not $iconFile -and -not $requireAdmin) {
# TODO: GUI(MassageBoxW)、icon
Write-I18n Verbose TryingTinySharpCompile
Write-I18n Host CompilingFile
try {
. $PSScriptRoot\src\TinySharpCompiler.ps1
$TinySharpSuccess = $TRUE
}
catch {
RollUp
Write-I18n Verbose TinySharpFailedFallback
Write-Error $_
}
}
#_endif
try {
if (!$TinySharpSuccess) {
Write-I18n Host CompilingFile
if ($targetRuntime -eq 'Framework2.0') { $TargetFramework = ".NETFramework,Version=v2.0" }
if ($PSVersionTable.PSEdition -eq "Core") {
# unfinished!
if (!$TargetFramework) {
$Info = [System.Environment]::Version
$TargetFramework = ".NETCore,Version=v$($Info.Major).$($Info.Minor)"
}
. $PSScriptRoot\src\CodeAnalysisCompiler.ps1
}
else {
if (!$TargetFramework) {
$TargetFramework = ".NETFramework,Version=v4.7"
}
. $PSScriptRoot\src\CodeDomCompiler.ps1
}
}
RollUp
}
catch {
RollUp
Write-I18n Host CompilationFailed -ForegroundColor Red
throw $_
}
if (!(Test-Path $outputFile)) {
Write-I18n Error OutputFileNotWritten -Category WriteError
$global:LastExitCode = 3
return
}
else {
#_if PSScript
if (-not $TinySharpSuccess) {
& $PSScriptRoot\src\ExeSinker.ps1 $outputFile -removeResources:$(
$NoResource -and $AstAnalyzeResult.IsConst -and -not $requireAdmin
) -removeVersionInfo:$($resourceParams.Count -eq 0)
}
#_endif
Write-I18n Host CompiledFileSize $((Get-Item $outputFile).Length)
Write-I18n Verbose OutputPath $outputFile
if ($configFile) {
$configFileForEXE3 | Set-Content ($outputFile + ".config") -Encoding UTF8
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-I18n Host SourceFileCopied $dstSrc
Copy-Item -Path $_ -Destination $dstSrc -Force
}
$cr.TempFiles | Remove-Item -Verbose:$FALSE -Force -ErrorAction SilentlyContinue
}
}
}
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-I18n Host RoslynFailedFallback -ForegroundColor Yellow
UsingWinPowershell $Params
}
elseif (!$GuestMode) {
$global:LastExitCode = 3
$githubfeedback = "https://github.com/steve02081504/ps12exe/issues/new?assignees=steve02081504&labels=bug&projects=&template=bug-report.yaml"
$urlParams = @{
title = "$_"
"latest-release" = if (Get-Module -ListAvailable ps12exe) { "true" } else { "false" }
"bug-description" = 'Compilation failed'
"expected-behavior" = 'Compilation should succeed'
"additional-context" = @"
Version infos:
``````
$($PSVersionTable | Format-List | Out-String)
``````
Error message:
``````
$($_ | Format-List | Out-String)
``````
"@
}
foreach ($key in $urlParams.Keys) {
$githubfeedback += "&$key=$([system.uri]::EscapeDataString($urlParams[$key]))"
}
Write-I18n Host OppsSomethingWentWrong -ForegroundColor Yellow
$versionNow = Get-Module -ListAvailable ps12exe | Sort-Object -Property Version -Descending | Select-Object -First 1
if ($versionNow.Version -eq '0.0.0') { break }
$versionOnline = Find-Module ps12exe | Sort-Object -Property Version -Descending | Select-Object -First 1
if ("$($versionNow.Version)" -ne "$($versionOnline.Version)") {
Write-I18n Host TryUpgrade $($versionOnline.Version) -ForegroundColor Yellow
}
else {
Write-I18n Host EnterToSubmitIssue -ForegroundColor Yellow
Read-Host | Out-Null
Start-Process $githubfeedback
}
}
}
finally {
if ($TempTempDir) {
Remove-Item $TempTempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
#_if PSEXE
#_!! exit $LastExitCode
#_endif