Skip to content

Commit

Permalink
Ant task check dependencies
Browse files Browse the repository at this point in the history
- checks for unused dependencies
  • Loading branch information
pazi146 authored and oberlehner committed Sep 24, 2024
1 parent 3c845c5 commit 859f741
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/org.eclipse.fordiac.ide.ant/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ Require-Bundle: org.eclipse.core.resources,
org.eclipse.ant.core,
org.eclipse.fordiac.ide.systemmanagement,
org.eclipse.fordiac.ide.model,
org.eclipse.fordiac.ide.model.eval,
org.eclipse.fordiac.ide.export.ui,
org.eclipse.fordiac.ide.export.forte_ng,
org.eclipse.fordiac.ide.export,
org.eclipse.fordiac.ide.typemanagement,
org.eclipse.fordiac.ide.export.xmi,
org.eclipse.fordiac.ide.library,
org.eclipse.fordiac.ide.library.model,
org.eclipse.fordiac.ide.hierarchymanager.model,
org.eclipse.fordiac.ide.hierarchymanager.ui,
Expand Down
8 changes: 8 additions & 0 deletions plugins/org.eclipse.fordiac.ide.ant/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@
name="fordiac.validateHierarchy">
</antTask>
</extension>
<extension
point="org.eclipse.ant.core.antTasks">
<antTask
class="org.eclipse.fordiac.ide.ant.ant.CheckUnusedDependencies"
library="ant_tasks/ant-ant.jar"
name="fordiac.checkUnusedDependencies">
</antTask>
</extension>
<extension
point="org.eclipse.ant.core.extraClasspathEntries">
<extraClasspathEntry
Expand Down
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;
}
}

0 comments on commit 859f741

Please sign in to comment.