From 0b250f569edeb1ea983aa761337b7d57057ac328 Mon Sep 17 00:00:00 2001 From: Eike Stepper Date: Mon, 16 Apr 2018 07:48:55 +0200 Subject: [Releng] Add Ant task to source cleanup projects --- .../META-INF/MANIFEST.MF | 1 + plugins/org.eclipse.emf.cdo.migrator/plugin.xml | 1 + .../migrator/tasks/CleanupProjectTask.java | 137 +++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 plugins/org.eclipse.emf.cdo.migrator/src/org/eclipse/emf/cdo/internal/migrator/tasks/CleanupProjectTask.java (limited to 'plugins') 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 @@ + 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 compilationUnits = collectCompilationUnits(project, task); + SCHEDULER.schedule(compilationUnits); + } + } + + private static Set collectCompilationUnits(IProject project, CDOTask task) + { + Set compilationUnits = new LinkedHashSet(); + 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 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; + } + } + } +} -- cgit v1.2.3