Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

Commit

Permalink
Add a Tut configuration.
Browse files Browse the repository at this point in the history
This includes a few breaking changes:
* tutScalacOptions is replaced by (scalacOptions in Tut)
* the Test dependencies are no longer on the CLASSPATH
* -Ywarn-unused-imports is not filtered out anymore

Other, non-breaking behavioural changes:
* (scalacOptions in Tut) inherit from (scalacOptions in (Compile, console))
* it's now possible to set dependencies specific to tut with the % "tut" modifier
  • Loading branch information
nrinaudo committed Apr 21, 2017
1 parent 44a9189 commit adf668a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
36 changes: 16 additions & 20 deletions plugin/src/main/scala/tut/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ object Plugin extends sbt.Plugin {

type Dir = File

lazy val Tut = config("tut") extend Compile
lazy val tut = TaskKey[Seq[(File,String)]]("tut", "create tut documentation")
lazy val tutSourceDirectory = SettingKey[File]("tutSourceDirectory", "where to look for tut sources")
lazy val tutScalacOptions = TaskKey[Seq[String]]("tutScalacOptions", "scalac options")
lazy val tutPluginJars = TaskKey[Seq[File]]("tutPluginJars", "Plugin jars to be used by tut REPL.")
lazy val tutOnly = inputKey[Unit]("Run tut on a single file.")
lazy val tutTargetDirectory = SettingKey[File]("tutTargetDirectory", "Where tut output goes")
Expand Down Expand Up @@ -66,25 +66,22 @@ object Plugin extends sbt.Plugin {
def tutAll(streams: TaskStreams, r: ScalaRun, in: List[File], out: List[File], cp: Classpath, opts: Seq[String], pOpts: Seq[String], re: String): List[(File, String)] =
(in zip out).flatMap { case (in, out) => tutOne(streams, r, in, out, cp, opts, pOpts, re ) }

lazy val tutSettings =
lazy val tutSettings = inConfig(Tut)(Defaults.configSettings) ++
Seq(
resolvers += "tpolecat" at "http://dl.bintray.com/tpolecat/maven",
libraryDependencies += "org.tpolecat" %% "tut-core" % BuildInfo.version % "test",
tutSourceDirectory := sourceDirectory.value / "main" / "tut",
libraryDependencies += "org.tpolecat" %% "tut-core" % BuildInfo.version % Tut,
ivyConfigurations += Tut,
tutSourceDirectory := (sourceDirectory in Compile).value / "tut",
tutTargetDirectory := crossTarget.value / "tut",
tutScalacOptions := {
val testOptions = scalacOptions.in(Test).value
val unwantedOptions = Set("-Ywarn-unused-import")
testOptions.filterNot(unwantedOptions)
},
tutNameFilter := """.*\.(md|markdown|txt|htm|html)""".r,
watchSources in Defaults.ConfigGlobal ++= (tutSourceDirectory.value ** new NameFilter {
override def accept(name: String): Boolean = tutNameFilter.value.pattern.matcher(name).matches()
}).get,
tutFiles := tutFilesParser,
scalacOptions in Tut := (scalacOptions in (Compile, console)).value.filterNot(unsafeOptions),
tutPluginJars := {
// no idea if this is the right way to do this
val deps = (libraryDependencies in Test).value.filter(_.configurations.fold(false)(_.startsWith("plugin->")))
val deps = (libraryDependencies in Tut).value.filter(_.configurations.fold(false)(_.startsWith("plugin->")))
update.value.configuration("plugin").map(_.modules).getOrElse(Nil).filter { m =>
deps.exists { d =>
d.organization == m.module.organization &&
Expand All @@ -94,26 +91,26 @@ object Plugin extends sbt.Plugin {
}.flatMap(_.artifacts.map(_._2))
},
tut := {
val r = (runner in Test).value
val r = (runner in Tut).value
val in = tutSourceDirectory.value
val out = tutTargetDirectory.value
val cp = (fullClasspath in Test).value
val opts = tutScalacOptions.value
val cp = (fullClasspath in Tut).value
val opts = (scalacOptions in Tut).value
val pOpts = tutPluginJars.value.map(f => "–Xplugin:" + f.getAbsolutePath)
val re = tutNameFilter.value.pattern.toString
tutOne(streams.value, r, in, out, cp, opts, pOpts, re)
},
tutOnly <<= InputTask.createDyn(Def.setting((state: State) => Space ~> tutFilesParser(state))) {
Def.task{ in =>
Def.task{
val r = (runner in Test).value
val r = (runner in Tut).value
val inR = tutSourceDirectory.value // input root
val inDir = if (in.isDirectory) in
else in.getParentFile // input dir
val outR = tutTargetDirectory.value // output root
val out = new File(outR, inR.toURI.relativize(inDir.toURI).getPath) // output dir
val cp = (fullClasspath in Test).value
val opts = tutScalacOptions.value
val cp = (fullClasspath in Tut).value
val opts = (scalacOptions in Tut).value
val pOpts = tutPluginJars.value.map(f => "–Xplugin:" + f.getAbsolutePath)
val re = tutNameFilter.value.pattern.toString
tutOne(streams.value, r, in, out, cp, opts, pOpts, re)
Expand All @@ -122,11 +119,11 @@ object Plugin extends sbt.Plugin {
},
tutQuickCache := cacheDirectory.value / "tut",
tutQuick := {
val r = (runner in Test).value
val r = (runner in Tut).value
val inR = tutSourceDirectory.value
val outR = tutTargetDirectory.value
val cp = (fullClasspath in Test).value
val opts = tutScalacOptions.value
val cp = (fullClasspath in Tut).value
val opts = (scalacOptions in Tut).value
val pOpts = tutPluginJars.value.map(f => "–Xplugin:" + f.getAbsolutePath)
val re = tutNameFilter.value.pattern.toString
val cache = tutQuickCache.value
Expand All @@ -148,4 +145,3 @@ object Plugin extends sbt.Plugin {
)

}

5 changes: 3 additions & 2 deletions tests/src/sbt-test/tut/test-01-options/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ lazy val check = TaskKey[Unit]("check")
check := {
val expected = IO.readLines(file("expect.md"))
val actual = IO.readLines(crossTarget.value / "tut"/ "test.md")
if (expected != actual)
if (expected != actual)
error("Output doesn't match expected: \n" + actual.mkString("\n"))
}

scalacOptions += "-language:higherKinds"
scalacOptions ++= Seq("-language:higherKinds", "-Xfatal-warnings")
scalacOptions in Tut += "-language:existentials"
7 changes: 7 additions & 0 deletions tests/src/sbt-test/tut/test-01-options/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ val y = 2
// y: Int = 2
```

Should not fail the build:

```scala
case class A(c: Class[_])
// defined class A
```

The End
6 changes: 6 additions & 0 deletions tests/src/sbt-test/tut/test-01-options/src/main/tut/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ import scala.collection.immutable.List
val y = 2
```

Should not fail the build:

```tut:book
case class A(c: Class[_])
```

The End
6 changes: 3 additions & 3 deletions tests/src/sbt-test/tut/test-03-config/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ check := {
val ccp = (managedClasspath in Compile).value
if (ccp.exists(_.data.getName.contains("tut-core")))
error("Compile classpath contains tut-core.")
// verify that the test classpath *does* contain tut
val tcp = (managedClasspath in Test).value
// verify that the tut classpath *does* contain tut
val tcp = (managedClasspath in Tut).value
if (!tcp.exists(_.data.getName.contains("tut-core")))
error("Test classpath doesn't contain tut-core.")
error("Tut classpath doesn't contain tut-core.")
}
1 change: 1 addition & 0 deletions tests/src/sbt-test/tut/test-07-unused-imports/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ check := {
error("Output doesn't match expected: \n" + actual.mkString("\n"))
}
scalacOptions ++= Seq("-Xfatal-warnings", "-Ywarn-unused-import", "-feature")
(scalacOptions in (Compile, console)) := scalacOptions.value.filterNot(Set("-Ywarn-unused-import"))

0 comments on commit adf668a

Please sign in to comment.