Skip to content

Commit

Permalink
Remove special handling of multiple String targets
Browse files Browse the repository at this point in the history
Previously, the plugin attempted to create a single file collection when multiple String targets
were specified. However, this implementation failed to add automatic exclusion for `.git` etc
with multiple target strings.

This commit simplifies the implementation, removing the special handling of multiple target
Strings. Instead, a separate independent file collection is created for each target string.

This simpler implementation fixes the issue where standard directories were not excluded
when multiple target strings were specified.
  • Loading branch information
bigdaz committed Apr 8, 2021
1 parent be2af84 commit aff7705
Showing 1 changed file with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -37,6 +39,7 @@
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.tasks.util.PatternFilterable;
Expand Down Expand Up @@ -196,15 +199,11 @@ private FileCollection parseTargetsIsExclude(Object[] targets, boolean isExclude
} else if (targets.length == 1) {
return parseTargetIsExclude(targets[0], isExclude);
} else {
if (Stream.of(targets).allMatch(o -> o instanceof String)) {
return parseTargetIsExclude(Arrays.asList(targets), isExclude);
} else {
FileCollection union = getProject().files();
for (Object target : targets) {
union = union.plus(parseTargetIsExclude(target, isExclude));
}
return union;
FileCollection union = getProject().files();
for (Object target : targets) {
union = union.plus(parseTargetIsExclude(target, isExclude));
}
return union;
}
}

Expand All @@ -221,30 +220,20 @@ protected final FileCollection parseTarget(Object target) {
private final FileCollection parseTargetIsExclude(Object target, boolean isExclude) {
if (target instanceof FileCollection) {
return (FileCollection) target;
} else if (target instanceof String ||
(target instanceof List && ((List<?>) target).stream().allMatch(o -> o instanceof String))) {
} else if (target instanceof String) {
File dir = getProject().getProjectDir();
PatternFilterable userExact; // exactly the collection that the user specified
if (target instanceof String) {
userExact = getProject().fileTree(dir).include((String) target);
} else {
// target can only be a List<String> at this point
@SuppressWarnings("unchecked")
List<String> targetList = (List<String>) target;
userExact = getProject().fileTree(dir).include(targetList);
}
boolean filterOutGitAndGradle;
ConfigurableFileTree matchedFiles = getProject().fileTree(dir);
String targetString = (String) target;
matchedFiles.include(targetString);

// since people are likely to do '**/*.md', we want to make sure to exclude folders
// they don't want to format which will slow down the operation greatly
// but we only want to do that if they are *including* - if they are specifying
// what they want to exclude, we shouldn't filter at all
if (target instanceof String && !isExclude) {
String str = (String) target;
filterOutGitAndGradle = str.startsWith("**/*") || str.startsWith("**\\*");
} else {
filterOutGitAndGradle = false;
if (isExclude) {
return matchedFiles;
}
if (filterOutGitAndGradle) {
if (targetString.startsWith("**/*") || targetString.startsWith("**\\*")) {
List<String> excludes = new ArrayList<>();
// no git
excludes.add(".git");
Expand All @@ -257,9 +246,9 @@ private final FileCollection parseTargetIsExclude(Object target, boolean isExclu
for (Project subproject : getProject().getSubprojects()) {
relativizeIfSubdir(excludes, dir, subproject.getBuildDir());
}
userExact = userExact.exclude(excludes);
matchedFiles.exclude(excludes);
}
return (FileCollection) userExact;
return matchedFiles;
} else {
return getProject().files(target);
}
Expand Down

0 comments on commit aff7705

Please sign in to comment.