sid-srini commented on code in PR #9263:
URL: https://github.com/apache/netbeans/pull/9263#discussion_r3242086590
##########
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:
Yes, this is copied from `SimpleFileOwnerQueryImplementation` as noted in
the commit and PR. I will add a comment in both places to sync on change.
The trimming of leading and trailing spaces was prompted mostly from a
usability perspective where user-input may add additional leading and trailing
whitespace on paths. Guidance by Microsoft Windows is to avoid trailing
whitespaces since it is unsupported by various UIs, even though it is supported
by NTFS. These UIs end up trimming the input, causing problems when
copy-pasting to another terminal.
However, since this may cause the breakage on legitimate (although uncommon)
input, I will remove the trimming from here, as well as, from
`SimpleFileOwnerQueryImplementation`, except to detect empty input. Thanks.
--
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