Skip to content

Commit

Permalink
fix(gradle): check if java is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jan 9, 2025
1 parent 9c176d8 commit 01a42d2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 34 deletions.
55 changes: 29 additions & 26 deletions packages/gradle/src/utils/exec-gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,37 @@ export function execGradleAsync(
args: ReadonlyArray<string>,
execOptions: ExecFileOptions = {}
): Promise<Buffer> {
return new Promise<Buffer>((res, rej) => {
const cp = execFile(gradleBinaryPath, args, {
cwd: dirname(gradleBinaryPath),
shell: true,
windowsHide: true,
env: process.env,
...execOptions,
});
return new Promise<Buffer>(
(res, rej: (reason: { status: number; logs: string }) => void) => {
const cp = execFile(gradleBinaryPath, args, {
cwd: dirname(gradleBinaryPath),
shell: true,
windowsHide: true,
env: process.env,
...execOptions,
});

let stdout = Buffer.from('');
cp.stdout?.on('data', (data) => {
stdout += data;
});
let stdout = Buffer.from('');
let stderr = Buffer.from('');
cp.stdout?.on('data', (data) => {
stdout += data;
});
cp.stderr?.on('data', (data) => {
stderr += data;
});

cp.on('exit', (code) => {
if (code === 0) {
res(stdout);
} else {
rej(
new Error(
`Executing Gradle with ${args.join(
' '
)} failed with code: ${code}. \nLogs: ${stdout}`
)
);
}
});
});
cp.on('exit', (code) => {
if (code === 0) {
res(stdout);
} else {
rej({
status: code,
logs: `${stdout} ${stderr}`,
});
}
});
}
);
}

/**
Expand Down
48 changes: 40 additions & 8 deletions packages/gradle/src/utils/get-project-report-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AggregateCreateNodesError, logger } from '@nx/devkit';
import { execGradleAsync } from './exec-gradle';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import { execSync } from 'child_process';

export const fileSeparator = process.platform.startsWith('win')
? 'file:///'
Expand Down Expand Up @@ -37,20 +38,51 @@ export async function getProjectReportLines(
'projectReportAll',
]);
} catch (e) {
try {
projectReportBuffer = await execGradleAsync(gradlewFile, [
'projectReport',
]);
logger.warn(
`Could not run 'projectReportAll' task. Ran 'projectReport' instead. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks. ${e.message}`
if (e.status === 1 && e.logs.includes('ERROR: JAVA_HOME')) {
throw new AggregateCreateNodesError(
[
[
gradlewFile,
new Error(
`Could not find Java. Please install Java and try again: https://www.java.com/en/download/help/index_installing.html.\n\r${e.logs}`
),
],
],
[]
);
} catch (e) {
} else if (
e.status === 1 &&
e.logs.includes(`Task 'projectReportAll' not found`)
) {
try {
projectReportBuffer = await execGradleAsync(gradlewFile, [
'projectReport',
]);
logger.warn(
`Could not run 'projectReportAll' task. Ran 'projectReport' instead. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.\n\r${e.logs}`
);
} catch (e) {
throw new AggregateCreateNodesError(
[
[
gradlewFile,
new Error(
`Could not run 'projectReportAll' or 'projectReport' task. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.\n\r${e.logs}`
),
],
],
[]
);
}
} else {
throw new AggregateCreateNodesError(
[
[
gradlewFile,
new Error(
`Could not run 'projectReportAll' or 'projectReport' task. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks. ${e.message}`
`Could not run 'projectReportAll' or 'projectReport' Gradle task. Please install Gradle and try again: https://gradle.org/install/.\r\n${
e.logs ?? e.message
}`
),
],
],
Expand Down

0 comments on commit 01a42d2

Please sign in to comment.