Skip to content

Commit

Permalink
CAMEL-19120: export configurable template
Browse files Browse the repository at this point in the history
  • Loading branch information
Croway committed Mar 6, 2023
1 parent ce1fb37 commit d9af1db
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.catalog;

import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -563,4 +564,13 @@ default BaseModel<?> model(Kind kind, String name) {
*/
ArtifactModel<?> modelFromMavenGAV(String groupId, String artifactId, String version);

/**
* Load resource from catalog classpath
*
* @param kind The resource kind, ex. camel-jbang
* @param name The resource name
* @return An input stream for reading the resource; null if the resource could not be found
*/
InputStream loadResource(String kind, String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.catalog;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class DefaultCamelCatalog extends AbstractCamelCatalog implements CamelCa
private static final String MODELS_CATALOG = "org/apache/camel/catalog/models.properties";
private static final String SCHEMAS_XML = "org/apache/camel/catalog/schemas";
private static final String MAIN_DIR = "org/apache/camel/catalog/main";
private static final String BASE_RESOURCE_DIR = "org/apache/camel/catalog";

private final VersionHelper version = new VersionHelper();

Expand Down Expand Up @@ -481,6 +483,11 @@ public ArtifactModel<?> modelFromMavenGAV(String groupId, String artifactId, Str
return null;
}

@Override
public InputStream loadResource(String kind, String name) {
return versionManager.getResourceAsStream(BASE_RESOURCE_DIR + "/" + kind + "/" + name);
}

private static boolean matchArtifact(ArtifactModel<?> am, String groupId, String artifactId, String version) {
if (am == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.apache.camel.catalog;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -1519,4 +1522,19 @@ public void modelFromMavenGAV() {
Assertions.assertEquals("Sent and receive messages to/from a JMS Queue or Topic.", am.getDescription());
}

@Test
public void loadFooResourceWithBarKind() throws IOException {
InputStream is = catalog.loadResource("bar", "foo.txt");
Assertions.assertNotNull(is);

String content = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Assertions.assertEquals("Hello Camel", content);
}

@Test
public void loadNotExistingResource() {
InputStream is = catalog.loadResource("bar", "not_exists");
Assertions.assertNull(is);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Camel
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ protected Integer export(ExportBaseCommand cmd) throws Exception {
cmd.gradleWrapper = this.gradleWrapper;
cmd.buildTool = this.buildTool;
cmd.quiet = this.quiet;
cmd.additionalProperties = this.additionalProperties;
// run export
return cmd.export();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ abstract class ExportBaseCommand extends CamelCommand {
description = "Will be quiet, only print when error occurs")
boolean quiet;

@CommandLine.Option(names = { "--additional-properties" }, description = "Additional maven properties, ex. --properties=prop1=foo,prop2=bar")
protected String additionalProperties;

public ExportBaseCommand(CamelJBangMain main) {
super(main);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.dsl.jbang.core.common.CatalogLoader;
Expand Down Expand Up @@ -137,13 +139,20 @@ private void createSettingsGradle(File file) throws Exception {
private void createMavenPom(File settings, File profile, File pom, Set<String> deps) throws Exception {
String[] ids = gav.split(":");

String context = readResourceTemplate("templates/spring-boot-pom.tmpl");

Properties prop = new CamelCaseOrderedProperties();
RuntimeUtil.loadProperties(prop, settings);
String repos = getMavenRepos(settings, prop, camelSpringBootVersion);

CamelCatalog catalog = CatalogLoader.loadSpringBootCatalog(repos, camelSpringBootVersion);

String context;
InputStream template = catalog.loadResource("camel-jbang", "spring-boot-pom.tmpl");
if (template != null) {
context = new String(template.readAllBytes(), StandardCharsets.UTF_8);
} else {
context = readResourceTemplate("templates/spring-boot-pom.tmpl");
}

String camelVersion = catalog.getLoadedVersion();

context = context.replaceFirst("\\{\\{ \\.GroupId }}", ids[0]);
Expand All @@ -152,6 +161,20 @@ private void createMavenPom(File settings, File profile, File pom, Set<String> d
context = context.replaceAll("\\{\\{ \\.SpringBootVersion }}", springBootVersion);
context = context.replaceFirst("\\{\\{ \\.JavaVersion }}", javaVersion);
context = context.replaceFirst("\\{\\{ \\.CamelVersion }}", camelVersion);
if (camelSpringBootVersion != null) {
context = context.replaceFirst("\\{\\{ \\.CamelSpringBootVersion }}", camelSpringBootVersion);
}
if (additionalProperties != null) {
String properties = Arrays.stream(additionalProperties.split(","))
.map(property -> {
String[] keyValueProperty = property.split("=");
String mavenProperty = String.format("\t\t<%s>%s</%s>", keyValueProperty[0], keyValueProperty[1], keyValueProperty[0]);
return mavenProperty;
})
.map(property -> property + System.lineSeparator())
.collect(Collectors.joining());
context = context.replaceFirst("\\{\\{ \\.AdditionalProperties }}", properties);
}

// Convert jkube properties to maven properties
Properties allProps = new CamelCaseOrderedProperties();
Expand All @@ -166,7 +189,7 @@ private void createMavenPom(File settings, File profile, File pom, Set<String> d
context = context.replaceFirst(Pattern.quote("{{ .jkubeProperties }}"),
Matcher.quoteReplacement(sbJKube.toString()));

if (repos == null) {
if (repos == null || repos.isEmpty()) {
context = context.replaceFirst("\\{\\{ \\.MavenRepositories }}", "");
} else {
int i = 1;
Expand Down Expand Up @@ -266,7 +289,7 @@ private void createBuildGradle(File settings, File gradleBuild, Set<String> deps
context = context.replaceFirst("\\{\\{ \\.JavaVersion }}", javaVersion);
context = context.replaceAll("\\{\\{ \\.CamelVersion }}", camelVersion);

if (repos == null) {
if (repos == null || repos.isEmpty()) {
context = context.replaceFirst("\\{\\{ \\.MavenRepositories }}", "");
} else {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,7 @@ public void onAlreadyDownloadedDependency(String groupId, String artifactId, Str
// we want to register everything
onDownloadDependency(groupId, artifactId, version);
}

@Override

public void onExtraRepository(String repo) {
if (!repos.contains(repo)) {
writeSettings("repository", repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.camel.catalog.VersionManager;
import org.apache.camel.main.KameletMain;
import org.apache.camel.main.download.DependencyDownloaderClassLoader;
import org.apache.camel.main.download.DownloadException;
import org.apache.camel.main.download.MavenArtifact;
import org.apache.camel.main.download.MavenDependencyDownloader;

Expand Down Expand Up @@ -106,12 +107,21 @@ public static CamelCatalog loadSpringBootCatalog(String repos, String version) t

// download camel-catalog for that specific version
MavenDependencyDownloader downloader = main.getCamelContext().hasService(MavenDependencyDownloader.class);
MavenArtifact ma = downloader.downloadArtifact("org.apache.camel", "camel-catalog", version);
MavenArtifact ma;
String camelCatalogVersion = version;
try {
ma = downloader.downloadArtifact("org.apache.camel", "camel-catalog", camelCatalogVersion);
} catch (DownloadException ex) {
// fallback, in case camel spring boot version differ from camel version
camelCatalogVersion = answer.getCatalogVersion();
ma = downloader.downloadArtifact("org.apache.camel", "camel-catalog", camelCatalogVersion);
}
if (ma != null) {
cl.addFile(ma.getFile());
} else {
throw new IOException("Cannot download org.apache.camel:camel-catalog:" + version);
throw new IOException("Cannot download org.apache.camel:camel-catalog:" + camelCatalogVersion);
}

ma = downloader.downloadArtifact("org.apache.camel.springboot", "camel-catalog-provider-springboot", version);
if (ma != null) {
cl.addFile(ma.getFile());
Expand Down

0 comments on commit d9af1db

Please sign in to comment.