JaroslavTulach commented on a change in pull request #1077: [NETBEANS-1793] 
Gradle Support
URL: 
https://github.com/apache/incubator-netbeans/pull/1077#discussion_r246666597
 
 

 ##########
 File path: groovy/gradle/src/org/netbeans/modules/gradle/api/GradleFiles.java
 ##########
 @@ -0,0 +1,357 @@
+/*
+ * 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
+ *
+ *   http://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.netbeans.modules.gradle.api;
+
+import org.netbeans.modules.gradle.spi.GradleSettings;
+import java.io.File;
+import java.io.IOException;
+import java.io.Serializable;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.WeakHashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.netbeans.api.annotations.common.NonNull;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Utilities;
+
+/**
+ *
+ * @author Laszlo Kishalmi
+ */
+public final class GradleFiles implements Serializable {
+
+    public enum Kind {
+
+        BUILD_SCRIPT,
+        ROOT_SCRIPT,
+        SETTINGS_SCRIPT,
+        USER_PROPERTIES,
+        PROJECT_PROPERTIES,
+        ROOT_PROPERTIES;
+
+        public static final Set<Kind> SCRIPTS = EnumSet.of(ROOT_SCRIPT, 
BUILD_SCRIPT, SETTINGS_SCRIPT);
+        public static final Set<Kind> PROPERTIES = EnumSet.of(USER_PROPERTIES, 
PROJECT_PROPERTIES, ROOT_PROPERTIES);
+        public static final Set<Kind> PROJECT_FILES = EnumSet.of(ROOT_SCRIPT, 
BUILD_SCRIPT, SETTINGS_SCRIPT, PROJECT_PROPERTIES, ROOT_PROPERTIES);
+    }
+
+    public static final String SETTINGS_FILE_NAME     = "settings.gradle"; 
//NOI18N
+    public static final String BUILD_FILE_NAME        = "build.gradle"; 
//NOI18N
+    public static final String GRADLE_PROPERTIES_NAME = "gradle.properties"; 
//NOI18N
+    public static final String WRAPPER_PROPERTIES     = 
"gradle/wrapper/gradle-wrapper.properties"; //NOI18N
+
+    final File projectDir;
+    File rootDir;
+    File buildScript;
+    File parentScript;
+    File settingsScript;
+    List<File> propertyFiles;
+    File gradlew;
+    File wrapperProperties;
+
+    public GradleFiles(File dir) {
+        try {
+            dir = dir.getCanonicalFile();
+        } catch (IOException ex) {
+            dir = FileUtil.normalizeFile(dir);
+        }
+        projectDir = dir;
+        rootDir = projectDir;
+        searchBuildScripts();
+        searchWrapper();
+    }
+
+    private void searchBuildScripts() {
+        File f1 = new File(projectDir, BUILD_FILE_NAME);
+        File f2 = new File(projectDir, projectDir.getName() + ".gradle");
+
+        settingsScript = searchPathUp(projectDir, SETTINGS_FILE_NAME);
+        File settingsDir = settingsScript != null ? 
settingsScript.getParentFile() : null;
+        buildScript = f1.canRead() ? f1 : f2.canRead() ? f2 : null;
+        if (settingsDir != null) {
+            //Guessing subprojects
+            rootDir = settingsDir;
+            File rootScript = new File(settingsDir, BUILD_FILE_NAME);
+            if (rootScript.canRead() && !rootScript.equals(buildScript)) {
+                parentScript = rootScript;
+            }
+        } else {
+            if (buildScript != null) {
+                rootDir = buildScript.getParentFile();
+            } else {
+                // TODO: NotSupportedLayout
+            }
+        }
+    }
+
+    private void searchWrapper() {
+        File w = new File(rootDir, Utilities.isWindows() ? "gradlew.bat" : 
"gradlew");
+        if (w.isFile()) {
+            gradlew = w;
+            wrapperProperties = new File(rootDir, WRAPPER_PROPERTIES);
+        }
+    }
+
+    private File searchPathUp(@NonNull File baseDir, @NonNull String name) {
+        File ret = null;
+        File dir = baseDir;
+        do {
+            File f = new File(dir, name);
+            ret = f.canRead() ? f : null;
+            dir = f.canRead() ? dir : dir.getParentFile();
+        } while ((ret == null) && (dir != null));
+        return ret;
+    }
+
+    public File getBuildScript() {
+        return buildScript;
+    }
+
+    public File getParentScript() {
+        return parentScript;
+    }
+
+    public File getSettingsScript() {
+        return settingsScript;
+    }
+
+    public List<File> getPropertyFiles() {
+        return propertyFiles;
+    }
+
+    public File getProjectDir() {
+        return isProject() ? projectDir : null;
+    }
+
+    public File getRootDir() {
+        return rootDir;
+    }
+
+    public File getGradlew() {
+        return gradlew;
+    }
+
+    public File getWrapperProperties() {
+        return wrapperProperties;
+    }
+
+    public boolean hasWrapper() {
+        return wrapperProperties != null;
+    }
+
+    public boolean isRootProject() {
+        return (buildScript != null) && rootDir.equals(projectDir);
+    }
+
+    public boolean isSubProject() {
+        return isProject() && !isRootProject();
+    }
+
+    public boolean isScriptlessSubProject() {
+        return (buildScript == null) && isSubProject();
+    }
+
+    public boolean isProject() {
+        boolean ret = buildScript != null;
+        if (!ret && (settingsScript != null)) {
+            ret = 
SettingsFile.getSubProjects(settingsScript).contains(projectDir);
+        }
+        return ret;
+    }
+
+    /**
+     * Returns the main suspected build scripts available for this project. 
This includes the project build script (if
+     * has any). The root build script and the settings script as well.
+     *
+     * @return The set of the existing scripts. En empty set if this 
GradleFiles doesn't look like an ordinary Gradle
+     * project.
+     */
+    public Set<File> getProjectFiles() {
 
 Review comment:
   Works with `File` which restricts the support to local file system.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
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

Reply via email to