diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/TemplateSetConfiguration.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/TemplateSetConfiguration.java index f5f2d377b..1f7098aa1 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/TemplateSetConfiguration.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/TemplateSetConfiguration.java @@ -154,13 +154,9 @@ private void initializeTemplateSets(boolean isZipFile, Path configurationPath, P Map trigger = contextConfigurationReader.loadTriggers(); Trigger activeTrigger = trigger.get(trigger.keySet().toArray()[0]); - if (isZipFile) { - Map configLocations = this.templateSetConfigurationReader.getConfigLocations(); - Path jarPath = configLocations.get(templateSetFile); - this.utilFolders.put(activeTrigger.getId(), jarPath); - } else { - this.utilFolders.put(activeTrigger.getId(), getUtilSourceFolder(templateSetFile)); - } + Map configLocations = this.templateSetConfigurationReader.getConfigLocations(); + Path templateSetRootFolder = configLocations.get(templateSetFile); + this.utilFolders.put(activeTrigger.getId(), templateSetRootFolder); this.rootTemplateFolders.put(activeTrigger.getId(), templateFolder.getPath()); this.triggers.putAll(trigger); @@ -179,17 +175,6 @@ private void initializeTemplateSets(boolean isZipFile, Path configurationPath, P this.templatesConfigurations.add(templatesConfiguration); } - /** - * Gets the source folder for utility classes - * - * @return the source folder where utility classes are located - */ - private Path getUtilSourceFolder(Path path) { - - // TODO: replace with proper root template set folder, see: https://github.com/devonfw/cobigen/issues/1667 - return path.getParent().getParent().getParent().getParent(); - } - /** * @return templatesConfigurations */ diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplateSetConfigurationManager.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplateSetConfigurationManager.java index 6bb30e64b..ddc0cbb42 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplateSetConfigurationManager.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplateSetConfigurationManager.java @@ -24,65 +24,47 @@ public class TemplateSetConfigurationManager { /** List with the paths of the configuration locations for the template-set.xml files */ private Map configLocations; - /** - * @return configLocations - */ - public Map getConfigLocations() { - - return this.configLocations; - } - - /** List of template set paths */ - private List templateSetPaths; - /** * The constructor. */ public TemplateSetConfigurationManager() { this.configLocations = new HashMap<>(); - this.templateSetPaths = new ArrayList<>(); } /** - * Adds the path of a template-set.xml file to the list of all config files. Also adds the path of the - * template-set.xml file and its root directory to the configRoots map - * - * @param templateSetFilePath the {@link Path} to the template-set.xml file - * @param configRootPath the {@link Path} containing the config root directory for a template-set.xml - * @param templateSetPaths a list containing all paths to template-set.xml files + * @return configLocations */ - public void addConfigRoot(Path templateSetFilePath, Path configRootPath, List templateSetPaths) { + public Map getConfigLocations() { - if (Files.exists(templateSetFilePath)) { - templateSetPaths.add(templateSetFilePath); - this.configLocations.put(templateSetFilePath, configRootPath); - } + return this.configLocations; } /** - * Search for configuration files in the sub folder for adapted templates + * Search for configuration files in the sub folders of adapted templates * * @param configRoot root directory of the configuration template-sets/adapted * @return List of Paths to the adapted templateSetFiles */ protected List loadTemplateSetFilesAdapted(Path configRoot) { - // We need to empty this list to prevent duplicates from being added - this.templateSetPaths.clear(); - - // TODO: Make these directories accessible for the reader, see: https://github.com/devonfw/cobigen/issues/1667 List templateSetDirectories = retrieveTemplateSetDirectories(configRoot); + List adaptedTemplateSets = new ArrayList<>(); for (Path templateDirectory : templateSetDirectories) { Path templateSetFilePath = templateDirectory.resolve(ConfigurationConstants.MAVEN_CONFIGURATION_RESOURCE_FOLDER) .resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME); - addConfigRoot(templateSetFilePath, templateDirectory, this.templateSetPaths); + // makes sure that only valid template set folders get added + if (Files.exists(templateSetFilePath)) { + adaptedTemplateSets.add(templateSetFilePath); + + this.configLocations.put(templateSetFilePath, templateDirectory); + } } - return this.templateSetPaths; + return adaptedTemplateSets; } /** @@ -114,20 +96,24 @@ private List retrieveTemplateSetDirectories(Path configRoot) { */ protected List loadTemplateSetFilesDownloaded(Path configRoot) { - // We need to empty this list to prevent duplicates from being added - this.templateSetPaths.clear(); // TODO: add check for valid templatesetjar util List templateJars = TemplatesJarUtil.getJarFiles(configRoot); + List downloadedTemplateSets = new ArrayList<>(); if (!templateJars.isEmpty()) { for (Path jarPath : templateJars) { Path configurationPath = FileSystemUtil.createFileSystemDependentPath(jarPath.toUri()); Path templateSetFilePath = configurationPath.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME); - addConfigRoot(templateSetFilePath, jarPath, this.templateSetPaths); + // makes sure that only valid template set jars get added + if (Files.exists(templateSetFilePath)) { + downloadedTemplateSets.add(templateSetFilePath); + this.configLocations.put(templateSetFilePath, jarPath); + } + } } - return this.templateSetPaths; + return downloadedTemplateSets; } } diff --git a/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetConfigurationReaderTest.java b/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetConfigurationReaderTest.java index 889134916..9ac17f276 100644 --- a/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetConfigurationReaderTest.java +++ b/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetConfigurationReaderTest.java @@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.File; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -143,4 +144,29 @@ public void testTemplateSetsAdaptedAndDownloaded() throws Exception { }); } + /** + * Tests if template-set configuration can be found in both adapted and downloaded folder of the template sets + * directory, even if an invalid folder .settings was added to the adapted folder + * + * @throws Exception test fails + * + */ + @Test + public void testGetTemplatesWithInvalidAdaptedFolder() throws Exception { + + File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest"); + Path templateSetPath = TEST_FILE_ROOT_PATH.resolve("valid_template_sets/"); + FileUtils.copyDirectory(templateSetPath.toFile(), folder); + // create an invalid folder which has to be ignored + Files.createDirectory(folder.toPath().resolve("template-sets").resolve("adapted").resolve(".settings")); + + withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> { + + TemplateSetConfiguration templateSetConfiguration = new TemplateSetConfiguration( + folder.toPath().resolve("template-sets")); + + assertThat(templateSetConfiguration.getTemplatesConfigurations().size()).isEqualTo(3); + }); + } + } \ No newline at end of file