|
1 | 1 | /* |
2 | 2 | * IBM Confidential |
3 | | - * Copyright IBM Corp. 2024 |
| 3 | + * Copyright IBM Corp. 2024, 2026 |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import * as Path from "path"; |
@@ -52,16 +52,16 @@ export async function getCommandForMaven(pomPath: string, command: string, termi |
52 | 52 | */ |
53 | 53 | export async function getCommandForGradle(buildGradlePath: string, command: string, terminalType?: String, customCommand?: string): Promise<string> { |
54 | 54 | let gradleCmdStart = await gradleCmd(buildGradlePath); |
| 55 | + const projectDir = Path.dirname(buildGradlePath); |
55 | 56 |
|
56 | 57 | if (gradleCmdStart === "gradle") { |
57 | | - return formDefaultCommand(gradleCmdStart, buildGradlePath, command, "-b=", customCommand); |
| 58 | + return formGradleCommand(projectDir, "gradle", command, customCommand); |
58 | 59 | } |
59 | 60 | //checking the OS type for command customization |
60 | 61 | if (isWin()) { |
61 | | - return getGradleCommandForWin(gradleCmdStart, buildGradlePath, command, terminalType, customCommand); |
| 62 | + return getGradleCommandForWin(gradleCmdStart, projectDir, command, terminalType, customCommand); |
62 | 63 | } else { |
63 | | - gradleCmdStart = Path.join(gradleCmdStart, "gradlew"); |
64 | | - return formDefaultCommandWithPath(gradleCmdStart, buildGradlePath, command, "-b=", customCommand); |
| 64 | + return formGradleCommand(projectDir, "./gradlew", command, customCommand); |
65 | 65 | } |
66 | 66 | } |
67 | 67 |
|
@@ -158,20 +158,17 @@ async function getLocalMavenWrapperDir(projectFolder: string): Promise<string | |
158 | 158 | /** |
159 | 159 | * Returns the gradle command for windows OS based on the terminal configured |
160 | 160 | */ |
161 | | -function getGradleCommandForWin(gradleCmdStart: string, buildGradlePath: string, command: string, terminalType?: String, customCommand?: string): string { |
| 161 | +function getGradleCommandForWin(gradleCmdStart: string, projectDir: string, command: string, terminalType?: String, customCommand?: string): string { |
162 | 162 | switch (terminalType) { |
163 | 163 | case ShellType.GIT_BASH: |
164 | | - gradleCmdStart = Path.join(gradleCmdStart, "gradlew"); |
165 | | - return formDefaultCommandWithPath(gradleCmdStart, buildGradlePath, command, "-b=", customCommand); //Bash |
| 164 | + return formGradleCommand(projectDir, "./gradlew", command, customCommand); //Bash |
166 | 165 | case ShellType.POWERSHELL: |
167 | | - gradleCmdStart = Path.join(gradleCmdStart, "gradlew.bat"); |
168 | | - return formPowershellCommand(gradleCmdStart, buildGradlePath, command, "-b=", customCommand); |
| 166 | + return formGradlePowershellCommand(projectDir, "gradlew.bat", command, customCommand); |
169 | 167 | case ShellType.WSL: |
170 | | - return formLinuxBasedCommand(toDefaultWslPath(gradleCmdStart), command, "./gradlew ", customCommand); //Wsl |
| 168 | + return formGradleCommand(toDefaultWslPath(projectDir), "./gradlew", command, customCommand); //Wsl |
171 | 169 | default: |
172 | 170 | // The default case is ShellType CMD or OTHERS |
173 | | - gradleCmdStart = Path.join(gradleCmdStart, "gradlew.bat"); |
174 | | - return formDefaultCommandWithPath(gradleCmdStart, buildGradlePath, command, "-b=", customCommand); |
| 171 | + return formGradleWindowsCommand(projectDir, "gradlew.bat", command, customCommand); |
175 | 172 | } |
176 | 173 | } |
177 | 174 |
|
@@ -235,6 +232,38 @@ function formDefaultCommandWithPath(projectPath: string, buildFilePath: String, |
235 | 232 | return "\"" + projectPath + "\" " + `${command}` + ` ${cmdOption}"${buildFilePath}"`; |
236 | 233 | } |
237 | 234 |
|
| 235 | +/** |
| 236 | + * Returns the Gradle command by cd'ing into the project directory |
| 237 | + * This approach is compatible with all Gradle versions including Gradle 9+ |
| 238 | + * where the -b option was removed |
| 239 | + */ |
| 240 | +function formGradleCommand(projectDir: string, gradleCmd: string, command: string, customCommand?: string): string { |
| 241 | + if (customCommand) { |
| 242 | + return `cd "${projectDir}" && ${gradleCmd} ${command} ${customCommand}`; |
| 243 | + } |
| 244 | + return `cd "${projectDir}" && ${gradleCmd} ${command}`; |
| 245 | +} |
| 246 | + |
| 247 | +/** |
| 248 | + * Returns the Gradle command for PowerShell by cd'ing into the project directory |
| 249 | + */ |
| 250 | +function formGradlePowershellCommand(projectDir: string, gradleCmd: string, command: string, customCommand?: string): string { |
| 251 | + if (customCommand) { |
| 252 | + return `cd "${projectDir}"; .\\${gradleCmd} ${command} ${customCommand}`; |
| 253 | + } |
| 254 | + return `cd "${projectDir}"; .\\${gradleCmd} ${command}`; |
| 255 | +} |
| 256 | + |
| 257 | +/** |
| 258 | + * Returns the Gradle command for Windows CMD by cd'ing into the project directory |
| 259 | + */ |
| 260 | +function formGradleWindowsCommand(projectDir: string, gradleCmd: string, command: string, customCommand?: string): string { |
| 261 | + if (customCommand) { |
| 262 | + return `cd "${projectDir}" && ${gradleCmd} ${command} ${customCommand}`; |
| 263 | + } |
| 264 | + return `cd "${projectDir}" && ${gradleCmd} ${command}`; |
| 265 | +} |
| 266 | + |
238 | 267 | /** |
239 | 268 | * Reused from vscode-maven - currentWindowsShell() |
240 | 269 | * https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts |
|
0 commit comments