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 2022/08/15 09:44:45 UTC

[GitHub] [netbeans] jtulach commented on a diff in pull request #4495: Support for project output artifacts

jtulach commented on code in PR #4495:
URL: https://github.com/apache/netbeans/pull/4495#discussion_r945562704


##########
enterprise/micronaut/nbproject/project.xml:
##########
@@ -68,6 +68,32 @@
                         <specification-version>1.18</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.modules.maven</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>2</release-version>
+                        <specification-version>2.154</specification-version>
+                    </run-dependency>
+                </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.modules.project.dependency</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <implementation-version/>
+                    </run-dependency>
+                </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.modules.maven.embedder</code-name-base>

Review Comment:
   These modules don't provide real API. Depending on them from foreign "micronaut" module is strange. Also adding a dependency on Maven support, when "micronaut" supports both, Maven and Gradle is strange.



##########
ide/project.dependency/src/org/netbeans/modules/project/dependency/spi/ProjectArtifactsImplementation.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.project.dependency.spi;
+
+import java.util.Collection;
+import java.util.List;
+import javax.swing.event.ChangeListener;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.project.dependency.ArtifactSpec;
+import org.netbeans.modules.project.dependency.ProjectArtifactsQuery;
+
+/**
+ *
+ * @author sdedic
+ */
+public interface ProjectArtifactsImplementation {
+    /**
+     * Finds project artifact(s) that match the query. 
+     * @param query the query to answer
+     * @param previous result of previous buildsteps
+     * @param all
+     * @return 
+     */
+    public Result findArtifacts(ProjectArtifactsQuery.Filter query);

Review Comment:
   I prefer [singletonizer](https://bits.netbeans.org/14/javadoc/org-netbeans-api-java-classpath/org/netbeans/spi/java/queries/BinaryForSourceQueryImplementation2.html) SPI over `ProjectArtifactsImplementation` and `Result` inner class.



##########
java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/commands/ProjectMetadataCommand.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.nbcode.integration.commands;
+
+import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldAttributes;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonPrimitive;
+import com.google.gson.TypeAdapter;
+import com.google.gson.TypeAdapterFactory;
+import com.google.gson.reflect.TypeToken;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonToken;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import org.eclipse.lsp4j.CodeAction;
+import org.eclipse.lsp4j.CodeActionParams;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectActionContext;
+import org.netbeans.modules.java.lsp.server.protocol.CodeActionsProvider;
+import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
+import org.netbeans.modules.parsing.api.ResultIterator;
+import org.netbeans.modules.project.dependency.ArtifactSpec;
+import org.netbeans.modules.project.dependency.ProjectArtifactsQuery;
+import org.netbeans.modules.project.dependency.ProjectArtifactsQuery.ArtifactsResult;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.RequestProcessor;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ *
+ * @author sdedic
+ */
+@ServiceProvider(service = CodeActionsProvider.class)
+public class ProjectMetadataCommand extends CodeActionsProvider {
+    private static final String COMMAND_ARTIFACTS = "nbls.gcn.project.artifacts"; // NOI18N
+
+    private static final Set<String> COMMANDS = new HashSet<>(Arrays.asList(
+            COMMAND_ARTIFACTS
+    ));
+    private static final Set<String> ARTIFACT_BLOCK_FIELDS = new HashSet<>(Arrays.asList(
+        "data" // NOI18N
+    ));
+    
+    private static final RequestProcessor METADATA_PROCESSOR = new RequestProcessor(ProjectMetadataCommand.class);
+    
+    private final Gson gson;
+    
+    public ProjectMetadataCommand() {
+        gson = new GsonBuilder()
+             // block the opaque 'data' field 
+            .addSerializationExclusionStrategy(new ExclusionStrategy() {
+                    @Override
+                    public boolean shouldSkipField(FieldAttributes fa) {
+                        if (fa.getDeclaringClass() == ArtifactSpec.class) {
+                          return ARTIFACT_BLOCK_FIELDS.contains(fa.getName());
+                        }
+                        return false;
+                    }
+
+                    @Override
+                    public boolean shouldSkipClass(Class<?> type) {
+                        return false;
+                    }
+            })
+            // serialize FileObject as null|path
+            .registerTypeAdapterFactory(new TypeAdapterFactory() {
+                @Override
+                public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> tt) {
+                    if (tt.getRawType() != FileObject.class) {
+                        return null;
+                    }
+                    return new TypeAdapter<T>() {
+                        @Override
+                        public void write(JsonWriter writer, T t) throws IOException {
+                            FileObject f = (FileObject)t;
+                            writer.value(f == null ? null : f.getPath());
+                        }
+
+                        @Override
+                        public T read(JsonReader reader) throws IOException {
+                            if (reader.peek() == JsonToken.NULL) {
+                                reader.nextNull();
+                                return null;
+                            } else {
+                                String s = reader.nextString();
+                                if (s == null) {
+                                    return null;
+                                }
+                                FileObject fo = FileUtil.toFileObject(Paths.get(s).toFile());
+                                return (T)fo;
+                            }
+                        }
+                    };
+                }
+            })
+            .create();
+    }
+    
+    @Override
+    public List<CodeAction> getCodeActions(ResultIterator resultIterator, CodeActionParams params) throws Exception {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public Set<String> getCommands() {
+        return COMMANDS;
+    }
+
+    @Override
+    public CompletableFuture<Object> processCommand(NbCodeLanguageClient client, String command, List<Object> arguments) {
+        if (arguments.size() < 1) {
+            throw new IllegalArgumentException("Expected at least project URI/path");
+        }
+        FileObject f = Utils.extractFileObject(arguments.get(0), gson);
+        Project p = FileOwnerQuery.getOwner(f);
+        if (p == null || p.getProjectDirectory() != f) {
+            throw new IllegalArgumentException("Not a project " + f);
+        }
+        String artifactType = null;
+        ProjectActionContext ctx = null;
+        
+        if (arguments.size() > 1) {
+            // 2nd parameter is the project action
+            Object o = arguments.get(1);
+            if (!(o instanceof JsonPrimitive)) {
+                throw new IllegalArgumentException("String or null expected as parameter #3, got " + o);
+            }
+            ctx = ProjectActionContext.newBuilder(p).forProjectAction(((JsonPrimitive)o).getAsString()).context();
+        }
+        if (arguments.size() > 2) {
+            // 3rd parameter is the type of artifact
+            Object o = arguments.get(2);
+            if (!(o instanceof JsonPrimitive)) {
+                throw new IllegalArgumentException("String or null expected as parameter #2, got " + o);
+            }
+            artifactType = ((JsonPrimitive)o).getAsString();
+        }
+        ProjectArtifactsQuery.Filter filter = ProjectArtifactsQuery.newQuery(artifactType, null, ctx);
+        CompletableFuture result = new CompletableFuture();
+        METADATA_PROCESSOR.post(() -> {
+            try {
+                ArtifactsResult arts = ProjectArtifactsQuery.findArtifacts(p, filter);

Review Comment:
   All the changes _just_ to get name of artifact to VSCode TypeScript side...



##########
java/maven/src/org/netbeans/modules/maven/api/NbMavenProject.java:
##########
@@ -286,6 +287,22 @@ public void removePropertyChangeListener(PropertyChangeListener propertyChangeLi
         return project.getOriginalMavenProject();
     }
     
+    /**
+     * Returns a project evaluated for a certain purpose. The while {@link #getMavenProject}
+     * works with the <b>active configuration</b> and does not apply any action-specific properties,
+     * this method tries to apply mappings, configurations, etc when loading the project model.
+     * <p>
+     * Note that loading an evaluated project may take significant time (comparable to loading
+     * the base project itself). The implementation might optimize if the passed context does not
+     * prescribe different profiles, properties etc than have been used for the default model.
+     * 
+     * @param context the loading context
+     * @return evaluated project

Review Comment:
   Missing versioning, I assume.



##########
java/java.lsp.server/nbcode/integration/nbproject/project.xml:
##########
@@ -214,6 +214,14 @@
                         <specification-version>9.24</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.modules.project.dependency</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <implementation-version/>

Review Comment:
   Implementation dependency!?



##########
java/maven.embedder/nbproject/project.xml:
##########
@@ -182,6 +182,7 @@
                 <friend>io.github.jeddict.runtime</friend>
                 <friend>io.github.jeddict.rest.generator</friend>
                 <!-- XXX <subpackages> not permitted by schema -->
+                <friend>org.netbeans.modules.micronaut</friend>

Review Comment:
   I don't like this. `micronaut` module shall not have direct dependency on Maven (embedder).



##########
enterprise/micronaut/src/org/netbeans/modules/micronaut/maven/BuildActions.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.micronaut.maven;
+
+import java.util.Arrays;
+import javax.swing.Action;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.maven.api.NbMavenProject;
+import org.netbeans.spi.project.ActionProvider;
+import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
+import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionRegistration;
+import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author sdedic
+ */
+public class BuildActions {
+    
+    @ActionID(id = "org.netbeans.modules.micronaut.maven.nativeBuild", category = "Project")
+    @ActionRegistration(displayName = "#LBL_NativeBuild", lazy=false)
+    @ActionReference(position = 520, path = "Projects/org-netbeans-modules-maven/Actions")
+    @NbBundle.Messages({
+        "LBL_NativeBuild=Build Native Executable"
+    })
+    public static Action    createNativeBuild() {
+        return ProjectSensitiveActions.projectSensitiveAction(new NativeBuildPerformer(), Bundle.LBL_NativeBuild(), null);
+    }
+    
+    private static class NativeBuildPerformer implements ProjectActionPerformer {
+        @Override
+        public boolean enable(Project project) {
+            NbMavenProject nbmp = project.getLookup().lookup(NbMavenProject.class);
+            if (nbmp == null) {
+                return false;
+            }
+            ActionProvider ap = project.getLookup().lookup(ActionProvider.class);
+            return 
+                    nbmp.getMavenProject().getPlugin("io.micronaut.build:micronaut-maven-plugin") != null &&

Review Comment:
   I was hoping this (presence of some plugin) could be specified declaratively. Isn't there already a support for a plugin to trigger some configuration and configurations adding some actions?



##########
ide/project.dependency/src/org/netbeans/modules/project/dependency/spi/ProjectDependenciesImplementation.java:
##########
@@ -32,16 +29,9 @@
  * @author sdedic
  */
 public interface ProjectDependenciesImplementation {

Review Comment:
   Removing methods from `spi` package? Isn't that an API change?



##########
ide/projectapi/src/org/netbeans/api/project/ProjectUtils.java:
##########
@@ -57,6 +61,65 @@ private ProjectUtils() {}
 
     private static final Logger LOG = Logger.getLogger(ProjectUtils.class.getName());
     
+    /**
+     * Returns the active configuration for the given project. Returns {@code null},
+     * if the project does not support configurations at all.
+     * 
+     * @param p the project
+     * @return the active configuration, or {@code null} if configurations are not supported.
+     * @since 1.89
+     */
+    public ProjectConfiguration getActiveConfiguration(@NonNull Project p) {
+        ProjectConfigurationProvider pcp = p.getLookup().lookup(ProjectConfigurationProvider.class);
+        if (pcp == null) {
+            return null;
+        }
+        return ProjectManager.mutex().readAccess(() -> pcp.getActiveConfiguration());
+    }
+    
+    /**
+     * Sets the active configuration to a project. The configuration should have been previously obtained by 
+     * {@link #getActiveConfiguration(org.netbeans.api.project.Project)} from the same project. The method
+     * returns {@code false}, if the configuration could not be set: if the project does not support configurations
+     * at all, or the project failed to switch the configurations. Since the active configuration setting is persisted,
+     * the method throws {@link IOException} if the setting save fails.
+     * 
+     * @param <C> configuration type
+     * @param p the project
+     * @param cfg configuration or {@code null} for default configuration.
+     * @return true, if the configuration was successfully set.
+     * @throws IOException when the selected configuration cannot be persisted.
+     * @since 1.89
+     */
+    public <C extends ProjectConfiguration> boolean setActiveConfiguration(@NonNull Project p, @NonNull C cfg) throws IOException {

Review Comment:
   This would type the same without generics.



-- 
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: notifications-unsubscribe@netbeans.apache.org

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