jamesfredley commented on code in PR #415: URL: https://github.com/apache/grails-intellij-plugin/pull/415#discussion_r3972544420
########## plugin/src/main/java/org/apache/grails/intellij/plugin/config/GradleSettingsFile.java: ########## @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.config; + +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; +import java.nio.file.Path; + +/** + * Writes the {@code settings.gradle} a freshly created Grails module needs, the way the Gradle + * plugin's module builder does for its own modules, minus the platform-internal API. + * <p> + * A missing settings file gets {@code rootProject.name}, plus an {@code include} when the module + * does not sit at the Gradle root. An existing settings file is left alone except for that + * {@code include}, which is appended once: {@code grails create-app} may already have written the + * file, and a second {@code rootProject.name} would only override the first. + */ +final class GradleSettingsFile { + + static final String GROOVY_NAME = "settings.gradle"; + static final String KOTLIN_NAME = "settings.gradle.kts"; + + private GradleSettingsFile() { + } + + /** + * @param rootProjectPath the Gradle root project directory + * @param moduleRoot the module's content root, equal to or below {@code rootProjectPath} + * @param projectName the value for {@code rootProject.name} when the file is created + * @param moduleName the Gradle project name of the module + */ + static void setUp(@NotNull Path rootProjectPath, @NotNull Path moduleRoot, @NotNull String projectName, @NotNull String moduleName) + throws IOException { + final VirtualFile rootDir = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(rootProjectPath); + if (rootDir == null || !rootDir.isDirectory()) { + throw new IOException("Gradle root project directory not found: " + rootProjectPath); + } + + final Path relative = rootProjectPath.toAbsolutePath().normalize().relativize(moduleRoot.toAbsolutePath().normalize()); + final String moduleDir = FileUtil.toSystemIndependentName(relative.toString()); + final boolean moduleIsRoot = moduleDir.isEmpty(); + + VirtualFile settings = rootDir.findChild(KOTLIN_NAME); + if (settings == null) settings = rootDir.findChild(GROOVY_NAME); + + if (settings == null) { + final StringBuilder text = new StringBuilder(); + text.append(rootProjectNameLine(projectName, false)).append('\n'); + if (!moduleIsRoot) { + text.append(includeLines(moduleName, moduleDir, false)); + } + settings = rootDir.createChildData(GradleSettingsFile.class, GROOVY_NAME); + VfsUtil.saveText(settings, text.toString()); + return; + } + + if (moduleIsRoot) return; // the existing file already describes this project + + final boolean kotlinDsl = KOTLIN_NAME.equals(settings.getName()); + final String existing = VfsUtilCore.loadText(settings); + if (existing.contains("'" + moduleName + "'") || existing.contains("\"" + moduleName + "\"")) return; // already included Review Comment: This is not an inclusion check. Any quoted occurrence of `moduleName` (comment, unrelated literal, `includeBuild`, incomplete include) suppresses setup. That is real breakage on the `linkModule()` path when adding a Grails module under an existing Gradle parent. For `moduleName = shop-web` and `moduleDir = apps/web`, `include 'shop-web'` alone maps `:shop-web` to `<root>/shop-web`, not `apps/web`. Replace the global substring test with statement-level recognition: 1. Detect a real `include` for `moduleName`, ignoring comments and unrelated literals. 2. If `moduleDir != moduleName`, separately verify the matching `projectDir` assignment. 3. Append only the missing statement. 4. Treat the legacy IntelliJ form as already mapped and do not add `projectDir`: `include 'apps:web'` plus `findProject(':apps:web')?.name = 'shop-web'`. ########## plugin/src/main/java/org/apache/grails/intellij/plugin/config/GradleSettingsFile.java: ########## @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.config; + +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; +import java.nio.file.Path; + +/** + * Writes the {@code settings.gradle} a freshly created Grails module needs, the way the Gradle + * plugin's module builder does for its own modules, minus the platform-internal API. + * <p> + * A missing settings file gets {@code rootProject.name}, plus an {@code include} when the module + * does not sit at the Gradle root. An existing settings file is left alone except for that + * {@code include}, which is appended once: {@code grails create-app} may already have written the + * file, and a second {@code rootProject.name} would only override the first. + */ +final class GradleSettingsFile { + + static final String GROOVY_NAME = "settings.gradle"; + static final String KOTLIN_NAME = "settings.gradle.kts"; + + private GradleSettingsFile() { + } + + /** + * @param rootProjectPath the Gradle root project directory + * @param moduleRoot the module's content root, equal to or below {@code rootProjectPath} + * @param projectName the value for {@code rootProject.name} when the file is created + * @param moduleName the Gradle project name of the module + */ + static void setUp(@NotNull Path rootProjectPath, @NotNull Path moduleRoot, @NotNull String projectName, @NotNull String moduleName) + throws IOException { + final VirtualFile rootDir = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(rootProjectPath); + if (rootDir == null || !rootDir.isDirectory()) { + throw new IOException("Gradle root project directory not found: " + rootProjectPath); + } + + final Path relative = rootProjectPath.toAbsolutePath().normalize().relativize(moduleRoot.toAbsolutePath().normalize()); + final String moduleDir = FileUtil.toSystemIndependentName(relative.toString()); + final boolean moduleIsRoot = moduleDir.isEmpty(); + + VirtualFile settings = rootDir.findChild(KOTLIN_NAME); + if (settings == null) settings = rootDir.findChild(GROOVY_NAME); + + if (settings == null) { + final StringBuilder text = new StringBuilder(); + text.append(rootProjectNameLine(projectName, false)).append('\n'); + if (!moduleIsRoot) { + text.append(includeLines(moduleName, moduleDir, false)); + } + settings = rootDir.createChildData(GradleSettingsFile.class, GROOVY_NAME); + VfsUtil.saveText(settings, text.toString()); + return; + } + + if (moduleIsRoot) return; // the existing file already describes this project + + final boolean kotlinDsl = KOTLIN_NAME.equals(settings.getName()); + final String existing = VfsUtilCore.loadText(settings); + if (existing.contains("'" + moduleName + "'") || existing.contains("\"" + moduleName + "\"")) return; // already included + + final StringBuilder text = new StringBuilder(existing); + if (!existing.isEmpty() && !existing.endsWith("\n")) text.append('\n'); + text.append(includeLines(moduleName, moduleDir, kotlinDsl)); + VfsUtil.saveText(settings, text.toString()); + } + + private static @NotNull String rootProjectNameLine(@NotNull String projectName, boolean kotlinDsl) { + return "rootProject.name = " + quote(projectName, kotlinDsl); + } + + private static @NotNull String includeLines(@NotNull String moduleName, @NotNull String moduleDir, boolean kotlinDsl) { + final StringBuilder lines = new StringBuilder(); + lines.append(kotlinDsl ? "include(" + quote(moduleName, true) + ")" : "include " + quote(moduleName, false)).append('\n'); + if (!moduleDir.equals(moduleName)) { + lines.append("project(").append(quote(':' + moduleName, kotlinDsl)).append(").projectDir = file(") + .append(quote(moduleDir, kotlinDsl)).append(")\n"); Review Comment: Once the existing-file path can tell “has include” apart from “has mapping”, append only what is missing here. If the include is already present, write just the `projectDir` line (when `moduleDir != moduleName`). Do not rewrite a valid legacy path-plus-rename setup (`include 'apps:web'` / `findProject(':apps:web')?.name = 'shop-web'`). ########## plugin/src/test/java/org/apache/grails/intellij/plugin/config/GradleSettingsFileTest.java: ########## @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.config; + +import com.intellij.openapi.application.WriteAction; +import com.intellij.openapi.util.io.FileUtil; +import org.apache.grails.intellij.lib.testFramework.GrailsTestCase; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +public class GradleSettingsFileTest extends GrailsTestCase { + private Path myRoot; + + @Override + protected void setUp() throws Exception { + super.setUp(); + myRoot = FileUtil.createTempDirectory("gradle-settings", null, true).toPath(); + } + + public void testCreatesSettingsForSingleProjectModule() throws Exception { + setUp(myRoot, myRoot, "shop", "shop"); + assertEquals("rootProject.name = 'shop'\n", read("settings.gradle")); + } + + public void testCreatesSettingsWithIncludeForNestedModule() throws Exception { + Path module = Files.createDirectories(myRoot.resolve("web")); + setUp(myRoot, module, "shop", "web"); + assertEquals("rootProject.name = 'shop'\ninclude 'web'\n", read("settings.gradle")); + } + + public void testMapsProjectDirWhenModuleDirDiffersFromName() throws Exception { + Path module = Files.createDirectories(myRoot.resolve("apps/web")); + setUp(myRoot, module, "shop", "shop-web"); + assertEquals("rootProject.name = 'shop'\ninclude 'shop-web'\nproject(':shop-web').projectDir = file('apps/web')\n", + read("settings.gradle")); + } + + public void testLeavesExistingRootSettingsAlone() throws Exception { + write("settings.gradle", "rootProject.name = 'generated-by-grails'\n"); + setUp(myRoot, myRoot, "shop", "shop"); + assertEquals("rootProject.name = 'generated-by-grails'\n", read("settings.gradle")); + } + + public void testAppendsIncludeToExistingSettingsOnce() throws Exception { + write("settings.gradle", "rootProject.name = 'shop'"); + Path module = Files.createDirectories(myRoot.resolve("web")); + setUp(myRoot, module, "shop", "web"); + setUp(myRoot, module, "shop", "web"); + assertEquals("rootProject.name = 'shop'\ninclude 'web'\n", read("settings.gradle")); + } Review Comment: Please add coverage for the existing-file cases the substring check currently mishandles: - Groovy/Kotlin settings with `include 'shop-web'` (or `include("shop-web")`) and no `projectDir`, module at `apps/web`. - Module name present only in a comment or unrelated string — still append include + mapping. - Legacy form `include 'apps:web'` + `findProject(':apps:web')?.name = 'shop-web'` — leave unchanged. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
