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 16, 2022
1 parent 775164b commit 13c521a
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ private void checkMonolithicConfigurationException() {
CobiGenUtils.initializeCobiGen(this.templatesProject, false);
} catch (DeprecatedMonolithicConfigurationException e) {
if (this.templatesProject == null)
LOG.warn("Found old configuration at: {} ", ConfigurationFinder.findTemplatesLocation());
LOG.warn("Found monolithic configuration at: {} ", ConfigurationFinder.findTemplatesLocation());
else
LOG.warn("Found old configuration at: {} ", this.templatesProject);
LOG.warn("Found monolithic configuration at: {} ", this.templatesProject);
LOG.warn(e.getMessage());
if (this.upgradeConfiguration) {
startTemplatesUpgrader();
Expand All @@ -298,7 +298,7 @@ private void askUserToUpgradeTemplates() {
WikiConstants.WIKI_UPGRADE_MONOLITHIC_CONFIGURATION, System.getProperty("user.dir"));

boolean setToUserDir = ValidationUtils.yesNoPrompt("Upgrading templates configuration...: ",
MessagesConstants.INVALID_YES_NO_ANSWER_DESCRIPTION, "Continue generation with old templates...");
MessagesConstants.INVALID_YES_NO_ANSWER_DESCRIPTION, "Continue generation with monolithic templates...");

if (setToUserDir) {
startTemplatesUpgrader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,21 @@ public static void setUserWantsToDownloadTemplates(boolean userWantsToDownloadTe
}

/**
* start the Upgrader to upgrade the monolithic templates to the new template-set structure
* creates an instance of templatesAdapter and then upgrades the templates.
*
* @param monolithicConfiguration path to the monolithic Configuration
*
* @return
*
*/
public static void upgradeConfiguration(Path oldConfiguration) {
public static void upgradeConfiguration(Path monolithicConfiguration) {

TemplateAdapter templateAdapter;
if (oldConfiguration == null) {
if (monolithicConfiguration == null) {
templateAdapter = new TemplateAdapterImpl();
} else {
templateAdapter = new TemplateAdapterImpl(oldConfiguration);
templateAdapter = new TemplateAdapterImpl(monolithicConfiguration);
}
templateAdapter.upgradeMonolithicTemplates(oldConfiguration);
templateAdapter.upgradeMonolithicTemplates(monolithicConfiguration);
TemplatesUpgraded = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ private static CobiGen initializeCobiGen(IProject generatorProj, boolean allowMo
Path templateSetsAdaptedFolderPath = templatesDirectoryPath.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Path templateSetsDownloadedFolderPath = templatesDirectoryPath.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);

if (!ResourcesPluginUtil.generatorProjExists || ResourcesPluginUtil.TemplatesUpgraded) {
if (!ResourcesPluginUtil.generatorProjExists || ResourcesPluginUtil.TemplatesUpgraded
|| generatorProj.getLocationURI() == null) {

/*
* After the upgrade the new template-sets will be used. Only once! Then the old configuration has to be deleted
Expand All @@ -287,11 +288,12 @@ private static CobiGen initializeCobiGen(IProject generatorProj, boolean allowMo
return CobiGenFactory.create(jarPath.toUri(), allowMonolithicConfiguration);
}
}
if (generatorProj == null) {
if (generatorProj.getLocationURI() == null) {
throw new GeneratorCreationException(
"Configuration source could not be read. Have you downloaded the templates?");
} else {
return CobiGenFactory.create(generatorProj.getLocationURI(), allowMonolithicConfiguration);
}
return CobiGenFactory.create(generatorProj.getLocationURI(), allowMonolithicConfiguration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ public class ConfigurationConstants {
/** Name of the backup folder containing the old configuration */
public static final String BACKUP_FOLDER = "backup";

/**
* pom.xml file name
*/
public static final String POM_CONFIG_FILENAME = "pom.xml";

/**
* Default cobigen version
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.constants.MavenConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;

/**
Expand Down Expand Up @@ -177,28 +178,6 @@ public static Path getExternalProcessesPath(String processPath) {

}

/**
* checks the path for any root .cobigen Folder and returns its path
*
* @param templates path to the templates location
* @return path to the root .cobigen Folder
*/
public static Path checkCustomHomePath(Path templates) {

String cobigenHome = ConfigurationConstants.DEFAULT_HOME_DIR_NAME;

while (!templates.endsWith(cobigenHome)) {
templates = templates.getParent();
if (templates == null) {

return null;
}

}
return templates;

}

/**
* checks the path for any pom.xml in a parent Folder and returns its path
*
Expand All @@ -207,15 +186,18 @@ public static Path checkCustomHomePath(Path templates) {
*/
public static Path getTemplatesPomFileLocation(Path childPath) {

String pomFile = ConfigurationConstants.POM_CONFIG_FILENAME;

while (!Files.exists(childPath.resolve(pomFile))) {
String pomFile = MavenConstants.POM;
int i = 0;
while (!Files.exists(childPath.resolve(pomFile)) && i <= 4) {
childPath = childPath.getParent();
if (childPath == null) {

return null;
}

i++;
}
if (!Files.exists(childPath.resolve(pomFile))) {
throw new CobiGenRuntimeException("Could not find any pom file in the given Path!");
}
return childPath;

Expand Down
2 changes: 0 additions & 2 deletions cobigen/cobigen-core/somePath

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public Path upgradeMonolithicTemplates(Path templatesProject) {
Path templatesPath = null;
if (templatesProject != null) {
List<Path> contextXml = FileSystemUtil.collectAllContextXML(templatesProject);
templatesPath = contextXml.get(0).getParent();
templatesPath = FileSystemUtil.createFileSystemDependentPath(contextXml.get(0).getParent().toUri());
} else {
templatesPath = FileSystemUtil.createFileSystemDependentPath(
CobiGenPaths.getTemplatesFolderPath().resolve(ConfigurationConstants.COBIGEN_TEMPLATES)
Expand All @@ -383,13 +383,11 @@ public Path upgradeMonolithicTemplates(Path templatesProject) {
contextUpgraderObject.resolveLatestCompatibleSchemaVersion(templatesPath);
contextUpgraderObject.upgradeConfigurationToLatestVersion(templatesPath, BackupPolicy.NO_BACKUP);

LOG.info("context.xml upgraded succsessfully. {}", templatesPath);
LOG.info("context.xml upgraded successfully. {}", templatesPath);
LOG.info("Templates successfully upgraded. \n ");

Path cobigenHome = CobiGenPaths.checkCustomHomePath(templatesPath);
if (cobigenHome == null) {
cobigenHome = CobiGenPaths.getCobiGenHomePath();
}
Path cobigenHome = CobiGenPaths.getCobiGenHomePath();

Path newTemplates = cobigenHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
LOG.info("New templates location: {} ", newTemplates);
return newTemplates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.constants.MavenConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.util.CobiGenPaths;
Expand Down Expand Up @@ -97,9 +97,7 @@ public TemplateSetUpgrader() {
public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration, Path> upgradeTemplatesToTemplateSets(
Path contextLocation) throws Exception {

Path cobigenHome = CobiGenPaths.checkCustomHomePath(contextLocation);
if (cobigenHome == null)
cobigenHome = CobiGenPaths.getCobiGenHomePath();
Path cobigenHome = CobiGenPaths.getCobiGenHomePath();

List<Path> newContextLocation = FileSystemUtil.collectAllContextXML(contextLocation);
Path folderOfContextLocation = newContextLocation.get(0).getParent();
Expand Down Expand Up @@ -128,8 +126,10 @@ public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration,
throw new CobiGenRuntimeException(e.getMessage(), e);
}
try {
FileUtils.copyDirectory(utilsPath.toFile(),
newTriggerFolder.resolve(ConfigurationConstants.UTIL_RESOURCE_FOLDER).toFile());
if (Files.exists(utilsPath)) {
FileUtils.copyDirectory(utilsPath.toFile(),
newTriggerFolder.resolve(ConfigurationConstants.UTIL_RESOURCE_FOLDER).toFile());
}
} catch (Exception e) {
LOG.error("An error occurred while copying the template utilities Folder", e);
throw new CobiGenRuntimeException(e.getMessage(), e);
Expand Down Expand Up @@ -169,16 +169,15 @@ public Map<com.devonfw.cobigen.impl.config.entity.io.v3_0.ContextConfiguration,
* @param {@link Path}newTemplateFolder Path to the split template folder
* @param {@link com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger }trigger to the related template folder
* @throws IOException
* @throws XmlPullParserException
* @throws ClassNotFoundException
* @throws FileNotFoundException
*/
private void writeNewPomFile(Path cobigenTemplates, Path newTemplateFolder,
com.devonfw.cobigen.impl.config.entity.io.v3_0.Trigger trigger) throws IOException, FileNotFoundException {

// Pom.xml creation
try {
Path oldPom = cobigenTemplates.resolve("pom.xml");
Path newPom = newTemplateFolder.resolve("pom.xml");
Path oldPom = cobigenTemplates.resolve(MavenConstants.POM);
Path newPom = newTemplateFolder.resolve(MavenConstants.POM);
Files.createFile(newPom);

// read the content of the pom.xml then replace it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,7 @@ public static boolean isZipFile(URI uri) {
*/
public static List<Path> collectAllContextXML(Path templatesPath) {

if (templatesPath == null)
throw new CobiGenRuntimeException(
"The templatespath cannot be null. Please try again with a proper templatespath!");
List<Path> result = null;
try (Stream<Path> pathStream = Files.find(templatesPath, Integer.MAX_VALUE, (p, basicFileAttributes) -> p
.getFileName().toString().equalsIgnoreCase(ConfigurationConstants.CONTEXT_CONFIG_FILENAME))) {
result = pathStream.collect(Collectors.toList());
} catch (IOException e) {
throw new CobiGenRuntimeException("An error occured while collecing the context.xml files!");
}
return result;
return collectAllFilesByName(templatesPath, ConfigurationConstants.CONTEXT_CONFIG_FILENAME);
}

/**
Expand All @@ -137,12 +127,24 @@ public static List<Path> collectAllContextXML(Path templatesPath) {
*/
public static List<Path> collectAllTemplatesXML(Path templatesPath) {

return collectAllFilesByName(templatesPath, ConfigurationConstants.TEMPLATES_CONFIG_FILENAME);
}

/**
* collect all templates.xml from a given path
*
* @param templatesPath the path to a templates location or any given path to search for
* @param fileName the name of a file to search for
* @return a path list of all found files by the filename given
*/
public static List<Path> collectAllFilesByName(Path templatesPath, String fileName) {

if (templatesPath == null)
throw new CobiGenRuntimeException(
"The templatespath cannot be null. Please try again with a proper templatespath!");
List<Path> result = null;
try (Stream<Path> pathStream = Files.find(templatesPath, Integer.MAX_VALUE, (p, basicFileAttributes) -> p
.getFileName().toString().equalsIgnoreCase(ConfigurationConstants.TEMPLATES_CONFIG_FILENAME))) {
try (Stream<Path> pathStream = Files.find(templatesPath, Integer.MAX_VALUE,
(p, basicFileAttributes) -> p.getFileName().toString().equalsIgnoreCase(fileName))) {
result = pathStream.collect(Collectors.toList());
} catch (IOException e) {
throw new CobiGenRuntimeException("An error occured while collecing the templates.xml files");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
public class FileSystemUtilTest {

/**
* Test methods collectAllContextXML and collectAllTemplatesXML
* Test method testCollectAllFilesByName
*/
@Test
public void testCollectAllContextAndTemplatesXMLs() {
public void testCollectAllFilesByName() {

Path xmlsPath = Paths.get("src/test/resources/testdata/unittest/config/util/collectors");

List<Path> allContexts = FileSystemUtil.collectAllContextXML(xmlsPath);
List<Path> alltemplates = FileSystemUtil.collectAllTemplatesXML(xmlsPath);
List<Path> allContexts = FileSystemUtil.collectAllFilesByName(xmlsPath, "context.xml");
List<Path> alltemplates = FileSystemUtil.collectAllFilesByName(xmlsPath, "templates.xml");

Assert.assertEquals(allContexts.get(0),
Paths.get("src/test/resources/testdata/unittest/config/util/collectors/contextXMLs/context.xml"));
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.sql.Timestamp;

import org.assertj.core.util.Files;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -34,6 +35,8 @@ public void testWriteAndReadTimestamp() throws IOException {

Assert.assertEquals(InstantTimestamp, InstantTimestampCopy);

Files.delete(configFile.toFile());

}

}

This file was deleted.

0 comments on commit 13c521a

Please sign in to comment.