From 9ac912871274b6a9b8090740eb331cdb165902a6 Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 7 Mar 2024 19:13:22 +0000 Subject: [PATCH] Terminal launch fixes --- .../xpipe/app/prefs/ExternalTerminalType.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java index e985417a1..dde5126af 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java @@ -11,6 +11,7 @@ import lombok.Value; import lombok.With; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -61,7 +62,10 @@ public interface ExternalTerminalType extends PrefsChoiceValue { .addFile(configuration.getScriptFile()); } - return CommandBuilder.of().add("-Command").add(configuration.getDialectLaunchCommand()); + return CommandBuilder.of().add("-ExecutionPolicy", "Bypass").add("-EncodedCommand").add(sc -> { + var base64 = Base64.getEncoder().encodeToString(configuration.getDialectLaunchCommand().buildCommandBase(sc).getBytes(StandardCharsets.UTF_16LE)); + return "\"" + base64 + "\""; + }); } }; @@ -79,19 +83,20 @@ public interface ExternalTerminalType extends PrefsChoiceValue { @Override protected CommandBuilder toCommand(LaunchConfiguration configuration) { - if (configuration.getScriptDialect().equals(ShellDialects.POWERSHELL_CORE)) { - return CommandBuilder.of() - .add("-ExecutionPolicy", "Bypass") - .add("-File") - .addFile(configuration.getScriptFile()); - } - - // Fix for https://github.com/PowerShell/PowerShell/issues/18530#issuecomment-1325691850 - var script = ScriptHelper.createLocalExecScript( - "set \"PSModulePath=\"\r\n& \"" + configuration.getScriptFile() + "\""); - return CommandBuilder.of() - .add("-Command") - .add(configuration.withScriptFile(script).getDialectLaunchCommand()); + return CommandBuilder.of().add("-ExecutionPolicy", "Bypass").add("-EncodedCommand").add(sc -> { + // Fix for https://github.com/PowerShell/PowerShell/issues/18530#issuecomment-1325691850 + String script; + if (configuration.getScriptDialect().equals(ShellDialects.CMD)) { + script = ScriptHelper.createLocalExecScript( + "set \"PSModulePath=\"\r\n" + configuration.getDialectLaunchCommand().buildCommandBase(sc)); + } else { + script = ScriptHelper.createLocalExecScript( + "$env:PSModulePath=\"\"\r\n" + configuration.getDialectLaunchCommand().buildCommandBase(sc)); + } + var base64 = Base64.getEncoder().encodeToString(configuration.withScriptFile(script) + .getDialectLaunchCommand().buildCommandBase(sc).getBytes(StandardCharsets.UTF_16LE)); + return "\"" + base64 + "\""; + }); } };