diff options
3 files changed, 96 insertions, 15 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.tools/META-INF/MANIFEST.MF b/plugins/infra/core/org.eclipse.papyrus.infra.tools/META-INF/MANIFEST.MF index a9e4437ec4e..0669725e0da 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.tools/META-INF/MANIFEST.MF +++ b/plugins/infra/core/org.eclipse.papyrus.infra.tools/META-INF/MANIFEST.MF @@ -9,7 +9,8 @@ Export-Package: org.eclipse.papyrus.infra.tools, Require-Bundle: org.eclipse.papyrus.infra.core.log;bundle-version="[1.2.0,2.0.0)";visibility:=reexport, org.eclipse.core.databinding.observable;bundle-version="[1.6.0,2.0.0)";visibility:=reexport, org.eclipse.emf.common;bundle-version="[2.12.0,3.0.0)";visibility:=reexport, - org.eclipse.core.resources;bundle-version="3.11.0" + org.eclipse.core.resources;bundle-version="3.11.0", + org.apache.commons.io;bundle-version="2.2.0" Bundle-Vendor: %Bundle-Vendor Bundle-ActivationPolicy: lazy Bundle-Version: 3.0.0.qualifier diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ICleanUntouched.java b/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ICleanUntouched.java new file mode 100644 index 00000000000..9429dc13223 --- /dev/null +++ b/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ICleanUntouched.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2018 CEA LIST. + * 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: + * CEA LIST - initial API and implementation + *******************************************************************************/ + +package org.eclipse.papyrus.infra.tools.file; + +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; + +/** + * Manage the deletion of code that has not been access in the code generation + * process and can be removed. + * @since 3.0 + */ +public interface ICleanUntouched { + + /** + * Remove files for which no code has been generated. This enables the + * removal of old code e.g. after a suppression or renaming of elements + * in the model. + * + * @param folder + * a folder from which cleaning should start + * @param monitor + * a progress monitor + * @throws CoreException + * a possible exception during file operations + */ + public void cleanUntouched(IFolder folder, IProgressMonitor monitor) throws CoreException; +} diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ProjectBasedFileAccess.java b/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ProjectBasedFileAccess.java index 7a9523bd30b..690dc53fa20 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ProjectBasedFileAccess.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/file/ProjectBasedFileAccess.java @@ -1,6 +1,6 @@ /***************************************************************************** - * Copyright (c) 2014 CEA LIST and others. - * + * Copyright (c) 2014, 2018 CEA LIST 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 @@ -8,49 +8,59 @@ * * Contributors: * CEA LIST - Initial API and implementation - * + * *****************************************************************************/ package org.eclipse.papyrus.infra.tools.file; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.io.IOUtils; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; /** * A simple class providing access to the file system relative to a specified project * */ -public class ProjectBasedFileAccess implements IPFileSystemAccess { +public class ProjectBasedFileAccess implements IPFileSystemAccess, ICleanUntouched { IProject project; String subFolderName; /** + * Store information which files have been created in a hash map + */ + protected Map<String, Boolean> touched; + + /** * force an update of resources (currently always true) */ final static boolean force = true; /** * Create a project based file access for a specific project. - * + * * @param project * the project for which file system access is provided */ public ProjectBasedFileAccess(IProject project) { - setProject(project, null); + this(project, null); } /** * Create a project based file access for a specific project. - * + * * @param project * the project for which file system access is provided * @param subFolderName @@ -58,11 +68,12 @@ public class ProjectBasedFileAccess implements IPFileSystemAccess { */ public ProjectBasedFileAccess(IProject project, String subFolderName) { setProject(project, subFolderName); + touched = new HashMap<String, Boolean>(); } /** * Set/update project, reset subFolderName to null - * + * * @param project * the project for which file system access is provided */ @@ -94,22 +105,33 @@ public class ProjectBasedFileAccess implements IPFileSystemAccess { */ public void generateFile(String fileName, String content) { IFile file = getFile(fileName); + touched.put(file.getFullPath().toString(), true); InputStream contentStream = new ByteArrayInputStream(content.getBytes()); try { + boolean needsRefresh = false; if (file.exists()) { - if (force) { - file.setContents(contentStream, true, true, null); + if (!IOUtils.contentEquals( file.getContents(), contentStream)) { + if (force) { + contentStream.reset(); + file.setContents(contentStream, true, true, null); + needsRefresh = true; + } + // else - file is not updated } - // else - file is not updated } else { // the file does not exists file.create(contentStream, true, null); + needsRefresh = true; } // Refresh the container for the newly created files. This needs to be done even // during error because of the possibility for partial results. - file.refreshLocal(IResource.DEPTH_INFINITE, null); + if (needsRefresh) { + file.refreshLocal(IResource.DEPTH_INFINITE, null); + } } catch (CoreException e) { throw new RuntimeException(e); + } catch (IOException ioe) { + throw new RuntimeException(ioe); } } @@ -145,6 +167,7 @@ public class ProjectBasedFileAccess implements IPFileSystemAccess { for (int i = 0; i < paths.length - 1; i++) { String path = paths[i]; packageContainer = getFolder(packageContainer, path); + touched.put(packageContainer.getFullPath().toString(), true); if (!packageContainer.exists()) { // if packageContainer is a Project, it necessarily exists ((IFolder) packageContainer).create(false, true, null); @@ -159,7 +182,7 @@ public class ProjectBasedFileAccess implements IPFileSystemAccess { /** * Return the a sub-folder from a given container - * + * * @param container * a container (must be a project or a folder) * @param folderName @@ -180,7 +203,7 @@ public class ProjectBasedFileAccess implements IPFileSystemAccess { /** * Return a file within a container - * + * * @param container * a container (must be a project or a folder) * @param fileName @@ -195,4 +218,23 @@ public class ProjectBasedFileAccess implements IPFileSystemAccess { } return null; } + + /** + * @see org.eclipse.papyrus.infra.tools.file.ICleanUntouched#cleanOldCode(org.eclipse.core.resources.IFolder, org.eclipse.core.runtime.IProgressMonitor) + * + * @param folder + * @param monitor + * @throws CoreException + */ + @Override + public void cleanUntouched(IFolder folder, IProgressMonitor monitor) throws CoreException { + for (IResource resource : folder.members()) { + if (resource instanceof IFolder) { + cleanUntouched((IFolder) resource, monitor); + } + if (!touched.containsKey(resource.getFullPath().toString())) { + resource.delete(false, monitor); + } + } + } } |