Skip to content

Commit

Permalink
Merge pull request sbt#77 from dwijnand/source-filters-toString
Browse files Browse the repository at this point in the history
Source and filters toStrings
  • Loading branch information
eed3si9n authored Oct 24, 2017
2 parents 62004b2 + c0d925b commit e8c9757
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
10 changes: 8 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Dependencies._
// import com.typesafe.tools.mima.core._, ProblemFilters._
import com.typesafe.tools.mima.core._, ProblemFilters._

def baseVersion: String = "1.1.0"

Expand Down Expand Up @@ -36,5 +36,11 @@ val io = (project in file("io"))
libraryDependencies ++= Seq(scalaCompiler.value % Test, scalaCheck % Test, scalatest % Test),
sourceManaged in (Compile, generateContrabands) := baseDirectory.value / "src" / "main" / "contraband-scala",
initialCommands in console += "\nimport sbt.io._, syntax._",
mimaPreviousArtifacts := Set(organization.value %% moduleName.value % "1.0.0")
mimaPreviousArtifacts := Set(organization.value %% moduleName.value % "1.0.0"),
mimaBinaryIssueFilters ++= Seq(
// MiMa doesn't treat effectively final members as final
// WORKAROUND typesafehub/migration-manager#162
exclude[FinalMethodProblem]("sbt.io.SimpleFilter.accept"),
exclude[FinalMethodProblem]("sbt.io.SimpleFileFilter.accept"),
),
)
28 changes: 25 additions & 3 deletions io/src/main/scala/sbt/internal/io/SourceModificationWatch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,17 @@ private[sbt] final class WatchState private (
* @param base Where to start looking for files.
* @param includeFilter Filter to apply to determine whether to include a file.
* @param excludeFilter Filter to apply to determine whether to ignore a file.
* @param recursive Whether the lists is recursive or immediate children.
*/
final class Source(base: File, includeFilter: FileFilter, excludeFilter: FileFilter) {
final class Source(
base: File,
includeFilter: FileFilter,
excludeFilter: FileFilter,
recursive: Boolean
) {

def this(base: File, includeFilter: FileFilter, excludeFilter: FileFilter) =
this(base, includeFilter, excludeFilter, true)

/**
* Determine whether `p` should be included in this source.
Expand All @@ -151,8 +160,21 @@ final class Source(base: File, includeFilter: FileFilter, excludeFilter: FileFil
* Gathers all the paths from this source without applying filters.
* @return A sequence of all the paths collected from this source.
*/
private[sbt] def getUnfilteredPaths(): Seq[Path] =
base.allPaths.get.map(_.toPath)
private[sbt] def getUnfilteredPaths(): Seq[Path] = {
val pathFinder = if (recursive) base.allPaths else base.glob(AllPassFilter)
pathFinder.get.map(_.toPath)
}

def withRecursive(recursive: Boolean): Source =
new Source(base, includeFilter, excludeFilter, recursive)

override def toString =
s"""Source(
| base = $base,
| includeFilter = $includeFilter,
| excludeFilter = $excludeFilter,
| recursive = $recursive,
|)""".stripMargin

}

Expand Down
44 changes: 31 additions & 13 deletions io/src/main/scala/sbt/io/NameFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ trait FileFilter extends java.io.FileFilter {

/** Constructs a filter that accepts a `File` if it matches either this filter or the given `filter`. */
def ||(filter: FileFilter): FileFilter =
new SimpleFileFilter(file => accept(file) || filter.accept(file))
new SimpleFileFilter(file => accept(file) || filter.accept(file)) {
override def toString = s"SimpleFileFilter(${FileFilter.this} || $filter)"
}

/** Constructs a filter that accepts a `File` if it matches both this filter and the given `filter`. */
def &&(filter: FileFilter): FileFilter =
new SimpleFileFilter(file => accept(file) && filter.accept(file))
new SimpleFileFilter(file => accept(file) && filter.accept(file)) {
override def toString = s"SimpleFileFilter(${FileFilter.this} && $filter)"
}

/** Constructs a filter that accepts a `File` if it matches this filter but does not match the given `filter`. */
def --(filter: FileFilter): FileFilter =
new SimpleFileFilter(file => accept(file) && !filter.accept(file))
new SimpleFileFilter(file => accept(file) && !filter.accept(file)) {
override def toString = s"SimpleFileFilter(${FileFilter.this} -- $filter)"
}

/** Constructs a filter that accepts a `File` if it does not match this filter. */
def unary_- : FileFilter = new SimpleFileFilter(file => !accept(file))
def unary_- : FileFilter = new SimpleFileFilter(file => !accept(file)) {
override def toString = s"SimpleFileFilter(-${FileFilter.this})"
}

}

Expand All @@ -38,18 +46,26 @@ trait NameFilter extends FileFilter {

/** Constructs a filter that accepts a `String` if it matches either this filter or the given `filter`. */
def |(filter: NameFilter): NameFilter =
new SimpleFilter(name => accept(name) || filter.accept(name))
new SimpleFilter(name => accept(name) || filter.accept(name)) {
override def toString = s"SimpleFilter(${NameFilter.this} | $filter)"
}

/** Constructs a filter that accepts a `String` if it matches both this filter and the given `filter`. */
def &(filter: NameFilter): NameFilter =
new SimpleFilter(name => accept(name) && filter.accept(name))
new SimpleFilter(name => accept(name) && filter.accept(name)) {
override def toString = s"SimpleFilter(${NameFilter.this} & $filter)"
}

/** Constructs a filter that accepts a `String` if it matches this filter but not the given `filter`. */
def -(filter: NameFilter): NameFilter =
new SimpleFilter(name => accept(name) && !filter.accept(name))
new SimpleFilter(name => accept(name) && !filter.accept(name)) {
override def toString = s"SimpleFilter(${NameFilter.this} - $filter)"
}

/** Constructs a filter that accepts a `String` if it does not match this filter. */
override def unary_- : NameFilter = new SimpleFilter(name => !accept(name))
override def unary_- : NameFilter = new SimpleFilter(name => !accept(name)) {
override def toString = s"SimpleFilter(-${NameFilter.this})"
}

}

Expand All @@ -69,23 +85,25 @@ object DirectoryFilter extends FileFilter {
}

/** A [[FileFilter]] that selects files according the predicate `acceptFunction`. */
final class SimpleFileFilter(val acceptFunction: File => Boolean) extends FileFilter {
def accept(file: File) = acceptFunction(file)
sealed class SimpleFileFilter(val acceptFunction: File => Boolean) extends FileFilter {
final def accept(file: File) = acceptFunction(file)
}

/** A [[NameFilter]] that accepts a name if it is exactly equal to `matchName`. */
final class ExactFilter(val matchName: String) extends NameFilter {
def accept(name: String) = matchName == name
override def toString = s"ExactFilter($matchName)"
}

/** A [[NameFilter]] that accepts a name if the predicate `acceptFunction` accepts it. */
final class SimpleFilter(val acceptFunction: String => Boolean) extends NameFilter {
def accept(name: String) = acceptFunction(name)
sealed class SimpleFilter(val acceptFunction: String => Boolean) extends NameFilter {
final def accept(name: String) = acceptFunction(name)
}

/** A [[NameFilter]] that accepts a name if it matches the regular expression defined by `pattern`. */
final class PatternFilter(val pattern: Pattern) extends NameFilter {
def accept(name: String) = pattern.matcher(name).matches
override def toString = s"PatternFilter($pattern)"
}

/** A [[NameFilter]] that accepts all names. That is, `accept` always returns `true`. */
Expand All @@ -110,7 +128,7 @@ object FileFilter {

}

/** Constructs a filter from a String, interpreting wildcards. See the [[apply]] method. */
/** Constructs a filter from a String, interpreting wildcards. See the [[GlobFilter.apply]] method. */
object GlobFilter {

/**
Expand Down

0 comments on commit e8c9757

Please sign in to comment.