Skip to content

Commit

Permalink
1455 adaption of template sets (#1520)
Browse files Browse the repository at this point in the history
* Adjust documentation (#1491)

* fixed some types and links and corrected some parts of the documentation

* adjust angular client guide

* fixed typo

* fixed requested changes

* implemented independent adaption of template sets

* fix adapt template cli test

* ignore AdaptTemplatesCommandTest and further implementations for TemplateAdapter

* added tests for the template adapter

* implemented requested changes

* changed TemplateAdapter
  • Loading branch information
GuentherJulian authored Apr 27, 2022
1 parent a39ec1c commit 4e4b6d3
Show file tree
Hide file tree
Showing 15 changed files with 801 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Path;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
Expand Down Expand Up @@ -47,6 +48,7 @@ public void initAdaptTemplatesTest() throws URISyntaxException, IOException {
*
* @throws Exception test fails
*/
@Ignore
@Test
public void adaptTemplatesTest() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.devonfw.cobigen.cli.commands;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.TemplateAdapter;
import com.devonfw.cobigen.api.exception.TemplateSelectionForAdaptionException;
import com.devonfw.cobigen.api.exception.UpgradeTemplatesNotificationException;
import com.devonfw.cobigen.cli.CobiGenCLI;
import com.devonfw.cobigen.cli.constants.MessagesConstants;
import com.devonfw.cobigen.impl.CobiGenFactory;
import com.devonfw.cobigen.cli.utils.ValidationUtils;
import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl;

import picocli.CommandLine.Command;

Expand All @@ -13,10 +25,100 @@
"a" }, mixinStandardHelpOptions = true)
public class AdaptTemplatesCommand extends CommandCommons {

/**
* Logger to output useful information to the user
*/
private static Logger LOG = LoggerFactory.getLogger(CobiGenCLI.class);

@Override
public Integer doAction() throws Exception {

CobiGenFactory.extractTemplates();
TemplateAdapter templateAdapter;
if (this.templatesProject == null) {
templateAdapter = new TemplateAdapterImpl();
} else {
templateAdapter = new TemplateAdapterImpl(this.templatesProject);
}

try {
templateAdapter.adaptTemplates();
} catch (UpgradeTemplatesNotificationException e) {
if (askUserToContinueWithUpgrade(e)) {
templateAdapter.upgradeMonolithicTemplates();
}
} catch (TemplateSelectionForAdaptionException e) {
List<Path> templateJars = e.getTemplateSets();
if (templateJars != null && !templateJars.isEmpty()) {
List<Path> templateJarsToAdapt = getJarsToAdapt(templateAdapter, templateJars);
if (!templateJarsToAdapt.isEmpty()) {
templateAdapter.adaptTemplateSets(templateJarsToAdapt, false);
}
} else {
LOG.info("No template set jars found to extract.");
}
}

return 0;
}

/**
* Gives the user a selection of available template set jars to adapt.
*
* @param templateJars A {@link List} with all available template set jars.
* @return A {@link List} with the template set jars selected by the user to adapt.
*/
private List<Path> getJarsToAdapt(TemplateAdapter templateAdapter, List<Path> templateJars) {

List<Path> jarsToAdapt = new ArrayList<>();
if (templateJars != null && templateJars.size() > 0) {
printJarsForSelection(templateAdapter, templateJars);

List<String> userSelection = new ArrayList<>();
for (String templateSelection : ValidationUtils.getUserInput().split(",")) {
userSelection.add(templateSelection);
}

if (userSelection.contains("0")) {
jarsToAdapt = templateJars;
} else {
for (String jarSelected : userSelection) {
jarsToAdapt.add(templateJars.get(Integer.parseInt(jarSelected) - 1));
}
}
}

return jarsToAdapt;
}

/**
* Prints the available template set jars
*
* @param templateSetJarPaths List of {@link Path} to available template jar files
*/
private void printJarsForSelection(TemplateAdapter templateAdapter, List<Path> templateSetJarPaths) {

LOG.info("(0) " + "All");
for (Path templateSetJarPath : templateSetJarPaths) {
LOG.info("(" + (templateSetJarPaths.indexOf(templateSetJarPath) + 1) + ") "
+ templateSetJarPath.getFileName().toString().replace(".jar", "")
+ (templateAdapter.isTemplateSetAlreadyAdapted(templateSetJarPath) ? " (already adapted)" : ""));
}
LOG.info("Please enter the number(s) of jar(s) that you want to adapt separated by comma.");
}

/**
* Ask the user to continue with the upgrade of the templates.
*
* @return Returns {@code true} if the user want to continue with the uprade of the templates.
*/
private boolean askUserToContinueWithUpgrade(UpgradeTemplatesNotificationException e) {

LOG.info(e.getMessage());
LOG.info("Type 'y' or 'yes' to upgrade the configuration?");
String userInput = ValidationUtils.getUserInput();
if (userInput != null && (userInput.toLowerCase().equals("y") || userInput.toLowerCase().equals("yes"))) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.TemplateAdapter;
import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.TemplatesJarUtil;
import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
import com.devonfw.cobigen.eclipse.common.exceptions.GeneratorProjectNotExistentException;
import com.devonfw.cobigen.eclipse.updatetemplates.UpdateTemplatesDialog;
import com.devonfw.cobigen.impl.util.ExtractTemplatesUtil;
import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl;

/** Util for NPE save access of {@link ResourcesPlugin} utils */
public class ResourcesPluginUtil {
Expand Down Expand Up @@ -228,7 +229,9 @@ public static void processJar(String fileName) throws MalformedURLException, IOE
}

try {
ExtractTemplatesUtil.extractTemplates(cobigenFolderPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES), false);
TemplateAdapter templateAdapter = new TemplateAdapterImpl(cobigenFolderPath);
templateAdapter.adaptMonolithicTemplates(cobigenFolderPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES),
false);
} catch (Exception e) {
LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e);
PlatformUIUtil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.devonfw.cobigen.api;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import com.devonfw.cobigen.api.exception.TemplateSelectionForAdaptionException;
import com.devonfw.cobigen.api.exception.UpgradeTemplatesNotificationException;

/** The TemplateAdapter implements methods for adapting template jars */
public interface TemplateAdapter {

/**
* Adapt the templates. Can either adapt an old monolithic template structure or independent template sets.
*
* @throws IOException If CobiGen is not able to extract the jar file to the destination folder
* @throws UpgradeTemplatesNotificationException If an old monolithic structure was adapted. Can be catched to ask the
* user for an upgrade of the templates.
* @throws TemplateSelectionForAdaptionException If a new template structure is given. To ask the user to select the
* template sets to adapt.
*/
public void adaptTemplates()
throws IOException, UpgradeTemplatesNotificationException, TemplateSelectionForAdaptionException;

/**
* Adapt a given set of template set jars.
*
* @param templateSetJars A {@link List} of the {@link Path} of the template set jars to adapt
* @param forceOverride Indicator whether an already adapted template set should be overridden
* @throws IOException If CobiGen is not able to extract the jar file to the destination folder
*/
public void adaptTemplateSets(List<Path> templateSetJars, boolean forceOverride) throws IOException;

/**
* Adapt a set of template set jars to a given destination folder.
*
* @param templateSetJars A {@link List} of the {@link Path} of the template set jars to adapt
* @param destinationPath The parent folder where the jars should be extracted to
* @param forceOverride Indicator whether an already adapted template set should be overridden
* @throws IOException If CobiGen is not able to extract the jar file to the destination folder
*/
public void adaptTemplateSets(List<Path> templateSetJars, Path destinationPath, boolean forceOverride)
throws IOException;

/**
* Adapt an old monolithic template jar structure.
*
* @param forceOverride Indicator whether an already adapted template set should be overridden
* @throws IOException If CobiGen is not able to extract the jar file to the destination folder
*/
public void adaptMonolithicTemplates(boolean forceOverride) throws IOException;

/**
* Adapt an old monolithic template jar structure to a given destination folder.
*
* @param destinationPath The folder where the jars should be extracted to
* @param forceOverride Indicator whether an already adapted template set should be overridden
* @throws IOException If CobiGen is not able to extract the jar file to the destination folder
*/
public void adaptMonolithicTemplates(Path destinationPath, boolean forceOverride) throws IOException;

/**
* Get a list of available template set jars to adapt.
*
* @return A {@link List} of {@link Path} with all template set jar files found.
*/
public List<Path> getTemplateSetJars();

/**
* Checks if the template configuration consists of an old monolithic template set or independent template sets.
*
* @return Returns {@code true} if the template structure consists of an old monolithic template set. Otherwise false.
*/
public boolean isMonolithicTemplatesConfiguration();

/**
* Upgrade an adapted monolithic template structure to the new template structure consisting of template sets.
*/
public void upgradeMonolithicTemplates();

/**
* Get the parent location of the templates.
*
* @return The {@link Path} of the templates location.
*/
public Path getTemplatesLocation();

/**
* Checks if a given template set is already adapted
*
* @param templateSetJar The {@link Path} to the template set to check.
* @return Returns {@code true} if the template set is already adapted. Otherwise false.
*/
public boolean isTemplateSetAlreadyAdapted(Path templateSetJar);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.devonfw.cobigen.api.exception;

import java.nio.file.Path;
import java.util.List;

/**
* Exception that indicates that a new template structure is available. For asking which template sets should be
* adapted.
*/
public class TemplateSelectionForAdaptionException extends Exception {

/** Generated serial version UID */
private static final long serialVersionUID = 1;

/** List of available template sets. */
private List<Path> templateSets;

/**
* Creates a new {@link TemplateSelectionForAdaptionException}
*
* @param templateSets A list with available template sets to adapt.
*
*/
public TemplateSelectionForAdaptionException(List<Path> templateSets) {

super("Select the template sets you want to adapt.");
this.templateSets = templateSets;
}

/**
* @return templateSets All available template sets.
*/
public List<Path> getTemplateSets() {

return this.templateSets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.devonfw.cobigen.api.exception;

/**
* Exception that indicates that an old monolithic template structure has been adapted. For asking if the template
* structure should be upgraded.
*/
public class UpgradeTemplatesNotificationException extends Exception {

/** Generated serial version UID */
private static final long serialVersionUID = 1;

/**
* Creates a new {@link UpgradeTemplatesNotificationException} with a proper notification message
*
*/
public UpgradeTemplatesNotificationException() {

super(
"You are using an old, monolithic template project. Do you want to upgrade your template project to the new template structure with independent template sets?");
}

}
Loading

0 comments on commit 4e4b6d3

Please sign in to comment.