|
| 1 | +package sbtunidoc |
| 2 | + |
| 3 | +import sbt._ |
| 4 | +import sbt.Keys._ |
| 5 | +import sbt.internal.inc.{ AnalyzingCompiler, ManagedLoggedReporter } |
| 6 | +import sbt.internal.util.Attributed.data |
| 7 | +import xsbti.compile.{ Compilers, IncToolOptionsUtil } |
| 8 | +import xsbti.Reporter |
| 9 | + |
| 10 | +object Unidoc { |
| 11 | + import java.io.PrintWriter |
| 12 | + |
| 13 | + // This is straight out of docTaskSettings in Defaults.scala. |
| 14 | + def apply(cache: File, cs: Compilers, srcs: Seq[File], cp: Classpath, |
| 15 | + sOpts: Seq[String], jOpts: Seq[String], xapis: Map[File, URL], maxErrors: Int, |
| 16 | + out: File, config: Configuration, s: TaskStreams, spm: Seq[xsbti.Position => Option[xsbti.Position]]): File = { |
| 17 | + val hasScala = srcs.exists(_.name.endsWith(".scala")) |
| 18 | + val hasJava = srcs.exists(_.name.endsWith(".java")) |
| 19 | + val label = nameForSrc(config.name) |
| 20 | + val reporter = new ManagedLoggedReporter( |
| 21 | + maxErrors, |
| 22 | + s.log, |
| 23 | + foldMappers(spm)) |
| 24 | + (hasScala, hasJava) match { |
| 25 | + case (true, _) => |
| 26 | + val options = sOpts ++ Opts.doc.externalAPI(xapis) |
| 27 | + val runDoc = Doc.scaladoc(label, s.cacheStoreFactory sub "scala", cs.scalac match { |
| 28 | + case ac: AnalyzingCompiler => ac.onArgs(exported(s, "scaladoc")) |
| 29 | + }, Nil) |
| 30 | + runDoc(srcs, data(cp).toList, out, options, maxErrors, s.log) |
| 31 | + case (_, true) => |
| 32 | + val javadoc = |
| 33 | + sbt.inc.Doc.cachedJavadoc(label, s.cacheStoreFactory sub "java", cs.javaTools) |
| 34 | + javadoc.run(srcs.toList, |
| 35 | + data(cp).toList, |
| 36 | + out, |
| 37 | + jOpts.toList, |
| 38 | + IncToolOptionsUtil.defaultIncToolOptions(), |
| 39 | + s.log, |
| 40 | + reporter) |
| 41 | + case _ => () // do nothing |
| 42 | + } |
| 43 | + out |
| 44 | + } |
| 45 | + |
| 46 | + private[this] def exported(w: PrintWriter, command: String): Seq[String] => Unit = args => |
| 47 | + w.println( (command +: args).mkString(" ") ) |
| 48 | + private[this] def exported(s: TaskStreams, command: String): Seq[String] => Unit = args => |
| 49 | + exported(s.text("export"), command) |
| 50 | + private[this] def foldMappers[A](mappers: Seq[A => Option[A]]) = |
| 51 | + mappers.foldRight({ p: A => |
| 52 | + p |
| 53 | + }) { (mapper, mappers) => |
| 54 | + { p: A => |
| 55 | + mapper(p).getOrElse(mappers(p)) |
| 56 | + } |
| 57 | + } |
| 58 | + def nameForSrc(name: String): String = name match { |
| 59 | + case "compile"|"javaunidoc"|"scalaunidoc" => "main" |
| 60 | + case _ => name |
| 61 | + } |
| 62 | +} |
0 commit comments