You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2020/10/25 02:11:24 UTC

[GitHub] [netbeans] jtulach commented on a change in pull request #2485: [NETBEANS-4938] Fixed Gradle Java Frontend Application generation

jtulach commented on a change in pull request #2485:
URL: https://github.com/apache/netbeans/pull/2485#discussion_r511535914



##########
File path: java/gradle.htmlui/src/org/netbeans/modules/gradle/htmlui/GradleArchetype.java
##########
@@ -18,59 +18,74 @@
  */
 package org.netbeans.modules.gradle.htmlui;
 
-import java.io.IOException;
+import java.io.File;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
-import org.netbeans.api.project.ProjectManager;
-import org.netbeans.api.templates.FileBuilder;
-import org.netbeans.api.templates.FileBuilder.Mode;
+import org.netbeans.modules.gradle.spi.newproject.TemplateOperation;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
 import org.openide.util.MapFormat;
 
 public final class GradleArchetype {
     private final FileObject templates;
-    private final FileObject projectFo;

Review comment:
       I always prefer the `FileObject` abstraction over using plain `File`. It makes me sad when I see code moving to the opposite direction.
   
   What is the reason for making such change? The fact that modifications using `File` aren't notified as soon as they are made to various listeners?

##########
File path: java/gradle.htmlui/src/org/netbeans/modules/gradle/htmlui/HtmlJavaApplicationProjectWizard.java
##########
@@ -57,47 +53,24 @@ public HtmlJavaApplicationProjectWizard() {
 
     @Override
     protected void collectOperations(TemplateOperation ops, Map<String, Object> params) {
-        super.collectOperations(ops, params);
-        String packageBase = (String) params.get(PROP_PACKAGE_BASE);
-        String mainClassName = (String) params.get("mainClassName");
+        String name = (String) params.get(PROP_NAME);
+        File loc = (File) params.get(CommonProjectActions.PROJECT_PARENT_FOLDER);
+
+        File rootDir = new File(loc, name);
+        params.put(PROP_PROJECT_ROOT, rootDir);
         
-        File projectDir = (File) params.get("projectDir");
+        ops.createFolder(rootDir);
 
-        File mainJava = (File) params.get(PROP_MAIN_JAVA_DIR);
-        File packageDir = new File(mainJava, packageBase.replace('.', '/'));
-        Map<String, Object> mainParams = new HashMap<>(params);
-        mainParams.put("project", new DummyProject());
-        mainParams.put("package", packageBase); //NOI18N
-        mainParams.put("name", mainClassName); //NOI18N
 
         FileObject folder = ((TemplateWizard)this.getData()).getTemplate().getPrimaryFile();
-        ops.addConfigureProject(projectDir, new CopyTree(folder, projectDir, mainParams));
-    }
-
-    private static class CopyTree implements TemplateOperation.ProjectConfigurator {
-        private final FileObject templateFolder;
-        private final File projectDir;
-        private final Map<String, Object> params;
+        GradleArchetype ga = new GradleArchetype(folder, rootDir, params);

Review comment:
       This certainly looks like a simplification.

##########
File path: java/gradle.htmlui/src/org/netbeans/modules/gradle/htmlui/GradleArchetype.java
##########
@@ -18,59 +18,74 @@
  */
 package org.netbeans.modules.gradle.htmlui;
 
-import java.io.IOException;
+import java.io.File;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
-import org.netbeans.api.project.ProjectManager;
-import org.netbeans.api.templates.FileBuilder;
-import org.netbeans.api.templates.FileBuilder.Mode;
+import org.netbeans.modules.gradle.spi.newproject.TemplateOperation;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
 import org.openide.util.MapFormat;
 
 public final class GradleArchetype {
     private final FileObject templates;
-    private final FileObject projectFo;
+    private final File rootDir;
     private final Map<String, Object> params;
 
-    public GradleArchetype(FileObject templates, FileObject projectFo, Map<String, Object> params) {
+    public GradleArchetype(FileObject templates, File rootDir, Map<String, Object> params) {
         this.templates = templates;
-        this.projectFo = projectFo;
+        this.rootDir = rootDir;
         this.params = params;
     }
 
-    public final void copyTemplates() throws IOException {
+    public final void copyTemplates(TemplateOperation ops) {
         MapFormat mf = new MapFormat(params);
         mf.setLeftBrace("${"); // NOI18N
         mf.setRightBrace("}"); // NOI18N
+        List<File> projectDirs = new LinkedList<>();
+        projectDirs.add(rootDir);
+        
         Enumeration<? extends FileObject> en = templates.getChildren(true);
         while (en.hasMoreElements()) {
             FileObject template = en.nextElement();
-            if (!template.isData()) {
-                continue;
-            }
-            String relativeParent = FileUtil.getRelativePath(templates, template.getParent());
-            Object packageAttr = template.getAttribute("package"); // NOI18N
-            if (packageAttr instanceof String) {
-                String packageName = mf.format(packageAttr).replace('.', '/');
-                relativeParent += "/" + packageName;
-            }
-            FileObject destinationFolder = FileUtil.createFolder(projectFo, relativeParent);
+            String relativePath = FileUtil.getRelativePath(templates, template);
+            if (template.isFolder()) {
+                File dir = new File(rootDir, relativePath);
+                ops.createFolder(dir);
+                Object projectAttr = template.getAttribute("project"); // NOI18N
+                if (Boolean.TRUE == projectAttr) {
+                    projectDirs.add(dir);
+                }
+            } else if (template.isData()) {
+                Object packageAttr = template.getAttribute("package"); // NOI18N
+                if (packageAttr instanceof String) {
+                    String relativeParent = FileUtil.getRelativePath(templates, template.getParent());
+                    String packageName = mf.format(packageAttr);
+                    File sourceRoot = new File(rootDir, relativeParent);
+                    ops.createPackage(sourceRoot, packageName);
+                    File packageDir = new File(sourceRoot, packageName.replace('.', '/'));
 
-            FileObject previous = destinationFolder.getFileObject(template.getNameExt());
-            if (previous != null) {
-                previous.delete();
+                    Map<String, Object> pparams = new HashMap<>(params);
+                    pparams.put("package", packageName); //NOI18N
+                    copyDataTemplate(ops, template, new File(packageDir, template.getNameExt()), pparams);
+                } else {
+                    copyDataTemplate(ops, template, new File(rootDir, relativePath), params);
+                }
             }
+        }
+        for (File projectDir : projectDirs) {
+            ops.addProjectPreload(projectDir);
+        }
+    }
 
-            FileBuilder fb = new FileBuilder(template, destinationFolder);
-            fb.withParameters(params);
-            fb.defaultMode(Mode.COPY);
-
-            FileObject copied = fb.build().iterator().next();
-
-            assert copied != null && copied.getNameExt().equals(template.getNameExt()) : "Created " + copied;
+    private static void copyDataTemplate(TemplateOperation ops, FileObject template, File target, Map<String, Object> params) {
+        Object importantAttr = template.getAttribute("important");

Review comment:
       The `important` attribute enhances the API, but I am not sure if the previous API was documented anywhere.

##########
File path: java/gradle.htmlui/src/org/netbeans/modules/gradle/htmlui/GradleArchetype.java
##########
@@ -18,59 +18,74 @@
  */
 package org.netbeans.modules.gradle.htmlui;
 
-import java.io.IOException;
+import java.io.File;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
-import org.netbeans.api.project.ProjectManager;
-import org.netbeans.api.templates.FileBuilder;
-import org.netbeans.api.templates.FileBuilder.Mode;
+import org.netbeans.modules.gradle.spi.newproject.TemplateOperation;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
 import org.openide.util.MapFormat;
 
 public final class GradleArchetype {
     private final FileObject templates;
-    private final FileObject projectFo;
+    private final File rootDir;
     private final Map<String, Object> params;
 
-    public GradleArchetype(FileObject templates, FileObject projectFo, Map<String, Object> params) {
+    public GradleArchetype(FileObject templates, File rootDir, Map<String, Object> params) {
         this.templates = templates;
-        this.projectFo = projectFo;
+        this.rootDir = rootDir;
         this.params = params;
     }
 
-    public final void copyTemplates() throws IOException {
+    public final void copyTemplates(TemplateOperation ops) {
         MapFormat mf = new MapFormat(params);
         mf.setLeftBrace("${"); // NOI18N
         mf.setRightBrace("}"); // NOI18N
+        List<File> projectDirs = new LinkedList<>();
+        projectDirs.add(rootDir);
+        
         Enumeration<? extends FileObject> en = templates.getChildren(true);
         while (en.hasMoreElements()) {
             FileObject template = en.nextElement();
-            if (!template.isData()) {
-                continue;
-            }
-            String relativeParent = FileUtil.getRelativePath(templates, template.getParent());
-            Object packageAttr = template.getAttribute("package"); // NOI18N
-            if (packageAttr instanceof String) {
-                String packageName = mf.format(packageAttr).replace('.', '/');
-                relativeParent += "/" + packageName;
-            }
-            FileObject destinationFolder = FileUtil.createFolder(projectFo, relativeParent);
+            String relativePath = FileUtil.getRelativePath(templates, template);
+            if (template.isFolder()) {
+                File dir = new File(rootDir, relativePath);

Review comment:
       Relative path is using `/` as a directory separator. `File` is supposed to be using `File.separator`.




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists