Skip to content

Commit

Permalink
1452 Prepare for new ConfigurationReader and Adapt Templates process (#…
Browse files Browse the repository at this point in the history
…1499)

* #1452 changed dynamic groupId in master-pom.xml to fixed
separated all templates sets into deployables
moved util tests into templates-devon4j-utils plug-in
renamed devon4j-templates to devon4j-templates-tests

* Update  crud-java-server-app-complex/pom.xml

delete relative Path

* #1452 reformatted pom.xml codes

* #1452 added test resources for new TemplateSetTest

* #1452 split ContextConfigurationReader into AbstractConfigurationReader and ContextConfigurationSetReader
added new ContextConfigurationAnalyzer
implemented rough template type detection

* #1452 adapted core extract templates process and find templates methods to new template sets configuration
ensured backwards compatibility in ConfigurationFinder and ExtractTemplatesUtil
fixed some typos in ConfigurationFinder
refactored CobiGenPaths class
added getTemplateSetsFolderPath and createFolder methods to CobiGenPaths
added new constants for downloaded, adapted and template-sets to ConfigurationConstants
added processJars method to ExtractTemplatesUtil
set processJar and getJarFile methods deprecated
adjusted info messages in ExtractTemplatesUtil
added first TemplateProcessingTest class + resources
adjusted CobiGenFactory extractTemplate method

* #1452 fixed resolve of doubled CobiGen_Templates directory

* #1452 fixed ConfigurationUtilTest

* #1452 implemented requested changes
combined 3rd and 4th check into one in ConfigurationFinder
fixed old templates folder detection in ExtractTemplatesUtil
adjusted javadoc of processJars method in ExtractTemplatesUtil
adjusted return of getTemplateSetsFolderPath

* #1452 renamed variable name as requested

Co-authored-by: LarsReinken <[email protected]>
  • Loading branch information
jan-vcapgemini and LarsReinken authored Mar 21, 2022
1 parent 6f5e2ce commit e258c4b
Show file tree
Hide file tree
Showing 22 changed files with 726 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public class ConfigurationConstants {
/** Name of the templates folder */
public static final String TEMPLATES_FOLDER = "templates";

/** Name of the template sets downloaded folder */
public static final String DOWNLOADED_FOLDER = "downloaded";

/** Name of the template sets adapted folder */
public static final String ADAPTED_FOLDER = "adapted";

/** Name of the template sets folder */
public static final String TEMPLATE_SETS_FOLDER = "template-sets";

/** Name of the extracted templates project */
public static final String COBIGEN_TEMPLATES = "CobiGen_Templates";

Expand All @@ -57,6 +66,11 @@ public class ConfigurationConstants {
*/
public static final String CONFIG_PROPERTY_TEMPLATES_PATH = "templates";

/**
* Name of configuration key for location of template sets.
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_PATH = "template-sets";

// cobigen configuration environment variables

/** Name of the environment variable pointing to cobigen configuration file */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public static Path getTemplatesFolderPath() {
return getTemplatesFolderPath(getCobiGenHomePath());
}

/**
* Returns the template set home directory (which is located inside CobiGen home folder), or creates a new one if it
* does not exist
*
* @return {@link Path} of the templates home directory
*/
public static Path getTemplateSetsFolderPath() {

return getTemplateSetsFolderPath(getCobiGenHomePath());
}

/**
* Returns the templates home directory (which is located inside CobiGen home folder), or creates a new one if it does
* not exist
Expand All @@ -75,17 +86,43 @@ public static Path getTemplatesFolderPath(Path home) {

Path templatesPath = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER);

return createFolder(templatesPath);
}

/**
* Creates a directory at given path location
*
* @param folderPath Path of new folder
* @return
*/
private static Path createFolder(Path folderPath) {

// We first check whether we already have a directory
if (Files.exists(templatesPath)) {
return templatesPath;
if (Files.exists(folderPath)) {
return folderPath;
}

try {
Files.createDirectories(templatesPath);
Files.createDirectories(folderPath);
} catch (IOException e) {
throw new CobiGenRuntimeException("Unable to create path " + templatesPath);
throw new CobiGenRuntimeException("Unable to create path " + folderPath);
}
return templatesPath;

return folderPath;
}

/**
* Returns the template sets home directory (which is located inside CobiGen home folder), or creates a new one if it
* does not exist
*
* @param home cobigen configuration home directory
* @return {@link Path} of the template sets home directory
*/
public static Path getTemplateSetsFolderPath(Path home) {

Path templatesPath = home.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);

return createFolder(templatesPath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import com.devonfw.cobigen.api.constants.TemplatesJarConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
Expand Down Expand Up @@ -218,13 +221,45 @@ private static HttpURLConnection initializeConnection(String mavenUrl)
return conn;
}

/**
* Returns a list of the file paths of the template set jars
*
* @param templatesDirectory directory where the templates are located
*
* @return file of the jar downloaded or null if it was not found
*/
public static List<Path> getJarFiles(Path templatesDirectory) {

ArrayList<Path> jarPaths = new ArrayList<>();

try (Stream<Path> files = Files.list(templatesDirectory)) {
files.forEach(path -> {
if (path.toString().endsWith(".jar")) {
jarPaths.add(path);
}
});
} catch (IOException e) {
throw new CobiGenRuntimeException("Could not read configuration root directory.", e);
}

if (!jarPaths.isEmpty()) {
return jarPaths;
} else {
// There are no jars downloaded
return null;
}
}

/**
* Returns the file path of the templates jar
*
* @param isSource true if we want to get source jar file path
* @param templatesDirectory directory where the templates are located
* @return file of the jar downloaded or null if it was not found
*
* @deprecated use getJarFiles instead
*/
@Deprecated
public static File getJarFile(boolean isSource, File templatesDirectory) {

File[] jarFiles;
Expand All @@ -242,4 +277,5 @@ public static File getJarFile(boolean isSource, File templatesDirectory) {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.devonfw.cobigen.systemtest;

import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

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

/**
* Test suite for extract templates scenarios.
*/
public class TemplateProcessingTest extends AbstractApiTest {

/**
* Root path to all resources used in this test case
*/
private static String testFileRootPath = apiTestsRootPath + "TemplateProcessingTest/";

/** Temporary files rule to create temporary folders or files */
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

/**
* temporary project to store CobiGen home
*/
File cobiGenHome;

/**
* Creates a temporary CobiGen home directory for each test
*
* @throws IOException if an Exception occurs
*/
@Before
public void prepare() throws IOException {

this.cobiGenHome = this.tempFolder.newFolder("playground", "templatesHome");
}

/**
* @throws IOException if an Exception occurs
*/
public void extractTemplateSetsTest() throws IOException {

FileUtils.copyDirectory(new File(testFileRootPath + "templates"),
this.cobiGenHome.toPath().resolve("template-sets/downloaded").toFile());
CobiGenFactory.extractTemplates();
Path adaptedFolder = this.cobiGenHome.toPath().resolve(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_PATH)
.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Path extractedJar1 = adaptedFolder.resolve("template-test1-0.0.1");
Path extractedJar2 = adaptedFolder.resolve("template-test2-0.0.1");
assertThat(Files.exists(extractedJar1));
assertThat(Files.exists(extractedJar2));
}

/**
* @throws IOException if an Exception occurs
*/
public void extractTemplatesWithOldConfiguration() throws IOException {

Path cobigenTemplatesProject = this.cobiGenHome.toPath()
.resolve(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH)
.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
Files.createDirectories(cobigenTemplatesProject);
CobiGenFactory.extractTemplates();
assertThat(Files.exists(cobigenTemplatesProject));
}

/**
* Test of extract templates with old CobiGen_Templates project existing with custom COBIGEN_HOME environment variable
*
* @throws Exception test fails
*/
@Test
public void testExtractTemplatesWithOldConfiguration() throws Exception {

withEnvironmentVariable("COBIGEN_HOME", this.cobiGenHome.toPath().toString())
.execute(() -> extractTemplatesWithOldConfiguration());
}

/**
* Test of extract template sets with custom COBIGEN_HOME environment variable
*
* @throws Exception test fails
*/
@Test
public void testExtractTemplateSets() throws Exception {

withEnvironmentVariable("COBIGEN_HOME", this.cobiGenHome.toPath().toString())
.execute(() -> extractTemplateSetsTest());
}

/**
* @throws IOException if an Exception occurs
*/
public void findTemplateSetJarsWithBackwardsCompatibilityTest() throws IOException {

FileUtils.createParentDirectories(new File(testFileRootPath + "template-sets"));
URI templatesLocationURI = ConfigurationFinder.findTemplatesLocation();
assertThat(templatesLocationURI.compareTo(this.cobiGenHome.toPath().resolve("template-sets").toUri()));

}

/**
* Test of find template set downloaded folder to ensure backwards compatibility with custom COBIGEN_HOME environment
* variable
*
* @throws Exception test fails
*/
@Test
public void testfindTemplateSetDownloadedWithBackwardsCompatibility() throws Exception {

withEnvironmentVariable("COBIGEN_HOME", this.cobiGenHome.toPath().toString())
.execute(() -> findTemplateSetJarsWithBackwardsCompatibilityTest());
}

}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contextConfiguration xmlns="http://capgemini.com/devonfw/cobigen/ContextConfiguration" version="2.1">
<trigger id="entities" type="mockplugin">
<matcher type="fqn" value=".+">
</matcher>
</trigger>
</contextConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
overwritten
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<templatesConfiguration xmlns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" version="2.1">
<templates>
<template name="t1" destinationPath="generated.txt" templateFile="generated.txt.ftl" mergeStrategy="override"/>
</templates>
<increments>
<increment name="i1" description="the only increment">
<templateRef ref="t1"/>
</increment>
</increments>
</templatesConfiguration>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@ public static CobiGen create() throws InvalidConfigurationException {
}

/**
* Extracts templates project to the given path
* Extracts template set projects
*
* @return path to have the templates extracted
* @return path to have the template sets extracted to
* @throws DirectoryNotEmptyException if the directory is not empty
*/
public static Path extractTemplates() throws DirectoryNotEmptyException {

Path templatesLocation = CobiGenPaths.getTemplatesFolderPath();
ExtractTemplatesUtil.extractTemplates(templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES), false);
return templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
Path extractedFolderLocation = CobiGenPaths.getTemplateSetsFolderPath()
.resolve(ConfigurationConstants.ADAPTED_FOLDER);
ExtractTemplatesUtil.extractTemplates(extractedFolderLocation, false);
return extractedFolderLocation;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader;
import com.devonfw.cobigen.impl.config.reader.AbstractContextConfigurationReader;
import com.devonfw.cobigen.impl.config.reader.ContextConfigurationAnalyzer;

/**
* The {@link ContextConfiguration} is a configuration data wrapper for all information about templates and the target
Expand Down Expand Up @@ -46,7 +47,8 @@ public ContextConfiguration(Path configRoot) throws InvalidConfigurationExceptio
*/
private void readConfiguration(Path configRoot) throws InvalidConfigurationException {

ContextConfigurationReader reader = new ContextConfigurationReader(configRoot);
AbstractContextConfigurationReader reader = ContextConfigurationAnalyzer.getReader(configRoot);

this.configurationPath = reader.getContextRoot();
this.triggers = reader.loadTriggers();
}
Expand Down
Loading

0 comments on commit e258c4b

Please sign in to comment.