-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Filtering] Add support for filtering packages like Maven plugin (#53)
This adds package filters similar to how the Maven plugin filters in its `<packages>` block. This supports ant-like patterns similar to how patterns filter. Partial implementation of #16.
- Loading branch information
Showing
7 changed files
with
669 additions
and
16 deletions.
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
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
90 changes: 90 additions & 0 deletions
90
...assertj/generator/gradle/tasks/config/patterns/JavaIdentifierNamePatternFilterableBase.kt
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,90 @@ | ||
/* | ||
* Copyright 2023. assertj-generator-gradle-plugin contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package org.assertj.generator.gradle.tasks.config.patterns | ||
|
||
import java.io.IOException | ||
import java.io.ObjectInputStream | ||
import java.io.ObjectOutputStream | ||
import java.util.function.Predicate | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
sealed class JavaIdentifierNamePatternFilterableBase<T, SELF> | ||
where SELF : JavaIdentifierNamePatternFilterableBase<T, SELF> { | ||
private var includePredicates = mutableSetOf<PatternPredicate<T>>() | ||
private var excludePredicates = mutableSetOf<PatternPredicate<T>>() | ||
|
||
internal abstract fun compilePatterns(patterns: Iterable<String>): Sequence<PatternPredicate<T>> | ||
|
||
internal fun asPredicate(): Predicate<T> { | ||
return Predicate { t -> | ||
(includePredicates.isEmpty() || includePredicates.any { it.test(t) }) && | ||
(excludePredicates.isEmpty() || excludePredicates.none { it.test(t) }) | ||
} | ||
} | ||
|
||
fun setIncludes(includes: Iterable<String>): SELF { | ||
this.includePredicates = compilePatterns(includes).toMutableSet() | ||
return this as SELF | ||
} | ||
|
||
fun setExcludes(excludes: Iterable<String>): SELF { | ||
this.excludePredicates = compilePatterns(excludes).toMutableSet() | ||
return this as SELF | ||
} | ||
|
||
fun include(vararg includes: String): SELF { | ||
this.includePredicates += compilePatterns(includes.asIterable()) | ||
return this as SELF | ||
} | ||
|
||
fun include(includes: Iterable<String>): SELF { | ||
this.includePredicates += compilePatterns(includes) | ||
return this as SELF | ||
} | ||
|
||
fun exclude(vararg excludes: String): SELF { | ||
this.excludePredicates += compilePatterns(excludes.asIterable()) | ||
return this as SELF | ||
} | ||
|
||
fun exclude(excludes: Iterable<String>): SELF { | ||
this.excludePredicates += compilePatterns(excludes) | ||
return this as SELF | ||
} | ||
|
||
internal interface PatternPredicate<T> : Predicate<T> { | ||
val pattern: String | ||
} | ||
|
||
@Throws(IOException::class) | ||
protected fun writeObjectImpl(o: ObjectOutputStream) { | ||
o.writeInt(includePredicates.size) | ||
for (pattern in includePredicates.map { it.pattern }) { | ||
o.writeUTF(pattern) | ||
} | ||
|
||
o.writeInt(excludePredicates.size) | ||
for (pattern in excludePredicates.map { it.pattern }) { | ||
o.writeUTF(pattern) | ||
} | ||
} | ||
|
||
@Throws(IOException::class, ClassNotFoundException::class) | ||
protected fun readObjectImpl(i: ObjectInputStream) { | ||
val includesSize = i.readInt() | ||
setIncludes((0 until includesSize).map { i.readUTF() }) | ||
|
||
val excludesSize = i.readInt() | ||
setExcludes((0 until excludesSize).map { i.readUTF() }) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...main/kotlin/org/assertj/generator/gradle/tasks/config/patterns/JavaNamePatternCompiler.kt
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,32 @@ | ||
/* | ||
* Copyright 2023. assertj-generator-gradle-plugin contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package org.assertj.generator.gradle.tasks.config.patterns | ||
|
||
private const val WILDCARD_MARKER = "!!WILDCARD_MARKER!!" | ||
|
||
internal object JavaNamePatternCompiler { | ||
fun compilePattern(pattern: String): Regex { | ||
val escapedPackageCharacters = pattern | ||
.replace(".", "\\.") | ||
.replace("$", "\\$") | ||
|
||
// Order matters because if we do single "*" first we double remove the "**" | ||
val withWildcardMarkers = escapedPackageCharacters | ||
.replace("**", "[\\w\\.]$WILDCARD_MARKER") | ||
.replace("*", "\\w$WILDCARD_MARKER") | ||
|
||
val withRegexWildcards = withWildcardMarkers.replace(WILDCARD_MARKER, "*") | ||
|
||
return Regex(withRegexWildcards) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...in/org/assertj/generator/gradle/tasks/config/patterns/JavaPackageNamePatternFilterable.kt
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,59 @@ | ||
/* | ||
* Copyright 2023. assertj-generator-gradle-plugin contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package org.assertj.generator.gradle.tasks.config.patterns | ||
|
||
import com.google.common.reflect.TypeToken | ||
import org.assertj.generator.gradle.tasks.config.patterns.JavaNamePatternCompiler.compilePattern | ||
import java.io.IOException | ||
import java.io.ObjectInputStream | ||
import java.io.ObjectOutputStream | ||
import java.io.Serializable | ||
|
||
/** | ||
* Implements a similar construction to [org.gradle.api.tasks.util.PatternFilterable] that will match | ||
* [TypeToken] instances. | ||
*/ | ||
open class JavaPackageNamePatternFilterable internal constructor() : | ||
JavaIdentifierNamePatternFilterableBase<String, JavaPackageNamePatternFilterable>(), Serializable { | ||
|
||
override fun compilePatterns(patterns: Iterable<String>): Sequence<PatternPredicate<String>> { | ||
return patterns.asSequence().map { PackagePatternPredicate.compile(it) } | ||
} | ||
|
||
@Throws(IOException::class) | ||
protected fun writeObject(o: ObjectOutputStream) { | ||
super.writeObjectImpl(o) | ||
} | ||
|
||
@Throws(IOException::class, ClassNotFoundException::class) | ||
protected fun readObject(i: ObjectInputStream) { | ||
super.readObjectImpl(i) | ||
} | ||
|
||
internal data class PackagePatternPredicate( | ||
override val pattern: String, | ||
private val namePattern: Regex, | ||
) : PatternPredicate<String> { | ||
override fun test(t: String): Boolean { | ||
return namePattern.matches(t) | ||
} | ||
|
||
companion object { | ||
fun compile(pattern: String) = PackagePatternPredicate(pattern, compilePattern(pattern)) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val serialVersionUID = 48634795L | ||
} | ||
} |
Oops, something went wrong.