diff options
3 files changed, 139 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.migrator/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.migrator/META-INF/MANIFEST.MF index 53f388ff58..c149623b60 100644 --- a/plugins/org.eclipse.emf.cdo.migrator/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.emf.cdo.migrator/META-INF/MANIFEST.MF @@ -13,6 +13,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)", org.eclipse.emf.ecore.xmi;bundle-version="[2.5.0,3.0.0)", org.eclipse.emf.importer;bundle-version="[2.5.0,3.0.0)", org.eclipse.net4j.util.ui;bundle-version="[3.6.0,4.0.0)", + org.eclipse.jdt.core;bundle-version="[3.5.0,4.0.0)";resolution:=optional, org.eclipse.ant.core;bundle-version="[3.5.0,4.0.0)";resolution:=optional, org.apache.ant;bundle-version="[1.0.0,2.0.0)";resolution:=optional Export-Package: org.eclipse.emf.cdo.internal.messages;version="3.0.800";x-internal:=true, diff --git a/plugins/org.eclipse.emf.cdo.migrator/plugin.xml b/plugins/org.eclipse.emf.cdo.migrator/plugin.xml index 2355dec4a1..77de0c8e1b 100644 --- a/plugins/org.eclipse.emf.cdo.migrator/plugin.xml +++ b/plugins/org.eclipse.emf.cdo.migrator/plugin.xml @@ -61,6 +61,7 @@ <extension point="org.eclipse.ant.core.antTasks"> <antTask library="ant_tasks/cdo.ant.tasks.jar" name="cdo.importProject" class="org.eclipse.emf.cdo.internal.migrator.tasks.ImportProjectTask" /> <antTask library="ant_tasks/cdo.ant.tasks.jar" name="cdo.deleteProject" class="org.eclipse.emf.cdo.internal.migrator.tasks.DeleteProjectTask" /> + <antTask library="ant_tasks/cdo.ant.tasks.jar" name="cdo.cleanupProject" class="org.eclipse.emf.cdo.internal.migrator.tasks.CleanupProjectTask" /> <antTask library="ant_tasks/cdo.ant.tasks.jar" name="cdo.migrateModel" class="org.eclipse.emf.cdo.internal.migrator.tasks.MigrateModelTask" /> <antTask library="ant_tasks/cdo.ant.tasks.jar" name="cdo.generateModel" class="org.eclipse.emf.cdo.internal.migrator.tasks.GenerateModelTask" /> <antTask library="ant_tasks/cdo.ant.tasks.jar" name="cdo.expandTemplate" class="org.eclipse.emf.cdo.internal.migrator.tasks.ExpandTemplateTask" /> diff --git a/plugins/org.eclipse.emf.cdo.migrator/src/org/eclipse/emf/cdo/internal/migrator/tasks/CleanupProjectTask.java b/plugins/org.eclipse.emf.cdo.migrator/src/org/eclipse/emf/cdo/internal/migrator/tasks/CleanupProjectTask.java new file mode 100644 index 0000000000..899906f7a8 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.migrator/src/org/eclipse/emf/cdo/internal/migrator/tasks/CleanupProjectTask.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2018 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.emf.cdo.internal.migrator.tasks; + +import org.eclipse.emf.codegen.ecore.generator.Generator.CleanupScheduler; +import org.eclipse.emf.common.CommonPlugin; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; + +import org.apache.tools.ant.BuildException; + +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * @author Eike Stepper + */ +public class CleanupProjectTask extends CDOTask +{ + private String projectName; + + public void setProjectName(String projectName) + { + this.projectName = projectName; + } + + @Override + protected void checkAttributes() throws BuildException + { + assertTrue("'projectName' must be specified.", projectName != null && projectName.length() != 0); + } + + @Override + protected void doExecute() throws Exception + { + IProject project = root.getProject(projectName); + if (!project.exists()) + { + verbose("Project " + projectName + " does not exist."); + return; + } + + verbose("Cleaning up project " + projectName + " ..."); + EclipseHelper.sourceCleanup(this, project); + } + + /** + * @author Eike Stepper + */ + private static class EclipseHelper + { + private static final CleanupScheduler SCHEDULER = getCleanupScheduler(); + + public static void sourceCleanup(CDOTask task, IProject project) + { + if (SCHEDULER != null) + { + Set<ICompilationUnit> compilationUnits = collectCompilationUnits(project, task); + SCHEDULER.schedule(compilationUnits); + } + } + + private static Set<ICompilationUnit> collectCompilationUnits(IProject project, CDOTask task) + { + Set<ICompilationUnit> compilationUnits = new LinkedHashSet<ICompilationUnit>(); + IJavaProject javaProject = JavaCore.create(project); + + try + { + for (IPackageFragmentRoot packageFragmentRoot : javaProject.getAllPackageFragmentRoots()) + { + IResource resource = packageFragmentRoot.getCorrespondingResource(); + if (resource instanceof IContainer && project.equals(resource.getProject())) + { + collectCompilationUnits((IContainer)resource, compilationUnits, task); + } + } + } + catch (Exception ex) + { + throw new BuildException(ex); + } + + return compilationUnits; + } + + private static void collectCompilationUnits(IContainer container, Set<ICompilationUnit> compilationUnits, CDOTask task) throws CoreException + { + for (IResource resource : container.members()) + { + if (resource instanceof IContainer) + { + collectCompilationUnits((IContainer)resource, compilationUnits, task); + } + else if (resource instanceof IFile) + { + ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom((IFile)resource); + if (compilationUnit != null) + { + task.verbose(" " + resource.getProjectRelativePath()); + compilationUnits.add(compilationUnit); + } + } + } + } + + private static CleanupScheduler getCleanupScheduler() + { + try + { + Class<?> generatorUIUtilClass = CommonPlugin.loadClass("org.eclipse.emf.codegen.ecore.ui", + "org.eclipse.emf.codegen.ecore.genmodel.presentation.GeneratorUIUtil"); + return (CleanupScheduler)generatorUIUtilClass.getField("CLEANUP_SCHEDULER").get(null); + } + catch (Throwable t) + { + return null; + } + } + } +} |