This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 0114fc970c2115c494790c3c0913f83dc8ef6b53 Author: Otavio Rodolfo Piske <orpi...@users.noreply.github.com> AuthorDate: Thu Jul 29 18:03:57 2021 +0200 camel-jbang: adjusted to automatically name the generated Kamelet (#5884) --- .../modules/ROOT/pages/camel-jbang.adoc | 2 +- .../core/templates/VelocityTemplateParser.java | 29 ++++++++++++++-------- .../camel-jbang-main/dist/CamelJBang.java | 10 +++++--- .../src/main/jbang/main/CamelJBang.java | 10 +++++--- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc index 3e6a23d..ff57168 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc @@ -90,7 +90,7 @@ After you have filled the values, you can generate the Kamelet using: CamelJBang init kamelet --properties-path work/init-template.properties ``` -Running this command will create a file named `init-template.kamelet.yaml` in the `work` directory. +Running this command will create a new file in the `work` directory. The name of the generated file is determined by the `kameletMetadataName` property in the properties file. As such, parsing the default properties file would generate a file named `my-sample-sink.kamelet.yaml` in the directory. After the file is generated, it may still need to require final adjustments, such as correctly setting the name, the icon and other requirements for official Kamelets. Please consult the Kamelet development documentation for updated details. diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/templates/VelocityTemplateParser.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/templates/VelocityTemplateParser.java index d6004b9..e0cdf33 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/templates/VelocityTemplateParser.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/templates/VelocityTemplateParser.java @@ -25,6 +25,7 @@ import java.util.Properties; import org.apache.camel.CamelException; import org.apache.camel.dsl.jbang.core.api.TemplateParser; +import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceAlreadyExists; import org.apache.camel.util.ObjectHelper; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; @@ -35,15 +36,18 @@ import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.RuntimeConstants; public class VelocityTemplateParser implements TemplateParser { - private final File propertiesFile; + private final Properties properties = new Properties(); - public VelocityTemplateParser(File templateDir, String propertiesFile) { + public VelocityTemplateParser(File templateDir, String propertiesFile) throws IOException { this(templateDir, new File(propertiesFile)); } - public VelocityTemplateParser(File templateDir, File propertiesFile) { - this.propertiesFile = propertiesFile; + public VelocityTemplateParser(File templateDir, File propertiesFile) throws IOException { initializeTemplateEngine(templateDir); + + try (FileReader propertiesReader = new FileReader(propertiesFile)) { + properties.load(propertiesReader); + } } private void initializeTemplateEngine(File templateDir) { @@ -90,12 +94,6 @@ public class VelocityTemplateParser implements TemplateParser { } private void loadTemplateProperties(VelocityContext context) throws IOException { - Properties properties = new Properties(); - - try (FileReader propertiesReader = new FileReader(propertiesFile)) { - properties.load(propertiesReader); - } - properties.forEach((k, v) -> context.put(k.toString(), v)); overridePropertyList(context, properties, "kameletProperties"); @@ -104,4 +102,15 @@ public class VelocityTemplateParser implements TemplateParser { overridePropertyList(context, properties, "fromParameters"); overridePropertyList(context, properties, "toParameters"); } + + public File getOutputFile(File outputDir) throws ResourceAlreadyExists { + String outputFileName = properties.getProperty("kameletMetadataName") + ".kamelet.yaml"; + + File outputFile = new File(outputDir, outputFileName); + if (outputFile.exists()) { + throw new ResourceAlreadyExists(outputFile); + } + + return outputFile; + } } diff --git a/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java b/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java index 4c048ad..049a0f1 100755 --- a/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java +++ b/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java @@ -480,15 +480,19 @@ class InitKamelet extends AbstractInitKamelet implements Callable<Integer> { System.err.println(e.getMessage()); return 1; } - localTemplateFile.deleteOnExit(); VelocityTemplateParser templateParser = new VelocityTemplateParser( localTemplateFile.getParentFile(), processOptions.propertiesPath); - String outputFileName = localTemplateFile.getName().replace(".vm", ""); - File outputFile = new File(localTemplateFile.getParentFile(), outputFileName); + File outputFile; + try { + outputFile = templateParser.getOutputFile(workDirectory); + } catch (ResourceAlreadyExists e) { + System.err.println(e.getMessage()); + return 1; + } try (FileWriter fw = new FileWriter(outputFile)) { templateParser.parse(localTemplateFile.getName(), fw); diff --git a/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java b/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java index 09dbe3e..06bdd6d 100755 --- a/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java +++ b/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java @@ -480,15 +480,19 @@ class InitKamelet extends AbstractInitKamelet implements Callable<Integer> { System.err.println(e.getMessage()); return 1; } - localTemplateFile.deleteOnExit(); VelocityTemplateParser templateParser = new VelocityTemplateParser( localTemplateFile.getParentFile(), processOptions.propertiesPath); - String outputFileName = localTemplateFile.getName().replace(".vm", ""); - File outputFile = new File(localTemplateFile.getParentFile(), outputFileName); + File outputFile; + try { + outputFile = templateParser.getOutputFile(workDirectory); + } catch (ResourceAlreadyExists e) { + System.err.println(e.getMessage()); + return 1; + } try (FileWriter fw = new FileWriter(outputFile)) { templateParser.parse(localTemplateFile.getName(), fw);