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

1452 Implemented new configuration reader to read the template sets #1504

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Expand Up @@ -8,12 +8,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import org.codehaus.plexus.util.Os;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.slf4j.LoggerFactory;
Expand All @@ -32,23 +35,60 @@ public class AbstractCliTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

/** Temporary directory for the templates project */
@ClassRule
public static TemporaryFolder tempFolderTemplates = new TemporaryFolder();

/** Current home directory */
protected Path currentHome;

/** The devon4j-templates development folder */
/** The templates development folder */
protected static Path devTemplatesPath;

/** A temp directory containing the templates development folder */
protected static Path devTemplatesPathTemp;

/**
* Determine the devon4j-templates development folder
*
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a template directory directory fails
*/
@BeforeClass
public static void determineDevTemplatesPath() throws URISyntaxException {
public static void determineDevTemplatesPath() throws URISyntaxException, IOException {

devTemplatesPath = new File(AbstractCliTest.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile().getParentFile().getParentFile().toPath().resolve("cobigen-templates")
.resolve("templates-devon4j");
.getParentFile().getParentFile().getParentFile().getParentFile().toPath().resolve("cobigen-templates");

// create a temporary directory cobigen-templates/template-sets/adapted containing the template sets
Path tempFolderPath = tempFolderTemplates.getRoot().toPath();
Path cobigenTemplatePath = tempFolderPath.resolve("cobigen-templates");
if (!Files.exists(cobigenTemplatePath)) {
Files.createDirectory(cobigenTemplatePath);

devTemplatesPathTemp = cobigenTemplatePath.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path templateSetsAdaptedFolder = devTemplatesPathTemp.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Files.createDirectory(devTemplatesPathTemp);
Files.createDirectory(templateSetsAdaptedFolder);

FileUtils.copyDirectory(devTemplatesPath.toFile(), templateSetsAdaptedFolder.toFile());

try (Stream<Path> files = Files.list(templateSetsAdaptedFolder)) {
files.forEach(path -> {
if (Files.isDirectory(path)) {
Path resourcesFolder = path.resolve("src/main/resources");
Path templatesFolder = path.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
if (Files.exists(resourcesFolder) && !Files.exists(templatesFolder)) {
try {
Files.move(resourcesFolder, templatesFolder);
} catch (IOException e) {
e.printStackTrace();
GuentherJulian marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
});
}
}
}

/**
Expand All @@ -71,7 +111,7 @@ public void runWithLatestTemplates() throws IOException {

Path configFile = this.currentHome.resolve(ConfigurationConstants.COBIGEN_CONFIG_FILE);
Files.write(configFile,
(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH + "=" + devTemplatesPath.toString()).getBytes());
(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH + "=" + devTemplatesPathTemp.toString()).getBytes());
}

/**
Expand Down Expand Up @@ -129,7 +169,7 @@ protected void execute(String[] args, boolean useDevTemplates, boolean assureFai
debugArgs = Arrays.copyOf(debugArgs, debugArgs.length + 3);
debugArgs[debugArgs.length - 3] = "-v";
debugArgs[debugArgs.length - 2] = "-tp";
debugArgs[debugArgs.length - 1] = devTemplatesPath.toString();
debugArgs[debugArgs.length - 1] = devTemplatesPathTemp.toString();
} else {
debugArgs = Arrays.copyOf(debugArgs, debugArgs.length + 1);
debugArgs[debugArgs.length - 1] = "-v";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.devonfw.cobigen.templates.devon4j.test.templates;

import java.io.File;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.maven.test.AbstractMavenTest;
import com.devonfw.cobigen.templates.devon4j.config.constant.MavenMetadata;

Expand All @@ -15,6 +25,58 @@ public class TemplatesGenerationTest extends AbstractMavenTest {
/** Root of all test resources of this test suite */
public static final String TEST_RESOURCES_ROOT = "src/test/resources/testdata/templatetest/";

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

/** The templates development folder */
protected static Path templatesProject;

/** The templates development folder */
protected static Path templatesProjectTemporary;

/**
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a template directory directory fails
*/
@BeforeClass
public static void setupDevTemplates() throws URISyntaxException, IOException {

templatesProject = new File(
TemplatesGenerationTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
.getParentFile().getParentFile().toPath();

// create a temporary directory cobigen-templates/template-sets/adapted containing the template sets
Path tempFolderPath = tempFolder.getRoot().toPath();
Path cobigenTemplatePath = tempFolderPath.resolve("cobigen-templates");
if (!Files.exists(cobigenTemplatePath)) {
Files.createDirectory(cobigenTemplatePath);

templatesProjectTemporary = cobigenTemplatePath.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path templateSetsAdaptedFolder = templatesProjectTemporary.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Files.createDirectory(templatesProjectTemporary);
Files.createDirectory(templateSetsAdaptedFolder);

FileUtils.copyDirectory(templatesProject.toFile(), templateSetsAdaptedFolder.toFile());

try (Stream<Path> files = Files.list(templateSetsAdaptedFolder)) {
files.forEach(path -> {
if (Files.isDirectory(path)) {
Path resourcesFolder = path.resolve("src/main/resources");
Path templatesFolder = path.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
if (Files.exists(resourcesFolder) && !Files.exists(templatesFolder)) {
try {
Files.move(resourcesFolder, templatesFolder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
}

/**
* Test successful generation of all templates based on an entity
*
Expand All @@ -24,7 +86,7 @@ public class TemplatesGenerationTest extends AbstractMavenTest {
public void testAllTemplatesGeneration_EntityInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesEntityInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -36,7 +98,7 @@ public void testAllTemplatesGeneration_EntityInput() throws Exception {
public void testAllTemplatesGeneration_EtoInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesEtoInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -48,7 +110,7 @@ public void testAllTemplatesGeneration_EtoInput() throws Exception {
public void testAllTemplatesGeneration_OpenApiInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesOpenApiInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -60,7 +122,7 @@ public void testAllTemplatesGeneration_OpenApiInput() throws Exception {
public void testAllTemplatesGeneration_RestServiceInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesRestServiceInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -72,7 +134,7 @@ public void testAllTemplatesGeneration_RestServiceInput() throws Exception {
public void testAllTemplatesGeneration_ToInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesToInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -84,7 +146,7 @@ public void testAllTemplatesGeneration_ToInput() throws Exception {
public void testAllTemplatesGeneration_XML() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesXMLInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ public static List<Path> getJarFiles(Path templatesDirectory) {
* @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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Paths;
import java.util.Map;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.extension.PluginRegistry;
Expand All @@ -16,8 +17,8 @@
*/
public class ConfigurationHolder {

/** Cached templates configurations. Configuration File URI -> Trigger ID -> configuration instance */
private Map<Path, Map<String, TemplatesConfiguration>> templatesConfigurations = Maps.newHashMap();
/** Cached templates configurations. Trigger ID -> Configuration File URI -> configuration instance */
private Map<String, Map<Path, TemplatesConfiguration>> templatesConfigurations = Maps.newHashMap();

/** Cached context configuration */
private ContextConfiguration contextConfiguration;
Expand Down Expand Up @@ -75,15 +76,16 @@ public Path getConfigurationPath() {
*/
public TemplatesConfiguration readTemplatesConfiguration(Trigger trigger) {

Path configRoot = readContextConfiguration().getConfigRootforTrigger(trigger.getId());
Path templateFolder = Paths.get(trigger.getTemplateFolder());
if (!this.templatesConfigurations.containsKey(templateFolder)) {
this.templatesConfigurations.put(templateFolder, Maps.<String, TemplatesConfiguration> newHashMap());
if (!this.templatesConfigurations.containsKey(trigger.getId())) {
TemplatesConfiguration config = new TemplatesConfiguration(configRoot, trigger, this);
this.templatesConfigurations.put(trigger.getId(), Maps.<Path, TemplatesConfiguration> newHashMap());
GuentherJulian marked this conversation as resolved.
Show resolved Hide resolved

TemplatesConfiguration config = new TemplatesConfiguration(this.configurationPath, trigger, this);
this.templatesConfigurations.get(templateFolder).put(trigger.getId(), config);
this.templatesConfigurations.get(trigger.getId()).put(templateFolder, config);
}

return this.templatesConfigurations.get(templateFolder).get(trigger.getId());
return this.templatesConfigurations.get(trigger.getId()).get(templateFolder);
}

/**
Expand All @@ -99,4 +101,16 @@ public ContextConfiguration readContextConfiguration() {
}
return this.contextConfiguration;
}

/**
* @return return if the template folder structure consists of template sets or if the old structure is used
*/
public boolean isTemplateSetConfiguration() {

if (this.configurationPath.toUri().getScheme().equals("jar")
|| !this.configurationPath.getFileName().toString().equals(ConfigurationConstants.TEMPLATE_SETS_FOLDER)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.config.reader.AbstractContextConfigurationReader;
import com.devonfw.cobigen.impl.config.reader.ContextConfigurationAnalyzer;
import com.devonfw.cobigen.impl.config.reader.ContextConfigurationReaderFactory;
import com.devonfw.cobigen.impl.config.reader.ContextConfigurationSetReader;

/**
* The {@link ContextConfiguration} is a configuration data wrapper for all information about templates and the target
Expand All @@ -27,6 +28,11 @@ public class ContextConfiguration {
*/
private Path configurationPath;

/**
* The reader to read the context.xml files
*/
private AbstractContextConfigurationReader contextConfigurationReader;

/**
* Creates a new {@link ContextConfiguration} with the contents initially loaded from the context.xml
*
Expand All @@ -47,10 +53,12 @@ public ContextConfiguration(Path configRoot) throws InvalidConfigurationExceptio
*/
private void readConfiguration(Path configRoot) throws InvalidConfigurationException {

AbstractContextConfigurationReader reader = ContextConfigurationAnalyzer.getReader(configRoot);
if (this.contextConfigurationReader == null) {
this.contextConfigurationReader = ContextConfigurationReaderFactory.getReader(configRoot);
}

this.configurationPath = reader.getContextRoot();
this.triggers = reader.loadTriggers();
this.configurationPath = this.contextConfigurationReader.getContextRoot();
this.triggers = this.contextConfigurationReader.loadTriggers();
}

/**
Expand Down Expand Up @@ -96,4 +104,16 @@ public Path getConfigurationPath() {
return this.configurationPath;
}

/**
* @param triggerId the trigger id to get the config root directory for
* @return the {@link Path} of the config root directory of the trigger
*/
public Path getConfigRootforTrigger(String triggerId) {

if (this.contextConfigurationReader instanceof ContextConfigurationSetReader) {
return ((ContextConfigurationSetReader) this.contextConfigurationReader).getConfigRootForTrigger(triggerId);
}
return this.contextConfigurationReader.getContextRoot();
}

}
Loading