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

fix(gradle): check if java is installed #29572

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions packages/gradle/src/utils/exec-gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function execGradleAsync(
args: ReadonlyArray<string>,
execOptions: ExecFileOptions = {}
): Promise<Buffer> {
return new Promise<Buffer>((res, rej) => {
return new Promise<Buffer>((res, rej: (stdout: Buffer) => void) => {
const cp = execFile(gradleBinaryPath, args, {
cwd: dirname(gradleBinaryPath),
shell: true,
Expand All @@ -36,18 +36,15 @@ export function execGradleAsync(
cp.stdout?.on('data', (data) => {
stdout += data;
});
cp.stderr?.on('data', (data) => {
stdout += 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}`
)
);
rej(stdout);
}
});
});
Expand Down
45 changes: 36 additions & 9 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 @@ -36,21 +37,47 @@ export async function getProjectReportLines(
projectReportBuffer = await execGradleAsync(gradlewFile, [
'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}`
} catch (e: Buffer | Error | any) {
if (e.toString()?.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.toString()}`
),
],
],
[]
);
} catch (e) {
} else if (e.toString()?.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.toString()}`
);
} 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.toString()}`
),
],
],
[]
);
}
} 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.toString()}`
),
],
],
Expand Down
Loading