Skip to content

Commit 16c63b5

Browse files
committed
Implement dotty bridge for doc
1 parent a2b24a9 commit 16c63b5

File tree

6 files changed

+57
-33
lines changed

6 files changed

+57
-33
lines changed

bridge/src/main/scala/xsbt/ScaladocInterface.scala

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package xsbt
55

66
import xsbti.Logger
77
import dotty.tools.dottydoc.api.scala.Dottydoc
8+
import java.net.URL
89

910
class ScaladocInterface {
1011
def run(args: Array[String], log: Logger, delegate: xsbti.Reporter) =
@@ -14,8 +15,8 @@ class ScaladocInterface {
1415
class DottydocRunner(args: Array[String], log: Logger, delegate: xsbti.Reporter) extends Dottydoc {
1516
def run(): Unit = getOutputFolder(args).map { outputFolder =>
1617
val index = createIndex(args)
17-
val template = getTemplate(args)
1818
val resources = getResources(args)
19+
val template = getTemplate(resources)
1920

2021
template.fold(writeJson(index, outputFolder)) { tpl =>
2122
buildDocs(outputFolder, tpl, resources, index)
@@ -44,15 +45,28 @@ class DottydocRunner(args: Array[String], log: Logger, delegate: xsbti.Reporter)
4445
private def getOutputFolder(args: Array[String]): Option[String] =
4546
args sliding(2) find { case Array(x, _) => x == "-d" } map (_.tail.head.trim)
4647

47-
private def getTemplate(args: Array[String]): Option[String] =
48-
getStringSetting("-template:")
48+
private def getTemplate(resources: List[URL]): Option[URL] =
49+
resources.find(_.getFile.endsWith("template.html"))
4950

50-
private def getResources(args: Array[String]): List[String] =
51-
getStringSetting("-resources:").map { path =>
52-
val dir = new java.io.File(path)
53-
if (dir.exists && dir.isDirectory)
54-
dir.listFiles.filter(_.isFile).map(_.getAbsolutePath).toList
55-
else Nil
56-
}.getOrElse(Nil)
51+
private def getResources(args: Array[String]): List[URL] = {
52+
val cp = args sliding (2) find { case Array(x, _) => x == "-classpath" } map (_.tail.head.trim) getOrElse ""
5753

54+
cp.split(":").find(_.endsWith("dottydoc-client.jar")).map { resourceJar =>
55+
import java.util.jar.JarFile
56+
val jarEntries = (new JarFile(resourceJar)).entries
57+
var entries: List[URL] = Nil
58+
59+
while (jarEntries.hasMoreElements) {
60+
val entry = jarEntries.nextElement()
61+
62+
if (!entry.isDirectory()) {
63+
val path = s"jar:file:$resourceJar!/${entry.getName}"
64+
val url = new URL(path)
65+
entries = url :: entries
66+
}
67+
}
68+
69+
entries
70+
} getOrElse (Nil)
71+
}
5872
}

dottydoc/src/dotty/tools/dottydoc/api/java/Dottydoc.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dotty.tools.dottydoc.util.OutputWriter;
66
import java.util.Map;
77
import java.util.List;
8+
import java.net.URL;
89

910
/** FIXME: document me! */
1011
public class Dottydoc extends DocDriver {
@@ -18,11 +19,11 @@ public String createJsonIndex(String[] args) {
1819

1920
public void buildDocs(
2021
String outputDir,
21-
String templatePath,
22-
List<String> resources,
22+
URL template,
23+
List<URL> resources,
2324
Map<String, Package> index
2425
) {
25-
new OutputWriter().writeJava(index, templatePath, outputDir, resources);
26+
new OutputWriter().writeJava(index, outputDir, template, resources);
2627
}
2728

2829
public void writeJson(Map<String, Package> index, String outputDir) {

dottydoc/src/dotty/tools/dottydoc/api/scala/Dottydoc.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import dotty.tools.dottydoc.model.Package
55
import dotty.tools.dottydoc.util.OutputWriter
66

77
import scala.collection.Map
8+
import java.net.URL
89

910
/** FIXME: document this class plz */
1011
trait Dottydoc extends DocDriver {
1112
def createIndex(args: Array[String]): Map[String, Package] =
1213
compiledDocs(args)
1314

14-
def buildDocs(outDir: String, templatePath: String, resources: List[String], index: Map[String, Package]) =
15-
new OutputWriter().write(index, templatePath, outDir, resources)
15+
def buildDocs(outDir: String, template: URL, resources: List[URL], index: Map[String, Package]) =
16+
new OutputWriter().write(index, outDir, template, resources)
1617

1718
def writeJson(index: Map[String, Package], outputDir: String) =
1819
new OutputWriter().writeJson(index, outputDir)

dottydoc/src/dotty/tools/dottydoc/util/OutputWriter.scala

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import _root_.java.io.{
88
PrintWriter => JPrintWriter,
99
FileReader => JFileReader,
1010
BufferedInputStream,
11-
FileInputStream,
11+
InputStream,
12+
InputStreamReader,
1213
FileOutputStream,
1314
BufferedOutputStream,
1415
FileNotFoundException
1516
}
17+
import _root_.java.net.URL
1618
import _root_.java.util.{ Map => JMap, List => JList }
1719
import model.{ Entity, Package }
1820
import model.json._
@@ -21,16 +23,16 @@ import scala.collection.JavaConverters._
2123

2224
class OutputWriter {
2325

24-
def writeJava(packs: JMap[String, Package], templatePath: String, outPath: String, resources: JList[String]): Unit = {
25-
write(packs.asScala, templatePath, outPath, resources.asScala)
26+
def writeJava(packs: JMap[String, Package], outPath: String, template: URL, resources: JList[URL]): Unit = {
27+
write(packs.asScala, outPath, template, resources.asScala)
2628
}
2729

28-
def write(packs: collection.Map[String, Package], templatePath: String, outPath: String, resources: Iterable[String]): Unit = {
30+
def write(packs: collection.Map[String, Package], outPath: String, template: URL, resources: Traversable[URL]): Unit = {
2931
// Write all packages to `outPath`
3032
for (pack <- packs.values) {
3133
println(s"""Writing '${pack.path.mkString(".")}'""")
3234
writeFile(
33-
expandTemplate(templatePath, pack, outPath),
35+
expandTemplate(template, pack, outPath),
3436
outPath + pack.path.mkString("/", "/", "/"),
3537
"index.html")
3638

@@ -41,7 +43,7 @@ class OutputWriter {
4143
} {
4244
println(s"""Writing '${child.path.mkString(".")}'""")
4345
writeFile(
44-
expandTemplate(templatePath, child, outPath),
46+
expandTemplate(template, child, outPath),
4547
outPath + child.path.dropRight(1).mkString("/", "/", "/"),
4648
child.path.last + ".html")
4749
}
@@ -55,7 +57,10 @@ class OutputWriter {
5557
// Write resources to outPath
5658
println("Copying CSS/JS resources to destination...")
5759
assert(resources.nonEmpty)
58-
resources.map(s => copy(new JFile(s), outPath))
60+
61+
// TODO: splitting the URL by '/' and taking the last means that we don't
62+
// allow folders among the resources
63+
resources.foreach(url => copy(url.openStream, outPath, url.getFile.split("/").last))
5964

6065
println("Done writing static material, building js-app")
6166
}
@@ -66,29 +71,31 @@ class OutputWriter {
6671
def writeJson(index: collection.Map[String, Package], outputDir: String): Unit =
6772
writeFile(index.json, outputDir + "/", "index.json")
6873

69-
def expandTemplate(templatePath: String, entity: Entity, outPath: String): String = try {
74+
def expandTemplate(template: URL, entity: Entity, outPath: String): String = try {
7075
import model.json._
7176
import model.java._
7277

78+
val inputStream = template.openStream
7379
val writer = new _root_.java.io.StringWriter()
7480
val mf = new DefaultMustacheFactory()
7581

76-
def toRoot = "../" * (entity.path.length - 1)
82+
def toRoot = "../" * (entity.path.length - { if (entity.isInstanceOf[Package]) 0 else 1 })
7783

7884
val entityWithExtras = entity.asJava(Map(
7985
"assets" -> s"${toRoot}docassets",
8086
"index" -> s"${toRoot}docassets/index.js",
8187
"currentEntity" -> entity.json
8288
))
8389

84-
mf.compile(new JFileReader(templatePath), "template")
90+
mf.compile(new InputStreamReader(inputStream), "template")
8591
.execute(writer, entityWithExtras)
8692

93+
inputStream.close()
8794
writer.flush()
8895
writer.toString
8996
} catch {
9097
case fnf: FileNotFoundException =>
91-
dottydoc.println(s"""Couldn't find the template: "$templatePath"...exiting""")
98+
dottydoc.println(s"""Couldn't find the template: "${template.getFile}"...exiting""")
9299
System.exit(1); ""
93100
}
94101

@@ -107,11 +114,12 @@ class OutputWriter {
107114
printToFile(new JFile(path + file))(printer => bytes.foreach(printer.print))
108115
}
109116

110-
def copy(src: JFile, path: String): Unit = {
111-
val reader = new BufferedInputStream(new FileInputStream(src))
117+
def copy(src: InputStream, path: String, name: String): Unit = {
118+
val reader = new BufferedInputStream(src)
112119
try {
113-
val bytes = Stream.continually(reader.read).takeWhile(-1 != _).map(_.toByte)
114-
writeFile(bytes.toArray, path + "/docassets/", src.getName)
120+
val bytes = Stream.continually(reader.read).takeWhile(-1 != _).map(_.toByte)
121+
writeFile(bytes.toArray, path + "/docassets/", name)
122+
src.close()
115123
} finally reader.close()
116124
}
117125
}

dottydoc/test/WhitelistedStdLibMain.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dottydoc
33

44
import scala.io.Source
55

6-
object WhitelistedStandardLib extends dottydoc.api.java.Dottydoc {
6+
object WhitelistedStandardLib extends dottydoc.api.scala.Dottydoc {
77
import scala.collection.JavaConverters._
88

99
val files: List[String] = {
@@ -16,7 +16,6 @@ object WhitelistedStandardLib extends dottydoc.api.java.Dottydoc {
1616
.map(_.takeWhile(_ != '#').trim) // allow comments in the end of line
1717
.filter(_.nonEmpty)
1818
.filterNot(_.endsWith("package.scala"))
19-
.map("." + _)
2019
.toList
2120
}
2221

@@ -42,6 +41,6 @@ object WhitelistedStandardLib extends dottydoc.api.java.Dottydoc {
4241
"-language:Scala2" +: "-Ydoc-output" +: "../build/dottydoc" +: files.toArray
4342

4443
val index = createIndex(compilerArgs)
45-
buildDocs("../build/dottydoc", "../../dottydoc-client/resources/template.html", resources.asJava, index)
44+
buildDocs("../build/dottydoc", "../../dottydoc-client/resources/template.html", resources, index)
4645
}
4746
}

project/Build.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ object DottyBuild extends Build {
100100
libraryDependencies ++= partestDeps.value,
101101
libraryDependencies ++= Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.1",
102102
"org.scala-lang.modules" %% "scala-partest" % "1.0.11" % "test",
103+
"ch.epfl.lamp" % "dottydoc-client" % "0.1-SNAPSHOT",
103104
"com.novocode" % "junit-interface" % "0.11" % "test",
104105
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
105106
"com.github.spullara.mustache.java" % "compiler" % "0.9.3",

0 commit comments

Comments
 (0)