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

1595 extend uprader to upgrade from monolithic structure to template set.xml #1610

Closed
2 changes: 1 addition & 1 deletion cobigen-cli/cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.6.2</version>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
2 changes: 1 addition & 1 deletion cobigen-plugins/cobigen-htmlplugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.2</version>
<version>1.15.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class ConfigurationConstants {
/** Templates configuration file name */
public static final String TEMPLATES_CONFIG_FILENAME = "templates.xml";

/**
* Template-set configuration file name (File obtained after merging context.xml and templates.xml)
*/
public static final String TEMPLATE_SET_CONFIG_FILENAME = "template-set.xml";

/** Filename of the {@link Properties} used to customize cobigen properties and template relocation. */
public static final String COBIGEN_PROPERTIES = "cobigen.properties";

Expand Down
17 changes: 15 additions & 2 deletions cobigen/cobigen-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@
</sources>
</configuration>
</execution>
<execution>
<id>generate-v6.0-configuration</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.devonfw.cobigen.impl.config.entity.io.v6_0</packageName>
<outputDirectory>${project.build.directory}/generated-sources/config/v6.0</outputDirectory>
<sources>
<source>${basedir}/src/main/resources/schema/v6.0</source>
</sources>
</configuration>
</execution>
<!-- Redundant generation due to stability reasons of the configuration upgrade mechanism -->
<execution>
<id>generate-current-configuration</id>
Expand All @@ -260,8 +274,7 @@
<packageName>com.devonfw.cobigen.impl.config.entity.io</packageName>
<outputDirectory>${project.build.directory}/generated-sources/config/current</outputDirectory>
<sources>
<source>${basedir}/src/main/resources/schema/v3.0/contextConfiguration.xsd</source>
<source>${basedir}/src/main/resources/schema/v5.0/templatesConfiguration.xsd</source>
<source>${basedir}/src/main/resources/schema/v6.0/templateSetConfiguration.xsd</source>
</sources>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.devonfw.cobigen.impl.config.ContextConfiguration;
import com.devonfw.cobigen.impl.config.constant.ContextConfigurationVersion;

import com.devonfw.cobigen.impl.config.entity.io.v6_0.TemplateSetConfiguration;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
Expand Down Expand Up @@ -73,11 +74,11 @@ protected List<ConfigurationUpgradeResult> performNextUpgradeStep(ContextConfigu
break;
case v2_1:
TemplateSetUpgrader templatesSetUpgrader = new TemplateSetUpgrader();
Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration, Path> contextMap = templatesSetUpgrader
.upgradeTemplatesToTemplateSets(templatesLocation);
for (com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration context : contextMap.keySet()) {
Map<TemplateSetConfiguration, Path> templateSetMap = templatesSetUpgrader
.upgradeTemplatesToTemplateSetsV6(templatesLocation);
for (TemplateSetConfiguration templateSetConfiguration : templateSetMap.keySet()) {
ConfigurationUpgradeResult tempResult = new ConfigurationUpgradeResult();
tempResult.setResultConfigurationJaxbRootNodeAndPath(context, contextMap.get(context));
tempResult.setResultConfigurationJaxbRootNodeAndPath(templateSetConfiguration, templateSetMap.get(templateSetConfiguration));
results.add(tempResult);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;
import java.util.Map;

import com.devonfw.cobigen.impl.config.entity.io.v6_0.TemplateSetConfiguration;
import com.devonfw.cobigen.impl.config.entity.io.v6_0.TemplatesConfiguration;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,10 +28,10 @@
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration;
import com.devonfw.cobigen.impl.config.entity.io.Trigger;
import com.devonfw.cobigen.impl.config.entity.io.v3_0.Link;
import com.devonfw.cobigen.impl.config.entity.io.v3_0.Links;
import com.devonfw.cobigen.impl.config.entity.io.v3_0.Tag;
import com.devonfw.cobigen.impl.config.entity.io.v3_0.Tags;
import com.devonfw.cobigen.impl.config.entity.io.v6_0.Link;
import com.devonfw.cobigen.impl.config.entity.io.v6_0.Links;
import com.devonfw.cobigen.impl.config.entity.io.v6_0.Tag;
import com.devonfw.cobigen.impl.config.entity.io.v6_0.Tags;

import jakarta.xml.bind.JAXB;
import jakarta.xml.bind.JAXBContext;
Expand Down Expand Up @@ -85,14 +87,14 @@ public TemplateSetUpgrader() {
* Upgrades the ContextConfiguration from v2.1 to the new structure from v3.0. The monolithic pom and context files
* will be split into multiple files corresponding to every template set that will be created.
*
* @param Path {@link Path} to the templatesLocation
* @param templatesLocation {@link Path} to the templatesLocation
*
* @return {@link Map} collection that contains the upgraded v3.0
* {@link com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration} as key and a {@link Path} for
* {@link com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration} as key and a {@link Path} for
* the new location of the context.xml as value
* @throws Exception if an issue occurred in directory copy operations
*/
public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration, Path> upgradeTemplatesToTemplateSets(
public Map<TemplateSetConfiguration, Path> upgradeTemplatesToTemplateSetsV6(
Path templatesLocation) throws Exception {

Path cobigenTemplatesFolder = CobiGenPaths.getPomLocation(templatesLocation);
Expand Down Expand Up @@ -131,11 +133,11 @@ public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration,
Path folderOfContextLocation = CobiGenPaths.getContextLocation(templatesLocation);
ContextConfiguration contextConfiguration = getContextConfiguration(folderOfContextLocation);

List<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration> contextFiles = splitContext(
List<com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration> contextFiles = splitContext(
contextConfiguration);
Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration, Path> contextMap = new HashMap<>();
for (com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration cc : contextFiles) {
for (com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger trigger : cc.getTrigger()) {
Map<TemplateSetConfiguration, Path> templateSetMap = new HashMap<>();
for (com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration cc : contextFiles) {
for (com.devonfw.cobigen.impl.config.entity.io.v6_0.Trigger trigger : cc.getTrigger()) {
Path triggerFolder = folderOfContextLocation.resolve(trigger.getTemplateFolder());
Path newTriggerFolder = adapted.resolve(trigger.getTemplateFolder());
Path utilsPath = folderOfContextLocation.getParent().resolve("java");
Expand All @@ -146,6 +148,7 @@ public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration,
LOG.error("An error occurred while copying the template Folder", e);
throw new CobiGenRuntimeException(e.getMessage(), e);
}

try {
if (Files.exists(utilsPath)) {
FileUtils.copyDirectory(utilsPath.toFile(),
Expand All @@ -156,18 +159,24 @@ public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration,
throw new CobiGenRuntimeException(e.getMessage(), e);
}

Path newContextPath = newTriggerFolder.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME);
contextMap.put(cc, newContextPath);
// creates actual context configuration file
try (OutputStream out = Files.newOutputStream(newContextPath)) {
JAXB.marshal(cc, out);
// Read templates.xml then delete it
Path tcPath = newTriggerFolder.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve("templates.xml");
TemplatesConfiguration tcV6 = readTemplatesConfigurationV6(tcPath);
Files.delete(tcPath);
// Use templates.xml and context.xml to generate template-set.xml
TemplateSetConfiguration tsc = buildTemplateSetConfigurationV6(tcV6, cc);
Path tscPath = newTriggerFolder.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME);
try (OutputStream out = Files.newOutputStream(tscPath)) {
JAXB.marshal(tsc, out);
}
// Figure out template-set.xml path in new folder structure
tscPath = templateSets.resolve(folderToRename.toPath().relativize(newTriggerFolder)
.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME));
templateSetMap.put(tsc, tscPath);
writeNewPomFile(cobigenTemplatesFolder, newTriggerFolder, trigger);
newContextPath = templateSets.resolve(folderToRename.toPath().relativize(newTriggerFolder)
.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME));
contextMap.put(cc, newContextPath);
}
}

Expand All @@ -179,21 +188,20 @@ public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration,
}
folderToRename.renameTo(templateSets.toFile());

return contextMap;

return templateSetMap;
}

/**
* Writes a pom.xml file for the split context and template folder
*
* @param Path {@link Path} to the CobiGen_Templates folder
* @param Path {@link Path} to the split template folder
* @param Trigger {@link com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger }to the related template folder
* @param cobigenTemplates {@link Path} to the CobiGen_Templates folder
* @param cobigenTemplates {@link Path} to the split template folder
* @param trigger {@link com.devonfw.cobigen.impl.config.entity.io.v6_0.Trigger} to the related template folder
* @throws IOException
* @throws FileNotFoundException
*/
private void writeNewPomFile(Path cobigenTemplates, Path newTemplateFolder,
com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger trigger) throws IOException, FileNotFoundException {
com.devonfw.cobigen.impl.config.entity.io.v6_0.Trigger trigger) throws IOException, FileNotFoundException {

// Pom.xml creation
try {
Expand Down Expand Up @@ -227,56 +235,90 @@ private void writeNewPomFile(Path cobigenTemplates, Path newTemplateFolder,
}

/**
* Splits a contextConfiguration and converts a {@link Trigger} and his data to a v3_0 Trigger
* Splits a contextConfiguration and converts a {@link Trigger} and his data to a v6_0 Trigger
*
* @param ContextConfiguration {@link ContextConfiguration} of the monolithic context that will be split
* @return {@link com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration} List of the split
* @param monolitic {@link ContextConfiguration} of the monolithic context that will be split
* @return {@link com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration} List of the split
* contextConfiguration files
*/
private List<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration> splitContext(
private List<com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration> splitContext(
ContextConfiguration monolitic) {

List<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration> splitContexts = new ArrayList<>();
List<com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration> splitContexts = new ArrayList<>();
List<Trigger> triggerList = monolitic.getTrigger();
for (Trigger trigger : triggerList) {
com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration contextConfiguration3_0 = new com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration();
com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger trigger3_0 = new com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger();
trigger3_0.setId(trigger.getId());
trigger3_0.setInputCharset(trigger.getInputCharset());
trigger3_0.setType(trigger.getType());
trigger3_0.setTemplateFolder(trigger.getTemplateFolder());

List<com.devonfw.cobigen.impl.config.entity.io.v3_0.Matcher> v3MList = this.mapper.mapAsList(trigger.getMatcher(),
com.devonfw.cobigen.impl.config.entity.io.v3_0.Matcher.class);
List<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContainerMatcher> v3CMList = this.mapper.mapAsList(
trigger.getContainerMatcher(), com.devonfw.cobigen.impl.config.entity.io.v3_0.ContainerMatcher.class);
trigger3_0.getContainerMatcher().addAll(v3CMList);
trigger3_0.getMatcher().addAll(v3MList);
contextConfiguration3_0.getTrigger().add(trigger3_0);
com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration contextConfiguration6_0 = new com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration();
com.devonfw.cobigen.impl.config.entity.io.v6_0.Trigger trigger6_0 = new com.devonfw.cobigen.impl.config.entity.io.v6_0.Trigger();
trigger6_0.setId(trigger.getId());
trigger6_0.setInputCharset(trigger.getInputCharset());
trigger6_0.setType(trigger.getType());
trigger6_0.setTemplateFolder(trigger.getTemplateFolder());

List<com.devonfw.cobigen.impl.config.entity.io.v6_0.Matcher> v3MList = this.mapper.mapAsList(trigger.getMatcher(),
com.devonfw.cobigen.impl.config.entity.io.v6_0.Matcher.class);
List<com.devonfw.cobigen.impl.config.entity.io.v6_0.ContainerMatcher> v3CMList = this.mapper.mapAsList(
trigger.getContainerMatcher(), com.devonfw.cobigen.impl.config.entity.io.v6_0.ContainerMatcher.class);
trigger6_0.getContainerMatcher().addAll(v3CMList);
trigger6_0.getMatcher().addAll(v3MList);
contextConfiguration6_0.getTrigger().add(trigger6_0);
Tags tags = new Tags();
Tag tag = new Tag();
tag.setName(
"PLACEHOLDER---This tag was inserted through the upgrade process and has to be changed manually---PLACEHOLDER");
tags.getTag().add(tag);
contextConfiguration3_0.setTags(tags);
contextConfiguration6_0.setTags(tags);
Links links = new Links();
Link link = new Link();
link.setUrl(
"PLACEHOLDER---This tag was inserted through the upgrade process and has to be changed manually---PLACEHOLDER");
links.getLink().add(link);
contextConfiguration3_0.setLinks(links);
contextConfiguration3_0.setVersion(new BigDecimal("3.0"));
splitContexts.add(contextConfiguration3_0);
contextConfiguration6_0.setLinks(links);
contextConfiguration6_0.setVersion(new BigDecimal("3.0"));
splitContexts.add(contextConfiguration6_0);
}
return splitContexts;
}

private TemplateSetConfiguration buildTemplateSetConfigurationV6(TemplatesConfiguration tcV6,
com.devonfw.cobigen.impl.config.entity.io.v6_0.ContextConfiguration ccV6) {

TemplateSetConfiguration tsc = new TemplateSetConfiguration();
tsc.setVersion(new BigDecimal("6.0"));
tsc.setTemplatesConfiguration(tcV6);
tsc.setContextConfiguration(ccV6);
return tsc;
}

/**
* Reads templates.xml file
*
* @param templatesFile {@link Path} to the templates.xml file
* @return {@link TemplatesConfiguration} V6 templates configuration object
*/
private TemplatesConfiguration readTemplatesConfigurationV6(Path templatesFile) throws IOException, JAXBException {

try (InputStream in = Files.newInputStream(templatesFile)) {
Unmarshaller um = JAXBContext.newInstance(TemplatesConfiguration.class).createUnmarshaller();

Object rootNode = um.unmarshal(in);
if (rootNode instanceof TemplatesConfiguration) {
TemplatesConfiguration tc = (TemplatesConfiguration) rootNode;
tc.setVersion(new BigDecimal("5.0"));
return tc;
}
} catch (IOException e) {
throw new InvalidConfigurationException("Templates file could not be found", e);
} catch (JAXBException e) {
throw new InvalidConfigurationException("Templates file provided some XML errors", e);
}
return null;
}

/**
* Locates and returns the correct context file
*
* @param path {@link Path} to the contextFile
* @param contextFile {@link Path} to the contextFile
* @return {@link ContextConfiguration}
* @throws Exception
*/
private ContextConfiguration getContextConfiguration(Path contextFile) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" xmlns:tns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="5.0">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" targetNamespace="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" elementFormDefault="qualified" version="5.0">

<xs:element name="templatesConfiguration">
<xs:complexType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://capgemini.com/devonfw/cobigen/TemplateSetConfiguration"
targetNamespace="http://capgemini.com/devonfw/cobigen/TemplateSetConfiguration"
xmlns:cc="http://capgemini.com/devonfw/cobigen/ContextConfiguration"
xmlns:tc="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" elementFormDefault="qualified" version="6.0">


<xs:import schemaLocation="../v3.0/contextConfiguration.xsd"
namespace="http://capgemini.com/devonfw/cobigen/ContextConfiguration"/>
<xs:import schemaLocation="../v5.0/templatesConfiguration.xsd"
namespace="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration"/>

<xs:element name="templateSetConfiguration">
<xs:complexType>
<xs:all>
<xs:element ref="cc:contextConfiguration"/>
<xs:element ref="tc:templatesConfiguration"/>
</xs:all>
<xs:attribute name="version" use="required" type="xs:decimal"/>
</xs:complexType>
</xs:element>
</xs:schema>
Loading