You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/03/07 07:11:31 UTC
[camel] branch camel-3.x updated: CAMEL-19122: camel-jbang - Export java code with existing package name
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.x by this push:
new 4ed89beee88 CAMEL-19122: camel-jbang - Export java code with existing package name
4ed89beee88 is described below
commit 4ed89beee88c2f0511110f2a72e3034affc09f98
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Mar 7 08:09:35 2023 +0100
CAMEL-19122: camel-jbang - Export java code with existing package name
---
.../dsl/jbang/core/commands/ExportBaseCommand.java | 42 ++++++++++++++++++----
.../dsl/jbang/core/commands/ExportCamelMain.java | 11 +++---
.../dsl/jbang/core/commands/ExportQuarkus.java | 5 +--
.../dsl/jbang/core/commands/ExportSpringBoot.java | 5 +--
4 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
index 856b505707b..cdfc6d35113 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
@@ -18,6 +18,7 @@ package org.apache.camel.dsl.jbang.core.commands;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -27,11 +28,14 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeSet;
import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.camel.catalog.DefaultCamelCatalog;
@@ -57,6 +61,9 @@ abstract class ExportBaseCommand extends CamelCommand {
"camel.jbang.localKameletDir"
};
+ private static final Pattern PACKAGE_PATTERN = Pattern.compile(
+ "^\\s*package\\s+([a-zA-Z][.\\w]*)\\s*;.*$", Pattern.MULTILINE);
+
@CommandLine.Option(names = { "--profile" }, scope = CommandLine.ScopeType.INHERIT, defaultValue = "application",
description = "Profile to use, which refers to loading properties file with the given profile name. By default application.properties is loaded.")
protected String profile;
@@ -292,7 +299,7 @@ abstract class ExportBaseCommand extends CamelCommand {
}
protected void copySourceFiles(
- File settings, File profile, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir,
+ File settings, File profile, File srcJavaDirRoot, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir,
String packageName)
throws Exception {
// read the settings file and find the files to copy
@@ -324,13 +331,28 @@ abstract class ExportBaseCommand extends CamelCommand {
} else {
out = new File(target, source.getName());
}
- safeCopy(source, out, true);
- if (java) {
+ if (!java) {
+ safeCopy(source, out, true);
+ } else {
// need to append package name in java source file
- List<String> lines = Files.readAllLines(out.toPath());
- lines.add(0, "");
- lines.add(0, "package " + packageName + ";");
- FileOutputStream fos = new FileOutputStream(out);
+ List<String> lines = Files.readAllLines(source.toPath());
+ Optional<String> hasPackage = lines.stream().filter(l -> l.trim().startsWith("package ")).findFirst();
+ FileOutputStream fos;
+ if (hasPackage.isPresent()) {
+ String pn = determinePackageName(hasPackage.get());
+ if (pn != null) {
+ File dir = new File(srcJavaDirRoot, pn.replace('.', File.separatorChar));
+ dir.mkdirs();
+ out = new File(dir, source.getName());
+ fos = new FileOutputStream(out);
+ } else {
+ throw new IOException("Cannot determine package name from source: " + source);
+ }
+ } else {
+ fos = new FileOutputStream(out);
+ lines.add(0, "");
+ lines.add(0, "package " + packageName + ";");
+ }
for (String line : lines) {
adjustJavaSourceFileLine(line, fos);
fos.write(line.getBytes(StandardCharsets.UTF_8));
@@ -551,4 +573,10 @@ abstract class ExportBaseCommand extends CamelCommand {
Files.copy(source, target.toPath());
}
}
+
+ private static String determinePackageName(String content) {
+ final Matcher matcher = PACKAGE_PATTERN.matcher(content);
+ return matcher.find() ? matcher.group(1) : null;
+ }
+
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
index fe86d723193..d25e7945322 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
@@ -84,13 +84,14 @@ class ExportCamelMain extends Export {
// copy source files
String packageName = exportPackageName(ids[0], ids[1]);
- File srcJavaDir = new File(BUILD_DIR, "src/main/java/" + packageName.replace('.', '/'));
+ File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+ File srcJavaDir = new File(srcJavaDirRoot, packageName.replace('.', File.separatorChar));
srcJavaDir.mkdirs();
File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
srcResourcesDir.mkdirs();
File srcCamelResourcesDir = new File(BUILD_DIR, "src/main/resources/camel");
srcCamelResourcesDir.mkdirs();
- copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
+ copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
// copy from settings to profile
copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
if (!prop.containsKey("camel.main.basePackageScan") && !prop.containsKey("camel.main.base-package-scan")) {
@@ -234,10 +235,12 @@ class ExportCamelMain extends Export {
@Override
protected void copySourceFiles(
- File settings, File profile, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir, String packageName)
+ File settings, File profile, File srcJavaDirRoot, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir,
+ String packageName)
throws Exception {
- super.copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
+ super.copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir,
+ packageName);
// log4j configuration
InputStream is = ExportCamelMain.class.getResourceAsStream("/log4j2.properties");
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
index 923ddcee67f..0bc1edf443c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
@@ -89,13 +89,14 @@ class ExportQuarkus extends Export {
// copy source files
String packageName = exportPackageName(ids[0], ids[1]);
- File srcJavaDir = new File(BUILD_DIR, "src/main/java/" + packageName.replace('.', '/'));
+ File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+ File srcJavaDir = new File(srcJavaDirRoot, packageName.replace('.', File.separatorChar));
srcJavaDir.mkdirs();
File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
srcResourcesDir.mkdirs();
File srcCamelResourcesDir = new File(BUILD_DIR, "src/main/resources/camel");
srcCamelResourcesDir.mkdirs();
- copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
+ copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
// copy from settings to profile
copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
if (!hasModeline(settings)) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
index d91e804b140..955e8502f28 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
@@ -88,13 +88,14 @@ class ExportSpringBoot extends Export {
// copy source files
String packageName = exportPackageName(ids[0], ids[1]);
- File srcJavaDir = new File(BUILD_DIR, "src/main/java/" + packageName.replace('.', '/'));
+ File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+ File srcJavaDir = new File(srcJavaDirRoot, packageName.replace('.', File.separatorChar));
srcJavaDir.mkdirs();
File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
srcResourcesDir.mkdirs();
File srcCamelResourcesDir = new File(BUILD_DIR, "src/main/resources/camel");
srcCamelResourcesDir.mkdirs();
- copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
+ copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
// copy from settings to profile
copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
if (!hasModeline(settings)) {