Skip to content

Commit

Permalink
#1667 Made template set root directories accessible for the reader
Browse files Browse the repository at this point in the history
removed templateSetPaths field in TemplateSetConfigurationManager
adjusted addConfigRoot to provide the root template set folder
added test to check for invalid template set folders
removed addConfigRoot method
removed getUtilSourceFolder method (now the same way to determine utility folders works for adapted and downloaded template sets)
  • Loading branch information
jan-vcapgemini committed Apr 3, 2023
1 parent 0611e0f commit 5113482
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,9 @@ private void initializeTemplateSets(boolean isZipFile, Path configurationPath, P
Map<String, Trigger> trigger = contextConfigurationReader.loadTriggers();
Trigger activeTrigger = trigger.get(trigger.keySet().toArray()[0]);

if (isZipFile) {
Map<Path, Path> configLocations = this.templateSetConfigurationReader.getConfigLocations();
Path jarPath = configLocations.get(templateSetFile);
this.utilFolders.put(activeTrigger.getId(), jarPath);
} else {
this.utilFolders.put(activeTrigger.getId(), getUtilSourceFolder(templateSetFile));
}
Map<Path, Path> 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);
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,47 @@ public class TemplateSetConfigurationManager {
/** List with the paths of the configuration locations for the template-set.xml files */
private Map<Path, Path> configLocations;

/**
* @return configLocations
*/
public Map<Path, Path> getConfigLocations() {

return this.configLocations;
}

/** List of template set paths */
private List<Path> 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<Path> templateSetPaths) {
public Map<Path, Path> 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<Path> 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<Path> templateSetDirectories = retrieveTemplateSetDirectories(configRoot);

List<Path> 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;
}

/**
Expand Down Expand Up @@ -114,20 +96,24 @@ private List<Path> retrieveTemplateSetDirectories(Path configRoot) {
*/
protected List<Path> 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<Path> templateJars = TemplatesJarUtil.getJarFiles(configRoot);
List<Path> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
}

}

0 comments on commit 5113482

Please sign in to comment.