diff --git a/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java b/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java index 77fcf2921..597cecce7 100644 --- a/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java +++ b/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java @@ -26,8 +26,6 @@ public class AdaptTemplatesCommandIT extends AbstractCliTest { * @throws Exception test fails */ @Test - @Ignore - // TODO: re-enable when template set adaptation is implemented public void adaptTemplateSetTest() throws Exception { Path devTemplateSetPath = new File( @@ -68,19 +66,27 @@ public void adaptTemplateSetTest() throws Exception { assertThat(downloadedTemplateSetsFolderPath).exists(); assertThat(adaptedTemplateSetsFolderPath).exists(); - // check if adapted template set exists Path templateSet = adaptedTemplateSetsFolderPath.resolve("crud-java-server-app-2021.12.007"); - Path templateSetSources = adaptedTemplateSetsFolderPath.resolve("crud-java-server-app-2021.12.007-sources"); - // check if template and sources exist + + // check if adapted template set exists assertThat(templateSet).exists(); - assertThat(templateSetSources).exists(); - // check if context configuration exists - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATES_FOLDER)).exists(); - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); - assertThat(templateSetSources.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); + Path templateSetResourcesPath = templateSet.resolve(ConfigurationConstants.MAVEN_CONFIGURATION_RESOURCE_FOLDER); + + // check if templates folder exists + assertThat(templateSet.resolve(templateSetResourcesPath).resolve(ConfigurationConstants.TEMPLATES_FOLDER)).exists(); + + // check if template-set.xml exists + assertThat( + templateSet.resolve(templateSetResourcesPath).resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)) + .exists(); + // validate correct folder structure - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); - assertThat(templateSetSources.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); + assertThat(templateSet.resolve(templateSetResourcesPath) + .resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); + + // check if template set utility resource folder exists + assertThat(templateSet.resolve(ConfigurationConstants.UTIL_RESOURCE_FOLDER)).exists(); + // validate maven specific contents assertThat(templateSet.resolve("pom.xml")).exists(); diff --git a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/AdaptTemplatesCommand.java b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/AdaptTemplatesCommand.java index 7929a09ef..cb13328c5 100644 --- a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/AdaptTemplatesCommand.java +++ b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/AdaptTemplatesCommand.java @@ -129,8 +129,8 @@ private void printJarsForSelection(TemplateAdapter templateAdapter, templateSetMavenCoordinatePairs.forEach(pair -> { - MavenCoordinateState nonSourcesMember = pair.getValue0(); - MavenCoordinateState sourcesMember = pair.getValue1(); + MavenCoordinateState nonSourcesMember = pair.getSourcesJar(); + MavenCoordinateState sourcesMember = pair.getClassesJar(); int nonSourcesMemberIdx = listViewOfPairs.indexOf(nonSourcesMember) + this.HUMAN_READABLE; int sourcesMemberIdx = listViewOfPairs.indexOf(sourcesMember) + this.HUMAN_READABLE; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java index f384959d8..670e00ea6 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java @@ -340,7 +340,7 @@ public static List getJarFilesToMavenCoordinateState(P List mavenCoordinateStatePair = groupedByGroupArtifactVersion.entrySet().stream() .map(entry -> new MavenCoordinateStatePair(entry.getValue().get(0), entry.getValue().get(1))) - .filter(pair -> pair.getValue0() != null && pair.getValue1() != null).collect(Collectors.toList()); + .filter(pair -> pair.getSourcesJar() != null && pair.getClassesJar() != null).collect(Collectors.toList()); return mavenCoordinateStatePair; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateStatePair.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateStatePair.java index f1cf8f12e..4bab688b8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateStatePair.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateStatePair.java @@ -25,22 +25,22 @@ public class MavenCoordinateStatePair implements Iterable * Initialize a MavenCoordinatePair with the given values. Ensures that the {@linkplain MavenCoordinateState} with a * truly {@linkplain MavenCoordinateState#isSource isSource} flag is the first element of the pair. * - * @param value0 any MavenCoordinateState - * @param value1 any MavenCoordinateState + * @param sourcesJar any MavenCoordinateState + * @param classesJar any MavenCoordinateState */ - public MavenCoordinateStatePair(MavenCoordinateState value0, MavenCoordinateState value1) { + public MavenCoordinateStatePair(MavenCoordinateState sourcesJar, MavenCoordinateState classesJar) { - if (!value0.isSource() && value1.isSource()) { - this.pair = Pair.with(value1, value0); + if (!sourcesJar.isSource() && classesJar.isSource()) { + this.pair = Pair.with(classesJar, sourcesJar); return; } - this.pair = Pair.with(value0, value1); + this.pair = Pair.with(sourcesJar, classesJar); } /** * @return the first value of a pair */ - public MavenCoordinateState getValue0() { + public MavenCoordinateState getSourcesJar() { return this.pair.getValue0(); } @@ -48,7 +48,7 @@ public MavenCoordinateState getValue0() { /** * @return the second value of a pair */ - public MavenCoordinateState getValue1() { + public MavenCoordinateState getClassesJar() { return this.pair.getValue1(); } @@ -58,7 +58,7 @@ public MavenCoordinateState getValue1() { */ public boolean isValidJarAndSourcesJarPair() { - return !getValue0().isSource() && getValue1().isSource() || getValue0().isSource() && !getValue1().isSource(); + return !getSourcesJar().isSource() && getClassesJar().isSource() || getSourcesJar().isSource() && !getClassesJar().isSource(); } /** @@ -67,7 +67,7 @@ public boolean isValidJarAndSourcesJarPair() { */ public static List flattenPairs(List pairs) { - return pairs.stream().flatMap(pair -> Stream.of(pair.getValue0(), pair.getValue1())).collect(Collectors.toList()); + return pairs.stream().flatMap(pair -> Stream.of(pair.getSourcesJar(), pair.getClassesJar())).collect(Collectors.toList()); } @Override @@ -89,7 +89,7 @@ public MavenCoordinateState next() { if (!hasNext()) { throw new NoSuchElementException(); } - return this.index++ == 0 ? getValue0() : getValue1(); + return this.index++ == 0 ? getSourcesJar() : getClassesJar(); } }; } @@ -97,8 +97,8 @@ public MavenCoordinateState next() { @Override public int hashCode() { - int value0_hash = getValue0().hashCode(); - int value1_hash = getValue1().hashCode(); + int value0_hash = getSourcesJar().hashCode(); + int value1_hash = getClassesJar().hashCode(); return Objects.hash(value0_hash, value1_hash); } @@ -113,7 +113,7 @@ public boolean equals(Object obj) { } MavenCoordinateStatePair other = (MavenCoordinateStatePair) obj; - return getValue0().equals(other.getValue0()) && getValue1().equals(other.getValue1()); + return getSourcesJar().equals(other.getSourcesJar()) && getClassesJar().equals(other.getClassesJar()); } } diff --git a/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java b/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java index 57acb0da3..7a773be57 100644 --- a/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java +++ b/cobigen/cobigen-core-systemtest/src/test/java/com/devonfw/cobigen/systemtest/TemplateProcessingTest.java @@ -139,19 +139,26 @@ public void extractTemplateSetsTest() throws IOException, Exception { assertThat(downloadedTemplateSetsFolderPath).exists(); assertThat(adaptedTemplateSetsFolderPath).exists(); - // check if adapted template set exists Path templateSet = adaptedTemplateSetsFolderPath.resolve("crud-java-server-app-2021.12.007"); - Path templateSetSources = adaptedTemplateSetsFolderPath.resolve("crud-java-server-app-2021.12.007-sources"); - // throwing a error + + // check if adapted template set exists assertThat(templateSet).exists(); - assertThat(templateSetSources).exists(); - // check if context configuration exists - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATES_FOLDER)).exists(); - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); - assertThat(templateSetSources.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); + Path templateSetResourcesPath = templateSet.resolve(ConfigurationConstants.MAVEN_CONFIGURATION_RESOURCE_FOLDER); + + // check if templates folder exists + assertThat(templateSet.resolve(templateSetResourcesPath).resolve(ConfigurationConstants.TEMPLATES_FOLDER)).exists(); + + // check if template-set.xml exists + assertThat( + templateSet.resolve(templateSetResourcesPath).resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)) + .exists(); // validate correct folder structure - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); - assertThat(templateSetSources.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); + assertThat(templateSet.resolve(templateSetResourcesPath) + .resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); + + // check if template set utility resource folder exists + assertThat(templateSet.resolve(ConfigurationConstants.UTIL_RESOURCE_FOLDER)).exists(); + // validate maven specific contents assertThat(templateSet.resolve("pom.xml")).exists(); } @@ -240,20 +247,20 @@ public void extractTemplateSetsTestWithTemplateSetJarFolderStructure() throws IO for (MavenCoordinateStatePair pair : mavenCoordinateStatePairs) { // check if MavenCoordinateState specific attributes are set assertThat(pair.isValidJarAndSourcesJarPair()).isTrue(); - assertThat(pair.getValue0().getMavenCoordinateLocalPath()).exists(); - assertThat(pair.getValue1().getMavenCoordinateLocalPath()).exists(); - assertThat(pair.getValue0().isPresent()).isTrue(); - assertThat(pair.getValue1().isPresent()).isTrue(); - assertThat(pair.getValue0().isValidMavenCoordinate()).isTrue(); - assertThat(pair.getValue1().isValidMavenCoordinate()).isTrue(); + assertThat(pair.getSourcesJar().getMavenCoordinateLocalPath()).exists(); + assertThat(pair.getClassesJar().getMavenCoordinateLocalPath()).exists(); + assertThat(pair.getSourcesJar().isPresent()).isTrue(); + assertThat(pair.getClassesJar().isPresent()).isTrue(); + assertThat(pair.getSourcesJar().isValidMavenCoordinate()).isTrue(); + assertThat(pair.getClassesJar().isValidMavenCoordinate()).isTrue(); // Check if the data structure contains the specific output Optional notSourcesJar = flatAdaptedTemplates.stream() - .filter(str -> str.equals(pair.getValue0().getArtifactId() + "-" + pair.getValue0().getVersion()) - && pair.getValue0().isSource()) + .filter(str -> str.equals(pair.getSourcesJar().getArtifactId() + "-" + pair.getSourcesJar().getVersion()) + && pair.getSourcesJar().isSource()) .findFirst(); Optional sourcesJar = flatAdaptedTemplates.stream() - .filter(str -> str.equals(pair.getValue1().getArtifactId() + "-" + pair.getValue1().getVersion()) - && !pair.getValue1().isSource()) + .filter(str -> str.equals(pair.getClassesJar().getArtifactId() + "-" + pair.getClassesJar().getVersion()) + && !pair.getClassesJar().isSource()) .findFirst(); assertThat(notSourcesJar.isPresent() && sourcesJar.isPresent()); @@ -277,20 +284,16 @@ public void extractTemplateSetsTestWithTemplateSetJarFolderStructure() throws IO for (List adapted : adaptedTemplates) { String notSourceDir = adapted.get(0); - String sourceDir = adapted.get(1); // check if adapted template set exists Path templateSet = adaptedTemplateSetsFolderPath.resolve(notSourceDir); - Path templateSetSources = adaptedTemplateSetsFolderPath.resolve(sourceDir); // throwing a error assertThat(templateSet).exists(); - assertThat(templateSetSources).exists(); + Path templateSetResourcesPath = templateSet.resolve(ConfigurationConstants.MAVEN_CONFIGURATION_RESOURCE_FOLDER); // check if context configuration exists - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATES_FOLDER)).exists(); - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); - assertThat(templateSetSources.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); + assertThat(templateSetResourcesPath).exists(); + assertThat(templateSetResourcesPath.resolve(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)).exists(); // validate correct folder structure - assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)).exists(); - assertThat(templateSetSources.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)) + assertThat(templateSetResourcesPath.resolve(ConfigurationConstants.TEMPLATE_SET_FREEMARKER_FUNCTIONS_FILE_NAME)) .exists(); // validate maven specific contents assertThat(templateSet.resolve("pom.xml")).exists(); diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java index baac27dfb..560a79259 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Objects; -import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -119,34 +118,42 @@ private void processTemplateSetJars(List templateSetMa Path destinationPath, boolean forceOverride) throws IOException { for (MavenCoordinateStatePair mavenCoordinateStatePair : templateSetMavenCoordinatePairs) { - for (MavenCoordinateState member : mavenCoordinateStatePair) { // Maximum of two iterations for each pair - Path path = member.getMavenCoordinateLocalPath(); - - LOG.debug("Processing jar file @ {}", path); - Path destination = destinationPath.resolve(member.getRealDirectoryName()); - boolean extract = false; - try { - extract = validatePaths(destination, forceOverride); - } catch (IOException e) { - LOG.info("Unable to extract template jar file to {}", destination); + MavenCoordinateState sourcesJar = mavenCoordinateStatePair.getSourcesJar(); + MavenCoordinateState classesJar = mavenCoordinateStatePair.getClassesJar(); + + Path path = classesJar.getMavenCoordinateLocalPath(); + + LOG.debug("Processing jar file @ {}", path); + Path destination = destinationPath.resolve(classesJar.getRealDirectoryName()); + boolean extract = false; + try { + extract = validatePaths(destination, forceOverride); + } catch (IOException e) { + LOG.info("Unable to extract template jar file to {}", destination); + } + + if (extract) { + if (!isEmpty(destination) && forceOverride) { + LOG.info("Override the existing destination folder {}", destination); + deleteDirectoryRecursively(destination); } - if (extract) { - if (Files.exists(destination) && forceOverride) { - LOG.info("Override the existing destination folder {}", destination); - deleteDirectoryRecursively(destination); - } - - if (extractArchive(path, destination)) { - member.setAdapted(true); - } - - // com folder with precompiled util classes is not needed. The utils compiled at first generation into the - // target folder - if (Files.exists(destination.resolve("com"))) { - FileUtils.deleteDirectory(destination.resolve("com").toFile()); - } + Path resourcesDestinationPath = destination.resolve(ConfigurationConstants.MAVEN_CONFIGURATION_RESOURCE_FOLDER); + // extract sources jar to target directory + extractArchive(sourcesJar.getMavenCoordinateLocalPath(), resourcesDestinationPath); + + // create src/main/java directory + Files.createDirectories(destination.resolve("src/main/java")); + + // move com folder to src/main/java/com + Files.move(resourcesDestinationPath.resolve("com"), destination.resolve("src/main/java/com"), + StandardCopyOption.REPLACE_EXISTING); + + URI zipFile = URI.create("jar:file:" + classesJar.getMavenCoordinateLocalPath().toUri().getPath()); + try (FileSystem fs = FileSystemUtil.getOrCreateFileSystem(zipFile)) { + Files.copy(fs.getPath("pom.xml"), destination.resolve("pom.xml"), StandardCopyOption.REPLACE_EXISTING); } + } } }