|
1 | 1 | package com.devonfw.cobigen.cli.commands;
|
2 | 2 |
|
| 3 | +import java.nio.file.Path; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import com.devonfw.cobigen.api.TemplateAdapter; |
| 11 | +import com.devonfw.cobigen.api.exception.TemplateSelectionForAdaptionException; |
| 12 | +import com.devonfw.cobigen.api.exception.UpgradeTemplatesNotificationException; |
| 13 | +import com.devonfw.cobigen.cli.CobiGenCLI; |
3 | 14 | import com.devonfw.cobigen.cli.constants.MessagesConstants;
|
4 |
| -import com.devonfw.cobigen.impl.CobiGenFactory; |
| 15 | +import com.devonfw.cobigen.cli.utils.ValidationUtils; |
| 16 | +import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl; |
5 | 17 |
|
6 | 18 | import picocli.CommandLine.Command;
|
7 | 19 |
|
|
13 | 25 | "a" }, mixinStandardHelpOptions = true)
|
14 | 26 | public class AdaptTemplatesCommand extends CommandCommons {
|
15 | 27 |
|
| 28 | + /** |
| 29 | + * Logger to output useful information to the user |
| 30 | + */ |
| 31 | + private static Logger LOG = LoggerFactory.getLogger(CobiGenCLI.class); |
| 32 | + |
16 | 33 | @Override
|
17 | 34 | public Integer doAction() throws Exception {
|
18 | 35 |
|
19 |
| - CobiGenFactory.extractTemplates(); |
| 36 | + TemplateAdapter templateAdapter; |
| 37 | + if (this.templatesProject == null) { |
| 38 | + templateAdapter = new TemplateAdapterImpl(); |
| 39 | + } else { |
| 40 | + templateAdapter = new TemplateAdapterImpl(this.templatesProject); |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + templateAdapter.adaptTemplates(); |
| 45 | + } catch (UpgradeTemplatesNotificationException e) { |
| 46 | + if (askUserToContinueWithUpgrade(e)) { |
| 47 | + templateAdapter.upgradeMonolithicTemplates(); |
| 48 | + } |
| 49 | + } catch (TemplateSelectionForAdaptionException e) { |
| 50 | + List<Path> templateJars = e.getTemplateSets(); |
| 51 | + if (templateJars != null && !templateJars.isEmpty()) { |
| 52 | + List<Path> templateJarsToAdapt = getJarsToAdapt(templateAdapter, templateJars); |
| 53 | + if (!templateJarsToAdapt.isEmpty()) { |
| 54 | + templateAdapter.adaptTemplateSets(templateJarsToAdapt, false); |
| 55 | + } |
| 56 | + } else { |
| 57 | + LOG.info("No template set jars found to extract."); |
| 58 | + } |
| 59 | + } |
| 60 | + |
20 | 61 | return 0;
|
21 | 62 | }
|
| 63 | + |
| 64 | + /** |
| 65 | + * Gives the user a selection of available template set jars to adapt. |
| 66 | + * |
| 67 | + * @param templateJars A {@link List} with all available template set jars. |
| 68 | + * @return A {@link List} with the template set jars selected by the user to adapt. |
| 69 | + */ |
| 70 | + private List<Path> getJarsToAdapt(TemplateAdapter templateAdapter, List<Path> templateJars) { |
| 71 | + |
| 72 | + List<Path> jarsToAdapt = new ArrayList<>(); |
| 73 | + if (templateJars != null && templateJars.size() > 0) { |
| 74 | + printJarsForSelection(templateAdapter, templateJars); |
| 75 | + |
| 76 | + List<String> userSelection = new ArrayList<>(); |
| 77 | + for (String templateSelection : ValidationUtils.getUserInput().split(",")) { |
| 78 | + userSelection.add(templateSelection); |
| 79 | + } |
| 80 | + |
| 81 | + if (userSelection.contains("0")) { |
| 82 | + jarsToAdapt = templateJars; |
| 83 | + } else { |
| 84 | + for (String jarSelected : userSelection) { |
| 85 | + jarsToAdapt.add(templateJars.get(Integer.parseInt(jarSelected) - 1)); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return jarsToAdapt; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Prints the available template set jars |
| 95 | + * |
| 96 | + * @param templateSetJarPaths List of {@link Path} to available template jar files |
| 97 | + */ |
| 98 | + private void printJarsForSelection(TemplateAdapter templateAdapter, List<Path> templateSetJarPaths) { |
| 99 | + |
| 100 | + LOG.info("(0) " + "All"); |
| 101 | + for (Path templateSetJarPath : templateSetJarPaths) { |
| 102 | + LOG.info("(" + (templateSetJarPaths.indexOf(templateSetJarPath) + 1) + ") " |
| 103 | + + templateSetJarPath.getFileName().toString().replace(".jar", "") |
| 104 | + + (templateAdapter.isTemplateSetAlreadyAdapted(templateSetJarPath) ? " (already adapted)" : "")); |
| 105 | + } |
| 106 | + LOG.info("Please enter the number(s) of jar(s) that you want to adapt separated by comma."); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Ask the user to continue with the upgrade of the templates. |
| 111 | + * |
| 112 | + * @return Returns {@code true} if the user want to continue with the uprade of the templates. |
| 113 | + */ |
| 114 | + private boolean askUserToContinueWithUpgrade(UpgradeTemplatesNotificationException e) { |
| 115 | + |
| 116 | + LOG.info(e.getMessage()); |
| 117 | + LOG.info("Type 'y' or 'yes' to upgrade the configuration?"); |
| 118 | + String userInput = ValidationUtils.getUserInput(); |
| 119 | + if (userInput != null && (userInput.toLowerCase().equals("y") || userInput.toLowerCase().equals("yes"))) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + return false; |
| 123 | + } |
22 | 124 | }
|
0 commit comments