Skip to content

Commit 5d6942f

Browse files
authored
fix: triggering gradle on windows (#303)
* fix: triggering gradle on windows * remove redundant if check
1 parent 33b6736 commit 5d6942f

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

lib/index.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,6 @@ export async function getGradleVersion(
411411
return gradleVersionOutput;
412412
}
413413

414-
export function generateWrapperProcessArgs(
415-
commandPath: string,
416-
args: string[],
417-
): { command: string; args: string[] } {
418-
let parseArgs: string[] = [];
419-
let command = commandPath;
420-
const isWinLocal = /^win/.test(os.platform());
421-
if (isWinLocal && command !== 'gradle') {
422-
command = 'cmd.exe';
423-
parseArgs.push(commandPath);
424-
}
425-
parseArgs = parseArgs.concat(args);
426-
return { command, args: parseArgs };
427-
}
428-
429414
async function getAllDepsWithPlugin(
430415
root: string,
431416
targetFile: string,
@@ -444,15 +429,11 @@ async function getAllDepsWithPlugin(
444429
gradleVersion,
445430
);
446431

447-
const { command: wrapperedCommand, args: wrapperArgs } =
448-
generateWrapperProcessArgs(command, args);
449-
450-
const fullCommandText =
451-
'gradle command: ' + wrapperedCommand + ' ' + wrapperArgs.join(' ');
432+
const fullCommandText = 'gradle command: ' + command + ' ' + args.join(' ');
452433
debugLog('Executing ' + fullCommandText);
453434
const stdoutText = await subProcess.execute(
454-
wrapperedCommand,
455-
wrapperArgs,
435+
command,
436+
args,
456437
{ cwd: root },
457438
printIfEcho,
458439
);
@@ -487,13 +468,7 @@ async function getAllDeps(
487468
snykHttpClient: SnykHttpClient,
488469
): Promise<JsonDepsScriptResult> {
489470
const command = getCommand(root, targetFile);
490-
const { command: wrapperedCommand, args: wrapperArgs } =
491-
generateWrapperProcessArgs(command, []);
492-
const gradleVersion = await getGradleVersion(
493-
root,
494-
wrapperedCommand,
495-
wrapperArgs,
496-
);
471+
const gradleVersion = await getGradleVersion(root, command, []);
497472
if (gradleVersion.match(/Gradle 1/)) {
498473
throw new Error('Gradle 1.x is not supported');
499474
}

lib/sub-process.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ export function execute(
2525

2626
args = escapeAll(args, spawnOptions);
2727

28-
if (/^win/.test(os.platform()) && command === 'cmd.exe') {
28+
if (/^win/.test(os.platform())) {
2929
spawnOptions.windowsVerbatimArguments = true; // makes windows process " correctly
30-
args = ['/c', `"${quoteAll(args).join(' ')}"`];
31-
}
3230

31+
const updated = updateCommandAndArgsForWindows(command, args);
32+
command = updated.command;
33+
args = updated.args;
34+
}
3335
// Before spawning an external process, we look if we need to restore the system proxy configuration,
3436
// which overides the cli internal proxy configuration.
3537
if (process.env.SNYK_SYSTEM_HTTP_PROXY !== undefined) {
@@ -86,3 +88,21 @@ ${stderr}
8688
});
8789
});
8890
}
91+
92+
function updateCommandAndArgsForWindows(
93+
command: string,
94+
args: string[],
95+
): { command: string; args: string[] } {
96+
args = quoteAll(args); // to handle any spaces in args relating to file path
97+
98+
if (command !== 'gradle') {
99+
// when command is not gradle we need to wrap the command in "
100+
// then wrap the combined string of all args to enable windows to interpret the command correctly
101+
args = [`"${command}"`, ...args];
102+
args = ['/c', `"${args.join(' ')}"`];
103+
} else {
104+
args = ['/c', command, ...args];
105+
}
106+
107+
return { command: 'cmd.exe', args };
108+
}

test/system/windows.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ if (isWinLocal) {
3131
inspect(subWithWrapper, path.join('app', 'build.gradle')),
3232
).rejects.toThrow();
3333
expect(subProcessExecSpy.mock.calls[0]).toEqual([
34-
'cmd.exe',
35-
[`${subWithWrapper}\\gradlew.bat`, '-v'],
34+
`${subWithWrapper}\\gradlew.bat`,
35+
['-v'],
3636
{
3737
cwd: `${subWithWrapper}`,
3838
},
@@ -42,8 +42,8 @@ if (isWinLocal) {
4242
test('windows with wrapper invokes wrapper bat', async () => {
4343
await expect(inspect(rootWithWrapper, 'build.gradle')).rejects.toThrow();
4444
expect(subProcessExecSpy.mock.calls[0]).toEqual([
45-
'cmd.exe',
46-
[`${rootWithWrapper}\\gradlew.bat`, '-v'],
45+
`${rootWithWrapper}\\gradlew.bat`,
46+
['-v'],
4747
{
4848
cwd: `${rootWithWrapper}`,
4949
},

0 commit comments

Comments
 (0)