tmysik commented on a change in pull request #2173:
URL: https://github.com/apache/netbeans/pull/2173#discussion_r437215346



##########
File path: 
php/php.composer/src/org/netbeans/modules/php/composer/files/ComposerJson.java
##########
@@ -75,6 +78,14 @@ public File getVendorDir() {
         return new File(getFile().getParentFile(), vendorDir);
     }
 
+    public Set<String> getScripts() {
+        Map<String, Object> scripts = composerJson.getContentValue(Map.class, 
FIELD_SCRIPTS);
+        if (scripts == null) {
+            return Collections.emptySet();
+        }
+        return scripts.keySet();

Review comment:
       Actually, not sure what Map implementation is used by the JSON parser so 
it could be linked hashMap. Could you pleae verify it? Thank you.
   

##########
File path: 
php/php.composer/src/org/netbeans/modules/php/composer/files/ComposerJson.java
##########
@@ -75,6 +78,14 @@ public File getVendorDir() {
         return new File(getFile().getParentFile(), vendorDir);
     }
 
+    public Set<String> getScripts() {
+        Map<String, Object> scripts = composerJson.getContentValue(Map.class, 
FIELD_SCRIPTS);
+        if (scripts == null) {
+            return Collections.emptySet();
+        }
+        return scripts.keySet();

Review comment:
       Aha, now I see that the sorting is in the action itself.
   

##########
File path: 
php/php.composer/src/org/netbeans/modules/php/composer/ui/actions/ComposerScriptsAction.java
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.php.composer.ui.actions;
+
+import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
+import org.netbeans.modules.php.api.phpmodule.PhpModule;
+import org.netbeans.modules.php.composer.commands.Composer;
+import org.netbeans.modules.php.composer.files.ComposerJson;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionRegistration;
+import org.openide.awt.Actions;
+import org.openide.awt.DynamicMenuContent;
+import org.openide.util.ContextAwareAction;
+import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+import org.openide.util.actions.Presenter;
+
+@ActionID(id = 
"org.netbeans.modules.php.composer.ui.actions.ComposerScriptsAction", category 
= "Project")
+@ActionRegistration(displayName = "#ComposerScriptsAction.name", lazy = false)
+@ActionReference(path = "Projects/org-netbeans-modules-php-project/Actions", 
position = 1051)
+@NbBundle.Messages("ComposerScriptsAction.name=Composer Scripts")
+public class ComposerScriptsAction extends AbstractAction implements 
ContextAwareAction, Presenter.Popup {
+
+    final Project project;
+    final List<String> scripts = new CopyOnWriteArrayList<>();
+
+    public ComposerScriptsAction() {
+        this(null, null);
+    }
+
+    public ComposerScriptsAction(Project project, Collection<String> scripts) {
+        this.project = project;
+        if (scripts != null) {
+            this.scripts.addAll(scripts);
+        }
+        setEnabled(project != null);
+        putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
+        // hide this action from Tools > Keymap
+        putValue(Action.NAME, ""); // NOI18N
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        assert false;
+    }
+
+    @Override
+    public Action createContextAwareInstance(Lookup context) {
+        Project contextProject = context.lookup(Project.class);
+        if (contextProject == null) {
+            return this;
+        }
+        PhpModule phpModule = PhpModule.Factory.lookupPhpModule(context);
+        ComposerJson composerJson = new 
ComposerJson(phpModule.getProjectDirectory());
+        if (composerJson == null) {
+            return this;
+        }
+        Set<String> allScripts = composerJson.getScripts();
+        if (allScripts.isEmpty()) {
+            return this;
+        }
+        List<String> scripts = new ArrayList<>(allScripts);
+        Collections.sort(scripts);
+        return new ComposerScriptsAction(contextProject, scripts);
+    }
+
+    @Override
+    public JMenuItem getPopupPresenter() {
+        if (project == null) {
+            return new Actions.MenuItem(this, false);
+        }
+        return createScriptsMenu();
+    }
+
+    private JMenuItem createScriptsMenu() {
+        assert project != null;
+        assert !scripts.isEmpty();
+        JMenu menu = new JMenu(Bundle.ComposerScriptsAction_name());
+        for (final String command : scripts) {
+            RunScriptAction scriptAction = new RunScriptAction(command);
+            menu.add(scriptAction);
+        }
+        return menu;
+    }
+
+    //~ Inner classes
+    private class RunScriptAction extends BaseComposerAction {

Review comment:
       Could this class be `static`?
   

##########
File path: 
php/php.composer/src/org/netbeans/modules/php/composer/files/ComposerJson.java
##########
@@ -75,6 +78,14 @@ public File getVendorDir() {
         return new File(getFile().getParentFile(), vendorDir);
     }
 
+    public Set<String> getScripts() {
+        Map<String, Object> scripts = composerJson.getContentValue(Map.class, 
FIELD_SCRIPTS);
+        if (scripts == null) {
+            return Collections.emptySet();
+        }
+        return scripts.keySet();

Review comment:
       This means that the order of the scripts is undefined - is this 
expected? I would likely expect (a) order as it is in the file or maybe better 
(b) alphabetical sort.
   

##########
File path: 
php/php.composer/src/org/netbeans/modules/php/composer/ui/actions/ComposerScriptsAction.java
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.php.composer.ui.actions;
+
+import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
+import org.netbeans.modules.php.api.phpmodule.PhpModule;
+import org.netbeans.modules.php.composer.commands.Composer;
+import org.netbeans.modules.php.composer.files.ComposerJson;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionRegistration;
+import org.openide.awt.Actions;
+import org.openide.awt.DynamicMenuContent;
+import org.openide.util.ContextAwareAction;
+import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+import org.openide.util.actions.Presenter;
+
+@ActionID(id = 
"org.netbeans.modules.php.composer.ui.actions.ComposerScriptsAction", category 
= "Project")
+@ActionRegistration(displayName = "#ComposerScriptsAction.name", lazy = false)
+@ActionReference(path = "Projects/org-netbeans-modules-php-project/Actions", 
position = 1051)
+@NbBundle.Messages("ComposerScriptsAction.name=Composer Scripts")
+public class ComposerScriptsAction extends AbstractAction implements 
ContextAwareAction, Presenter.Popup {
+
+    final Project project;
+    final List<String> scripts = new CopyOnWriteArrayList<>();
+
+    public ComposerScriptsAction() {
+        this(null, null);
+    }
+
+    public ComposerScriptsAction(Project project, Collection<String> scripts) {
+        this.project = project;
+        if (scripts != null) {
+            this.scripts.addAll(scripts);
+        }
+        setEnabled(project != null);
+        putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
+        // hide this action from Tools > Keymap
+        putValue(Action.NAME, ""); // NOI18N
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        assert false;
+    }
+
+    @Override
+    public Action createContextAwareInstance(Lookup context) {
+        Project contextProject = context.lookup(Project.class);
+        if (contextProject == null) {
+            return this;
+        }
+        PhpModule phpModule = PhpModule.Factory.lookupPhpModule(context);
+        ComposerJson composerJson = new 
ComposerJson(phpModule.getProjectDirectory());
+        if (composerJson == null) {
+            return this;
+        }
+        Set<String> allScripts = composerJson.getScripts();
+        if (allScripts.isEmpty()) {
+            return this;
+        }
+        List<String> scripts = new ArrayList<>(allScripts);
+        Collections.sort(scripts);
+        return new ComposerScriptsAction(contextProject, scripts);
+    }
+
+    @Override
+    public JMenuItem getPopupPresenter() {
+        if (project == null) {
+            return new Actions.MenuItem(this, false);
+        }
+        return createScriptsMenu();
+    }
+
+    private JMenuItem createScriptsMenu() {
+        assert project != null;
+        assert !scripts.isEmpty();
+        JMenu menu = new JMenu(Bundle.ComposerScriptsAction_name());
+        for (final String command : scripts) {
+            RunScriptAction scriptAction = new RunScriptAction(command);
+            menu.add(scriptAction);
+        }
+        return menu;
+    }
+
+    //~ Inner classes
+    private class RunScriptAction extends BaseComposerAction {
+
+        private String script;

Review comment:
       Could be `final` I think.
   

##########
File path: 
php/php.composer/src/org/netbeans/modules/php/composer/ui/actions/ComposerScriptsAction.java
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.php.composer.ui.actions;
+
+import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
+import org.netbeans.modules.php.api.phpmodule.PhpModule;
+import org.netbeans.modules.php.composer.commands.Composer;
+import org.netbeans.modules.php.composer.files.ComposerJson;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionRegistration;
+import org.openide.awt.Actions;
+import org.openide.awt.DynamicMenuContent;
+import org.openide.util.ContextAwareAction;
+import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+import org.openide.util.actions.Presenter;
+
+@ActionID(id = 
"org.netbeans.modules.php.composer.ui.actions.ComposerScriptsAction", category 
= "Project")
+@ActionRegistration(displayName = "#ComposerScriptsAction.name", lazy = false)
+@ActionReference(path = "Projects/org-netbeans-modules-php-project/Actions", 
position = 1051)
+@NbBundle.Messages("ComposerScriptsAction.name=Composer Scripts")
+public class ComposerScriptsAction extends AbstractAction implements 
ContextAwareAction, Presenter.Popup {
+
+    final Project project;

Review comment:
       Missing `@Nullable` annotation.
   




----------------------------------------------------------------
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:
us...@infra.apache.org



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

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

Reply via email to