-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ipse.fordiac.ide.ant/src_ant/org/eclipse/fordiac/ide/ant/ant/CheckUnusedDependencies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.eclipse.fordiac.ide.ant.ant; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apache.tools.ant.BuildException; | ||
import org.apache.tools.ant.Task; | ||
import org.eclipse.core.resources.IFolder; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IWorkspace; | ||
import org.eclipse.core.resources.ResourcesPlugin; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.fordiac.ide.library.LibraryManager; | ||
import org.eclipse.fordiac.ide.library.model.library.Manifest; | ||
import org.eclipse.fordiac.ide.library.model.util.ManifestHelper; | ||
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibrary; | ||
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryManager; | ||
|
||
public class CheckUnusedDependencies extends Task { | ||
private String projectName; | ||
|
||
public void setProjectName(final String value) { | ||
projectName = value; | ||
} | ||
|
||
@Override | ||
public void execute() throws BuildException { | ||
if (projectName == null) { | ||
throw new BuildException("Project name not specified!"); //$NON-NLS-1$ | ||
} | ||
final IWorkspace workspace = ResourcesPlugin.getWorkspace(); | ||
final IProject fordiacProject = workspace.getRoot().getProject(projectName); | ||
|
||
final Manifest manifest = ManifestHelper.getContainerManifest(fordiacProject); | ||
if (manifest == null) { | ||
throw new BuildException("Project named '" + projectName + "' lacks a manifest file"); //$NON-NLS-1$ //$NON-NLS-2$ | ||
} | ||
if (manifest.getDependencies() == null || manifest.getDependencies().getRequired().isEmpty()) { | ||
log("No dependencies present in manifest file"); //$NON-NLS-1$ | ||
return; | ||
} | ||
|
||
final IFolder libFolder = fordiacProject.getFolder(LibraryManager.TYPE_LIB_FOLDER_NAME); | ||
final URI libURI = URI.createPlatformResourceURI(libFolder.getFullPath().toString(), true); | ||
|
||
final List<String> symb = new ArrayList<>(); | ||
final Set<URI> libs = new HashSet<>(); | ||
manifest.getDependencies().getRequired().forEach(req -> symb.add(req.getSymbolicName())); | ||
|
||
try { | ||
for (final var res : libFolder.members()) { | ||
if (res instanceof final IFolder folder && folder.isLinked()) { | ||
final Manifest libManifest = ManifestHelper.getContainerManifest(folder); | ||
if (libManifest != null && libManifest.getDependencies() != null) { | ||
libManifest.getDependencies().getRequired().forEach(dep -> { | ||
libs.add(libURI.appendSegment(dep.getSymbolicName())); | ||
symb.remove(dep.getSymbolicName()); | ||
}); | ||
} | ||
} | ||
} | ||
} catch (final CoreException e) { | ||
throw new BuildException(e); | ||
} | ||
|
||
final Set<URI> unused = new HashSet<>(); | ||
symb.forEach(s -> unused.add(libURI.appendSegment(s))); | ||
|
||
final TypeLibrary typeLibrary = TypeLibraryManager.INSTANCE.getTypeLibrary(fordiacProject); | ||
|
||
for (final var typeEntry : typeLibrary.getAllTypes()) { | ||
if (unused.isEmpty()) { | ||
break; | ||
} | ||
|
||
if (libs.stream().anyMatch(l -> uriContained(l, typeEntry.getURI()))) { | ||
continue; // don't check references contained inside dependencies | ||
} | ||
|
||
typeEntry.getDependencies().forEach(dep -> { | ||
final var found = unused.stream().filter(u -> uriContained(u, dep.getURI())).toList(); | ||
if (!found.isEmpty()) { | ||
unused.removeAll(found); | ||
} | ||
}); | ||
} | ||
|
||
if (!unused.isEmpty()) { | ||
throw new BuildException("Unused dependencies detected: " //$NON-NLS-1$ | ||
+ String.join(", ", unused.stream().map(URI::lastSegment).toList())); //$NON-NLS-1$ | ||
} | ||
log("No unused dependencies detected"); //$NON-NLS-1$ | ||
} | ||
|
||
private static boolean uriContained(final URI base, final URI other) { | ||
final String[] baseSegments = base.segments(); | ||
final String[] otherSegments = other.segments(); | ||
|
||
if (baseSegments.length > otherSegments.length) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < baseSegments.length; i++) { | ||
if (!baseSegments[i].equals(otherSegments[i])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |