Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1452 Implemented new configuration reader to read the template sets #1504

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import org.codehaus.plexus.util.Os;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.slf4j.LoggerFactory;
Expand All @@ -32,23 +37,84 @@ public class AbstractCliTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

/** Temporary directory for the templates project */
@ClassRule
public static TemporaryFolder tempFolderTemplates = new TemporaryFolder();

/** Current home directory */
protected Path currentHome;

/** The devon4j-templates development folder */
/** The templates development folder */
protected static Path devTemplatesPath;

/** A temp directory containing the templates development folder */
protected static Path devTemplatesPathTemp;

/**
* Determine the devon4j-templates development folder
* Determine the templates development folder and create a copy of it in the temp directory
*
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a template directory directory fails
*/
@BeforeClass
public static void determineDevTemplatesPath() throws URISyntaxException {
public static void determineDevTemplatesPath() throws URISyntaxException, IOException {

devTemplatesPath = new File(AbstractCliTest.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile().getParentFile().getParentFile().toPath().resolve("cobigen-templates");

Path utilsPom = new File(AbstractCliTest.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile().getParentFile().getParentFile().toPath().resolve("cobigen-templates")
.resolve("templates-devon4j");
.resolve("templates-devon4j-tests/src/test/resources/utils/pom.xml");

// create a temporary directory cobigen-templates/template-sets/adapted containing the template sets
Path tempFolderPath = tempFolderTemplates.getRoot().toPath();
Path cobigenTemplatePath = tempFolderPath.resolve("cobigen-templates");
if (!Files.exists(cobigenTemplatePath)) {
Files.createDirectory(cobigenTemplatePath);

devTemplatesPathTemp = cobigenTemplatePath.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path templateSetsAdaptedFolder = devTemplatesPathTemp.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Files.createDirectory(devTemplatesPathTemp);
Files.createDirectory(templateSetsAdaptedFolder);

FileUtils.copyDirectory(devTemplatesPath.toFile(), templateSetsAdaptedFolder.toFile());

List<Path> devTemplateSets = new ArrayList<>();
try (Stream<Path> files = Files.list(templateSetsAdaptedFolder)) {
files.forEach(path -> {
devTemplateSets.add(path);
});
}

for (Path path : devTemplateSets) {
if (Files.isDirectory(path)) {
Path resourcesFolder = path.resolve("src/main/resources");
Path templatesFolder = path.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
if (Files.exists(resourcesFolder) && !Files.exists(templatesFolder)) {
try {
Files.move(resourcesFolder, templatesFolder);
} catch (IOException e) {
throw new IOException("Error moving directory " + resourcesFolder, e);
}
}

if (path.getFileName().toString().equals("templates-devon4j-utils")) {
if (Files.exists(path.resolve("pom.xml"))) {
try {
Files.delete(path.resolve("pom.xml"));
} catch (IOException e) {
throw new IOException("Error deleting file " + path.resolve("pom.xml"), e);
}
}
try {
Files.copy(utilsPom, path.resolve("pom.xml"));
} catch (IOException e) {
throw new IOException("Error copying file " + utilsPom, e);
}
}
}
}
}
}

/**
Expand All @@ -71,7 +137,7 @@ public void runWithLatestTemplates() throws IOException {

Path configFile = this.currentHome.resolve(ConfigurationConstants.COBIGEN_CONFIG_FILE);
Files.write(configFile,
(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH + "=" + devTemplatesPath.toString()).getBytes());
(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH + "=" + devTemplatesPathTemp.toString()).getBytes());
}

/**
Expand Down Expand Up @@ -129,7 +195,7 @@ protected void execute(String[] args, boolean useDevTemplates, boolean assureFai
debugArgs = Arrays.copyOf(debugArgs, debugArgs.length + 3);
debugArgs[debugArgs.length - 3] = "-v";
debugArgs[debugArgs.length - 2] = "-tp";
debugArgs[debugArgs.length - 1] = devTemplatesPath.toString();
debugArgs[debugArgs.length - 1] = devTemplatesPathTemp.toString();
} else {
debugArgs = Arrays.copyOf(debugArgs, debugArgs.length + 1);
debugArgs[debugArgs.length - 1] = "-v";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

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

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
Expand All @@ -16,6 +19,29 @@
*/
public class AdaptTemplatesCommandTest extends AbstractCliTest {

/**
* Simulate the download of the template set jars, as this not yet implemented. This method can be removed later
*
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a directory or file fails
*/
@Before
public void initAdaptTemplatesTest() throws URISyntaxException, IOException {

Path cliSystemTestPath = new File(
AdaptTemplatesCommandTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
.getParentFile().toPath();
Path templateJar = cliSystemTestPath.resolve("src/test/resources/testdata/crud-java-server-app.jar");
if (Files.exists(templateJar)) {
Path downloadedTemplateSetsPath = this.currentHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER)
.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);
if (!Files.exists(downloadedTemplateSetsPath)) {
Files.createDirectories(downloadedTemplateSetsPath);
}
Files.copy(templateJar, downloadedTemplateSetsPath.resolve(templateJar.getFileName()));
}
}

/**
* Checks if adapt-templates command successfully created cobigen templates folder and its sub folders
*
Expand All @@ -29,19 +55,21 @@ public void adaptTemplatesTest() throws Exception {

execute(args, false);

Path cobigenTemplatesFolderPath = this.currentHome.resolve(ConfigurationConstants.TEMPLATES_FOLDER)
.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
assertThat(cobigenTemplatesFolderPath).exists();
// check if templates exist
assertThat(Paths
.get(cobigenTemplatesFolderPath.toString() + File.separator + ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER))
.exists();
Path cobigenTemplateSetsFolderPath = this.currentHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path downloadedTemplateSetsFolderPath = cobigenTemplateSetsFolderPath
.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);
Path adaptedTemplateSetsFolderPath = cobigenTemplateSetsFolderPath.resolve(ConfigurationConstants.ADAPTED_FOLDER);

assertThat(cobigenTemplateSetsFolderPath).exists();
assertThat(downloadedTemplateSetsFolderPath).exists();
assertThat(adaptedTemplateSetsFolderPath).exists();

// check if adapted template set exists
Path templateSet = adaptedTemplateSetsFolderPath.resolve("crud-java-server-app");
assertThat(templateSet).exists();
// check if context configuration exists
assertThat(Paths
.get(cobigenTemplatesFolderPath.toString() + File.separator + ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER
+ File.separator + ConfigurationConstants.CONTEXT_CONFIG_FILENAME)).exists();
// check if sources of utility classes exist
assertThat(Paths.get(cobigenTemplatesFolderPath.toString() + File.separator + "src" + File.separator + "main"
+ File.separator + "java")).exists();
assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)).exists();
assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME)).exists();
}
}
Binary file not shown.
5 changes: 0 additions & 5 deletions cobigen-cli/cli/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@
<artifactId>jsonplugin</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>templates-devon4j</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -98,6 +99,7 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception {
*
* @throws Exception test fails
*/
@Ignore
@Test
public void testAdaptTemplatesAndGenerate() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.MalformedURLException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -88,9 +89,9 @@ public static IProject getGeneratorConfigurationProject() throws GeneratorProjec
if (!generatorProj.exists()) {
if (!isUpdateDialogShown) {
if (templatesDirectory.exists()) {
File jarFile = TemplatesJarUtil.getJarFile(false, templatesDirectory);
Path jarFilePath = TemplatesJarUtil.getJarFile(false, templatesDirectory.toPath());
// If we don't find at least one jar, then we do need to download new templates
if (jarFile == null) {
if (jarFilePath == null || !Files.exists(jarFilePath)) {
int result = createUpdateTemplatesDialog();
isUpdateDialogShown = true;
if (result == 1) {
Expand Down Expand Up @@ -168,14 +169,14 @@ public static String getJarPath(boolean isSource) {

File templatesDirectory = getTemplatesDirectory();

File jarFile = TemplatesJarUtil.getJarFile(isSource, templatesDirectory);
Path jarFilePath = TemplatesJarUtil.getJarFile(isSource, templatesDirectory.toPath());

if (jarFile == null) {
if (jarFilePath == null || !Files.exists(jarFilePath)) {
return "";
}

String fileName = jarFile.getPath().substring(jarFile.getPath().lastIndexOf(File.separator) + 1);

String fileName = jarFilePath.toFile().getPath()
.substring(jarFilePath.toFile().getPath().lastIndexOf(File.separator) + 1);
return fileName;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.cobigen.eclipse.generator;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -219,14 +220,14 @@ private static CobiGen initializeGenerator() throws InvalidConfigurationExceptio

// If it is not valid, we should use the jar
if (null == generatorProj.getLocationURI() || !configJavaProject.exists()) {
File templatesDirectory = CobiGenPaths.getTemplatesFolderPath().toFile();
File jarPath = TemplatesJarUtil.getJarFile(false, templatesDirectory);
boolean fileExists = jarPath.exists();
Path templatesDirectoryPath = CobiGenPaths.getTemplatesFolderPath();
Path jarPath = TemplatesJarUtil.getJarFile(false, templatesDirectoryPath);
boolean fileExists = (jarPath != null && Files.exists(jarPath));
if (!fileExists) {
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
"Not Downloaded the CobiGen Template Jar");
}
return CobiGenFactory.create(jarPath.toURI());
return CobiGenFactory.create(jarPath.toUri());
} else {
return CobiGenFactory.create(generatorProj.getLocationURI());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.devonfw.cobigen.eclipse.healthcheck;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand Down Expand Up @@ -79,9 +79,9 @@ public void execute() {
if (generatorConfProj != null && generatorConfProj.getLocationURI() != null) {
CobiGenFactory.create(generatorConfProj.getLocationURI());
} else {
File templatesDirectory = CobiGenPaths.getTemplatesFolderPath().toFile();
File jarPath = TemplatesJarUtil.getJarFile(false, templatesDirectory);
boolean fileExists = jarPath.exists();
Path templatesDirectoryPath = CobiGenPaths.getTemplatesFolderPath();
Path jarPath = TemplatesJarUtil.getJarFile(false, templatesDirectoryPath);
boolean fileExists = (jarPath != null && Files.exists(jarPath));
if (!fileExists) {
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
"Not Downloaded the CobiGen Template Jar");
Expand Down Expand Up @@ -226,7 +226,7 @@ private void openSuccessDialog(String healthyCheckMessage, boolean warn) {

/**
* Performs a HealthCheckReport on CobiGen_Templates in workspace or on latest templates jar.
*
*
* @return HealthCheckReport the {@link HealthCheckReport} created by the HealthCheck
* @throws GeneratorProjectNotExistentException if no generator configuration project called
* @throws CoreException if an existing generator configuration project could not be opened
Expand Down
Loading