This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new f8f1498294a CAMEL-19349: camel-jbang - Dev console
f8f1498294a is described below

commit f8f1498294afe969e5d37c8f645f7aa9c8c6a549
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon May 15 09:16:39 2023 +0200

    CAMEL-19349: camel-jbang - Dev console
---
 .../apache/camel/impl/console/ConsoleHelper.java   | 48 +++++++++------
 .../camel/jbang/console/SourceDirDevConsole.java   | 68 +++++++++++++++++++++-
 2 files changed, 96 insertions(+), 20 deletions(-)

diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
index 7e1ece0378f..0a6efe362aa 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/ConsoleHelper.java
@@ -17,7 +17,9 @@
 package org.apache.camel.impl.console;
 
 import java.io.LineNumberReader;
+import java.io.Reader;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.camel.CamelContext;
@@ -40,33 +42,43 @@ public final class ConsoleHelper {
             return null;
         }
         Integer lineNumber = extractSourceLocationLineNumber(location);
-        List<JsonObject> code = new ArrayList<>();
         try {
             location = LoggerHelper.stripSourceLocationLineNumber(location);
             Resource resource = 
PluginHelper.getResourceLoader(camelContext).resolveResource(location);
             if (resource != null) {
-                LineNumberReader reader = new 
LineNumberReader(resource.getReader());
-                int i = 0;
-                String t;
-                do {
-                    t = reader.readLine();
-                    if (t != null) {
-                        i++;
-                        JsonObject c = new JsonObject();
-                        c.put("line", i);
-                        c.put("code", Jsoner.escape(t));
-                        if (lineNumber != null && lineNumber == i) {
-                            c.put("match", true);
-                        }
-                        code.add(c);
-                    }
-                } while (t != null);
-                IOHelper.close(reader);
+                return loadSourceAsJson(resource.getReader(), lineNumber);
             }
         } catch (Exception e) {
             // ignore
         }
 
+        return Collections.EMPTY_LIST;
+    }
+
+    public static List<JsonObject> loadSourceAsJson(Reader reader, Integer 
lineNumber) {
+        List<JsonObject> code = new ArrayList<>();
+        try {
+            LineNumberReader lnr = new LineNumberReader(reader);
+            int i = 0;
+            String t;
+            do {
+                t = lnr.readLine();
+                if (t != null) {
+                    i++;
+                    JsonObject c = new JsonObject();
+                    c.put("line", i);
+                    c.put("code", Jsoner.escape(t));
+                    if (lineNumber != null && lineNumber == i) {
+                        c.put("match", true);
+                    }
+                    code.add(c);
+                }
+            } while (t != null);
+            IOHelper.close(lnr);
+        } catch (Exception e) {
+            // ignore
+        }
+
         return code.isEmpty() ? null : code;
     }
 
diff --git 
a/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
 
b/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
index 8b6d9524f44..87e2bf48ba3 100644
--- 
a/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
+++ 
b/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
@@ -17,12 +17,20 @@
 package org.apache.camel.jbang.console;
 
 import java.io.File;
+import java.io.FileReader;
+import java.io.LineNumberReader;
 import java.util.Arrays;
+import java.util.List;
 import java.util.Map;
 
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.console.ConsoleHelper;
 import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.PatternHelper;
 import org.apache.camel.support.RouteOnDemandReloadStrategy;
 import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.json.JsonArray;
 import org.apache.camel.util.json.JsonObject;
@@ -30,12 +38,21 @@ import org.apache.camel.util.json.JsonObject;
 @DevConsole("source-dir")
 public class SourceDirDevConsole extends AbstractDevConsole {
 
+    /**
+     * Whether to show the source in the output
+     */
+    public static final String SOURCE = "source";
+
     public SourceDirDevConsole() {
         super("camel", "source-dir", "Source Directory", "Information about 
Camel JBang source files");
     }
 
     @Override
     protected String doCallText(Map<String, Object> options) {
+        String path = (String) options.get(Exchange.HTTP_PATH);
+        String subPath = path != null ? StringHelper.after(path, "/") : null;
+        String source = (String) options.get(SOURCE);
+
         final StringBuilder sb = new StringBuilder();
 
         RouteOnDemandReloadStrategy reload = 
getCamelContext().hasService(RouteOnDemandReloadStrategy.class);
@@ -51,11 +68,39 @@ public class SourceDirDevConsole extends AbstractDevConsole 
{
                     Arrays.sort(files, (o1, o2) -> 
o1.getName().compareToIgnoreCase(o2.getName()));
                     for (File f : files) {
                         boolean skip = f.getName().startsWith(".") || 
f.isHidden();
-                        if (!skip) {
+                        if (skip) {
+                            continue;
+                        }
+                        boolean match = subPath == null || 
f.getName().startsWith(subPath) || f.getName().endsWith(subPath)
+                                || PatternHelper.matchPattern(f.getName(), 
subPath);
+                        if (match) {
                             long size = f.length();
                             long ts = f.lastModified();
                             String age = ts > 0 ? TimeUtils.printSince(ts) : 
"n/a";
                             sb.append(String.format("    %s (size: %d age: 
%s)%n", f.getName(), size, age));
+                            if ("true".equals(source)) {
+                                StringBuilder code = new StringBuilder();
+                                try {
+                                    LineNumberReader reader = new 
LineNumberReader(new FileReader(f));
+                                    int i = 0;
+                                    String t;
+                                    do {
+                                        t = reader.readLine();
+                                        if (t != null) {
+                                            i++;
+                                            code.append(String.format("\n    
#%s %s", i, t));
+                                        }
+                                    } while (t != null);
+                                    IOHelper.close(reader);
+                                } catch (Exception e) {
+                                    // ignore
+                                }
+                                if (code.length() > 0) {
+                                    sb.append("    ").append("-".repeat(40));
+                                    sb.append(code);
+                                    sb.append("\n\n");
+                                }
+                            }
                         }
                     }
                 }
@@ -67,6 +112,10 @@ public class SourceDirDevConsole extends AbstractDevConsole 
{
 
     @Override
     protected Map<String, Object> doCallJson(Map<String, Object> options) {
+        String path = (String) options.get(Exchange.HTTP_PATH);
+        String subPath = path != null ? StringHelper.after(path, "/") : null;
+        String source = (String) options.get(SOURCE);
+
         JsonObject root = new JsonObject();
 
         RouteOnDemandReloadStrategy reload = 
getCamelContext().hasService(RouteOnDemandReloadStrategy.class);
@@ -83,11 +132,26 @@ public class SourceDirDevConsole extends 
AbstractDevConsole {
                     root.put("files", arr);
                     for (File f : files) {
                         boolean skip = f.getName().startsWith(".") || 
f.isHidden();
-                        if (!skip) {
+                        if (skip) {
+                            continue;
+                        }
+                        boolean match = subPath == null || 
f.getName().startsWith(subPath) || f.getName().endsWith(subPath)
+                                || PatternHelper.matchPattern(f.getName(), 
subPath);
+                        if (match) {
                             JsonObject jo = new JsonObject();
                             jo.put("name", f.getName());
                             jo.put("size", f.length());
                             jo.put("lastModified", f.lastModified());
+                            if ("true".equals(source)) {
+                                try {
+                                    List<JsonObject> code = 
ConsoleHelper.loadSourceAsJson(new FileReader(f), null);
+                                    if (code != null) {
+                                        jo.put("code", code);
+                                    }
+                                } catch (Exception e) {
+                                    // ignore
+                                }
+                            }
                             arr.add(jo);
                         }
                     }

Reply via email to