Skip to content

Commit

Permalink
devonfw#1509 Integrated the templateSetsAdapter into eclipse to adapt…
Browse files Browse the repository at this point in the history
… template-set->downloaded to adapted directory
  • Loading branch information
MansourD committed Feb 1, 2023
1 parent 02d03d6 commit ee7c05f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
import com.devonfw.cobigen.eclipse.test.common.SystemTest;
import com.devonfw.cobigen.eclipse.test.common.swtbot.AllJobsAreFinished;
import com.devonfw.cobigen.eclipse.test.common.utils.EclipseCobiGenUtils;
Expand Down Expand Up @@ -73,11 +72,8 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception {
* TODO Before the templates are made available online, the update (download) command cannot be tested.
*/
// EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot);
EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot);
EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME);

EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot);
EclipseUtils.openErrorsTreeInProblemsView(bot);

// expand the new file in the package explorer
SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,21 @@ public static void runAndCaptureAdaptTemplates(SWTWorkbenchBot bot) throws Excep
informationDialog.bot().button("Ok").click();
}

/**
* Checks the CobiGen Adapt TemplateSets and takes screenshots of it.
*
* @param bot to process the Adapt Templates command
* @throws Exception test fails
*/
public static void runAndCaptureAdaptTemplatesSets(SWTWorkbenchBot bot) throws Exception {

ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
bot.waitUntil(new AllJobsAreFinished(), DEFAULT_TIMEOUT); // build might take some time

SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
view.bot().tree().contextMenu("CobiGen").menu("Adapt Templates...").click();
}

/**
* Takes a screenshot (*.jpeg) of the current screen encoding test method and class and appends the given identifier
* to the file name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
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 org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
Expand All @@ -24,12 +22,13 @@
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.CobiGenFactory;
import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl;

Expand Down Expand Up @@ -107,16 +106,29 @@ public static void refreshConfigurationProject() {
*/
public static IProject getGeneratorConfigurationProject() throws GeneratorProjectNotExistentException, CoreException {

File templatesDirectory = getTemplatesDirectory();

generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
Path templateSetDirectory = getTemplateSetDirectory();

if (!Files.exists(templateSetDirectory)) {
File templatesDirectory = getTemplatesDirectory();
generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);

if (!generatorProj.exists()) {
if (!isUpdateDialogShown) {
if (templatesDirectory.exists()) {
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 (jarFilePath == null || !Files.exists(jarFilePath)) {
int result = createUpdateTemplatesDialog();
isUpdateDialogShown = true;
if (result == 1) {
// User does not want to download templates.
userWantsToDownloadTemplates = false;
} else {
userWantsToDownloadTemplates = true;
}
}

if (!generatorProj.exists()) {
if (!isUpdateDialogShown) {
if (templatesDirectory.exists()) {
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 (jarFilePath == null || !Files.exists(jarFilePath)) {
} else {
int result = createUpdateTemplatesDialog();
isUpdateDialogShown = true;
if (result == 1) {
Expand All @@ -126,24 +138,15 @@ public static IProject getGeneratorConfigurationProject() throws GeneratorProjec
userWantsToDownloadTemplates = true;
}
}

} else {
int result = createUpdateTemplatesDialog();
isUpdateDialogShown = true;
if (result == 1) {
// User does not want to download templates.
userWantsToDownloadTemplates = false;
} else {
userWantsToDownloadTemplates = true;
}
}
}
if (userWantsToDownloadTemplates) {
return generatorProj;
} else {
return null;
}
}
if (userWantsToDownloadTemplates) {
return generatorProj;
} else {
return null;
}
return null;
}

/**
Expand Down Expand Up @@ -219,9 +222,9 @@ private static File getTemplatesDirectory() {
*
* @return the templateDirectory
*/
public static File getTemplateSetDirectory() {
public static Path getTemplateSetDirectory() {

return CobiGenPaths.getTemplateSetsFolderPath().toFile();
return CobiGenPaths.getTemplateSetsFolderPath();
}

/**
Expand Down Expand Up @@ -271,6 +274,27 @@ public static void processJar(String fileName) throws MalformedURLException, IOE
}
}

/**
* Process Jar method is responsible for unzip the Jar files of templateSets from downloaded to adapted
*
* @param fileName Name of source jar file downloaded
* @throws IOException {@link IOException} occurred
* @throws MalformedURLException {@link MalformedURLException} occurred
*/
public static void adaptTemplateSet(Path templateSetsLocation) {

try {
TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateSetsLocation);
List<Path> jarFilePath = TemplatesJarUtil.getJarFiles(templateSetsLocation.resolve("downloaded"));
templateAdapter.adaptTemplateSets(jarFilePath, templateSetsLocation.resolve("adapted"), false);

} catch (Exception e) {
LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e);
PlatformUIUtil
.openErrorDialog("An exception occurred while processing Jar file to create CobiGen_Templates folder", e);
}
}

/**
* @return workspace location
*/
Expand Down Expand Up @@ -298,7 +322,6 @@ public static void startTemplatesUpgrader(Path configurationPath) {

setTemplateSetPathAfterUpgrade(CobiGenFactory.startTemplatesUpgrader(configurationPath));


// FIXME after the upgrade adapt the new template-set folder. should be done in #1587
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ private static CobiGen initializeGenerator(boolean allowMonolithicConfiguration)
throws InvalidConfigurationException, GeneratorCreationException {

try {
ResourcesPluginUtil.refreshConfigurationProject();
generatorProj = ResourcesPluginUtil.getGeneratorConfigurationProject();

initializeCobiGen(generatorProj, allowMonolithicConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,27 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
IProject generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot()
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory();

if (generatorProjOfTempltesSets.exists()) {
// 1. the project is imported so, do not adapt(no need to extract the jar files) For now nothing to do.

// 1. TODO import project, do not adapt(no need to extract the jar files)
}
} else if (Files.exists(templateSet)) {

// 2. downloaded exists? first adapt the jar file (files), then import project to eclipse

Path downloadedPath = templateSet.resolve("downloaded");
Path adaptedPath = templateSet.resolve("adapted");

// A. adapt the jar files if not already adapted
if (Files.exists(downloadedPath) && !Files.exists(adaptedPath))
ResourcesPluginUtil.adaptTemplateSet(templateSet);

else if (ResourcesPluginUtil.getTemplateSetDirectory().exists()) {
// 2.TODO donwloaded exists? first adapt the jar file (files), then import project to eclipse
// 3. TODO downloaded does not exists? update command must be executed. then go to 2.
// (step 3 can be ignored for now until the new template-sets are deployed online.)
//
// B. TODO Import the project
/*
* 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored
* for now until the new template-sets are deployed online.)
*/
} else {
if (generatorProj.exists()) {
MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null,
Expand Down

0 comments on commit ee7c05f

Please sign in to comment.