From 2a5a9017acce529854b5cd35b6b5224c65bfceb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 15 Feb 2025 07:10:00 +0100 Subject: [PATCH] Add support to filter files to perform cleanup (cherry picked from commit b7dd0ee2725c6f8511121e6f9fc456b94f39b200) --- .../org/eclipse/tycho/cleancode/CleanUp.java | 26 ++++++++++++++++++- .../eclipse/tycho/cleancode/CleanUpMojo.java | 18 ++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUp.java b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUp.java index ac7f54f505..3ed3064604 100644 --- a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUp.java +++ b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUp.java @@ -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; @@ -43,11 +45,14 @@ public class CleanUp extends AbstractEclipseBuild { private Map customProfile; private boolean applyCleanupsIndividually; + private List ignores; - CleanUp(Path projectDir, boolean debug, Map customProfile, boolean applyCleanupsIndividually) { + CleanUp(Path projectDir, boolean debug, Map customProfile, boolean applyCleanupsIndividually, + List ignores) { super(projectDir, debug); this.customProfile = customProfile; this.applyCleanupsIndividually = applyCleanupsIndividually; + this.ignores = ignores; } @Override @@ -101,6 +106,9 @@ private List getCompilationUnits(IProject project) throws Java IPackageFragment pf = (IPackageFragment) javaElement; ICompilationUnit[] compilationUnits = pf.getCompilationUnits(); for (ICompilationUnit compilationUnit : compilationUnits) { + if (isIgnored(compilationUnit)) { + continue; + } units.add(compilationUnit); } } @@ -110,6 +118,22 @@ private List 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) { diff --git a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java index 94123c62ea..3ef25a1b97 100644 --- a/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java +++ b/tycho-cleancode-plugin/src/main/java/org/eclipse/tycho/cleancode/CleanUpMojo.java @@ -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; @@ -43,6 +45,12 @@ public class CleanUpMojo extends AbstractEclipseBuildMojo { @Parameter private boolean applyCleanupsIndividually; + /** + * Specifies patterns of files that should be excluded + */ + @Parameter + private List ignores; + @Override protected String[] getRequireBundles() { return new String[] { "org.eclipse.jdt.ui" }; @@ -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 getIgnores() { + if (ignores == null || ignores.isEmpty()) { + return null; + } + return ignores.stream().map(Pattern::compile).toList(); } @Override