Skip to content

Commit 176c31c

Browse files
authored
fix: Close terminal after receiving messages completely (#510)
1 parent 10475dc commit 176c31c

File tree

7 files changed

+50
-31
lines changed

7 files changed

+50
-31
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/ProjectCommand.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public static boolean exportJar(List<Object> arguments, IProgressMonitor monitor
146146
String mainClass = gson.fromJson(gson.toJson(arguments.get(0)), String.class);
147147
Classpath[] classpaths = gson.fromJson(gson.toJson(arguments.get(1)), Classpath[].class);
148148
String destination = gson.fromJson(gson.toJson(arguments.get(2)), String.class);
149-
String taskLabel = gson.fromJson(gson.toJson(arguments.get(3)), String.class);
149+
String terminalId = gson.fromJson(gson.toJson(arguments.get(3)), String.class);
150150
Manifest manifest = new Manifest();
151151
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
152152
if (mainClass.length() > 0) {
@@ -164,28 +164,28 @@ public static boolean exportJar(List<Object> arguments, IProgressMonitor monitor
164164
int severity = resultStatus.getSeverity();
165165
if (severity == IStatus.OK) {
166166
java.nio.file.Path path = java.nio.file.Paths.get(classpath.source);
167-
reportExportJarMessage(taskLabel, IStatus.OK, "Successfully added the file to the exported jar: " + path.getFileName().toString());
167+
reportExportJarMessage(terminalId, IStatus.OK, "Successfully extracted the file to the exported jar: " + path.getFileName().toString());
168168
continue;
169169
}
170170
if (resultStatus.isMultiStatus()) {
171171
for (IStatus childStatus : resultStatus.getChildren()) {
172-
reportExportJarMessage(taskLabel, severity, childStatus.getMessage());
172+
reportExportJarMessage(terminalId, severity, childStatus.getMessage());
173173
}
174174
} else {
175-
reportExportJarMessage(taskLabel, severity, resultStatus.getMessage());
175+
reportExportJarMessage(terminalId, severity, resultStatus.getMessage());
176176
}
177177
} else {
178178
try {
179179
writeFile(new File(classpath.source), new Path(classpath.destination), /* areDirectoryEntriesIncluded = */true,
180180
/* isCompressed = */true, target, directories);
181-
reportExportJarMessage(taskLabel, IStatus.OK, "Successfully added the file to the exported jar: " + classpath.destination);
181+
reportExportJarMessage(terminalId, IStatus.OK, "Successfully added the file to the exported jar: " + classpath.destination);
182182
} catch (CoreException e) {
183-
reportExportJarMessage(taskLabel, IStatus.ERROR, e.getMessage());
183+
reportExportJarMessage(terminalId, IStatus.ERROR, e.getMessage());
184184
}
185185
}
186186
}
187187
} catch (IOException e) {
188-
reportExportJarMessage(taskLabel, IStatus.ERROR, e.getMessage());
188+
reportExportJarMessage(terminalId, IStatus.ERROR, e.getMessage());
189189
return false;
190190
}
191191
return true;
@@ -254,11 +254,11 @@ public static String getModuleName(IJavaProject project) {
254254
}
255255
}
256256

257-
private static void reportExportJarMessage(String taskLabel, int severity, String message) {
258-
if (StringUtils.isNotBlank(message) && StringUtils.isNotBlank(taskLabel)) {
257+
private static void reportExportJarMessage(String terminalId, int severity, String message) {
258+
if (StringUtils.isNotBlank(message) && StringUtils.isNotBlank(terminalId)) {
259259
String readableSeverity = getSeverityString(severity);
260260
JavaLanguageServerPlugin.getInstance().getClientConnection().executeClientCommand(COMMAND_EXPORT_JAR_REPORT,
261-
taskLabel, "[" + readableSeverity + "] " + message);
261+
terminalId, "[" + readableSeverity + "] " + message);
262262
}
263263
}
264264

src/exportJarSteps/ExportJarTaskProvider.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { IClasspath, IStepMetadata } from "./IStepMetadata";
2222
import { IMainClassInfo } from "./ResolveMainClassExecutor";
2323
import {
2424
ExportJarConstants, ExportJarMessages, ExportJarStep, failMessage, getExtensionApi,
25-
resetStepMetadata, stepMap, successMessage, toPosixPath, toWinPath,
25+
resetStepMetadata, revealTerminal, stepMap, successMessage, toPosixPath, toWinPath,
2626
} from "./utility";
2727

2828
interface IExportJarTaskDefinition extends TaskDefinition {
@@ -33,6 +33,8 @@ interface IExportJarTaskDefinition extends TaskDefinition {
3333
}
3434

3535
let isExportingJar: boolean = false;
36+
// key: terminalId, value: ExportJarTaskTerminal
37+
const activeTerminalMap: Map<string, ExportJarTaskTerminal> = new Map<string, ExportJarTaskTerminal>();
3638

3739
export async function executeExportJarTask(node?: INodeData): Promise<void> {
3840
// save the workspace first
@@ -168,21 +170,32 @@ class ExportJarTaskTerminal implements Pseudoterminal {
168170
public onDidWrite: Event<string> = this.writeEmitter.event;
169171
public onDidClose?: Event<void> = this.closeEmitter.event;
170172

173+
public terminalId: string;
171174
private stepMetadata: IStepMetadata;
172175

173176
constructor(exportJarTaskDefinition: IExportJarTaskDefinition, stepMetadata: IStepMetadata) {
174177
this.stepMetadata = stepMetadata;
175178
this.stepMetadata.taskLabel = exportJarTaskDefinition.label || "";
179+
this.stepMetadata.terminalId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString();
176180
this.stepMetadata.mainClass = exportJarTaskDefinition.mainClass;
177181
this.stepMetadata.outputPath = exportJarTaskDefinition.targetPath;
178182
this.stepMetadata.elements = exportJarTaskDefinition.elements || [];
183+
this.terminalId = this.stepMetadata.terminalId;
179184
}
180185

181-
public handleInput(data: string): void {
182-
this.writeEmitter.fire(data + EOL);
186+
public exit(message?: string) {
187+
if (message) {
188+
this.writeEmitter.fire(message);
189+
}
190+
if (activeTerminalMap.has(this.terminalId)) {
191+
activeTerminalMap.delete(this.terminalId);
192+
this.closeEmitter.fire();
193+
}
183194
}
184195

185196
public async open(_initialDimensions: TerminalDimensions | undefined): Promise<void> {
197+
activeTerminalMap.set(this.terminalId, this);
198+
revealTerminal(this.stepMetadata.taskLabel);
186199
let exportResult: boolean | undefined;
187200
try {
188201
if (!this.stepMetadata.workspaceFolder) {
@@ -210,17 +223,21 @@ class ExportJarTaskTerminal implements Pseudoterminal {
210223
} catch (err) {
211224
if (err) {
212225
failMessage(`${err}`);
226+
this.exit("[ERROR] An error occurs during export Jar process");
227+
} else {
228+
this.exit("[CANCEL] Export Jar process is cancelled by user");
213229
}
214230
} finally {
215231
isExportingJar = false;
216232
if (exportResult === true) {
217233
successMessage(this.stepMetadata.outputPath);
234+
this.exit("[SUCCESS] Export Jar process is finished successfully");
218235
} else if (exportResult === false) {
219236
// We call `executeExportJarTask()` with the same entry here
220237
// to help the user reselect the Java project.
221238
executeExportJarTask(this.stepMetadata.entry);
222239
}
223-
this.closeEmitter.fire();
240+
this.exit();
224241
}
225242
}
226243

@@ -406,3 +423,11 @@ class ExportJarTaskTerminal implements Pseudoterminal {
406423
return negative ? "!" + positivePath : positivePath;
407424
}
408425
}
426+
427+
export function appendOutput(terminalId: string, message: string): void {
428+
const terminal = activeTerminalMap.get(terminalId);
429+
if (!terminal) {
430+
return;
431+
}
432+
terminal.writeEmitter.fire(message + EOL);
433+
}

src/exportJarSteps/GenerateJarExecutor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Jdtls } from "../java/jdtls";
1111
import { INodeData } from "../java/nodeData";
1212
import { IExportJarStepExecutor } from "./IExportJarStepExecutor";
1313
import { IClasspath, IStepMetadata } from "./IStepMetadata";
14-
import { createPickBox, ExportJarMessages, ExportJarStep, ExportJarTargets, getExtensionApi, revealTerminal, toPosixPath } from "./utility";
14+
import { createPickBox, ExportJarMessages, ExportJarStep, ExportJarTargets, getExtensionApi, toPosixPath } from "./utility";
1515

1616
export class GenerateJarExecutor implements IExportJarStepExecutor {
1717

@@ -82,9 +82,11 @@ export class GenerateJarExecutor implements IExportJarStepExecutor {
8282
if (_.isEmpty(classpaths)) {
8383
return reject(new Error(ExportJarMessages.CLASSPATHS_EMPTY));
8484
}
85-
revealTerminal(stepMetadata.taskLabel);
85+
if (!stepMetadata.terminalId) {
86+
return reject(new Error("Can't find related terminal."));
87+
}
8688
const exportResult: boolean | undefined = await Jdtls.exportJar(basename(mainClass),
87-
classpaths, destPath, stepMetadata.taskLabel, token);
89+
classpaths, destPath, stepMetadata.terminalId, token);
8890
if (exportResult === true) {
8991
stepMetadata.outputPath = destPath;
9092
return resolve(true);

src/exportJarSteps/IStepMetadata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ExportJarStep } from "./utility";
88
export interface IStepMetadata {
99
entry?: INodeData;
1010
taskLabel: string;
11+
terminalId?: string;
1112
workspaceFolder?: WorkspaceFolder;
1213
mainClass?: string;
1314
outputPath?: string;

src/exportJarSteps/utility.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,6 @@ export async function getExtensionApi(): Promise<any> {
154154
return extensionApi;
155155
}
156156

157-
export function showExportJarReport(taskLabel: string, message: string) {
158-
const terminals = window.terminals;
159-
const presenterTerminals = terminals.filter((terminal) => terminal.name.indexOf(taskLabel) >= 0);
160-
if (presenterTerminals.length > 0) {
161-
presenterTerminals[presenterTerminals.length - 1].sendText(message);
162-
}
163-
}
164-
165157
export function revealTerminal(terminalName: string) {
166158
const terminals = window.terminals;
167159
const presenterTerminals = terminals.filter((terminal) => terminal.name.indexOf(terminalName) >= 0);

src/java/jdtls.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export namespace Jdtls {
2929
}
3030

3131
export async function exportJar(mainClass: string, classpaths: IClasspath[],
32-
destination: string, taskLabel: string, token: CancellationToken): Promise<boolean | undefined> {
32+
destination: string, terminalId: string, token: CancellationToken): Promise<boolean | undefined> {
3333
return commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.JAVA_PROJECT_GENERATEJAR,
34-
mainClass, classpaths, destination, taskLabel, token);
34+
mainClass, classpaths, destination, terminalId, token);
3535
}
3636

3737
export enum CompileWorkspaceStatus {

src/views/dependencyDataProvider.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { instrumentOperation, instrumentOperationAsVsCodeCommand } from "vscode-
1010
import { contextManager } from "../../extension.bundle";
1111
import { Commands } from "../commands";
1212
import { Context } from "../constants";
13-
import { executeExportJarTask } from "../exportJarSteps/ExportJarTaskProvider";
14-
import { showExportJarReport } from "../exportJarSteps/utility";
13+
import { appendOutput, executeExportJarTask } from "../exportJarSteps/ExportJarTaskProvider";
1514
import { Jdtls } from "../java/jdtls";
1615
import { INodeData, NodeKind } from "../java/nodeData";
1716
import { languageServerApiManager } from "../languageServerApi/languageServerApiManager";
@@ -36,8 +35,8 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
3635
constructor(public readonly context: ExtensionContext) {
3736
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_REFRESH, (debounce?: boolean, element?: ExplorerNode) =>
3837
this.refreshWithLog(debounce, element)));
39-
context.subscriptions.push(commands.registerCommand(Commands.EXPORT_JAR_REPORT, (taskLabel: string, message: string) => {
40-
showExportJarReport(taskLabel, message);
38+
context.subscriptions.push(commands.registerCommand(Commands.EXPORT_JAR_REPORT, (terminalId: string, message: string) => {
39+
appendOutput(terminalId, message);
4140
}));
4241
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_EXPORT_JAR, async (node: INodeData) => {
4342
executeExportJarTask(node);

0 commit comments

Comments
 (0)