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

1502 config upgrade #1528

Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
f0ac1d5
Adjust documentation (#1491)
GuentherJulian Mar 10, 2022
d50efe5
changed eclipse repository to https
maybeec Mar 23, 2022
c90973d
Merge branch 'template-set-deployables' of https://github.com/devonfw…
GuentherJulian Apr 12, 2022
afa4009
Merge branch 'template-set-deployables' of https://github.com/devonfw…
GuentherJulian Apr 27, 2022
99d0412
Merge branch 'template-set-deployables' of https://github.com/devonfw…
GuentherJulian Apr 27, 2022
ac142b3
implemented draft for template set upgrader
GuentherJulian Apr 27, 2022
de42673
trigger getting converted to xml
EduardKrieger May 3, 2022
e32c2f9
context will be splitted, pom generation in progress
EduardKrieger May 3, 2022
366015c
removed useless fields
EduardKrieger May 3, 2022
00ea9f7
removed some comments
EduardKrieger May 3, 2022
b03f97a
implemented some test
EduardKrieger May 5, 2022
962fc72
upgrader implemented
EduardKrieger May 10, 2022
07df50c
added backup folder for old templates
EduardKrieger May 10, 2022
d4409aa
added ignore to templatesetupgradetests
EduardKrieger May 11, 2022
1414163
added ignore to templatesetupgradetests
EduardKrieger May 11, 2022
deff99f
chaanged upgrading process and changed the tests of the upgrade
EduardKrieger Jun 3, 2022
107583c
test for sucessful backup
EduardKrieger Jun 3, 2022
fed6aeb
changed test to use multiple context files
EduardKrieger Jun 14, 2022
2d85462
#1502 changed context version of reader test files to lates compatible
MikeSchumacherCapgemini Jun 14, 2022
faa4d39
changed context version in the jars from the test files
EduardKrieger Jun 14, 2022
f64a021
reworked upgrade step to work with the upgrade prozess to 3.0
EduardKrieger Jun 14, 2022
f81c815
#1502 fixed latest context version in javaplugin integration test
MikeSchumacherCapgemini Jun 14, 2022
50da8b6
Merge branch '1502_config_upgrade' of https://github.com/EduardKriege…
MikeSchumacherCapgemini Jun 14, 2022
691bffa
#1502 fixed some more context versions in tests
MikeSchumacherCapgemini Jun 14, 2022
46ad6b3
moved template upgrade process out of the abstract class
EduardKrieger Jun 14, 2022
e8dfe31
Merge branch '1502_config_upgrade' of https://github.com/EduardKriege…
EduardKrieger Jun 14, 2022
e5641ca
implemented requested changes
EduardKrieger Jun 15, 2022
7e38e28
removed some files
EduardKrieger Jun 16, 2022
8a22fbb
Merge branch 'master' into 1502_config_upgrade
EduardKrieger Jun 16, 2022
dbf2ac7
the requested changes have been added
EduardKrieger Jun 21, 2022
29bff44
changed use of validate process for the readers
EduardKrieger Jun 21, 2022
0398156
Merge branch 'template-set-deployables' into 1502_config_upgrade
EduardKrieger Jun 21, 2022
35b5b29
removed log
EduardKrieger Jun 21, 2022
541aa4d
removed import
EduardKrieger Jun 21, 2022
e4bb73f
#1502 added tags to template sets
jan-vcapgemini Jun 23, 2022
b3fd84b
used correct formatting for few files
EduardKrieger Jun 23, 2022
ae0b497
#1502 addressed requested changes
MikeSchumacherCapgemini Jun 28, 2022
9f22aeb
#1502 replaced generic exceptions with CobigenRuntimeExceptions
MikeSchumacherCapgemini Jun 28, 2022
bfe0ba2
#1502 applied proper code reformat
jan-vcapgemini Jul 1, 2022
3b2cdce
#1502 fixed template set upgrader and tests
jan-vcapgemini Jul 1, 2022
d5654b2
#1502 fixed template set upgrader and tests
jan-vcapgemini Jul 1, 2022
ef431dc
#1502 fixed template set upgrader and tests
jan-vcapgemini Jul 1, 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
6 changes: 6 additions & 0 deletions cobigen/cobigen-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
<artifactId>orika-core</artifactId>
<version>1.5.4</version>
</dependency>

<dependency>
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.8.5</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, Backu
}
if (currentVersion == this.versions[i]) {
LOG.info("Upgrading {} '{}' from version {} to {}...", this.configurationName, configurationFile.toUri(),
this.versions[i], this.versions[i + 1]);
this.versions[i], this.versions[i + 1]); // TODO

Object rootNode;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import ma.glasnost.orika.impl.DefaultMapperFactory;

/**
* This class encompasses all logic for legacy context configuration detection and upgrading these to the latest
* This class encompasses all logic for legacy context configuration detection
* and upgrading these to the latest
* supported version.
*
* @author mbrunnli (Jun 22, 2015)
Expand Down Expand Up @@ -73,6 +74,9 @@ protected ConfigurationUpgradeResult performNextUpgradeStep(ContextConfiguration
upgradedConfig_3_0.setVersion(new BigDecimal("3.0"));

result.setResultConfigurationJaxbRootNode(upgradedConfig_3_0);

TemplateSetUpgrader templateSetUpgrader = new TemplateSetUpgrader(ConfigurationConstants.DEFAULT_HOME);
templateSetUpgrader.upradeTemplatesToTemplateSets();
break;
default:
throw new NotYetSupportedException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
package com.devonfw.cobigen.impl.config.upgrade;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.impl.config.entity.io.ContextConfiguration;
import com.devonfw.cobigen.impl.config.entity.io.Trigger;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Upgrader for the templates from v2_1 to v3_0 that splits the monolithic
* template structure into version 3.0 with template sets
*/
public class TemplateSetUpgrader {

private Path templatesLocation;

/** Logger instance. */
GuentherJulian marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger LOG = LoggerFactory.getLogger(TemplateSetUpgrader.class);
/** Mapper factory instance. */
private MapperFactory mapperFactory;
/** Mapper Facade instance. */
private MapperFacade mapper;

/**
* Creates a new {@link TemplateSetUpgrader} instance
*/
public TemplateSetUpgrader(Path templatesLocation) {
this.templatesLocation = templatesLocation;
this.mapperFactory = new DefaultMapperFactory.Builder().useAutoMapping(true).mapNulls(true).build();
this.mapperFactory
.classMap(com.devonfw.cobigen.impl.config.entity.io.ContainerMatcher.class,
com.devonfw.cobigen.impl.config.entity.io.v3_0.ContainerMatcher.class)
.field("retrieveObjectsRecursively:{isRetrieveObjectsRecursively|setRetrieveObjectsRecursively(new Boolean(%s))|type=java.lang.Boolean}",
"retrieveObjectsRecursively:{isRetrieveObjectsRecursively|setRetrieveObjectsRecursively(new Boolean(%s))|type=java.lang.Boolean}")
.byDefault().register();
this.mapperFactory.classMap(com.devonfw.cobigen.impl.config.entity.io.Trigger.class,
com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger.class).byDefault().register();
this.mapperFactory.classMap(com.devonfw.cobigen.impl.config.entity.io.Matcher.class,
com.devonfw.cobigen.impl.config.entity.io.v3_0.Matcher.class).byDefault().register();
this.mapperFactory
.classMap(com.devonfw.cobigen.impl.config.entity.io.ContainerMatcher.class,
com.devonfw.cobigen.impl.config.entity.io.v3_0.ContainerMatcher.class)
.field("retrieveObjectsRecursively:{isRetrieveObjectsRecursively|setRetrieveObjectsRecursively(new Boolean(%s))|type=java.lang.Boolean}",
"retrieveObjectsRecursively:{isRetrieveObjectsRecursively|setRetrieveObjectsRecursively(new Boolean(%s))|type=java.lang.Boolean}")
.byDefault().register();
this.mapper = mapperFactory.getMapperFacade();
}

/**
* Upgrades the template structure 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.
* @throws Exception
*/
public void upradeTemplatesToTemplateSets() throws Exception {

if (this.templatesLocation == null) {
throw new Exception("Templates location cannot be null!");
}

if (this.templatesLocation.endsWith(ConfigurationConstants.TEMPLATES_FOLDER)) {
Path cobigenTemplates = this.templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
if (Files.exists(cobigenTemplates)) {
Path contextFile = cobigenTemplates.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME);
if (Files.exists(contextFile)) {
ContextConfiguration contextConfiguration = getContextConfiguration(contextFile);
if (contextConfiguration != null) {
// create new template set folder
Path templateSetsPath = Files.createDirectory(this.templatesLocation.getParent()
.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER));
Path adaptedFolder = Files
.createDirectory(templateSetsPath.resolve(ConfigurationConstants.ADAPTED_FOLDER));
List<Trigger> triggers = contextConfiguration.getTrigger();
for (Trigger trigger : triggers) {
processTrigger(trigger, cobigenTemplates, adaptedFolder);
}

// backup of old files
Path backupFolder = templatesLocation.resolve("backup");
if (this.templatesLocation.endsWith(ConfigurationConstants.TEMPLATES_FOLDER)) {
backupFolder = templatesLocation.getParent().resolve("backup");
}
File f = backupFolder.toFile();
if (!f.exists()) {
f.mkdir();
}
try {
FileUtils.moveDirectoryToDirectory(cobigenTemplates.getParent().toFile(), f, false);
} catch (IOException e) {
LOG.error("Error copying and deleting the old template files", e);
throw e;
}
} else {
LOG.info("Unable to parse context.xml file {}.", contextFile);
}
} else {
LOG.info("No context.xml file found. {}", contextFile);
}
} else {
LOG.info("No CobiGen_Templates folder found. Upgrade needs an adapted templates folder.");
}
} else {
LOG.info("The path {} is no valid templates location.", this.templatesLocation);
}

}

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

try (InputStream in = Files.newInputStream(contextFile)) {
Unmarshaller unmarschaller = JAXBContext.newInstance(ContextConfiguration.class).createUnmarshaller();

Object rootNode = unmarschaller.unmarshal(in);
if (rootNode instanceof ContextConfiguration) {
return (ContextConfiguration) rootNode;
}
} catch (IOException e) {
throw new InvalidConfigurationException("Context file could not be found", e);
} catch (JAXBException e) {
throw new InvalidConfigurationException("Context file provided some XML errors", e);
}
return null;
}

/**
* Converts a {@link Trigger} to a v3 Trigger and uses the data from the Trigger to create a new Template Set with the
* template files from the old structure and builds context.xml and pom.xml from the Trigger data.
* @param {@link Trigger} trigger for a template set
* @param {@link Path} Path to the cobigen templates folder
* @param {@link Path} Path to the adapted Folder in template sets
* @throws IOException
* @throws XmlPullParserException
*/
private void processTrigger(Trigger trigger, Path cobigenTemplates, Path templateSetsAdapted)
throws IOException, XmlPullParserException {

Path templatesPath = cobigenTemplates.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(trigger.getTemplateFolder());
Path templateSetPath = Files.createDirectory(templateSetsAdapted.resolve(trigger.getTemplateFolder()));

// copy template files
FileUtils.copyDirectory(templatesPath.toFile(),
templateSetPath.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER).toFile());

// copy java utils
Path utilsPath = cobigenTemplates.resolve("src/main/java");
FileUtils.copyDirectory(utilsPath.toFile(), templateSetPath.resolve("src/main/java").toFile());
// src main ressources copieren

// create context.xml
com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration contextConfiguration = new com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration();
contextConfiguration.setVersion(new BigDecimal(3.0));

// create new trigger
List<com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger> triggerList = contextConfiguration.getTrigger();
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());

// map containerMatcher and matcher to v.3_0
List<com.devonfw.cobigen.impl.config.entity.io.v3_0.Matcher> v3MList = 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 = 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);

// add trigger to context
triggerList.add(trigger3_0);

// write context.xml
Path newContextPath = templateSetPath.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
newContextPath = newContextPath.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME);
try {
Marshaller marshaller = JAXBContext.newInstance("com.devonfw.cobigen.impl.config.entity.io.v3_0")
.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(contextConfiguration, newContextPath.toFile());
} catch (JAXBException e) {
throw new InvalidConfigurationException("Parsing of the context file provided some XML errors", e);
}

// Pom.xml creation
MavenXpp3Reader reader = new MavenXpp3Reader();
MavenXpp3Writer writer = new MavenXpp3Writer();
Model mMonolithicPom;
FileInputStream pomInputStream = new FileInputStream(cobigenTemplates.resolve("pom.xml").toFile());
try {
mMonolithicPom = reader.read(pomInputStream);
} catch (FileNotFoundException e) {
LOG.error("Monolitic pom file could not be found", e);
pomInputStream.close();
throw e;
} catch (IOException e) {
LOG.error("IOError while reading the monolithic pom file", e);
pomInputStream.close();
throw e;
} catch (XmlPullParserException e) {
LOG.error("XMLError while parsing the monolitic pom file", e);
pomInputStream.close();
throw e;
}
Model m = new Model();
GuentherJulian marked this conversation as resolved.
Show resolved Hide resolved
Parent p = new Parent();
p.setArtifactId(mMonolithicPom.getArtifactId());
p.setGroupId(mMonolithicPom.getGroupId());
p.setVersion(mMonolithicPom.getVersion());
m.setParent(p);
m.setDependencies(mMonolithicPom.getDependencies());
m.setArtifactId(trigger.getId().replace('_', '-'));
m.setName("PLACEHOLDER---Replace this text with a correct template name---PLACEHOLDER");
FileOutputStream pomOutputStream = new FileOutputStream(templateSetPath.resolve("pom.xml").toFile());
try {
writer.write(new FileOutputStream(templateSetPath.resolve("pom.xml").toFile()), m);
} catch (FileNotFoundException e) {
LOG.error("Error while creating the new v3_0 pom file", e);
pomInputStream.close();
pomOutputStream.close();
throw e;
} catch (IOException e) {
LOG.error("IOError while writing the new v3_0 pom file", e);
pomInputStream.close();
pomOutputStream.close();
throw e;
}
pomInputStream.close();
pomOutputStream.close();

}

}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class ContextConfigurationUpgraderTest extends AbstractUnitTest {
*
* @throws Exception test fails
*/
@Ignore
@Test
public void testCorrectUpgrade_v2_0_TO_LATEST() throws Exception {

Expand Down Expand Up @@ -70,7 +69,6 @@ public void testCorrectUpgrade_v2_0_TO_LATEST() throws Exception {
*
* @throws Exception test fails
*/
@Ignore
@Test
public void testCorrectUpgrade_v2_1_TO_LATEST() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class TemplateConfigurationUpgraderTest extends AbstractUnitTest {
*
* @throws Exception test fails
*/
@Ignore

MikeSchumacherCapgemini marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testCorrectUpgrade_v1_2_TO_LATEST() throws Exception {

Expand Down Expand Up @@ -71,7 +71,6 @@ public void testCorrectUpgrade_v1_2_TO_LATEST() throws Exception {
*
* @throws Exception test fails
*/
@Ignore
@Test
MikeSchumacherCapgemini marked this conversation as resolved.
Show resolved Hide resolved
public void testCorrectUpgrade_v2_1_TO_LATEST() throws Exception {

Expand Down
Loading