Skip to content

Commit b7dd0ee

Browse files
committed
Add support to filter files to perform cleanup
1 parent 41fe987 commit b7dd0ee

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUp.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818
import java.util.Map;
19+
import java.util.regex.Pattern;
1920
import java.util.stream.Collectors;
2021

2122
import org.eclipse.core.resources.IProject;
2223
import org.eclipse.core.resources.ProjectScope;
2324
import org.eclipse.core.runtime.CoreException;
25+
import org.eclipse.core.runtime.IPath;
2426
import org.eclipse.jdt.core.ICompilationUnit;
2527
import org.eclipse.jdt.core.IJavaElement;
2628
import org.eclipse.jdt.core.IJavaProject;
@@ -43,11 +45,14 @@ public class CleanUp extends AbstractEclipseBuild<CleanupResult> {
4345

4446
private Map<String, String> customProfile;
4547
private boolean applyCleanupsIndividually;
48+
private List<Pattern> ignores;
4649

47-
CleanUp(Path projectDir, boolean debug, Map<String, String> customProfile, boolean applyCleanupsIndividually) {
50+
CleanUp(Path projectDir, boolean debug, Map<String, String> customProfile, boolean applyCleanupsIndividually,
51+
List<Pattern> ignores) {
4852
super(projectDir, debug);
4953
this.customProfile = customProfile;
5054
this.applyCleanupsIndividually = applyCleanupsIndividually;
55+
this.ignores = ignores;
5156
}
5257

5358
@Override
@@ -101,6 +106,9 @@ private List<ICompilationUnit> getCompilationUnits(IProject project) throws Java
101106
IPackageFragment pf = (IPackageFragment) javaElement;
102107
ICompilationUnit[] compilationUnits = pf.getCompilationUnits();
103108
for (ICompilationUnit compilationUnit : compilationUnits) {
109+
if (isIgnored(compilationUnit)) {
110+
continue;
111+
}
104112
units.add(compilationUnit);
105113
}
106114
}
@@ -110,6 +118,22 @@ private List<ICompilationUnit> getCompilationUnits(IProject project) throws Java
110118
return units;
111119
}
112120

121+
private boolean isIgnored(ICompilationUnit compilationUnit) {
122+
if (ignores == null || ignores.isEmpty()) {
123+
return false;
124+
}
125+
IProject project = compilationUnit.getJavaProject().getProject();
126+
IPath location = project.getFullPath();
127+
IPath path = compilationUnit.getPath().makeRelativeTo(location);
128+
String pathString = path.toString();
129+
for (Pattern ignored : ignores) {
130+
if (ignored.matcher(pathString).matches()) {
131+
return true;
132+
}
133+
}
134+
return false;
135+
}
136+
113137
private ICleanUp[] getCleanups(CleanupResult result, CleanUpOptions options) {
114138
ICleanUp[] cleanUps = JavaPlugin.getDefault().getCleanUpRegistry().createCleanUps();
115139
for (ICleanUp cleanUp : cleanUps) {

tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
package org.eclipse.tycho.cleancode;
1414

1515
import java.io.File;
16+
import java.util.List;
1617
import java.util.Map;
18+
import java.util.regex.Pattern;
1719

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

48+
/**
49+
* Specifies patterns of files that should be excluded
50+
*/
51+
@Parameter
52+
private List<String> ignores;
53+
4654
@Override
4755
protected String[] getRequireBundles() {
4856
return new String[] { "org.eclipse.jdt.ui" };
@@ -55,7 +63,15 @@ protected String getName() {
5563

5664
@Override
5765
protected CleanUp createExecutable() {
58-
return new CleanUp(project.getBasedir().toPath(), debug, cleanUpProfile, applyCleanupsIndividually);
66+
return new CleanUp(project.getBasedir().toPath(), debug, cleanUpProfile, applyCleanupsIndividually,
67+
getIgnores());
68+
}
69+
70+
private List<Pattern> getIgnores() {
71+
if (ignores == null || ignores.isEmpty()) {
72+
return null;
73+
}
74+
return ignores.stream().map(Pattern::compile).toList();
5975
}
6076

6177
@Override

0 commit comments

Comments
 (0)