Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Mar 19, 2024
1 parent 6103511 commit c176e1d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ $NextScript | ps12exe -outputFile $PSScriptRoot/$NextNumber.exe *> $null
$Number
```

`#_require` 统计整个脚本中需要的模块,并在正式脚本前加入等价以下代码的脚本
`#_require` 统计整个脚本中需要的模块,并在第一次`#_require`前加入等价以下代码的脚本

```powershell
$modules | ForEach-Object{
Expand Down
2 changes: 1 addition & 1 deletion docs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ $NextScript | ps12exe -outputFile $PSScriptRoot/$NextNumber.exe *> $null
$Number
```

`#_require` count the modules needed in the entire script and add the script equivalent of the following code before the official script:
`#_require` count the modules needed in the entire script and add the script equivalent of the following code before the first `#_require`:

```powershell
$modules | ForEach-Object{
Expand Down
16 changes: 16 additions & 0 deletions src/GUI/Main.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Requires -Version 5.0

#_if PSScript
<#
.SYNOPSIS
ps12exeGUI is a GUI tool for ps12exe.
Expand All @@ -21,13 +22,16 @@ ps12exeGUI -ConfigFile 'ps12exe.json' -Localize 'en-UK' -UIMode 'Dark'
ps12exeGUI -help
#>
[CmdletBinding()]
#_endif
param(
[ValidatePattern('|.(psccfg|xml)$')]
[string]$ConfigFile,
#_if PSScript
[ArgumentCompleter({
Param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
. "$PSScriptRoot\..\LocaleArgCompleter.ps1" @PSBoundParameters
})]
#_endif
[string]$Localize,
[ValidateSet('Light', 'Dark', 'Auto')]
[string]$UIMode = 'Auto',
Expand All @@ -36,6 +40,7 @@ param(
[switch]$help
)

#_if PSScript
if ($help) {
$LocalizeData = . $PSScriptRoot\..\LocaleLoader.ps1 -Localize $Localize
$MyHelp = $LocalizeData.GUIHelpData
Expand Down Expand Up @@ -76,3 +81,14 @@ finally {
# Restore Console Window Title
$Host.UI.RawUI.WindowTitle = $BackUpTitle
}
#_else
#_require ps12exe
#_pragma Console 0
#_pragma iconFile $PSScriptRoot/../../../img/icon.ico
#_pragma title ps12exeGUI
#_pragma description 'A super cool GUI for compile powershell scripts'
#_!!if (!(Test-Path -LiteralPath "Registry::HKEY_CURRENT_USER\Software\Classes\ps12exeGUI.psccfg")){
#_!! Set-ps12exeContextMenu 1
#_!!}
#_!!ps12exeGUI @PSBoundParameters
#_endif
23 changes: 17 additions & 6 deletions src/ReadScriptFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
function Preprocessor($Content, $FilePath) {
$Result = @()
$requiredModules = @()
$requireFlag = $False
# 处理#_if <PSEXE/PSScript>、#_else、#_endif
for ($index = 0; $index -lt $Content.Count; $index++) {
$Line = $Content[$index]
Expand All @@ -33,14 +34,12 @@ function Preprocessor($Content, $FilePath) {
while ($index -lt $Content.Count) {
$index++
$Line = $Content[$index]
if ($Line -match "^\s*#_else\s*(?!#.*)") {
$condition = -not $condition
}
if ($Line -match "^\s*#_endif\s*(?!#.*)") {
break
}
if ($condition) {
$Result += $Line
if ($condition) { $Result += $Line }
if ($Line -match "^\s*#_else\s*(?!#.*)") {
$condition = -not $condition
}
}
if ($Line -notmatch "^\s*#_endif\s*(?!#.*)") {
Expand Down Expand Up @@ -145,6 +144,10 @@ function Preprocessor($Content, $FilePath) {
ForEach-Object {
if ($_ -match "^(\s*)#_require\s+(?<moduleList>[^#]+)\s*(?!#.*)") {
$requiredModules += $Matches["moduleList"].Split(', |;、 ') | Where-Object { $_.Trim('"''') -ne '' }
if (!$requireFlag) {
$requireFlag = $true
[bigint]::Parse('72')
}
}
else { $_ }
} |
Expand Down Expand Up @@ -206,5 +209,13 @@ function Preprocessor($Content, $FilePath) {
elseif ($requiredModules.Count -eq 1) {
"if(!(gmo $requiredModules -ListAvailable -ea SilentlyContinue)){Install-Module $requiredModules -Scope CurrentUser -Force -ea Stop}"
}
(,$LoadModuleScript + $Content) -join "`n"
if ($LoadModuleScript) {
$Content = $Content | ForEach-Object {
# 在第一次#_require的前方加入$LoadModuleScript
if ($_ -is [bigint]) {
$LoadModuleScript
} else { $_ }
}
}
$Content -join "`n"
}

0 comments on commit c176e1d

Please sign in to comment.