Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #337 - Command fix for project path with spaces #355

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
104 changes: 9 additions & 95 deletions src/liberty/devCommands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020, 2022 IBM Corporation.
* Copyright (c) 2020, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -18,9 +18,9 @@ import { LibertyProject, ProjectProvider } from "./libertyProject";
import { getReport, filterProjects } from "../util/helperUtil";
import { COMMAND_TITLES, LIBERTY_MAVEN_PROJECT, LIBERTY_GRADLE_PROJECT, LIBERTY_MAVEN_PROJECT_CONTAINER, LIBERTY_GRADLE_PROJECT_CONTAINER, LIBERTY_SERVER_ENV_PORT_REGEX } from "../definitions/constants";
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 @@ -109,12 +109,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 mvnCmdStart = await mvnCmd(libProject.getPath());
const cmd = `${mvnCmdStart} io.openliberty.tools:liberty-maven-plugin:dev -f "${libProject.getPath()}"`;
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:dev");
terminal.sendText(cmd); // start dev mode on current project
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT || libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
const gradleCmdStart = await gradleCmd(libProject.getPath());
const cmd = `${gradleCmdStart} libertyDev -b="${libProject.getPath()}"`;
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDev");
terminal.sendText(cmd); // start dev mode on current project
}
}
Expand Down Expand Up @@ -408,12 +406,10 @@ 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 = `${mvnCmdStart} io.openliberty.tools:liberty-maven-plugin:dev ${customCommand} -f "${libProject.getPath()}"`;
const cmd = await getCommandForMaven(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 = `${gradleCmdStart} libertyDev ${customCommand} -b="${libProject.getPath()}"`;
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDev",customCommand);
terminal.sendText(cmd);
}
}
Expand Down Expand Up @@ -443,12 +439,10 @@ export async function startContainerDevMode(libProject?: LibertyProject | undefi
terminal.show();
libProject.setTerminal(terminal);
if (libProject.getContextValue() === LIBERTY_MAVEN_PROJECT_CONTAINER) {
const mvnCmdStart = await mvnCmd(libProject.getPath());
const cmd = `${mvnCmdStart} io.openliberty.tools:liberty-maven-plugin:devc -f "${libProject.getPath()}"`;
const cmd = await getCommandForMaven(libProject.getPath(),"io.openliberty.tools:liberty-maven-plugin:devc");
terminal.sendText(cmd);
} else if (libProject.getContextValue() === LIBERTY_GRADLE_PROJECT_CONTAINER) {
const gradleCmdStart = await gradleCmd(libProject.getPath());
const cmd = `${gradleCmdStart} libertyDevc -b="${libProject.getPath()}"`;
const cmd = await getCommandForGradle(libProject.getPath(),"libertyDevc");
terminal.sendText(cmd);
}
}
Expand Down Expand Up @@ -510,7 +504,7 @@ export async function openReport(reportType: string, libProject?: LibertyProject
} else {
const message = localize("test.report.does.not.exist.run.test.first", report);
vscode.window.showInformationMessage(message);
}
}
});
}
} else if (ProjectProvider.getInstance() && reportType) {
Expand All @@ -530,84 +524,4 @@ export function deleteTerminal(terminal: vscode.Terminal): void {
} catch {
console.error(localize("unable.to.delete.terminal", terminal.name));
}
}


// return Maven executable path, Maven wrapper, or mvn
export async function mvnCmd(pomPath: 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");
if (mavenExecutablePath) {
return mavenExecutablePath;
}
const preferMavenWrapper: boolean | undefined = vscode.workspace.getConfiguration("maven").get<boolean>("executable.preferMavenWrapper");
if (preferMavenWrapper) {
const localMvnwPath: string | undefined = await getLocalMavenWrapper(Path.dirname(pomPath));
if (localMvnwPath) {
return localMvnwPath;
}
}
return "mvn";
}

export async function gradleCmd(buildGradle: string): Promise<string> {
const preferGradleWrapper: boolean | undefined = vscode.workspace.getConfiguration("java").get<boolean>("import.gradle.wrapper.enabled");
if (preferGradleWrapper) {
const localGradlewPath: string | undefined = await getLocalGradleWrapper(Path.dirname(buildGradle));
if (localGradlewPath) {
return localGradlewPath;
}
}
return "gradle";
}

/**
* Search for potential Maven wrapper, return undefined if does not exist
*
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/2ab8f392f418c8e0fe2903387f2b0013a1c50e78/src/utils/mavenUtils.ts
* @param projectFolder
*/
async function getLocalMavenWrapper(projectFolder: string): Promise<string | undefined> {
const mvnw: string = isWin() ? "mvnw.cmd" : "mvnw";

// walk up parent folders
let current: string = projectFolder;
while (Path.basename(current)) {
const potentialMvnwPath: string = Path.join(current, mvnw);
if (await pathExists(potentialMvnwPath)) {
return potentialMvnwPath;
}
current = Path.dirname(current);
}
return undefined;
}

/**
* Search for potential Gradle wrapper, return undefined if it does not exist
* Modified from vscode-maven, see getLocalMavenWrapper method above
* @param projectFolder
*/
async function getLocalGradleWrapper(projectFolder: string): Promise<string | undefined> {
const gradlew: string = isWin() ? "gradlew.bat" : "gradlew";

// walk up parent folders
let current: string = projectFolder;
while (Path.basename(current)) {
const potentialGradlewPath: string = Path.join(current, gradlew);
if (await pathExists(potentialGradlewPath)) {
return potentialGradlewPath;
}
current = Path.dirname(current);
}
return undefined;
}

/**
* Reused from vscode-maven
* https://github.com/microsoft/vscode-maven/blob/2ab8f392f418c8e0fe2903387f2b0013a1c50e78/src/utils/mavenUtils.ts
*/
function isWin(): boolean {
return process.platform.startsWith("win");
}
Loading
Loading