This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
Improve exclude rules #8
Merged
takecare
merged 6 commits into
master
from
feature/PT-311/PT-313_exclude_file_collections
Nov 14, 2016
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af85b0f
Move exclude filters setup to base configurator class
mr-archano 15c158a
Introduce SourceFilter to hold and apply exclude rules to SourceTask
mr-archano 0899aea
Use SourceFilter in all configurators
mr-archano af043be
Add support for single File as well as Iterable<File> as exclude rules
mr-archano 01b2681
Rename tests to avoid confusion
mr-archano 77be5ee
Add missing tests using source sets as exclude rules
mr-archano File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
plugin/src/main/groovy/com/novoda/staticanalysis/internal/SourceFilter.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.novoda.staticanalysis.internal | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.file.FileTree | ||
import org.gradle.api.tasks.SourceTask | ||
|
||
class SourceFilter { | ||
|
||
private final Project project | ||
private final List<Object> excludes = [] | ||
|
||
SourceFilter(Project project) { | ||
this.project = project | ||
} | ||
|
||
void exclude(Object exclude) { | ||
excludes.add(exclude) | ||
} | ||
|
||
void applyTo(SourceTask task) { | ||
excludes.each { exclude -> | ||
if (exclude instanceof File) { | ||
apply(task, project.files(exclude)) | ||
} else if (exclude instanceof FileCollection) { | ||
apply(task, exclude) | ||
} else if (exclude instanceof Iterable<File>) { | ||
apply(task, exclude.inject(null, accumulateIntoTree()) as FileTree) | ||
} else { | ||
task.exclude(exclude as String) | ||
} | ||
} | ||
} | ||
|
||
private void apply(SourceTask task, FileCollection excludedFiles) { | ||
task.source = task.source.findAll { !excludedFiles.contains(it) } | ||
} | ||
|
||
private def accumulateIntoTree() { | ||
return { tree, file -> tree?.plus(project.fileTree(file)) ?: project.fileTree(file) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HAX so much |
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
plugin/src/test/groovy/com/novoda/staticanalysis/internal/SourceFilterTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.novoda.staticanalysis.internal | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.tasks.SourceTask | ||
import org.gradle.testfixtures.ProjectBuilder | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
|
||
import static com.google.common.truth.Truth.assertThat | ||
import static com.novoda.test.Fixtures.Checkstyle.SOURCES_WITH_ERRORS | ||
import static com.novoda.test.Fixtures.Checkstyle.SOURCES_WITH_WARNINGS | ||
|
||
class SourceFilterTest { | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder() | ||
|
||
private Project project | ||
private SourceFilter filter | ||
|
||
@Before | ||
public void setUp() { | ||
project = ProjectBuilder.builder() | ||
.withProjectDir(temporaryFolder.newFolder()) | ||
.build() | ||
filter = new SourceFilter(project) | ||
} | ||
|
||
@Test | ||
public void shouldKeepAllSourcesWhenNoExcludeFilterProvided() { | ||
SourceTask task = givenTaskWith(errorsSources + warningsSources) | ||
|
||
filter.applyTo(task) | ||
|
||
assertThat(task.source).containsExactlyElementsIn(errorsSources + warningsSources) | ||
} | ||
|
||
@Test | ||
public void shouldRemoveFilesMatchingThePatternWhenExcludePatternProvided() { | ||
SourceTask task = givenTaskWith(errorsSources + warningsSources) | ||
filter.exclude('**/*.java') | ||
|
||
filter.applyTo(task) | ||
|
||
assertThat(task.source).isEmpty() | ||
} | ||
|
||
@Test | ||
public void shouldRemoveFilesInSpecifiedFileCollectionWhenExcludeFileCollectionProvided() { | ||
SourceTask task = givenTaskWith(errorsSources + warningsSources) | ||
filter.exclude(project.fileTree(SOURCES_WITH_ERRORS)) | ||
|
||
filter.applyTo(task) | ||
|
||
assertThat(task.source).containsExactlyElementsIn(warningsSources) | ||
} | ||
|
||
@Test | ||
public void shouldRemoveFilesInSpecifiedFileCollectionWhenExcludeSourceSetProvided() { | ||
project.apply plugin: 'java' | ||
project.sourceSets.main { | ||
java { | ||
srcDir project.file(SOURCES_WITH_ERRORS) | ||
} | ||
} | ||
SourceTask task = givenTaskWith(errorsSources) | ||
filter.exclude(project.sourceSets.main.java.srcDirs) | ||
|
||
filter.applyTo(task) | ||
|
||
assertThat(task.source).isEmpty() | ||
} | ||
|
||
@Test | ||
public void shouldRemoveFilesInSpecifiedFileCollectionWhenSourceFileExcluded() { | ||
project.apply plugin: 'java' | ||
project.sourceSets.main { | ||
java { | ||
srcDir project.file(SOURCES_WITH_ERRORS) | ||
} | ||
} | ||
SourceTask task = givenTaskWith(errorsSources) | ||
filter.exclude(new File(SOURCES_WITH_ERRORS, 'Greeter.java')) | ||
|
||
filter.applyTo(task) | ||
|
||
assertThat(task.source).isEmpty() | ||
} | ||
|
||
private SourceTask givenTaskWith(Iterable<File> files) { | ||
project.tasks.create('someSourceTask', SourceTask) { | ||
source = project.files(files) | ||
} | ||
} | ||
|
||
private Set<File> getErrorsSources() { | ||
project.fileTree(SOURCES_WITH_ERRORS).files | ||
} | ||
|
||
private Set<File> getWarningsSources() { | ||
project.fileTree(SOURCES_WITH_WARNINGS).files | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So is
inject
some kind offold
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. why
inject
instead offold
or the more popularreduce
is beyond me :/