Skip to content

Commit

Permalink
refactor: Move Gradle Daemons implementation from Java into Typescript (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiaaming committed May 29, 2024
1 parent 57e1cf0 commit 0806406
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 180 deletions.
6 changes: 1 addition & 5 deletions extension/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ export class Extension {
treeDataProvider: this.gradleTasksTreeDataProvider,
showCollapseAll: true,
});
this.gradleDaemonsTreeDataProvider = new GradleDaemonsTreeDataProvider(
this.context,
this.rootProjectsStore,
this.client
);
this.gradleDaemonsTreeDataProvider = new GradleDaemonsTreeDataProvider(this.context, this.rootProjectsStore);
this.gradleDaemonsTreeView = vscode.window.createTreeView(GRADLE_DAEMONS_VIEW, {
treeDataProvider: this.gradleDaemonsTreeDataProvider,
showCollapseAll: false,
Expand Down
4 changes: 2 additions & 2 deletions extension/src/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export class Commands {
COMMAND_REFRESH_DAEMON_STATUS,
new RefreshDaemonStatusCommand(this.gradleDaemonsTreeDataProvider)
);
this.registerCommand(COMMAND_STOP_DAEMONS, new StopDaemonsCommand(this.client, this.rootProjectsStore));
this.registerCommand(COMMAND_STOP_DAEMON, new StopDaemonCommand(this.client));
this.registerCommand(COMMAND_STOP_DAEMONS, new StopDaemonsCommand(this.rootProjectsStore));
this.registerCommand(COMMAND_STOP_DAEMON, new StopDaemonCommand());
this.registerCommand(COMMAND_EXPLORER_TREE, new ExplorerTreeCommand(this.gradleTasksTreeDataProvider));
this.registerCommand(COMMAND_EXPLORER_FLAT, new ExplorerFlatCommand(this.gradleTasksTreeDataProvider));
this.registerCommand(COMMAND_OPEN_SETTINGS, new OpenSettingsCommand());
Expand Down
24 changes: 19 additions & 5 deletions extension/src/commands/StopDaemonCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@ import { GradleDaemonTreeItem } from "../views";
import { confirmModal } from "../util/input";
import { logger } from "../logger";
import { Command } from "./Command";
import { GradleClient } from "../client";
import { execAsync } from "../util/execAsync";
import * as vscode from "vscode";
import { COMMAND_REFRESH_DAEMON_STATUS } from "./RefreshDaemonStatusCommand";

export const COMMAND_STOP_DAEMON = "gradle.stopDaemon";

export class StopDaemonCommand extends Command {
constructor(private client: GradleClient) {
constructor() {
super();
}
async run(treeItem: GradleDaemonTreeItem): Promise<void> {
if (!(await confirmModal("Are you sure you want to stop the daemon?"))) {
return;
}
const pid = treeItem.pid;
const stopDaemonReply = await this.client.stopDaemon(pid);
if (stopDaemonReply) {
logger.info(stopDaemonReply.getMessage());
try {
await this.stopDaemon(pid);
logger.info(`Successfully stopped daemon with PID ${pid}.`);
} catch (error) {
logger.error(`Failed to stop daemon with PID ${pid}: ${error.message}.`);
}
}

async stopDaemon(pid: string): Promise<void> {
if (!pid) {
throw new Error("PID is required to stop the daemon.");
}

const command = process.platform === "win32" ? `taskkill /PID ${pid} /F` : `kill ${pid}`;
await execAsync(command);
await vscode.commands.executeCommand(COMMAND_REFRESH_DAEMON_STATUS);
}
}
44 changes: 32 additions & 12 deletions extension/src/commands/StopDaemonsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import * as vscode from "vscode";
import { confirmModal } from "../util/input";
import { StopDaemonsReply } from "../proto/gradle_pb";
import { logger } from "../logger";
import { Command } from "./Command";
import { RootProjectsStore } from "../stores";
import { GradleClient } from "../client";
import { getGradleConfig } from "../util/config";
import { GradleStatus } from "../views/gradleDaemons/services/GradleStatus";
import { GradleConnectionType } from "../views/gradleDaemons/models/GradleConnectionType";
import { GradleWrapper } from "../views/gradleDaemons/services/GradleWrapper";
import { GradleLocalInstallation } from "../views/gradleDaemons/services/GradleLocalInstallation";
import { COMMAND_REFRESH_DAEMON_STATUS } from "./RefreshDaemonStatusCommand";

export const COMMAND_STOP_DAEMONS = "gradle.stopDaemons";

export class StopDaemonsCommand extends Command {
constructor(private client: GradleClient, private rootProjectsStore: RootProjectsStore) {
constructor(private rootProjectsStore: RootProjectsStore) {
super();
}
async run(): Promise<void> {
Expand All @@ -20,14 +25,29 @@ export class StopDaemonsCommand extends Command {
return;
}
const gradleRootFolders = await this.rootProjectsStore.getProjectRootsWithUniqueVersions();
const promises: Promise<StopDaemonsReply | void>[] = gradleRootFolders.map((rootProject) =>
this.client.stopDaemons(rootProject.getProjectUri().fsPath)
);
const replies = await Promise.all(promises);
replies.forEach((reply) => {
if (reply) {
logger.info(reply.getMessage());
}
});
try {
const promises: Promise<void>[] = gradleRootFolders.map((rootProject) =>
this.stopDaemons(rootProject.getProjectUri().fsPath)
);
await Promise.all(promises);
logger.info(`Successfully stopped all daemons.`);
await vscode.commands.executeCommand(COMMAND_REFRESH_DAEMON_STATUS);
} catch (error) {
logger.error(`Failed to stop daemons: ${error.message}.`);
}
}

async stopDaemons(projectFolder: string): Promise<void> {
const gradleConfig = getGradleConfig();
const connectType = await GradleStatus.getConnectionType(gradleConfig);
if (connectType === GradleConnectionType.WRAPPER) {
const gradleExecution = new GradleWrapper(projectFolder);
await gradleExecution.exec(["--stop"]);
} else if (connectType === GradleConnectionType.LOCALINSTALLATION) {
const gradleExecution = new GradleLocalInstallation(gradleConfig.getGradleHome());
await gradleExecution.exec(["--stop"]);
} else {
logger.info("No daemons to stop.");
}
}
}
Loading

0 comments on commit 0806406

Please sign in to comment.