Skip to content

Commit 17781a8

Browse files
committed
fix: jdkman script dir path be properly handled
now the path will be stored in a tmp file, which is considered not to contain special chars, then the sciprt will read the tmp file
1 parent 91b4325 commit 17781a8

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

src/main/java/io/vproxy/jdkman/action/InitAction.java

+36-30
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import io.vproxy.jdkman.entity.JDKManConfig;
88
import io.vproxy.jdkman.ex.ErrorResult;
99
import io.vproxy.jdkman.res.ResConsts;
10-
import vjson.Stringifier;
11-
import vjson.simple.SimpleString;
1210

1311
import java.io.*;
1412
import java.nio.charset.StandardCharsets;
@@ -71,14 +69,14 @@ public boolean execute(JDKManConfig config, String[] options) throws Exception {
7169
}
7270
}
7371

74-
var jdkman = jdkmanScriptPathFile();
72+
var jdkmanScriptDir = jdkmanScriptPathFile();
7573

7674
for (var exe : EXECUTABLES) {
7775
var suffix = "";
7876
if (OS.isWindows()) {
7977
suffix = ".exe"; // use jdkman-proxy binary
8078
}
81-
var path = Path.of(jdkman.getAbsolutePath(), exe + suffix);
79+
var path = Path.of(jdkmanScriptDir.getAbsolutePath(), exe + suffix);
8280
var file = path.toFile();
8381
if (file.exists()) {
8482
if (!file.isFile()) {
@@ -115,25 +113,25 @@ public boolean execute(JDKManConfig config, String[] options) throws Exception {
115113
}
116114
}
117115

118-
var scriptToEval = getScriptToEval(jdkman, shellType);
116+
var scriptToEval = getScriptToEval(jdkmanScriptDir, shellType);
119117
System.out.println(scriptToEval.trim());
120118

121119
return false;
122120
}
123121

124122
private File jdkmanScriptPathFile() throws ErrorResult {
125-
var jdkman = Path.of(Utils.homedir(), "jdkman-scripts").toFile();
126-
if (jdkman.exists()) {
127-
if (!jdkman.isDirectory()) {
128-
throw new ErrorResult(STR."\{jdkman} is not a directory");
123+
var jdkmanScriptDir = Path.of(Utils.homedir(), "jdkman-scripts").toFile();
124+
if (jdkmanScriptDir.exists()) {
125+
if (!jdkmanScriptDir.isDirectory()) {
126+
throw new ErrorResult(STR."\{jdkmanScriptDir} is not a directory");
129127
}
130128
} else {
131-
var ok = jdkman.mkdirs();
129+
var ok = jdkmanScriptDir.mkdirs();
132130
if (!ok) {
133-
throw new ErrorResult(STR."failed to create directory: \{jdkman}");
131+
throw new ErrorResult(STR."failed to create directory: \{jdkmanScriptDir}");
134132
}
135133
}
136-
return jdkman;
134+
return jdkmanScriptDir;
137135
}
138136

139137
private static InputStream getScriptContent(String exe) {
@@ -151,37 +149,44 @@ private static InputStream getScriptContent(String exe) {
151149
}
152150
}
153151

154-
private static String getScriptToEval(File jdkman, ShellType shellType) throws ErrorResult {
152+
private static String getScriptToEval(File jdkmanScriptDir, ShellType shellType) throws ErrorResult {
155153
if (shellType == ShellType.pwsh) {
156-
return buildPowershellEval(jdkman);
154+
return buildPowershellEval(jdkmanScriptDir);
157155
} else {
158-
return buildBashEval(jdkman);
156+
return buildBashEval(jdkmanScriptDir);
159157
}
160158
}
161159

162-
private static String jsonStringify(String s) {
163-
var stringiferBuilder = new Stringifier.StringOptions.Builder();
164-
stringiferBuilder.setPrintableChar(_ -> true);
165-
return new SimpleString(s).stringify(stringiferBuilder.build());
160+
private static String writeJDKManScriptDirPathToTmpFile(File jdkmanScriptDir) throws ErrorResult {
161+
try {
162+
var file = File.createTempFile("jdkman-script-dir-path", ".txt");
163+
Files.writeString(file.toPath(), jdkmanScriptDir.getAbsolutePath());
164+
return file.getAbsolutePath();
165+
} catch (IOException e) {
166+
throw new ErrorResult("failed to create temporary file for jdkman-script path", e);
167+
}
166168
}
167169

168-
private static String buildBashEval(File jdkman) {
170+
private static String buildBashEval(File jdkmanScriptDir) throws ErrorResult {
171+
var jdkmanScriptDirPathTmpFile = writeJDKManScriptDirPathToTmpFile(jdkmanScriptDir);
169172
return STR."""
170-
JDKMAN_SCRIPT_PATH=\{jsonStringify(jdkman.getAbsolutePath())}
173+
JDKMAN_SCRIPT_PATH=`cat \{jdkmanScriptDirPathTmpFile}`
171174
export PATH="$JDKMAN_SCRIPT_PATH:$PATH"
172175
function cdjh() {
173176
builtin cd "$@"
174177
export JAVA_HOME="`jdkman which`"
175178
}
176179
alias cd=cdjh
177180
export JAVA_HOME="`jdkman which`"
181+
rm -f \{jdkmanScriptDirPathTmpFile}
178182
""";
179183
}
180184

181-
private static String buildPowershellEval(File jdkman) throws ErrorResult {
182-
Path tmpFile;
185+
private static String buildPowershellEval(File jdkmanScriptDir) throws ErrorResult {
186+
var jdkmanScriptDirPathTmpFile = writeJDKManScriptDirPathToTmpFile(jdkmanScriptDir);
187+
Path initPs1File;
183188
try {
184-
tmpFile = Files.createTempFile("jdkman-init", ".ps1");
189+
initPs1File = Files.createTempFile("jdkman-init", ".ps1");
185190
} catch (IOException e) {
186191
Logger.error(LogType.FILE_ERROR, "failed to create jdkman-init.ps1 tmp file", e);
187192
throw new ErrorResult("failed to create jdkman-init.ps1 file");
@@ -200,9 +205,9 @@ private static String buildPowershellEval(File jdkman) throws ErrorResult {
200205
sb.append("\n");
201206
sb.append("}\n");
202207
}
203-
sb.append("$JDKMAN_SCRIPT_PATH = ")
204-
.append(jsonStringify(jdkman.getAbsolutePath()))
205-
.append(" | ConvertFrom-Json\n");
208+
sb.append("$JDKMAN_SCRIPT_PATH = (Get-Content ")
209+
.append(jdkmanScriptDirPathTmpFile)
210+
.append(")\n");
206211
sb.append(STR."""
207212
$env:PATH = "$JDKMAN_SCRIPT_PATH\{pathSeparatorInPSStr()}${env:PATH}"
208213
$env:JAVA_HOME = jdkman which
@@ -212,16 +217,17 @@ private static String buildPowershellEval(File jdkman) throws ErrorResult {
212217
$env:JAVA_HOME = jdkman which
213218
}
214219
Set-Alias -Name cd -Value cdjh -Option AllScope
215-
Remove-Item \{tmpFile}
220+
Remove-Item \{initPs1File}
221+
Remove-Item \{jdkmanScriptDirPathTmpFile}
216222
""");
217223

218224
try {
219-
Files.writeString(tmpFile, sb.toString());
225+
Files.writeString(initPs1File, sb.toString());
220226
} catch (IOException e) {
221227
Logger.error(LogType.FILE_ERROR, "failed to release jdkman-init.ps1 tmp file", e);
222228
throw new ErrorResult("failed to release jdkman-init.ps1 file");
223229
}
224-
return STR.". \{tmpFile}";
230+
return STR.". \{initPs1File}";
225231
}
226232

227233
private static String pathSeparatorInPSStr() {

src/main/java/io/vproxy/jdkman/ex/ErrorResult.java

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ public class ErrorResult extends Exception {
44
public ErrorResult(String message) {
55
super(message);
66
}
7+
8+
public ErrorResult(String message, Throwable cause) {
9+
super(message, cause);
10+
}
711
}

0 commit comments

Comments
 (0)