diff --git a/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala b/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala index 07bf2d4d..353cfb6a 100644 --- a/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala +++ b/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala @@ -1,6 +1,8 @@ package com.typesafe.tools.mima package plugin +import java.io.File + import com.typesafe.tools.mima.core._ import com.typesafe.tools.mima.core.util.log.Logging import sbt.Keys.TaskStreams @@ -119,32 +121,48 @@ object SbtMima { val ExclusionPattern = """ProblemFilters\.exclude\[([^\]]+)\]\("([^"]+)"\)""".r def findFiles(): Seq[(File, String)] = directory.listFiles().flatMap(f => fileExtension.findFirstIn(f.getName).map((f, _))) - def parseFile(file: File, extension: String): Either[Seq[Throwable], (String, Seq[ProblemFilter])] = { - val version = file.getName.dropRight(extension.size) - - def parseLine(text: String, line: Int): Try[ProblemFilter] = - Try { - text match { - case ExclusionPattern(className, target) => ProblemFilters.exclude(className, target) - case x => throw new RuntimeException(s"Couldn't parse '$x'") - } - }.transform(Success(_), ex => Failure(new ParsingException(file, line, ex))) - - val lines = try { - Source.fromFile(file).getLines().toVector - } catch { - case NonFatal(t) => throw new RuntimeException(s"Couldn't load '$file'", t) + def parseFile(fileOrDir: File, extension: String): Either[Seq[Throwable], (String, Seq[ProblemFilter])] = { + val version = fileOrDir.getName.dropRight(extension.size) + + def parseOneFile(file: File): Either[Seq[Throwable],Seq[ProblemFilter]] = { + def parseLine(text: String, line: Int): Try[ProblemFilter] = + Try { + text match { + case ExclusionPattern(className, target) => ProblemFilters.exclude(className, target) + case x => throw new RuntimeException(s"Couldn't parse '$x'") + } + }.transform(Success(_), ex => Failure(new ParsingException(file, line, ex))) + + val lines = try { + Source.fromFile(file).getLines().toVector + } catch { + case NonFatal(t) => throw new RuntimeException(s"Couldn't load '$file'", t) + } + + val (excludes, failures) = + lines + .zipWithIndex + .filterNot { case (str, line) => str.trim.isEmpty || str.trim.startsWith("#") } + .map((parseLine _).tupled) + .partition(_.isSuccess) + + if (failures.isEmpty) Right(excludes.map(_.get)) + else Left(failures.map(_.failed.get)) } - val (excludes, failures) = - lines - .zipWithIndex - .filterNot { case (str, line) => str.trim.isEmpty || str.trim.startsWith("#") } - .map((parseLine _).tupled) - .partition(_.isSuccess) - - if (failures.isEmpty) Right(version -> excludes.map(_.get)) - else Left(failures.map(_.failed.get)) + if (fileOrDir.isDirectory) { + val allResults = + fileOrDir.listFiles().filter(_.getName.endsWith(".excludes")) + .toSeq + .map(parseOneFile) + val (mappings, failures) = allResults.partition(_.isRight) + if (failures.nonEmpty) Left(failures.flatMap(_.left.get)) + else { + val allMappings = mappings.flatMap(_.right.get) + Right(version -> allMappings) + } + } else + parseOneFile(fileOrDir).right.map(version -> _) } require(directory.exists(), s"Mima filter directory did not exist: ${directory.getAbsolutePath}") diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/build.sbt b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/build.sbt new file mode 100644 index 00000000..2ab8eda7 --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/build.sbt @@ -0,0 +1 @@ +mimaPreviousArtifacts := Set(organization.value %% name.value % "0.0.1-SNAPSHOT") diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/project/plugins.sbt b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/project/plugins.sbt new file mode 100644 index 00000000..3a6fa8ab --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % System.getProperty("plugin.version")) diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/a.excludes b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/a.excludes new file mode 100644 index 00000000..a3c631a3 --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/a.excludes @@ -0,0 +1 @@ +ProblemFilters.exclude[MissingMethodProblem]("A.foz") \ No newline at end of file diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/b.excludes b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/b.excludes new file mode 100644 index 00000000..64798497 --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/b.excludes @@ -0,0 +1 @@ +ProblemFilters.exclude[MissingMethodProblem]("A.baz") \ No newline at end of file diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/c.excludes b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/c.excludes new file mode 100644 index 00000000..fb4e6445 --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/mima-filters/0.0.1-SNAPSHOT.backwards.excludes/c.excludes @@ -0,0 +1 @@ +ProblemFilters.exclude[MissingMethodProblem]("A.bar") \ No newline at end of file diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/scala/v1/A.scala b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/scala/v1/A.scala new file mode 100644 index 00000000..1e2762d9 --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/scala/v1/A.scala @@ -0,0 +1,6 @@ +class A { + def foo = 1 + def bar = foo + def baz = foo + def foz = foo +} diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/scala/v2/A.scala b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/scala/v2/A.scala new file mode 100644 index 00000000..816b5a4e --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/src/main/scala/v2/A.scala @@ -0,0 +1,3 @@ +class A { + def foo = 1 +} diff --git a/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/test b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/test new file mode 100644 index 00000000..0af9db4d --- /dev/null +++ b/sbtplugin/src/sbt-test/sbt-mima-plugin/backward-filters-from-dir-per-version/test @@ -0,0 +1,13 @@ +> set scalaSource in Compile := baseDirectory.value /"src" /"main" /"scala" /"v1" +> set version := s"0.0.1-SNAPSHOT" +> publishLocal + +> set scalaSource in Compile := baseDirectory.value /"src" /"main" /"scala" /"v2" +> set version := s"0.0.2-SNAPSHOT" + +# filters are read from file, so mima check should pass +> mimaReportBinaryIssues + +# remove all filters so mima check fails +> set mimaBackwardIssueFilters := Map.empty +-> mimaReportBinaryIssues