Skip to content

Commit

Permalink
fixed wsl commands and added code for startinContainer and strat
Browse files Browse the repository at this point in the history
  • Loading branch information
SuparnaSuresh committed Oct 22, 2024
1 parent f61c7d5 commit 595a16e
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 112 deletions.
118 changes: 6 additions & 112 deletions src/liberty/devCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getGradleTestReport } from "../util/gradleUtil";
import { pathExists } from "fs-extra";
import { DashboardData } from "./dashboard";
import { ProjectStartCmdParam } from "./projectStartCmdParam";
import { getCommandForMaven, getCommandForGradle } from "../util/commandUtils";

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

Expand Down Expand Up @@ -409,11 +410,11 @@ export async function customDevMode(libProject?: LibertyProject | undefined, par

if (libProject.getContextValue() === LIBERTY_MAVEN_PROJECT || libProject.getContextValue() === LIBERTY_MAVEN_PROJECT_CONTAINER) {
const mvnCmdStart = await mvnCmd(libProject.getPath());
const cmd = getCommandForMaven(mvnCmdStart,libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev");
const cmd = getCommandForMaven(mvnCmdStart,libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev",customCommand);
terminal.sendText(cmd);
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT || libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
const gradleCmdStart = await gradleCmd(libProject.getPath());
const cmd=getCommandForGradle(gradleCmdStart,libProject.getPath(),"libertyDev");
const cmd=getCommandForGradle(gradleCmdStart,libProject.getPath(),"libertyDev",customCommand);
terminal.sendText(cmd);
}
}
Expand Down Expand Up @@ -444,11 +445,11 @@ export async function startContainerDevMode(libProject?: LibertyProject | undefi
libProject.setTerminal(terminal);
if (libProject.getContextValue() === LIBERTY_MAVEN_PROJECT_CONTAINER) {
const mvnCmdStart = await mvnCmd(libProject.getPath());
const cmd = getCommandForMaven(mvnCmdStart,libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev");
const cmd = getCommandForMaven(mvnCmdStart,libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:devc");
terminal.sendText(cmd);
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
let gradleCmdStart = await gradleCmd(libProject.getPath());
const cmd=getCommandForGradle(gradleCmdStart,libProject.getPath(),"libertyDev");
const cmd=getCommandForGradle(gradleCmdStart,libProject.getPath(),"libertyDevc");
terminal.sendText(cmd);
}
}
Expand Down Expand Up @@ -605,113 +606,6 @@ async function getLocalGradleWrapper(projectFolder: string): Promise<string | un
* 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");
}

function updateCmdForWin(gradleCmdStart: string, path: string, command: string) : string{
const dirs = gradleCmdStart.split('\\');
const quotedDir = dirs.map(dir => {
return dir.indexOf(' ') !== -1 ? `"${dir}"` : dir;
});
const modifiedPath=quotedDir.join('\\');

const cmd = `${modifiedPath} ${command} -b="${path}"`;
return cmd;
}
/**
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts
*/
enum ShellType {
CMD = "Command Prompt",
POWERSHELL = "PowerShell",
GIT_BASH = "Git Bash",
WSL = "WSL Bash",
OTHERS = "Others"
}
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 windows and Terminal
*/
function getCommandForMaven(mvnCmdStart: string, pomPath: string,command:string) : string {

if (isWin()) {
switch (currentWindowsShell()) {
case ShellType.GIT_BASH:
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` -f "${pomPath}"`; //Bash
case ShellType.POWERSHELL: {

mvnCmdStart=Path.join(mvnCmdStart, "mvnw.cmd");
return "& \""+ mvnCmdStart +"\"" + `${command}`+ ` -f "${pomPath}"`; // PowerShell
}
case ShellType.CMD:
mvnCmdStart=Path.join(mvnCmdStart, "mvnw.cmd");
return "\""+ mvnCmdStart +"\"" + `${command}`+ ` -f "${pomPath}"`; // CMD
case ShellType.WSL:
// return
default:
mvnCmdStart=Path.join(mvnCmdStart, "mvnw.cmd");
return "\""+ mvnCmdStart +"\"" + `${command}`+ ` -f "${pomPath}"`;
}
} else {
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` -f "${pomPath}"`;;
}

}

/**
* Return the Gradle commands based on the windows and Terminal
*/
function getCommandForGradle(gradleCmdStart: string, buildGradlePath: string,command: string) : string {

if (isWin()) {
switch (currentWindowsShell()) {
case ShellType.GIT_BASH:
gradleCmdStart=Path.join(gradleCmdStart, "gradlew");
return "\""+ gradleCmdStart +"\"" + `${command}` + ` -b=" "${buildGradlePath}"`; //Bash
case ShellType.POWERSHELL: {

gradleCmdStart=Path.join(gradleCmdStart, "gradlew.bat");
return "& \""+ gradleCmdStart +"\" " + `${command}` + ` -b=" "${buildGradlePath}"`;// PowerShell
}
case ShellType.CMD:
gradleCmdStart=Path.join(gradleCmdStart, "gradlew.bat");
return "\""+ gradleCmdStart +"\"" + `${command}` + ` -b=" "${buildGradlePath}"`; // CMD
case ShellType.WSL:
// return
default:
gradleCmdStart=Path.join(gradleCmdStart, "gradlew.bat");
return "\""+ gradleCmdStart +"\"" + `${command}` + ` -b=" "${buildGradlePath}"`;;
}
} else {
gradleCmdStart=Path.join(gradleCmdStart, "gradlew");
return "\""+ gradleCmdStart +"\" " + `${command}` + ` -b=" "${buildGradlePath}"`;
}

}
159 changes: 159 additions & 0 deletions src/util/commandUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as Path from "path";
import * as vscode from "vscode";
import { isWin } from "../liberty/devCommands";

/**
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts
*/
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
*/
export function getCommandForMaven(mvnCmdStart: string, pomPath: string,command:string,customCommand?: string) : string {

if (isWin()) {
switch (currentWindowsShell()) {
case ShellType.GIT_BASH:
if(customCommand){
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` ${customCommand}` +` -f "${pomPath}"`; //Bash
}
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` -f "${pomPath}"`; //Bash for start..

case ShellType.POWERSHELL: {
mvnCmdStart=Path.join(mvnCmdStart, "mvnw.cmd");
if(customCommand){

return "& \""+ mvnCmdStart +"\" " + `${command}`+` ${customCommand}` +` -f "${pomPath}"`; //Poweshell for start..
}
return "& \""+ mvnCmdStart +"\" " + `${command}`+ ` -f "${pomPath}"`; // PowerShell
}
case ShellType.CMD:
mvnCmdStart=Path.join(mvnCmdStart, "mvnw.cmd");
if(customCommand){

return "\""+ mvnCmdStart +"\" " + `${command}`+` ${customCommand}` + ` -f "${pomPath}"`; //cmd for start..
}
return "\""+ mvnCmdStart +"\" " + `${command}`+ ` -f "${pomPath}"`; // CMD
case ShellType.WSL:
mvnCmdStart=toDefaultWslPath(mvnCmdStart);
pomPath=toDefaultWslPath(pomPath);
if(customCommand){
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` ${customCommand}` +` -f "${pomPath}"`; //Bash
}
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` -f "${pomPath}"`; //Bash for start..


default:
mvnCmdStart=Path.join(mvnCmdStart, "mvnw.cmd");
if(customCommand){
return "\""+ mvnCmdStart +"\" " + `${command}`+` ${customCommand}` + ` -f "${pomPath}"`;
}
return "\""+ mvnCmdStart +"\" " + `${command}`+ ` -f "${pomPath}"`;
}
} else {
if(customCommand){
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+` ${customCommand}` + ` -f "${pomPath}"`;
}
return "cd \""+ mvnCmdStart +"\" && "+"./mvnw "+`${command}`+ ` -f "${pomPath}"`;
}

}

/**
* Return the Gradle commands based on the OS and Terminal
*/
export function getCommandForGradle(gradleCmdStart: string, buildGradlePath: string, command: string, customCommand?: string) : string {

if (isWin()) {
switch (currentWindowsShell()) {
case ShellType.GIT_BASH:
gradleCmdStart=Path.join(gradleCmdStart, "gradlew");
if(customCommand){
return "\""+ gradleCmdStart +"\" " + `${command}` + ` ${customCommand}` + ` -b="${buildGradlePath}"`; //bash start..
}
return "\""+ gradleCmdStart +"\" " + `${command}` + ` -b="${buildGradlePath}"`; //Bash
case ShellType.POWERSHELL: {
gradleCmdStart=Path.join(gradleCmdStart, "gradlew.bat");
if(customCommand){
return "& \""+ gradleCmdStart +"\" " + `${command}` + ` ${customCommand}` + ` -b="${buildGradlePath}"`;// PowerShell strat..
}
return "& \""+ gradleCmdStart +"\" " + `${command}` + ` -b="${buildGradlePath}"`;// PowerShell
}
case ShellType.CMD:
gradleCmdStart=Path.join(gradleCmdStart, "gradlew.bat");
if(customCommand){
return "\""+ gradleCmdStart +"\" " + `${command}` + ` ${customCommand}` + ` -b="${buildGradlePath}"`; // CMD start..
}
return "\""+ gradleCmdStart +"\" " + `${command}` + ` -b="${buildGradlePath}"`; // CMD
case ShellType.WSL:
buildGradlePath=toDefaultWslPath(buildGradlePath);
gradleCmdStart=toDefaultWslPath(gradleCmdStart)
gradleCmdStart=Path.join(gradleCmdStart, "gradlew");
if(customCommand){
return "\""+ gradleCmdStart +"\" " + `${command}` + ` ${customCommand}` + ` -b="${buildGradlePath}"`; //wsl start..
}
return "\""+ gradleCmdStart +"\" " + `${command}` + ` -b="${buildGradlePath}"`; //wsl
default:
gradleCmdStart=Path.join(gradleCmdStart, "gradlew.bat");
if(customCommand){
"\""+ gradleCmdStart +"\" " + `${command}` + ` ${customCommand}` + ` -b="${buildGradlePath}"`;
}
return "\""+ gradleCmdStart +"\" " + `${command}` + ` -b="${buildGradlePath}"`;
}
} else {
gradleCmdStart=Path.join(gradleCmdStart, "gradlew");
if(customCommand){
return "\""+ gradleCmdStart +"\" " + `${command}` + ` ${customCommand}` + ` -b="${buildGradlePath}"`;
}
return "\""+ gradleCmdStart +"\" " + `${command}` + ` -b="${buildGradlePath}"`;
}

}

function toDefaultWslPath(p: string): string {
const arr: string[] = p.split(":\\");
if (arr.length === 2) {
const drive: string = arr[0].toLowerCase();
const dir: string = arr[1].replace(/\\/g, "/");
return `/mnt/${drive}/${dir}`;
} else {
return p.replace(/\\/g, "/");
}
}

0 comments on commit 595a16e

Please sign in to comment.