From ee7c05f2e51ae121b827c76cf4ae5bcdc7d97b49 Mon Sep 17 00:00:00 2001 From: MansourD Date: Wed, 1 Feb 2023 16:51:08 +0100 Subject: [PATCH] #1509 Integrated the templateSetsAdapter into eclipse to adapt template-set->downloaded to adapted directory --- .../eclipse/test/AdaptTemplatesTest.java | 6 +- .../common/utils/EclipseCobiGenUtils.java | 15 ++++ .../common/tools/ResourcesPluginUtil.java | 87 ++++++++++++------- .../generator/GeneratorWrapperFactory.java | 1 - .../handler/AdaptTemplatesHandler.java | 24 +++-- 5 files changed, 88 insertions(+), 45 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index d27be7cef1..888a84ccbe 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -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; @@ -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"); diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java index 1175f3e229..71bd2e280b 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java @@ -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 diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java index d8f7f593dd..d09d183c75 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java @@ -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; @@ -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; @@ -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) { @@ -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; } /** @@ -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(); } /** @@ -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 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 */ @@ -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 } diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java index 4c0a6f5fd9..266c564749 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java @@ -216,7 +216,6 @@ private static CobiGen initializeGenerator(boolean allowMonolithicConfiguration) throws InvalidConfigurationException, GeneratorCreationException { try { - ResourcesPluginUtil.refreshConfigurationProject(); generatorProj = ResourcesPluginUtil.getGeneratorConfigurationProject(); initializeCobiGen(generatorProj, allowMonolithicConfiguration); diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java index 67672f7969..40aaf6121b 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java @@ -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,