From e258c4b00aa748f20f4f3b52bf5aaddd72f2e7c7 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com> Date: Mon, 21 Mar 2022 18:15:28 +0100 Subject: [PATCH] 1452 Prepare for new ConfigurationReader and Adapt Templates process (#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 --- .../api/constants/ConfigurationConstants.java | 14 + .../cobigen/api/util/CobiGenPaths.java | 47 ++- .../cobigen/api/util/TemplatesJarUtil.java | 36 +++ .../systemtest/TemplateProcessingTest.java | 130 ++++++++ .../templates/template-test1-0.0.1.jar | Bin 0 -> 3031 bytes .../templates/template-test2-0.0.1.jar | Bin 0 -> 3032 bytes .../src/main/templates/context.xml | 7 + .../src/main/templates/generated.txt.ftl | 1 + .../src/main/templates/templates.xml | 11 + .../TemplateSetTest/template-test1-0.0.1.jar | Bin 0 -> 3031 bytes .../TemplateSetTest/template-test2-0.0.1.jar | Bin 0 -> 3032 bytes .../devonfw/cobigen/impl/CobiGenFactory.java | 11 +- .../impl/config/ContextConfiguration.java | 6 +- .../AbstractContextConfigurationReader.java | 298 ++++++++++++++++++ .../reader/ContextConfigurationAnalyzer.java | 29 ++ .../reader/ContextConfigurationReader.java | 287 +---------------- .../reader/ContextConfigurationSetReader.java | 21 ++ .../reader/TemplatesConfigurationReader.java | 2 +- .../impl/util/ConfigurationFinder.java | 97 +++--- .../impl/util/ExtractTemplatesUtil.java | 55 +++- .../ContextConfigurationReaderTest.java | 5 +- .../unittest/util/ConfigurationUtilTest.java | 23 +- 22 files changed, 726 insertions(+), 354 deletions(-) create mode 100644 cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateProcessingTest/templates/template-test1-0.0.1.jar create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateProcessingTest/templates/template-test2-0.0.1.jar create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/context.xml create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/generated.txt.ftl create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/templates.xml create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test1-0.0.1.jar create mode 100644 cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test2-0.0.1.jar create mode 100644 cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/AbstractContextConfigurationReader.java create mode 100644 cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/ContextConfigurationAnalyzer.java create mode 100644 cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/ContextConfigurationSetReader.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/ConfigurationConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/ConfigurationConstants.java index 94fa4ee66e..b906646a69 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/ConfigurationConstants.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/ConfigurationConstants.java @@ -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"; @@ -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 */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java index 0f746f9a72..2e30fc6128 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java @@ -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 @@ -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); } /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java index 40d7a28267..ff4aa5545b 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java @@ -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; @@ -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 getJarFiles(Path templatesDirectory) { + + ArrayList jarPaths = new ArrayList<>(); + + try (Stream 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; @@ -242,4 +277,5 @@ public static File getJarFile(boolean isSource, File templatesDirectory) { return null; } } + } diff --git a/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java b/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java new file mode 100644 index 0000000000..b8d8effa20 --- /dev/null +++ b/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java @@ -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()); + } + +} diff --git a/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateProcessingTest/templates/template-test1-0.0.1.jar b/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateProcessingTest/templates/template-test1-0.0.1.jar new file mode 100644 index 0000000000000000000000000000000000000000..c993d07eb487b9b7f1824ef19f61658b6cb228ce GIT binary patch literal 3031 zcmWIWW@h1H00FVC^bjxuN^k;cU)K;vT~9wZ{Q#&k4hB)6ve$3kFURsgZ*3~+9=KSU$gDb`lo)+nNojal9t?R_W{$xqm6fx}s zDiu5DbO#B02L+eR)mkee!!0z~I7qZc_;k=`k)E$-zVra)y^M>!4s42){An3#xl&-E zNbLG4ISX_hvcKqj<*D3Rv6BJjK1^@$BfBp*u`D$Y?iozQx|oWS^KBr75~uiDikpsfoI!xjAsNIlxi#cB;(QPN10+ zfY^s(vkQQ}Dk#b?NG&SKOf42YkVAqv?Pd^?wH@)wv(Rt08d%9o8 zhBq5KB^Q!_|&4)GcC*Cl_pL0I;m?Q{PvX7i}r)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o8 zhBq5KB^Q!_|&4)GcC*Cl_pL0I;m?Q{PvX7i}r)Z zW@NP#dP)m)giXn1@SOViXHM7qz@|x#`^z8Syy*8?`TjQDtoJsRvsIk`*v{2ivt@~) z_^p702VR@VXWpOZ;l?}t5!c}pg1fkHzSJ>E=2wd556X7-n(6qF)hoLF%6X1`FDx~} zH?51?JmsWN>IILDd4iL*=S2%Mr-lYF&w9Z8OeI8e-?B3nQ&(-S@RpWh?tQ&U;YZGk z8)^AZn|L1|{avxIVrH$-ojI-oTUx$+O#Ugd_onZ~&lXXB>K6~rNw9zaT_&qOnDrXt zmf%~;zxoq5hwou%-Lgis`OEgrf87hr?`=I9FtvGS)n&PI!H=$P*CVg)InmVJv_bKR z{ndqg6eYvIiFtjUoON^!r}z&I#UCbjA0I69JG*SMy)?UZ&BJo7Qja>0SLYwhbN@MVDtkaGauYN0WmjQjWvH1FR4j2Y=$EF4RDRgAX+Mys4@^mN z*fk{Q=ar;Zl;{DIM{d7&-ys8@w(qvCPu^zCN(|#LO>I2F`G7&u^x&dGuh*Y;*s^II zWLm%H$G`BcdzM|Bvr%4Sb93ZRb~ei=Z5CEH@~-VKHfXNMtC=k+d#9iwH8wsF` zm)K~2#lk^M=n|h>-rQ@RmNL7`dX5PH4N=j5xS&gXv%PIkt@W$;e>FvAOv@FlRbSft z&W(?~!-xpEJ z!yw^!H8Crs-8ACGAD|&0f#Ilt-7t6*fJ1V^K~P3>dH<6u`~49SJr32Ej4RVwo-+IF zDb%_7`b3=9=kFFOnVH@Ba_7(OE8J>ZrOqcYnK$I{vOVmLSJ-k-Uiq-`{$1Nfk2N0J z;vCr$h6lH4YPs249GwxzRC=w(^{A!e6B94fFJ}&Kp0ZYor^zb0|Ehtw~inhsg{Ll z!(PvV6obH$Ms*+w*M?N<; + + + + + + diff --git a/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/generated.txt.ftl b/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/generated.txt.ftl new file mode 100644 index 0000000000..67c1a95c2d --- /dev/null +++ b/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/generated.txt.ftl @@ -0,0 +1 @@ +overwritten \ No newline at end of file diff --git a/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/templates.xml b/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/templates.xml new file mode 100644 index 0000000000..cdc5791993 --- /dev/null +++ b/cobigen/cobigen-core-systemtest/src/test/resources/testdata/systemtest/TemplateSetTest/template-test0/src/main/templates/templates.xml @@ -0,0 +1,11 @@ + + + +