Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-19120: Export configurable template #9470

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -54,6 +54,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 +482,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 @@ -129,6 +129,10 @@ abstract class ExportBaseCommand extends CamelCommand {
@CommandLine.Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
protected boolean fresh;

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

@CommandLine.Option(names = { "--logging" }, defaultValue = "false",
description = "Can be used to turn on logging (logs to file in <user home>/.camel directory)")
boolean logging;
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,22 @@ 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);

// First try to load a specialized template from the catalog, if the catalog does not provide it
// fallback to the template defined in camel-jbang-core
String context;
Croway marked this conversation as resolved.
Show resolved Hide resolved
InputStream template = catalog.loadResource("camel-jbang", "spring-boot-pom.tmpl");
if (template != null) {
context = IOHelper.loadText(template);
} else {
context = readResourceTemplate("templates/spring-boot-pom.tmpl");
}

String camelVersion = catalog.getLoadedVersion();

context = context.replaceFirst("\\{\\{ \\.GroupId }}", ids[0]);
Expand All @@ -152,6 +163,21 @@ 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 +192,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 +292,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 @@ -1067,7 +1067,6 @@ public void onAlreadyDownloadedDependency(String groupId, String artifactId, Str
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