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): run gradle init if no settings.gradle #23226

Merged
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: 9 additions & 4 deletions packages/gradle/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { execSync } from 'child_process';
import { nxVersion } from '../../utils/versions';
import { InitGeneratorSchema } from './schema';
import { hasGradlePlugin } from '../../utils/has-gradle-plugin';

export async function initGenerator(tree: Tree, options: InitGeneratorSchema) {
const tasks: GeneratorCallback[] = [];

if (!tree.exists('settings.gradle') && !tree.exists('settings.gradle.kts')) {
logger.warn(`Could not find 'settings.gradle' or 'settings.gradle.kts' file in your gradle workspace.
A Gradle build should contain a 'settings.gradle' or 'settings.gradle.kts' file in its root directory. It may also contain a 'build.gradle' or 'build.gradle.kts' file.
Running 'gradle init':`);
execSync('gradle init', { stdio: 'inherit' });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you do this with an execSync here because it would get picked up later on via the Tree? 👀

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. just needs to be called before the function addProjectReportToBuildGradle so when checking if (tree.exists('settings.gradle.kts')) {, it would be true

}

if (!options.skipPackageJson && tree.exists('package.json')) {
tasks.push(
addDependenciesToPackageJson(
Expand Down Expand Up @@ -66,11 +74,8 @@ function addProjectReportToBuildGradle(tree: Tree) {
buildGradleFile = 'build.gradle.kts';
} else if (tree.exists('settings.gradle')) {
buildGradleFile = 'build.gradle';
} else {
throw new Error(
'Could not find settings.gradle or settings.gradle.kts file in your gradle workspace.'
);
}

let buildGradleContent = '';
if (tree.exists(buildGradleFile)) {
buildGradleContent = tree.read(buildGradleFile).toString();
Expand Down