apupier commented on code in PR #13134: URL: https://github.com/apache/camel/pull/13134#discussion_r1491109580
########## dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java: ########## @@ -16,146 +16,89 @@ */ package org.apache.camel.dsl.jbang.core.commands; -import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import java.util.Properties; +import java.util.Set; +import java.util.TreeSet; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import org.apache.camel.dsl.jbang.core.common.CommandLineHelper; -import org.apache.camel.dsl.jbang.core.common.RuntimeUtil; -import org.apache.camel.dsl.jbang.core.common.XmlHelper; +import org.apache.camel.dsl.jbang.core.common.MavenGavComparator; +import org.apache.camel.main.KameletMain; +import org.apache.camel.main.download.DownloadListener; import org.apache.camel.tooling.maven.MavenGav; -import org.apache.camel.util.CamelCaseOrderedProperties; -import org.apache.camel.util.FileUtil; import picocli.CommandLine; -@CommandLine.Command(name = "list", - description = "Displays all Camel dependencies required to run") -public class DependencyList extends Export { +@CommandLine.Command(name = "list", description = "Displays all Camel dependencies required to run") +public class DependencyList extends CamelCommand { + + // ignored list of dependencies, can be either groupId or artifactId + private static final String[] SKIP_DEPS = new String[] { + "org.fusesource.jansi", "org.apache.logging.log4j", "camel-core-languages", "camel-endpointdsl", + "camel-java-joor-dsl" }; - protected static final String EXPORT_DIR = CommandLineHelper.CAMEL_JBANG_WORK_DIR + "/export"; + @CommandLine.Parameters(description = "The Camel file(s) to inspect for dependencies.", arity = "0..9", + paramLabel = "<files>") + protected Path[] filePaths; @CommandLine.Option(names = { "--output" }, description = "Output format (gav, maven, jbang)", defaultValue = "gav") protected String output; + @CommandLine.Option(names = { "-v" }, description = "Print additional verbose output.") + protected boolean verbose = false; + + private Set<MavenGav> dependencies; + public DependencyList(CamelJBangMain main) { super(main); + Arrays.sort(SKIP_DEPS); } @Override public Integer doCall() throws Exception { - this.quiet = true; // be quiet and generate from fresh data to ensure the output is up-to-date - return super.doCall(); - } - - @Override - protected Integer export() throws Exception { if (!"gav".equals(output) && !"maven".equals(output) && !"jbang".equals(output)) { - System.err.println("--output must be either gav or maven, was: " + output); + System.err.println("--output must be either gav, maven or jbang, was: " + output); return 1; } + calculate(); + return 0; + } - Integer answer = doExport(); - if (answer == 0) { - // read pom.xml - File pom = new File(EXPORT_DIR, "pom.xml"); - if (pom.exists()) { - DocumentBuilderFactory dbf = XmlHelper.createDocumentBuilderFactory(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document dom = db.parse(pom); - NodeList nl = dom.getElementsByTagName("dependency"); - List<MavenGav> gavs = new ArrayList<>(); - String camelVersion = null; - String springBootVersion = null; - String quarkusVersion = null; - for (int i = 0; i < nl.getLength(); i++) { - Element node = (Element) nl.item(i); - String g = node.getElementsByTagName("groupId").item(0).getTextContent(); - String a = node.getElementsByTagName("artifactId").item(0).getTextContent(); - String v = null; - NodeList vl = node.getElementsByTagName("version"); - if (vl.getLength() > 0) { - v = vl.item(0).getTextContent(); - } - - // BOMs - if ("org.apache.camel".equals(g) && "camel-bom".equals(a)) { - camelVersion = v; - continue; - } - if ("org.apache.camel.springboot".equals(g) && "camel-spring-boot-bom".equals(a)) { - camelVersion = v; - continue; - } - if ("org.springframework.boot".equals(g) && "spring-boot-dependencies".equals(a)) { - springBootVersion = v; - continue; - } - if (("${quarkus.platform.group-id}".equals(g) || "io.quarkus.platform".equals(g)) && - ("${quarkus.platform.artifact-id}".equals(a) || "quarkus-bom".equals(a))) { - if ("${quarkus.platform.version}".equals(v)) { - quarkusVersion = dom.getElementsByTagName("quarkus.platform.version").item(0).getTextContent(); - } else { - quarkusVersion = v; - } - continue; - } - - // scope - String scope = null; - NodeList sl = node.getElementsByTagName("scope"); - if (sl.getLength() > 0) { - scope = sl.item(0).getTextContent(); - } - if ("test".equals(scope) || "import".equals(scope)) { - // skip test/BOM import scopes - continue; - } - - // version - if (v == null && g.equals("org.apache.camel")) { - v = camelVersion; - } - if (v == null && g.equals("org.apache.camel.springboot")) { - v = camelVersion; - } - if (v == null && g.equals("org.springframework.boot")) { - v = springBootVersion; - } - if (v == null && (g.equals("io.quarkus") || g.equals("org.apache.camel.quarkus"))) { - v = quarkusVersion; - } - - if (skipArtifact(g, a, v)) { - continue; - } - if (v != null) { - gavs.add(MavenGav.parseGav(g + ":" + a + ":" + v)); - } else { - gavs.add(MavenGav.parseGav(g + ":" + a)); - } - } - // sort GAVs - gavs.sort(mavenGavComparator()); - int i = 0; - int total = gavs.size(); - for (MavenGav gav : gavs) { - outputGav(gav, i, total); - i++; - } - } - // cleanup dir after complete - File buildDir = new File(EXPORT_DIR); - FileUtil.removeDir(buildDir); + public void calculate() throws Exception { + List<String> routeFiles = new ArrayList<>(); + for (Path filePath : filePaths) { + routeFiles.add("file://" + filePath.toAbsolutePath()); + } + final KameletMain main = new KameletMain(); + main.setDownload(false); + main.setFresh(false); + RunDownloadListener downloadListener = new RunDownloadListener(); + main.setDownloadListener(downloadListener); + main.setSilent(!verbose); + // enable stub in silent mode so we do not use real components + main.setStubPattern("*"); + // do not run for very long in silent run + main.addInitialProperty("camel.main.autoStartup", "false"); + main.addInitialProperty("camel.main.durationMaxSeconds", "1"); + main.addInitialProperty("camel.jbang.verbose", Boolean.toString(verbose)); + main.addInitialProperty("camel.jbang.ignoreLoadingError", Boolean.toString(!verbose)); Review Comment: I fear it will break `dependency copy `on Camel routes which are in-progress. Which is a major use case when using in IDE integration (for reference https://github.com/camel-tooling/vscode-camelk/blob/b625bd537a52ca603edb0a92173dcd2292e48a37/src/JavaDependenciesManager.ts#L52 ) -- 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: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org