Skip to content

Commit

Permalink
devonfw#1642 made adapt template set process functional
Browse files Browse the repository at this point in the history
added proper adapt templates process
renamed getValue0 and getValue1 to getSourcesJar and getClassesJar
adjusted tests to check for correct folder structure after adapt process
re-enabled CLI adaptTemplateSetTest
  • Loading branch information
jan-vcapgemini committed Mar 21, 2023
1 parent c2b741f commit bb43ce7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public static List<MavenCoordinateStatePair> getJarFilesToMavenCoordinateState(P

List<MavenCoordinateStatePair> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ public class MavenCoordinateStatePair implements Iterable<MavenCoordinateState>
* 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();
}

/**
* @return the second value of a pair
*/
public MavenCoordinateState getValue1() {
public MavenCoordinateState getClassesJar() {

return this.pair.getValue1();
}
Expand All @@ -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();
}

/**
Expand All @@ -67,7 +67,7 @@ public boolean isValidJarAndSourcesJarPair() {
*/
public static List<MavenCoordinateState> flattenPairs(List<MavenCoordinateStatePair> 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
Expand All @@ -89,16 +89,16 @@ public MavenCoordinateState next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return this.index++ == 0 ? getValue0() : getValue1();
return this.index++ == 0 ? getSourcesJar() : getClassesJar();
}
};
}

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

Expand All @@ -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());
}

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

Expand All @@ -277,20 +284,16 @@ public void extractTemplateSetsTestWithTemplateSetJarFolderStructure() throws IO

for (List<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -119,34 +118,42 @@ private void processTemplateSetJars(List<MavenCoordinateStatePair> 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);
}

}
}
}
Expand Down

0 comments on commit bb43ce7

Please sign in to comment.