Skip to content

Commit

Permalink
Delete orphan files that are copied/generated by bnd automatic manifest
Browse files Browse the repository at this point in the history
Currently we copy everything that would normally go to the jar into the
output folder, but if on the next build the resource is not put anymore
we have some orphan files.

This records what resources are created as part of the project build and
finally deletes any orphans on close.

Fix #587
  • Loading branch information
laeubi committed Oct 25, 2023
1 parent dd321b5 commit a8ea012
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static void addResources(Jar jar, IContainer container, String prefix, P
return;
}
for (IResource resource : container.members()) {
if (filter == null || filter.test(container)) {
if (filter == null || filter.test(resource)) {
if (resource instanceof IFile) {
IPath projectRelativePath = resource.getProjectRelativePath();
String relativePath = projectRelativePath.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.pde.internal.core.bnd;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
Expand All @@ -27,9 +29,11 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.project.PDEProject;

import aQute.bnd.osgi.Jar;
Expand All @@ -38,13 +42,29 @@

public class ProjectJar extends Jar {

public static final QualifiedName GENERATED_PROPERTY = new QualifiedName(PDECore.PLUGIN_ID, "bndgenerated"); //$NON-NLS-1$

private final IContainer outputFolder;
private IFile manifestFile;
private final Map<String, IFile> orphanFilesMap = new HashMap<>();

public ProjectJar(IProject project, Predicate<IResource> filter) throws CoreException {
super(project.getName());
outputFolder = PDEProject.getJavaOutputFolder(project);
FileResource.addResources(this, outputFolder, filter);
Predicate<IResource> resourceScanner = r -> {
if (r instanceof IFile f) {
try {
String path = r.getPersistentProperty(GENERATED_PROPERTY);
if (path != null) {
orphanFilesMap.put(path, f);
}
} catch (CoreException e) {
// can't use that the ...
}
}
return filter.test(r);
};
FileResource.addResources(this, outputFolder, resourceScanner);
if (project.hasNature(JavaCore.NATURE_ID)) {
IJavaProject javaProject = JavaCore.create(project);
IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
Expand All @@ -54,7 +74,7 @@ public ProjectJar(IProject project, Predicate<IResource> filter) throws CoreExce
IPath location = cp.getOutputLocation();
if (location != null) {
IFolder otherOutputFolder = workspaceRoot.getFolder(location);
FileResource.addResources(this, otherOutputFolder, filter);
FileResource.addResources(this, otherOutputFolder, resourceScanner);
}
}
}
Expand Down Expand Up @@ -127,11 +147,13 @@ public boolean putResource(String path, Resource resource, boolean overwrite) {
file.create(stream, true, null);
}
}
file.setPersistentProperty(GENERATED_PROPERTY, path);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
orphanFilesMap.remove(path);
return super.putResource(path, new FileResource(file), overwrite);
}

Expand All @@ -152,4 +174,20 @@ public String toString() {
return "Project" + super.toString(); //$NON-NLS-1$
}

@Override
public void close() {
cleanup();
super.close();
}

private void cleanup() {
for (IFile file : orphanFilesMap.values()) {
try {
file.delete(true, null);
} catch (Exception e) {
// must wait for next clean then...
}
}
}

}

0 comments on commit a8ea012

Please sign in to comment.