davsclaus commented on code in PR #26623:
URL: https://github.com/apache/camel/pull/26623#discussion_r4056631048


##########
dsl/camel-kamelet-main/src/main/known-third-party-libraries.properties:
##########
@@ -163,6 +163,7 @@ org.xmlunit = org.xmlunit:xmlunit-core:${xmlunit-version}
 # Commons and utilities
 org.apache.commons.lang3 = 
org.apache.commons:commons-lang3:${commons-lang3-version}
 org.apache.commons.io = commons-io:commons-io:${commons-io-version}
+org.apache.commons.validator = 
commons-validator:commons-validator:${commons-validator-version}

Review Comment:
   The property exists: parent/pom.xml line 137, 
`<commons-validator-version>1.11.0</commons-validator-version>`, on main since 
well before this branch. The prepare-kamelet-main goal resolves it: the 
generated camel-thirdparty-known-dependencies.properties in this branch's build 
carries `org.apache.commons.validator = 
commons-validator:commons-validator:1.11.0`, and the groovy example of 
camel-jbang-examples runs with its dependency line removed (the CLI downloads 
1.11.0 when the expression is compiled). No change needed.



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/GroovyImportChecks.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.apache.camel.dsl.jbang.core.commands.ai;
+
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static 
org.apache.camel.dsl.jbang.core.commands.ai.YamlLines.countLeadingSpaces;
+
+/**
+ * Checks on the imports of the Groovy expressions of a YAML route 
(CAMEL-24843). A Groovy script compiles against the
+ * application's classpath: an import of a library the application does not 
declare fails at runtime with "unable to
+ * resolve class", which names the class and nothing else. The check names the 
library when it is a known one and says
+ * how to declare it for the runtime; with the Camel CLI a known library is 
downloaded when the script is compiled, so
+ * only an unknown one is reported there.
+ */
+public final class GroovyImportChecks {
+
+    private static final Pattern IMPORT
+            = 
Pattern.compile("^\\s*import\\s+(static\\s+)?([a-zA-Z][.\\w]*(?:\\.\\*)?)\\s*;?\\s*$");
+    /** Packages every application has: the JDK, Groovy and Camel itself. */
+    private static final List<String> ALWAYS_PRESENT
+            = List.of("java.", "javax.", "jakarta.", "groovy.", 
"org.codehaus.groovy.", "org.apache.camel.");
+
+    private GroovyImportChecks() {
+    }
+
+    /**
+     * @param content      the YAML route file
+     * @param runtime      null or "jbang" for the Camel CLI (a known library 
is downloaded when the script is
+     *                     compiled), "main", "spring-boot" or "quarkus" for a 
Maven project (the library must be
+     *                     declared in the pom)
+     * @param projectTypes the classes the project declares itself (simple 
name to fully qualified name), from the Java
+     *                     and Groovy files next to the route
+     */
+    public static List<String> validateYamlGroovyImports(String content, 
String runtime, Map<String, String> projectTypes) {
+        List<String> errors = new ArrayList<>();
+        if (content == null || content.isBlank()) {
+            return errors;
+        }
+        boolean maven = runtime != null && !runtime.isBlank() && 
!"jbang".equalsIgnoreCase(runtime);
+        Set<String> projectClasses = new LinkedHashSet<>(projectTypes != null 
? projectTypes.values() : Set.of());
+        String[] lines = content.split("\n", -1);
+        for (int i = 0; i < lines.length; i++) {
+            String trimmed = lines[i].trim();
+            String key = trimmed.startsWith("- ") ? trimmed.substring(2) : 
trimmed;
+            if (!key.startsWith("groovy:")) {
+                continue;
+            }
+            // the script: a block scalar under groovy: |, the lines under 
groovy: / expression: |, or the inline value
+            int indent = countLeadingSpaces(lines[i]);
+            List<int[]> scriptLines = new ArrayList<>();
+            String value = key.substring("groovy:".length()).trim();
+            if (value.isEmpty() || value.startsWith("|") || 
value.startsWith(">")) {
+                for (int j = i + 1; j < lines.length; j++) {
+                    if (lines[j].isBlank()) {
+                        continue;
+                    }
+                    if (countLeadingSpaces(lines[j]) <= indent) {
+                        break;
+                    }
+                    scriptLines.add(new int[] { j });
+                }
+            } else {
+                scriptLines.add(new int[] { i });
+            }
+            for (int[] sl : scriptLines) {
+                String line = lines[sl[0]].trim();
+                if (line.startsWith("expression:")) {

Review Comment:
   It is a real case, the canonical form: `groovy:` on its own line with the 
script under `expression: |`, which is what `camel validate normalize` writes. 
The collector takes every line more indented than the `groovy:` key, so the 
`expression: |` line comes with the script lines and is not one of them. 
Commented, and covered by `canonicalFormWithExpressionBlock` in 
GroovyImportChecksTest (commit 77ebadb76447).



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/GroovyImportChecks.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.apache.camel.dsl.jbang.core.commands.ai;
+
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static 
org.apache.camel.dsl.jbang.core.commands.ai.YamlLines.countLeadingSpaces;
+
+/**
+ * Checks on the imports of the Groovy expressions of a YAML route 
(CAMEL-24843). A Groovy script compiles against the
+ * application's classpath: an import of a library the application does not 
declare fails at runtime with "unable to
+ * resolve class", which names the class and nothing else. The check names the 
library when it is a known one and says
+ * how to declare it for the runtime; with the Camel CLI a known library is 
downloaded when the script is compiled, so
+ * only an unknown one is reported there.
+ */
+public final class GroovyImportChecks {
+
+    private static final Pattern IMPORT
+            = 
Pattern.compile("^\\s*import\\s+(static\\s+)?([a-zA-Z][.\\w]*(?:\\.\\*)?)\\s*;?\\s*$");
+    /** Packages every application has: the JDK, Groovy and Camel itself. */
+    private static final List<String> ALWAYS_PRESENT
+            = List.of("java.", "javax.", "jakarta.", "groovy.", 
"org.codehaus.groovy.", "org.apache.camel.");
+
+    private GroovyImportChecks() {
+    }
+
+    /**
+     * @param content      the YAML route file
+     * @param runtime      null or "jbang" for the Camel CLI (a known library 
is downloaded when the script is
+     *                     compiled), "main", "spring-boot" or "quarkus" for a 
Maven project (the library must be
+     *                     declared in the pom)
+     * @param projectTypes the classes the project declares itself (simple 
name to fully qualified name), from the Java
+     *                     and Groovy files next to the route
+     */
+    public static List<String> validateYamlGroovyImports(String content, 
String runtime, Map<String, String> projectTypes) {

Review Comment:
   Added (commit 77ebadb76447).



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

Reply via email to