Skip to content

Commit

Permalink
code changes for switch default terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
SuparnaSuresh committed Nov 11, 2024
1 parent 9f3c36c commit 955e735
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 45 deletions.
53 changes: 45 additions & 8 deletions src/liberty/devCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { COMMAND_TITLES, LIBERTY_MAVEN_PROJECT, LIBERTY_GRADLE_PROJECT, LIBERTY_
import { getGradleTestReport } from "../util/gradleUtil";
import { DashboardData } from "./dashboard";
import { ProjectStartCmdParam } from "./projectStartCmdParam";
import { getCommandForMaven, getCommandForGradle } from "../util/commandUtils";
import { getCommandForMaven, getCommandForGradle,ShellType } from "../util/commandUtils";

export const terminals: { [libProjectId: number]: LibertyProject } = {};

Expand Down Expand Up @@ -100,6 +100,8 @@ export async function startDevMode(libProject?: LibertyProject | undefined): Pro
let terminal = libProject.getTerminal();
if (terminal === undefined) {
const path = Path.dirname(libProject.getPath());
const terminalType = currentWindowsShell();
libProject.setTerminalType(terminalType);
terminal = libProject.createTerminal(path);
if (terminal !== undefined) {
terminals[Number(terminal.processId)] = libProject;
Expand All @@ -109,10 +111,10 @@ export async function startDevMode(libProject?: LibertyProject | undefined): Pro
terminal.show();
libProject.setTerminal(terminal);
if (libProject.getContextValue() === LIBERTY_MAVEN_PROJECT || libProject.getContextValue() === LIBERTY_MAVEN_PROJECT_CONTAINER) {
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev");
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev", libProject.getTerminalType());
terminal.sendText(cmd); // start dev mode on current project
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT || libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDev");
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDev", libProject.getTerminalType());
terminal.sendText(cmd); // start dev mode on current project
}
}
Expand Down Expand Up @@ -357,6 +359,8 @@ export async function customDevMode(libProject?: LibertyProject | undefined, par
let terminal = libProject.getTerminal();
if (terminal === undefined) {
const path = Path.dirname(libProject.getPath());
const terminalType = currentWindowsShell();
libProject.setTerminalType(terminalType);
terminal = libProject.createTerminal(path);
if (terminal !== undefined) {
terminals[Number(terminal.processId)] = libProject;
Expand Down Expand Up @@ -406,10 +410,10 @@ export async function customDevMode(libProject?: LibertyProject | undefined, par
}

if (libProject.getContextValue() === LIBERTY_MAVEN_PROJECT || libProject.getContextValue() === LIBERTY_MAVEN_PROJECT_CONTAINER) {
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev",customCommand);
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev", libProject.getTerminalType(), customCommand);
terminal.sendText(cmd);
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT || libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDev",customCommand);
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDev", libProject.getTerminalType(), customCommand);
terminal.sendText(cmd);
}
}
Expand All @@ -430,6 +434,8 @@ export async function startContainerDevMode(libProject?: LibertyProject | undefi
let terminal = libProject.getTerminal();
if (terminal === undefined) {
const path = Path.dirname(libProject.getPath());
const terminalType = currentWindowsShell();
libProject.setTerminalType(terminalType);
terminal = libProject.createTerminal(path);
if (terminal !== undefined) {
terminals[Number(terminal.processId)] = libProject;
Expand All @@ -439,10 +445,10 @@ export async function startContainerDevMode(libProject?: LibertyProject | undefi
terminal.show();
libProject.setTerminal(terminal);
if (libProject.getContextValue() === LIBERTY_MAVEN_PROJECT_CONTAINER) {
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:devc");
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:devc", libProject.getTerminalType());
terminal.sendText(cmd);
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDevc");
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDevc", libProject.getTerminalType());
terminal.sendText(cmd);
}
}
Expand Down Expand Up @@ -524,4 +530,35 @@ export function deleteTerminal(terminal: vscode.Terminal): void {
} catch {
console.error(localize("unable.to.delete.terminal", terminal.name));
}
}
}

/**
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts
*/

function currentWindowsShell(): ShellType {
const currentWindowsShellPath: string = vscode.env.shell;
const executable: string = Path.basename(currentWindowsShellPath);
switch (executable.toLowerCase()) {
case "cmd.exe":
return ShellType.CMD;
case "pwsh.exe":
case "powershell.exe":
case "pwsh": // pwsh on mac/linux
return ShellType.POWERSHELL;
case "bash.exe":
case 'git-cmd.exe':
return ShellType.GIT_BASH;
case 'wsl.exe':
case 'ubuntu.exe':
case 'ubuntu1804.exe':
case 'kali.exe':
case 'debian.exe':
case 'opensuse-42.exe':
case 'sles-12.exe':
return ShellType.WSL;
default:
return ShellType.OTHERS;
}
}
8 changes: 8 additions & 0 deletions src/liberty/libertyProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ export class LibertyProject extends vscode.TreeItem {
public contextValue: string,
public terminal?: vscode.Terminal,
public readonly command?: vscode.Command, // ? indicates optional param
public terminalType?: string,
) {
super(label, collapsibleState);
this.tooltip = this.path;
Expand Down Expand Up @@ -488,6 +489,13 @@ export class LibertyProject extends vscode.TreeItem {
public setTerminal(terminal: vscode.Terminal): void {
this.terminal = terminal;
}
public getTerminalType(): string | undefined {
return this.terminalType;
}

public setTerminalType(terminalType: string): void {
this.terminalType = terminalType;
}

public createTerminal(projectHome: string): vscode.Terminal | undefined {
if (this.terminal === undefined) {
Expand Down
45 changes: 8 additions & 37 deletions src/util/commandUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,19 @@ import { pathExists } from "fs-extra";
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts
*/
enum ShellType {
export enum ShellType {
CMD = "Command Prompt",
POWERSHELL = "PowerShell",
GIT_BASH = "Git Bash",
WSL = "WSL Bash",
OTHERS = "Others"
}
/**
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts
*/
function currentWindowsShell(): ShellType {
const currentWindowsShellPath: string = vscode.env.shell;
const executable: string = Path.basename(currentWindowsShellPath);
switch (executable.toLowerCase()) {
case "cmd.exe":
return ShellType.CMD;
case "pwsh.exe":
case "powershell.exe":
case "pwsh": // pwsh on mac/linux
return ShellType.POWERSHELL;
case "bash.exe":
case 'git-cmd.exe':
return ShellType.GIT_BASH;
case 'wsl.exe':
case 'ubuntu.exe':
case 'ubuntu1804.exe':
case 'kali.exe':
case 'debian.exe':
case 'opensuse-42.exe':
case 'sles-12.exe':
return ShellType.WSL;
default:
return ShellType.OTHERS;
}
}

/**
* Return the maven commands based on the OS and Terminal for start, startinContainer, start..
*/

export async function getCommandForMaven(pomPath: string, command: string, customCommand?: string): Promise<string> {
export async function getCommandForMaven(pomPath: string, command: string, terminalType?: string, customCommand?: string): Promise<string> {

// attempt to use the Maven executable path, if empty try using mvn or mvnw according to the preferMavenWrapper setting
const mavenExecutablePath: string | undefined = vscode.workspace.getConfiguration("maven").get<string>("executable.path");
Expand All @@ -80,7 +51,7 @@ export async function getCommandForMaven(pomPath: string, command: string, custo
}
//checking the OS type for command customization
if (isWin()) {
switch (currentWindowsShell()) {
switch (terminalType) {
case ShellType.GIT_BASH:
if (customCommand) {
return "cd \"" + mvnCmdStart + "\" && " + "./mvnw " + `${command}` + ` ${customCommand}`; //Bash
Expand Down Expand Up @@ -130,7 +101,7 @@ export async function getCommandForMaven(pomPath: string, command: string, custo
* Return the Gradle commands based on the OS and Terminal for start, startinContainer, start..
*/

export async function getCommandForGradle(buildGradlePath: string, command: string, customCommand?: string): Promise<string> {
export async function getCommandForGradle(buildGradlePath: string, command: string, terminalType?: String, customCommand?: string): Promise<string> {
let gradleCmdStart = await gradleCmd(buildGradlePath);

if (gradleCmdStart === "gradle") {
Expand All @@ -141,7 +112,7 @@ export async function getCommandForGradle(buildGradlePath: string, command: stri
}
//checking the OS type for command customization
if (isWin()) {
return getGradleCommandsForWin(gradleCmdStart, buildGradlePath, command, customCommand);
return getGradleCommandsForWin(gradleCmdStart, buildGradlePath, command, terminalType, customCommand);
} else {
gradleCmdStart = Path.join(gradleCmdStart, "gradlew");
if (customCommand) {
Expand Down Expand Up @@ -172,8 +143,8 @@ function toDefaultWslPath(p: string): string {
* Return the Gradle commands for windows OS based on the terminal configured
*/

function getGradleCommandsForWin(gradleCmdStart: string, buildGradlePath: string, command: string, customCommand?: string): string {
switch (currentWindowsShell()) {
function getGradleCommandsForWin(gradleCmdStart: string, buildGradlePath: string, command: string, terminalType?: String, customCommand?: string): string {
switch (terminalType) {
case ShellType.GIT_BASH:
gradleCmdStart = Path.join(gradleCmdStart, "gradlew");
if (customCommand) {
Expand Down Expand Up @@ -213,7 +184,7 @@ function getGradleCommandsForWin(gradleCmdStart: string, buildGradlePath: string
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/2ab8f392f418c8e0fe2903387f2b0013a1c50e78/src/utils/mavenUtils.ts
*/
function isWin(): boolean {
export function isWin(): boolean {
return process.platform.startsWith("win");
}

Expand Down

0 comments on commit 955e735

Please sign in to comment.