Skip to content

Commit

Permalink
Add support to filter files to perform cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Feb 15, 2025
1 parent 41fe987 commit b7dd0ee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -43,11 +45,14 @@ public class CleanUp extends AbstractEclipseBuild<CleanupResult> {

private Map<String, String> customProfile;
private boolean applyCleanupsIndividually;
private List<Pattern> ignores;

CleanUp(Path projectDir, boolean debug, Map<String, String> customProfile, boolean applyCleanupsIndividually) {
CleanUp(Path projectDir, boolean debug, Map<String, String> customProfile, boolean applyCleanupsIndividually,
List<Pattern> ignores) {
super(projectDir, debug);
this.customProfile = customProfile;
this.applyCleanupsIndividually = applyCleanupsIndividually;
this.ignores = ignores;
}

@Override
Expand Down Expand Up @@ -101,6 +106,9 @@ private List<ICompilationUnit> getCompilationUnits(IProject project) throws Java
IPackageFragment pf = (IPackageFragment) javaElement;
ICompilationUnit[] compilationUnits = pf.getCompilationUnits();
for (ICompilationUnit compilationUnit : compilationUnits) {
if (isIgnored(compilationUnit)) {
continue;
}
units.add(compilationUnit);
}
}
Expand All @@ -110,6 +118,22 @@ private List<ICompilationUnit> getCompilationUnits(IProject project) throws Java
return units;
}

private boolean isIgnored(ICompilationUnit compilationUnit) {
if (ignores == null || ignores.isEmpty()) {
return false;
}
IProject project = compilationUnit.getJavaProject().getProject();
IPath location = project.getFullPath();
IPath path = compilationUnit.getPath().makeRelativeTo(location);
String pathString = path.toString();
for (Pattern ignored : ignores) {
if (ignored.matcher(pathString).matches()) {
return true;
}
}
return false;
}

private ICleanUp[] getCleanups(CleanupResult result, CleanUpOptions options) {
ICleanUp[] cleanUps = JavaPlugin.getDefault().getCleanUpRegistry().createCleanUps();
for (ICleanUp cleanUp : cleanUps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.eclipse.tycho.cleancode;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -43,6 +45,12 @@ public class CleanUpMojo extends AbstractEclipseBuildMojo<CleanupResult> {
@Parameter
private boolean applyCleanupsIndividually;

/**
* Specifies patterns of files that should be excluded
*/
@Parameter
private List<String> ignores;

@Override
protected String[] getRequireBundles() {
return new String[] { "org.eclipse.jdt.ui" };
Expand All @@ -55,7 +63,15 @@ protected String getName() {

@Override
protected CleanUp createExecutable() {
return new CleanUp(project.getBasedir().toPath(), debug, cleanUpProfile, applyCleanupsIndividually);
return new CleanUp(project.getBasedir().toPath(), debug, cleanUpProfile, applyCleanupsIndividually,
getIgnores());
}

private List<Pattern> getIgnores() {
if (ignores == null || ignores.isEmpty()) {
return null;
}
return ignores.stream().map(Pattern::compile).toList();
}

@Override
Expand Down

0 comments on commit b7dd0ee

Please sign in to comment.