Skip to content

Commit f2dffa7

Browse files
authored
fix: Some regressions for Windows (quarkusio#1121)
* fix: Make sure jbang bash script can update on Windows Now that we no longer delegate to the jbang .cmd file when running inside a Bash shell on Windows the bash script needs to be able to perform the Windows update procedure. * fix: `jbang edit` using wrong paths on Windows Another regression now that we no longer delegate to CMD when using a bash environment on Windows.
1 parent 90f0f2c commit f2dffa7

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

src/main/java/dev/jbang/cli/Edit.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static dev.jbang.Settings.CP_SEPARATOR;
44
import static dev.jbang.cli.BaseBuildCommand.escapeOSArguments;
5-
import static dev.jbang.util.Util.isWindows;
65
import static dev.jbang.util.Util.verboseMsg;
76
import static java.lang.System.out;
87

@@ -36,6 +35,7 @@
3635
import dev.jbang.util.ConsoleInput;
3736
import dev.jbang.util.TemplateEngine;
3837
import dev.jbang.util.Util;
38+
import dev.jbang.util.Util.Shell;
3939

4040
import io.quarkus.qute.Template;
4141
import picocli.CommandLine;
@@ -78,6 +78,7 @@ public Integer doCall() throws IOException {
7878

7979
ScriptSource ssrc = (ScriptSource) src;
8080
File project = createProjectForEdit(ssrc, ctx, false);
81+
String projectPathString = Util.pathToString(project.getAbsoluteFile().toPath());
8182
// err.println(project.getAbsolutePath());
8283

8384
if (!noOpen) {
@@ -90,22 +91,22 @@ public Integer doCall() throws IOException {
9091
} else {
9192
List<String> optionList = new ArrayList<>();
9293
optionList.add(getEditorToUse().get());
93-
optionList.add(project.getAbsolutePath());
94+
optionList.add(projectPathString);
9495

9596
String[] cmd;
9697
final String editorCommand = String.join(" ", escapeOSArguments(optionList));
97-
if (isWindows()) {
98-
cmd = new String[] { "cmd", "/c", editorCommand };
99-
} else {
98+
if (Util.getShell() == Shell.bash) {
10099
cmd = new String[] { "sh", "-c", editorCommand };
100+
} else {
101+
cmd = new String[] { "cmd", "/c", editorCommand };
101102
}
102103
info("Running `" + String.join(" ", cmd) + "`");
103104
new ProcessBuilder(cmd).start();
104105
}
105106
}
106107

107108
if (!live) {
108-
out.println(project.getAbsolutePath()); // quit(project.getAbsolutePath());
109+
out.println(projectPathString); // quit(project.getAbsolutePath());
109110
} else {
110111
try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {
111112
File orginalFile = src.getResourceRef().getFile();

src/main/java/dev/jbang/cli/Jdk.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,36 @@ public Integer javaEnv(
8282
Path home = getJdkPath(version);
8383
if (home != null) {
8484
String homeStr = Util.pathToString(home);
85+
String homeOsStr = Util.pathToOsString(home);
8586
PrintStream out = System.out;
8687
switch (Util.getShell()) {
8788
case bash:
88-
out.println("export PATH=\"" + homeStr + "/bin:$PATH\"");
89-
out.println("export JAVA_HOME=\"" + homeStr + "\"");
90-
out.println("# Run this command to configure your shell:");
89+
// Not using `println()` here because it will output /n/r
90+
// on Windows which causes problems
91+
out.print("export PATH=\"" + homeStr + "/bin:$PATH\"\n");
92+
out.print("export JAVA_HOME=\"" + homeOsStr + "\"\n");
93+
out.print("# Run this command to configure your shell:\n");
9194
out.print("# eval $(jbang jdk java-env");
9295
if (version != null) {
9396
out.print(" " + version);
9497
}
95-
out.println(")");
98+
out.print(")\n");
9699
break;
97100
case cmd:
98101
out.println("set PATH=" + homeStr + "\\bin;%PATH%");
99-
out.println("set JAVA_HOME=" + homeStr);
102+
out.println("set JAVA_HOME=" + homeOsStr);
100103
out.println("rem Copy & paste the above commands in your CMD window or add");
101104
out.println("rem them to your Environment Variables in the System Settings.");
102105
break;
103106
case powershell:
104107
out.println("$env:PATH=\"" + homeStr + "\\bin:$env:PATH\"");
105-
out.println("$env:JAVA_HOME=\"" + homeStr + "\"");
106-
out.println("# Copy & paste the above commands in your Powershell window or add");
107-
out.println("# them to your Environment Variables in the System Settings.");
108+
out.println("$env:JAVA_HOME=\"" + homeOsStr + "\"");
109+
out.println("# Run this command to configure your environment:");
110+
out.print("# jbang jdk java-env");
111+
if (version != null) {
112+
out.print(" " + version);
113+
}
114+
out.println(" | iex");
108115
break;
109116
}
110117
}

src/main/java/dev/jbang/util/Util.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,10 +1160,10 @@ private static boolean isExecutable(Path file) {
11601160
}
11611161

11621162
/**
1163-
* Converts a Path to a String. This is normally a trivial operation, but in
1164-
* certain circumstances, like for example when we need to write a bash shell
1165-
* scripts when running inside a Cygwin bash on Windows, we need to do a bit
1166-
* more.
1163+
* Converts a Path to a String. This is normally a trivial operation, but this
1164+
* method handles the special case when running in a Bash shell on Windows,
1165+
* where paths have a special format that Java doesn't know about (eg.
1166+
* C:\Directory\File.txt becomes /c/Directory/File.txt).
11671167
*
11681168
* @param path the Path to convert
11691169
* @return a String representing the given Path
@@ -1192,6 +1192,24 @@ public static String pathToString(Path path) {
11921192
}
11931193
}
11941194

1195+
/**
1196+
* Converts a Path to a String. This is normally a trivial operation, but this
1197+
* method handles the special case when running in a Bash shell on Windows,
1198+
* where the default output format would cause problems because the backslashes
1199+
* would be interpreted as escape sequences. So we need to escape the
1200+
* backslashes.
1201+
*
1202+
* @param path the Path to convert
1203+
* @return a String representing the given Path
1204+
*/
1205+
public static String pathToOsString(Path path) {
1206+
if (isWindows() && getShell() == Shell.bash) {
1207+
return path.toString().replace("\\", "\\\\");
1208+
} else {
1209+
return path.toString();
1210+
}
1211+
}
1212+
11951213
/**
11961214
* Determines if the current JBang we're running was one installed using `app
11971215
* install` or not

src/main/scripts/jbang

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ else
117117
$JBDIR/bin/jbang "$@"
118118
exit $?
119119
fi
120+
if [ -f "$jarPath.new" ]; then
121+
# a new jbang version was found, we replace the old one with it
122+
mv "$jarPath.new" "$jarPath"
123+
fi
120124

121125
# Find/get a JDK
122126
unset JAVA_EXEC

0 commit comments

Comments
 (0)