Skip to content

Commit

Permalink
devonfw#1518 Implemented requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MansourD committed Aug 18, 2022
1 parent 13c521a commit 260c0e5
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void upgradeAndGenerateFromEntityTest() throws Exception {
FileUtils.copyDirectory(new File(testFileRootPath + "templatesproject/templates-devon4j"),
this.tmpProject.toFile());
File baseProject = this.tmpProject.resolve("maven.project/core/").toFile();
File monolithicConfiguration = this.tmpProject.resolve("src/main/templates").toFile();
File monolithicConfiguration = this.tmpProject.toFile();
String args[] = new String[7];
args[0] = "generate";
args[1] = this.entityInputFile.getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ private static void openMonolithicConfigurationErrorDialog(DeprecatedMonolithicC
if (result == 0) {
try {
ResourcesPluginUtil
.upgradeConfiguration(DeprecatedMonolithicConfigurationException.getMonolithicConfiguration());
.startTemplatesUpgrader(DeprecatedMonolithicConfigurationException.getMonolithicConfiguration());
successUpgrade.open();
} catch (Throwable a) {
LOG.error("An error occurred while upgrading the templates!.", a);
PlatformUIUtil.openErrorDialog("An error occurred while upgrading the templates! " + a.getMessage(), a);
LOG.error("An error has occurred while upgrading the templates !. ", a);
PlatformUIUtil.openErrorDialog("An error has occurred while upgrading the templates !. " + a.getMessage(), a);
}
}
if (result == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static void setUserWantsToDownloadTemplates(boolean userWantsToDownloadTe
*
*
*/
public static void upgradeConfiguration(Path monolithicConfiguration) {
public static void startTemplatesUpgrader(Path monolithicConfiguration) {

TemplateAdapter templateAdapter;
if (monolithicConfiguration == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,44 @@ public static Path getExternalProcessesPath(String processPath) {
}

/**
* checks the path for any pom.xml in a parent Folder and returns its path
* return the path for the context.xml in a monolithic structure
*
* @param childPath path of the templates
* @return path to the pom.xml
* @param templatesLocation the path to the Cobigen templates project
* @return the parent path to the context.xml
*/
public static Path getTemplatesPomFileLocation(Path childPath) {
public static Path getContextLocation(Path templatesLocation) {

if (Files.exists(templatesLocation.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME))) {
return templatesLocation;
} else if (Files.exists(templatesLocation.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME))) {
return templatesLocation.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
} else if (Files.exists(templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES)
.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME))) {
return templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES)
.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
} else {
throw new CobiGenRuntimeException("Could not find any context.xml !" + templatesLocation);
}
}

String pomFile = MavenConstants.POM;
int i = 0;
while (!Files.exists(childPath.resolve(pomFile)) && i <= 4) {
childPath = childPath.getParent();
if (childPath == null) {
/**
* return the path of the found pom.xml in a monolithic structure
*
* @param templatesLocation the path to the Cobigen templates project
* @return parent path to the found pom.xml
*/
public static Path getPomLocation(Path templatesLocation) {

return null;
}
i++;
}
if (!Files.exists(childPath.resolve(pomFile))) {
throw new CobiGenRuntimeException("Could not find any pom file in the given Path!");
if (Files.exists(templatesLocation.resolve(MavenConstants.POM))) {
return templatesLocation;
} else if (Files
.exists(templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES).resolve(MavenConstants.POM))) {
return templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
} else {
throw new CobiGenRuntimeException("Could not find any pom.xml !" + templatesLocation);
}
return childPath;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
Expand All @@ -32,6 +33,7 @@
import com.devonfw.cobigen.impl.config.constant.ContextConfigurationVersion;
import com.devonfw.cobigen.impl.config.upgrade.AbstractConfigurationUpgrader;
import com.devonfw.cobigen.impl.config.upgrade.ContextConfigurationUpgrader;
import com.devonfw.cobigen.impl.util.ConfigurationFinder;
import com.devonfw.cobigen.impl.util.FileSystemUtil;

/**
Expand All @@ -52,15 +54,11 @@ public class TemplateAdapterImpl implements TemplateAdapter {
*/
public TemplateAdapterImpl() {

Path templatesLocationPath = CobiGenPaths.getTemplateSetsFolderPath(false);
if (Files.exists(templatesLocationPath)) {
this.templatesLocation = templatesLocationPath;
} else {
templatesLocationPath = CobiGenPaths.getTemplatesFolderPath();
if (Files.exists(templatesLocationPath)) {
this.templatesLocation = templatesLocationPath;
}
URI templatesLocationPath = ConfigurationFinder.findTemplatesLocation();
if (Files.exists(Paths.get(templatesLocationPath))) {
this.templatesLocation = Paths.get(templatesLocationPath);
}

}

@Override
Expand Down Expand Up @@ -369,18 +367,13 @@ public Path upgradeMonolithicTemplates(Path templatesProject) {

Path templatesPath = null;
if (templatesProject != null) {
List<Path> contextXml = FileSystemUtil.collectAllContextXML(templatesProject);
templatesPath = FileSystemUtil.createFileSystemDependentPath(contextXml.get(0).getParent().toUri());
templatesPath = FileSystemUtil.createFileSystemDependentPath(templatesProject.toUri());
} else {
templatesPath = FileSystemUtil.createFileSystemDependentPath(
CobiGenPaths.getTemplatesFolderPath().resolve(ConfigurationConstants.COBIGEN_TEMPLATES)
.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER).toUri());
templatesPath = FileSystemUtil.createFileSystemDependentPath(ConfigurationFinder.findTemplatesLocation());
}

AbstractConfigurationUpgrader<ContextConfigurationVersion> contextUpgraderObject = new ContextConfigurationUpgrader();

// Upgrade the context.xml to the new template-set with latest version
contextUpgraderObject.resolveLatestCompatibleSchemaVersion(templatesPath);
contextUpgraderObject.upgradeConfigurationToLatestVersion(templatesPath, BackupPolicy.NO_BACKUP);

LOG.info("context.xml upgraded successfully. {}", templatesPath);
Expand All @@ -391,7 +384,5 @@ public Path upgradeMonolithicTemplates(Path templatesProject) {
Path newTemplates = cobigenHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
LOG.info("New templates location: {} ", newTemplates);
return newTemplates;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
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.constant.ContextConfigurationVersion;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.config.upgrade.AbstractConfigurationUpgrader;
import com.devonfw.cobigen.impl.config.upgrade.ContextConfigurationUpgrader;
import com.devonfw.cobigen.impl.extension.PluginRegistry;
import com.devonfw.cobigen.impl.util.FileSystemUtil;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -114,15 +112,18 @@ public ContextConfiguration readContextConfiguration() {
}

/**
* @return return if the template folder structure consists of template sets or if the old structure is used
* @return return if the template folder structure consists of template sets or if the monolithic structure is used.
* This is determined by searching the context.xml and check the version if it is compliant.
*
*/
public boolean isTemplateSetConfiguration() {

if (this.configurationPath.toUri().getScheme().equals("jar"))
if (this.configurationPath.toUri().getScheme().equals("jar")
|| !this.configurationPath.getFileName().toString().equals(ConfigurationConstants.TEMPLATE_SETS_FOLDER)) {
return false;
List<Path> result = FileSystemUtil.collectAllContextXML(this.configurationPath);
AbstractConfigurationUpgrader<ContextConfigurationVersion> contextConfigurationObject = new ContextConfigurationUpgrader();
return contextConfigurationObject.isCompliantToLatestSupportedVersion(result.get(0).getParent());
}
return true;

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.exception.NotYetSupportedException;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.ExceptionUtil;
import com.devonfw.cobigen.api.util.JvmUtil;
import com.devonfw.cobigen.impl.exceptions.BackupFailedException;
Expand Down Expand Up @@ -266,17 +267,23 @@ public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, Backu
/**
* Upgrades the configuration to the latest supported version with option to limit lookup of the maximum version.
*
* @param configurationRoot the root folder containing the configuration with the specified
* {@link #configurationFilename}. See {@link #AbstractConfigurationUpgrader(Enum, Class, String)} for more
* information.
* @param templatesProject the path to the templates project
* @param backupPolicy the {@link BackupPolicy} to choose if a backup is necessary or not.
* @param maxVersion the latest version to be used for the upgrade (limits the versions to choose from)
* @return if manual adoptions has to be performed after upgrading
* @return if a manual adoptions has to be performed after upgrading
* @throws BackupFailedException if the backup could not be created
*
* configurationRoot the root folder containing the configuration with the specified
* {@link #configurationFilename}. See {@link #AbstractConfigurationUpgrader(Enum, Class, String)} for more
* information.
*/
public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, BackupPolicy backupPolicy,
public boolean upgradeConfigurationToLatestVersion(Path templatesProject, BackupPolicy backupPolicy,
VERSIONS_TYPE maxVersion) {

Path configurationLocation = templatesProject;
if (this.getClass().equals(ContextConfigurationUpgrader.class))
configurationLocation = CobiGenPaths.getContextLocation(templatesProject);

boolean manualAdoptionsNecessary = false;

VERSIONS_TYPE validatedVersion;
Expand All @@ -285,13 +292,13 @@ public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, Backu
// check if versions need to be limited
if (maxVersion != null) {
versionsList = limitVersions(maxVersion);
validatedVersion = resolveLatestCompatibleSchemaVersion(configurationRoot, maxVersion);
validatedVersion = resolveLatestCompatibleSchemaVersion(configurationLocation, maxVersion);
} else {
versionsList = Arrays.asList(this.versions);
validatedVersion = resolveLatestCompatibleSchemaVersion(configurationRoot);
validatedVersion = resolveLatestCompatibleSchemaVersion(configurationLocation);
}

Path configurationFile = configurationRoot.resolve(this.configurationFilename);
Path configurationFile = configurationLocation.resolve(this.configurationFilename);
if (validatedVersion == null) {
throw new InvalidConfigurationException(configurationFile.toUri().toString(),
StringUtils.capitalize(this.configurationName) + " does not match any current or legacy schema definitions.");
Expand All @@ -314,7 +321,7 @@ public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, Backu
createBackup(configurationFile, backupPolicy);

List<ConfigurationUpgradeResult> results = performNextUpgradeStep(versionsList.get(i), rootNode,
configurationRoot);
templatesProject);
for (ConfigurationUpgradeResult result : results) {

manualAdoptionsNecessary |= result.areManualAdoptionsNecessary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.NotYetSupportedException;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.impl.config.ContextConfiguration;
import com.devonfw.cobigen.impl.config.constant.ContextConfigurationVersion;

Expand Down Expand Up @@ -35,8 +36,9 @@ public ContextConfigurationUpgrader() {

@Override
protected List<ConfigurationUpgradeResult> performNextUpgradeStep(ContextConfigurationVersion source,
Object previousConfigurationRootNode, Path configurationRoot) throws Exception {
Object previousConfigurationRootNode, Path templatesLocation) throws Exception {

Path configurationRoot = CobiGenPaths.getContextLocation(templatesLocation);
ConfigurationUpgradeResult result = new ConfigurationUpgradeResult();
MapperFactory mapperFactory;
MapperFacade mapper;
Expand Down Expand Up @@ -72,7 +74,7 @@ protected List<ConfigurationUpgradeResult> performNextUpgradeStep(ContextConfigu
case v2_1:
TemplateSetUpgrader templatesSetUpgrader = new TemplateSetUpgrader();
Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration, Path> contextMap = templatesSetUpgrader
.upgradeTemplatesToTemplateSets(configurationRoot);
.upgradeTemplatesToTemplateSets(templatesLocation);
for (com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration context : contextMap.keySet()) {
ConfigurationUpgradeResult tempResult = new ConfigurationUpgradeResult();
tempResult.setResultConfigurationJaxbRootNodeAndPath(context, contextMap.get(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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.util.FileSystemUtil;

import jakarta.xml.bind.JAXB;
import jakarta.xml.bind.JAXBContext;
Expand Down Expand Up @@ -86,7 +85,7 @@ 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 contextLocation the location of the context configuration file
* @param templatesLocation the location of the templates
*
* @param {@link Path} Path to the context.xml that will be upgraded
* @return {@link Map} collection that contains the upgraded v3.0
Expand All @@ -95,20 +94,19 @@ public TemplateSetUpgrader() {
* @throws Exception if an issue occurred in directory copy operations
*/
public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration, Path> upgradeTemplatesToTemplateSets(
Path contextLocation) throws Exception {
Path templatesLocation) throws Exception {

Path cobigenHome = CobiGenPaths.getCobiGenHomePath();
Path folderOfContextLocation = CobiGenPaths.getContextLocation(templatesLocation);

List<Path> newContextLocation = FileSystemUtil.collectAllContextXML(contextLocation);
Path folderOfContextLocation = newContextLocation.get(0).getParent();
ContextConfiguration contextConfiguration = getContextConfiguration(folderOfContextLocation);
Path templateSets = cobigenHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
if (!Files.exists(templateSets))
Files.createDirectory(templateSets);
Path adapted = templateSets.resolve(ConfigurationConstants.ADAPTED_FOLDER);
if (!Files.exists(adapted))
Files.createDirectory(adapted);
Path cobigenTemplatesFolder = CobiGenPaths.getTemplatesPomFileLocation(newContextLocation.get(0));
Path cobigenTemplatesFolder = CobiGenPaths.getPomLocation(templatesLocation);

List<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration> contextFiles = splitContext(
contextConfiguration);
Expand Down Expand Up @@ -178,7 +176,8 @@ private void writeNewPomFile(Path cobigenTemplates, Path newTemplateFolder,
try {
Path oldPom = cobigenTemplates.resolve(MavenConstants.POM);
Path newPom = newTemplateFolder.resolve(MavenConstants.POM);
Files.createFile(newPom);
if (!Files.exists(newPom))
Files.createFile(newPom);

// read the content of the pom.xml then replace it
Charset charset = StandardCharsets.UTF_8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private static URI findTemplates(Path home) {
Path templatesFolderPath = templatesPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);

// 1. create/use new template sets folder
Path templateSetsFolderPath = CobiGenPaths.getTemplateSetsFolderPath(home, true);
Path templateSetsFolderPath = CobiGenPaths.getTemplateSetsFolderPath(home, false);

Path templateSetsAdaptedFolderPath = templateSetsFolderPath.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Path templateSetsDownloadedFolderPath = templateSetsFolderPath.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);
Expand All @@ -188,14 +188,7 @@ private static URI findTemplates(Path home) {
return jarFilePath.toUri();
}
}

// 5. download template set jars

LOG.info("Could not find any templates in cobigen home directory {}. Downloading...",
CobiGenPaths.getCobiGenHomePath());

TemplatesJarUtil.downloadLatestDevon4jTemplates(true, templatesPath.toFile());
TemplatesJarUtil.downloadLatestDevon4jTemplates(false, templatesPath.toFile());
templateSetsFolderPath = CobiGenPaths.getTemplateSetsFolderPath(home, true);
return templateSetsFolderPath.toUri();
}

Expand Down
Loading

0 comments on commit 260c0e5

Please sign in to comment.