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

1510 allow template sets to be installed #1554

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e3e9185
initial commit
EduardKrieger Jul 7, 2022
d03e10d
template-sets will be installed at cobigen startup
EduardKrieger Jul 15, 2022
4ecb3bc
added tests
EduardKrieger Jul 19, 2022
3afb45a
removed old test
EduardKrieger Jul 19, 2022
060458d
removed comments
EduardKrieger Jul 19, 2022
7116b69
added mavenCoordinates dataholder and reworked the functions
EduardKrieger Jul 28, 2022
e4094a8
added systemtest
EduardKrieger Aug 2, 2022
3e39e32
added creation of downloaded folder
EduardKrieger Aug 4, 2022
0d2c8f9
trying to fix cli tests
EduardKrieger Aug 10, 2022
b6e179e
removed unnessesary download of templates
EduardKrieger Aug 10, 2022
787e592
added check for downloaded folder
EduardKrieger Aug 16, 2022
9612a43
revert changes to an old test
EduardKrieger Aug 16, 2022
89e0455
added javadoc and adjusted documentation
EduardKrieger Aug 17, 2022
0013541
fixed wrong push
EduardKrieger Aug 17, 2022
30edb31
#1510 implemented requested changes
jan-vcapgemini Aug 17, 2022
aca4e82
implemented requested changes
EduardKrieger Aug 18, 2022
1ebed3d
initial implenetation of mocking the downloads
EduardKrieger Aug 19, 2022
c71d13f
added todo
EduardKrieger Aug 24, 2022
494dba1
removed usage of Path
EduardKrieger Aug 24, 2022
e407089
removed initial try to mock downloads in tests
EduardKrieger Aug 24, 2022
4cbdaf3
chaged assertions to assertThat
EduardKrieger Aug 25, 2022
4d43f22
changed variables to correct camel case and used constants, Files and…
EduardKrieger Aug 26, 2022
3d9de79
fixed typo
EduardKrieger Aug 29, 2022
b0f400c
implemented requested changes
EduardKrieger Aug 31, 2022
41324cb
fixed nullPointerbug with lates commit
EduardKrieger Aug 31, 2022
6849aa6
implemented requested changes
EduardKrieger Sep 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contextConfiguration xmlns="http://capgemini.com/devonfw/cobigen/ContextConfiguration" version="3.0">
<contextConfiguration xmlns="http://capgemini.com/devonfw/cobigen/ContextConfiguration" version="2.1">

<trigger id="crud_openapi_java_server_app" type="openapi" templateFolder="templates">
<containerMatcher type="element" value="openAPIFile"/>
Expand Down Expand Up @@ -29,7 +29,5 @@
<variableAssignment type="property" key="entityName" value="name"/>
</matcher>
</trigger>
<tags>
<tag name="bla" />
</tags>

</contextConfiguration>
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public class ConfigurationConstants {
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_HIDE = "template-sets.hide";

/**
* Name of configuration key to preinstall specific template sets
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_INSTALLED = "template-sets.installed";
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

/**
* Default (public) cobigen GroupId
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public class TemplatesJarConstants {
*/
public static final String DOWNLOADED_JAR_FOLDER = "/.metadata/cobigen_jars";

/**
* Regular expression to check the correct definition of maven coordinates to download a template-set with the
* configuration key template-sets.installed in the properties.
*/
public static final String MAVEN_COORDINATES_CHECK = "([a-zA-Z0-9_\\-\\.]+):([a-zA-Z0-9_-]+)(:([0-9]+(\\.[0-9]+)*(-SNAPSHOT)?|LATEST))?";

/**
* Jar regular expression name to be used in a file name filter, so that we can check whether the templates are
* already downloaded. Checks "templates-anystring-anydigitbetweendots.jar"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.devonfw.cobigen.api.util;

/**
* TODO
*/
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
public class MavenCoordinate {

private String groupID;
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

private String artifactID;
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

private String version;

public MavenCoordinate(String groupID, String artifactID, String version) {
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

this.groupID = groupID;
this.artifactID = artifactID;
this.version = version;
}

public String getArtifactID() {
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

return this.artifactID;
}

public String getGroupID() {

return this.groupID;
}
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

public String getVersion() {

return this.version;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -21,6 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.constants.TemplatesJarConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;

Expand All @@ -45,7 +48,8 @@ private static String downloadJar(String groupId, String artifactId, String vers
File templatesDirectory) {

// By default the version should be latest
if (version.isEmpty() || version == null) {
if (version == null || version.isEmpty()) {
maybeec marked this conversation as resolved.
Show resolved Hide resolved

version = "LATEST";
}

Expand Down Expand Up @@ -84,7 +88,9 @@ private static String downloadJar(String groupId, String artifactId, String vers
return fileName;
}

// TODO
/**
*
* @param isDownloadSource true if downloading source jar file
* @param templatesDirectory directory where the templates jar are located
* @return fileName Name of the file downloaded
Expand All @@ -95,6 +101,53 @@ public static String downloadLatestDevon4jTemplates(boolean isDownloadSource, Fi
TemplatesJarConstants.DEVON4J_TEMPLATES_ARTIFACTID, "LATEST", isDownloadSource, templatesDirectory);
}

// TODO
/**
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
* @param templatesDirectory directory where the templates jar are located
* @return fileName Name of the file downloaded
*/
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
public static void downloadTemplatesByMavenCoordinates(File templatesDirectory,
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
List<MavenCoordinate> mavenCoordinates) {

if (mavenCoordinates == null || mavenCoordinates.isEmpty()) {
return;
// no templates specified
}
Set<MavenCoordinate> afterExistanceCheckList = new HashSet();
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved
// File adapted = new File(templatesDirectory, ConfigurationConstants.ADAPTED_FOLDER);
// File downloaded = new File(templatesDirectory, ConfigurationConstants.DOWNLOADED_FOLDER);
if (templatesDirectory.getName().equals(ConfigurationConstants.ADAPTED_FOLDER)) {
LOG.info("Found adapted folder no download of templates needed");
return;
}
// check if templates already exist
for (

MavenCoordinate mcoordinate : mavenCoordinates) {
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
if (templatesDirectory.listFiles().length > 0) {
for (File downloadedFile : templatesDirectory.listFiles()) {
if (!(downloadedFile.getName().contains(mcoordinate.getArtifactID()))) {
afterExistanceCheckList.add(mcoordinate);
LOG.info("Template specified in the properties file with ArtifactID: " + mcoordinate.getArtifactID()
+ " GroupID:" + mcoordinate.getGroupID() + " will be loaded");
}
}
maybeec marked this conversation as resolved.
Show resolved Hide resolved
} else {
afterExistanceCheckList.add(mcoordinate);
}
}
// download templates
afterExistanceCheckList.toArray();
for (

MavenCoordinate mavenCoordinate : afterExistanceCheckList) {
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
downloadJar(mavenCoordinate.getGroupID(), mavenCoordinate.getArtifactID(), mavenCoordinate.getVersion(), false,
templatesDirectory);
downloadJar(mavenCoordinate.getGroupID(), mavenCoordinate.getArtifactID(), mavenCoordinate.getVersion(), true,
templatesDirectory);
}
}

/**
* Checks whether there is a newer version of the templates on Maven
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.devonfw.cobigen.systemtest;

import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;

import org.hamcrest.Matchers;
import org.junit.Test;

import com.devonfw.cobigen.api.CobiGen;
import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.impl.CobiGenFactory;
import com.devonfw.cobigen.systemtest.common.AbstractApiTest;

/**
* TODO
*
*/
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved
public class TemplatesInstallationTest extends AbstractApiTest {

/**
* Tests that sources get overwritten if merge strategy override is configured.
*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartupOLD() throws Exception {
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templates = this.tmpFolder.newFolder("TemplateSetsInstalledTest", ConfigurationConstants.TEMPLATES_FOLDER);
File cobigenDir = templates.toPath().resolve(ConfigurationConstants.COBIGEN_TEMPLATES).toFile();
Files.createDirectories(cobigenDir.toPath());
File target = new File(folder, ".cobigen");
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved
BufferedWriter writer = new BufferedWriter(new FileWriter(target));
writer.write("template-sets.installed=com.devonfw.cobigen:templates-devon4j:2021.12.005");
writer.close();
CobiGen cobigen = CobiGenFactory.create(templates.toURI(), true);
assertEquals(2, cobigenDir.listFiles().length);
for (File f : cobigenDir.listFiles()) {
assertThat(f.getName(), Matchers.either(Matchers.is("templates-devon4j-2021.12.005.jar"))
.or(Matchers.is("templates-devon4j-2021.12.005-sources.jar")));
}
});
}

/**
* Tests that sources get overwritten if merge strategy override is configured.
*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartupNEW() throws Exception {
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templateSets = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER);
File downloaded = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER, "downloaded");
File target = new File(folder, ".cobigen");
BufferedWriter writer = new BufferedWriter(new FileWriter(target));
writer.write("template-sets.installed=com.devonfw.cobigen:templates-devon4j:2021.12.006");
writer.close();
CobiGen cobigen = CobiGenFactory.create(templateSets.toURI());
assertEquals(2, downloaded.listFiles().length);
for (File f : downloaded.listFiles()) {
assertThat(f.getName(), Matchers.either(Matchers.is("templates-devon4j-2021.12.006.jar"))
.or(Matchers.is("templates-devon4j-2021.12.006-sources.jar")));
}
});

}

// ohne downloaded Ordner
// /**
// * Tests that sources get overwritten if merge strategy override is configured.
// *
// * @throws Exception test fails.
// */
// @Test
// public void testNOInstallAtStartup() throws Exception {
//
// File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
// withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
// CobiGen cobigen = CobiGenFactory.create();
// Path templateSets = folder.toPath().resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
// assertEquals(templateSets.toFile().listFiles().length, 1);
// assertEquals(templateSets.resolve(ConfigurationConstants.DOWNLOADED_FOLDER).toFile().listFiles().length, 0);
// });
// }
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package com.devonfw.cobigen.impl;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.CobiGen;
import com.devonfw.cobigen.api.HealthCheck;
import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
import com.devonfw.cobigen.api.exception.DeprecatedMonolithicConfigurationException;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.TemplatesJarUtil;
import com.devonfw.cobigen.impl.aop.BeanFactory;
import com.devonfw.cobigen.impl.aop.ProxyFactory;
import com.devonfw.cobigen.impl.config.ConfigurationHolder;
import com.devonfw.cobigen.impl.config.TemplateSetConfiguration;
import com.devonfw.cobigen.impl.extension.PluginRegistry;
import com.devonfw.cobigen.impl.healthcheck.HealthCheckImpl;
import com.devonfw.cobigen.impl.util.ConfigurationClassLoaderUtil;
Expand Down Expand Up @@ -89,8 +98,25 @@ public static CobiGen create(URI configFileOrFolder, boolean allowMonolithicConf
// Notifies all plugins of new template root path
PluginRegistry.notifyPlugins(configurationHolder.getConfigurationPath());

if (!allowMonolithicConfiguration && !configurationHolder.isTemplateSetConfiguration())
if (!allowMonolithicConfiguration && !configurationHolder.isTemplateSetConfiguration()) {
throw new DeprecatedMonolithicConfigurationException();
}
// install Template Sets defined in .properties file
TemplateSetConfiguration config = ConfigurationFinder.loadTemplateSetConfigurations(
CobiGenPaths.getCobiGenHomePath().resolve(ConfigurationConstants.COBIGEN_CONFIG_FILE));
URI templatesLocation = ConfigurationFinder.findTemplatesLocation();
File downloadPath = new File(templatesLocation);
if (configurationHolder.isTemplateSetConfiguration()) {
downloadPath = Paths.get(templatesLocation).resolve(ConfigurationConstants.DOWNLOADED_FOLDER).toFile();
if (!downloadPath.exists()) {
try {
Files.createDirectory(downloadPath.toPath());
} catch (IOException e) {
throw new CobiGenRuntimeException("Could not create Download Folder", e);
}
}
TemplatesJarUtil.downloadTemplatesByMavenCoordinates(downloadPath, config.getMavenCoordinates());
}
return createBean;
}

Expand All @@ -109,6 +135,7 @@ public static CobiGen create(boolean allowMonolithicConfiguration) throws Invali
throw new InvalidConfigurationException(
"No valid templates can be found. Please configure your cobigen configuration file properly or place the templates in cobigen home directory. Creating CobiGen instance aborted.");
}

return create(configFileOrFolder, allowMonolithicConfiguration);
}

Expand Down
Loading