matthiasblaesing commented on code in PR #9263:
URL: https://github.com/apache/netbeans/pull/9263#discussion_r3190630877
##########
ide/projectapi/src/org/netbeans/modules/projectapi/SimpleFileOwnerQueryImplementation.java:
##########
@@ -210,6 +210,18 @@ public Project getOwner(FileObject f) {
return null;
}
+ private static boolean notWithinProjectScanRoots(FileObject f) {
+ if (projectScanRoots == null)
+ return false;
+ String path = f.getPath();
+ for (String scanRoot : projectScanRoots) {
+ if (path.startsWith(scanRoot)
+ && (path.length() == scanRoot.length()
+ || path.charAt(scanRoot.length()) == '/'))
Review Comment:
I assume this change intents to check that we don't get a substring match
for the last component. So far so sensible, but will it work on windows paths?
PS: Also does not match logic in
`GradleFiles.Searcher#notWithinProjectScanRoots`
##########
extide/gradle/test/unit/src/org/netbeans/modules/gradle/spi/GradleFilesTest.java:
##########
@@ -38,6 +40,15 @@ public class GradleFilesTest {
@Rule
public TemporaryFolder root = new TemporaryFolder();
+ private static File normalizeTempDir(File root) {
+ if (root != null && Utilities.isMac()) {
+ String absolutePath = root.getAbsolutePath();
+ if (absolutePath.startsWith("/private/")) {
+ return new File(absolutePath.substring(8));
+ }
+ }
Review Comment:
This needs a comment. What is so special about the temp on mac and why does
this break anything?
##########
extide/gradle/src/org/netbeans/modules/gradle/spi/GradleFiles.java:
##########
@@ -433,4 +414,123 @@ public static Set<File> getSubProjects(File f) {
}
}
+ /**
+ * Gradle sub-project directories may not be easily identifiable and
require
+ * scanning up the directory hierarchy up to the filesystem root for the
+ * settings file.
+ *
+ * Although some conventions for the {@code buildSrc} directory for shared
+ * modules exists, it is not generally applicable to sub-projects.
+ * See <a
href="https://docs.gradle.org/current/userguide/organizing_gradle_projects.html">
+ * Gradle Docs: Organizing Gradle Projects</a>
+ *
+ * This Searcher allows for safely scanning up the directory hierarchy up
to
+ * the globally configured scan root limits ({@code
-Dproject.limitScanRoot}),
+ * if any;
+ * and mindful of forbidden folders ({@code -Dproject.forbiddenFolders}
+ * or {@code -Dversioning.forbiddenFolders}), if any.
+ */
+ public static class Searcher {
+ private static final Set<String> forbiddenFolders;
+ private static final Set<String> projectScanRoots;
+
+ static {
+ Set<String> folders = null;
+ Set<String> roots = null;
+ try {
+ roots =
separatePaths(System.getProperty("project.limitScanRoot"), File.pathSeparator);
//NOI18N
+ folders =
separatePaths(System.getProperty("project.forbiddenFolders",
System.getProperty("versioning.forbiddenFolders")), ";"); //NOI18N
+ } catch (Exception e) {
+ LOG.log(Level.INFO, e.getMessage(), e);
+ }
+ forbiddenFolders = folders == null ? Collections.emptySet() :
folders;
+ projectScanRoots = roots;
+ }
+
+ public static File searchPath(@NonNull File baseDir, String... names) {
+ return resolvePathWithAlternatives(false, baseDir, names);
+ }
+
+ public static File searchPathUp(@NonNull File baseDir, String...
names) {
+ return resolvePathWithAlternatives(true, baseDir, names);
+ }
+
+ private static File resolvePathWithAlternatives(boolean recursive,
@NonNull File baseDir, String... names) {
+ File dir = baseDir;
+ if (names.length == 0) {
+ return null;
+ }
+ for (String name : names) {
+ Objects.requireNonNull(name);
+ }
+ while (dir != null) {
+ String path = dir.getAbsolutePath();
+ if (notWithinProjectScanRoots(path))
+ break;
+ if (!forbiddenFolders.contains(path)) {
+ for (String name : names) {
+ File f = new File(dir, name);
+ if (f.canRead()) {
+ return f;
+ }
+ }
+ }
+ dir = recursive ? dir.getParentFile() : null;
+ }
+ return null;
+ }
+
+ private static boolean notWithinProjectScanRoots(String path) {
+ if (projectScanRoots == null)
+ return false;
+ for (String scanRoot : projectScanRoots) {
+ if (path.startsWith(scanRoot)
+ && (path.length() == scanRoot.length()
+ || path.charAt(scanRoot.length()) ==
File.separatorChar))
+ return false;
+ }
+ return true;
+ }
+
+ private static Set<String> separatePaths(String joinedPaths, String
pathSeparator) {
+ if (joinedPaths == null || joinedPaths.isEmpty())
+ return null;
+
+ Set<String> paths = null;
+ for (String split : joinedPaths.split(pathSeparator)) {
+ if ((split = split.trim()).isEmpty()) continue;
Review Comment:
This will break when path contains leading or trailing spaces. Or is this
just a copy from `SimpleFileOwnerQueryImplementation`? If so, please mark both
places that they need to be synced on change.
##########
extide/gradle/src/org/netbeans/modules/gradle/NbGradleProjectFactory.java:
##########
@@ -57,23 +54,23 @@ public boolean isProject(FileObject dir) {
}
static boolean isProjectCheck(FileObject dir, final boolean preferMaven) {
- if (dir == null || FileUtil.toFile(dir) == null) {
+ File suspect = dir == null ? null : FileUtil.toFile(dir);
+ if (suspect == null) {
return false;
}
- FileObject pom = dir.getFileObject("pom.xml"); //NOI18N
- if (pom != null && pom.isData()) {
+ File pom = GradleFiles.Searcher.searchPath(suspect, "pom.xml");
//NOI18N
+ if (pom != null && pom.isFile()) {
if (preferMaven) {
return false;
}
- final FileObject parent = dir.getParent();
- if (parent != null && parent.getFileObject("pom.xml") != null) {
// NOI18N
- return isProjectCheck(parent, preferMaven);
+ FileObject parent = dir.getParent();
+ if (parent != null &&
GradleFiles.Searcher.searchPath(FileUtil.toFile(parent), "pom.xml") != null) {
// NOI18N
+ return isProjectCheck(parent, false);
Review Comment:
is `preferMaven` intentionally dropped here?
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists