/
PathModule.scala
329 lines (285 loc) · 11 KB
/
PathModule.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package org.bykn.bosatsu
import cats.effect.IO
import com.monovore.decline.Argument
import org.typelevel.paiges.{Doc, Document}
import java.nio.file.{Path => JPath}
import cats.data.NonEmptyList
import cats.effect.ExitCode
import cats.implicits.catsKernelOrderingForOrder
import cats.syntax.all._
object PathModule extends MainModule[IO] {
type Path = JPath
override def pathArg: Argument[Path] =
Argument.readPath
def readPath(path: Path): IO[String] =
IO.blocking(new String(java.nio.file.Files.readAllBytes(path), "utf-8"))
val resolvePath: Some[(Path, PackageName) => IO[Option[Path]]] =
Some(
{ (root: Path, pack: PackageName) =>
val dir = pack.parts.init.foldLeft(root)(_.resolve(_))
val filePath = dir.resolve(pack.parts.last + ".bosatsu")
IO.blocking {
// this is a side-effect since file is mutable
// and talks to the file system
val file = filePath.toFile
if (file.exists()) Some(filePath)
else None
}
}
)
def readPackages(paths: List[Path]): IO[List[Package.Typed[Unit]]] =
ProtoConverter.readPackages(paths)
def readInterfaces(paths: List[Path]): IO[List[Package.Interface]] =
ProtoConverter.readInterfaces(paths)
def writeInterfaces(
interfaces: List[Package.Interface],
path: Path
): IO[Unit] =
ProtoConverter.writeInterfaces(interfaces, path)
def writePackages[A](packages: List[Package.Typed[A]], path: Path): IO[Unit] =
ProtoConverter.writePackages(packages, path)
def unfoldDir: Option[Path => IO[Option[IO[List[Path]]]]] = Some {
(path: Path) =>
IO.blocking {
val f = path.toFile
if (f.isDirectory()) {
Some(IO.blocking {
f.listFiles.iterator.map(_.toPath).toList
})
} else None
}
}
def hasExtension(str: String): Path => Boolean = { (path: Path) =>
path.toString.endsWith(str)
}
def print(str: => String): IO[Unit] =
IO.println(str)
def delay[A](a: => A): IO[A] = IO(a)
def report(io: IO[Output]): IO[ExitCode] =
io.attempt.flatMap {
case Right(out) => reportOutput(out)
case Left(err) => reportException(err).as(ExitCode.Error)
}
private def writeOut(doc: Doc, out: Option[Path]): IO[Unit] =
out match {
case None =>
IO.blocking {
doc
.renderStreamTrim(80)
.iterator
.foreach(System.out.print)
System.out.println("")
}
case Some(p) =>
CodeGenWrite.writeDoc(p, doc)
}
def reportOutput(out: Output): IO[ExitCode] =
out match {
case Output.TestOutput(resMap, color) =>
val hasMissing = resMap.exists(_._2.isEmpty)
// it would be nice to run in parallel, but
// MatchlessToValue is not currently threadsafe
val evalTest = resMap.map {
case (p, Some(evalTest)) =>
(p, Some(evalTest.value))
case (p, None) => (p, None)
}
val testReport = Test.outputFor(evalTest, color)
val success = !hasMissing && (testReport.fails == 0)
val code = if (success) ExitCode.Success else ExitCode.Error
print(testReport.doc.render(80)).as(code)
case Output.EvaluationResult(_, tpe, resDoc) =>
val tDoc = rankn.Type.fullyResolvedDocument.document(tpe)
val doc =
resDoc.value + (Doc.lineOrEmpty + Doc.text(": ") + tDoc).nested(4)
print(doc.render(100)).as(ExitCode.Success)
case Output.JsonOutput(json, pathOpt) =>
writeOut(json.toDoc, pathOpt)
.as(ExitCode.Success)
case Output.TranspileOut(outs, base) =>
def path(p: List[String]): Path =
p.foldLeft(base)(_.resolve(_))
outs.toList
.map { case (p, d) =>
(p, CodeGenWrite.writeDoc(path(p.toList), d))
}
.sortBy(_._1)
.traverse_ { case (_, w) => w }
.as(ExitCode.Success)
case Output.CompileOut(packList, ifout, output) =>
val ifres = ifout match {
case None =>
IO.unit
case Some(ifacePath) =>
val ifs = packList.map(Package.interfaceOf(_))
writeInterfaces(ifs, ifacePath)
}
val out = output.fold(IO.unit)(writePackages(packList, _))
(ifres *> out).as(ExitCode.Success)
case Output.ShowOutput(packs, ifaces, output) =>
val pdocs = packs.map { pack =>
Document[Package.Typed[Any]].document(pack)
}
val idocs = ifaces.map { iface =>
Document[Package.Interface].document(iface)
}
val doc = Doc.intercalate(Doc.hardLine, idocs ::: pdocs)
writeOut(doc, output).as(ExitCode.Success)
case Output.DepsOutput(depinfo, output, style) =>
style match {
case GraphOutput.Json =>
def toJson(
dep: (Path, PackageName, FileKind, List[PackageName])
): Json =
Json.JObject(
("path", Json.JString(dep._1.toString())) ::
("package", Json.JString(dep._2.asString)) ::
("kind", Json.JString(dep._3.name)) ::
(
"dependsOn",
Json.JArray(
dep._4.map(pn => Json.JString(pn.asString)).toVector
)
) ::
Nil
)
val asJson = Json.JArray(
depinfo
.sortBy { case (path, pn, fk, _) => (path, pn, fk.name) }
.map(toJson)
.toVector
)
writeOut(asJson.toDoc, output).as(ExitCode.Success)
case GraphOutput.Dot =>
def shapeOf(fk: FileKind): String =
fk match {
case FileKind.Iface => "diamond"
case FileKind.Pack => "box"
case FileKind.Source => "circle"
}
type Ident = String
def makeNode(
idx: Int,
dep: (Path, PackageName, FileKind, List[PackageName])
): (Ident, String) = {
// C [shape=box, fillcolor=lightgrey, label="Node C"];
val ident = s"N$idx"
val decl =
s"$ident [shape=${shapeOf(dep._3)}, label=\"${dep._2.asString}\"];"
(ident, decl)
}
def makeMissing(idx: Int, pack: PackageName): (Ident, String) = {
// C [shape=box, fillcolor=lightgrey, label="Node C"];
val ident = s"N$idx"
val decl = s"$ident [shape=octogon, label=\"${pack.asString}\"];"
(ident, decl)
}
val knownPacks = depinfo.map(_._2).toSet
val allPacks =
depinfo.flatMap(dep => dep._2 :: dep._4).distinct.sorted
val unknownPacks = allPacks.filterNot(knownPacks)
type NodeMap = Map[PackageName, NonEmptyList[
(Int, Option[FileKind], String, String)
]]
val depinfoSize = depinfo.size
val nodes: NodeMap =
(depinfo.zipWithIndex.map { case (dep, idx) =>
val (ident, nstr) = makeNode(idx, dep)
(dep._2, (idx, Some(dep._3), ident, nstr))
} ::: unknownPacks.mapWithIndex { (pn, idx0) =>
val idx = depinfoSize + idx0
val (ident, nstr) = makeMissing(idx, pn)
(pn, (idx, None, ident, nstr))
})
.groupByNel(_._1)
.map { case (k, v) =>
(k, v.map(_._2))
}
// now NodeMap has everything
def makeEdge(
src: PackageName,
k: FileKind,
dst: PackageName,
nm: NodeMap
): String = {
implicit val orderKind: cats.Order[Option[FileKind]] =
new cats.Order[Option[FileKind]] {
def compare(a: Option[FileKind], b: Option[FileKind]) =
(a, b) match {
case (None, None) => 0
case (Some(_), None) => -1
case (None, Some(_)) => 1
case (Some(FileKind.Iface), Some(FileKind.Iface)) => 0
case (Some(FileKind.Iface), Some(_)) => -1
case (Some(FileKind.Pack), Some(FileKind.Iface)) => 1
case (Some(FileKind.Pack), Some(FileKind.Pack)) => 0
case (Some(FileKind.Pack), Some(FileKind.Source)) => -1
case (Some(FileKind.Source), Some(FileKind.Source)) => 0
case (Some(FileKind.Source), Some(_)) => 1
}
}
val srcNode = nm(src).find { case (_, sk, _, _) =>
sk == Some(k)
}.get
val dstNode = nm(dst).sortBy(rec => (rec._2, rec._1)).head
s"${srcNode._3} -> ${dstNode._3};"
}
val header = Doc.text("digraph G {")
val allNodes: List[Doc] = nodes.iterator
.flatMap { case (_, ns) =>
ns.map(rec => (rec._1, Doc.text(rec._4))).toList
}
.toList
.sortBy(_._1)
.map(_._2)
val nodesDoc = Doc.intercalate(Doc.hardLine, allNodes)
val edges: List[Doc] =
depinfo.flatMap { case (_, pn, k, deps) =>
deps.map { dep =>
Doc.text(makeEdge(pn, k, dep, nodes))
}
}
val edgesDoc = Doc.intercalate(Doc.hardLine, edges)
val fullDoc =
header + (Doc.hardLine + nodesDoc + Doc.hardLine + edgesDoc)
.nested(2) +
Doc.hardLine + Doc.char('}')
writeOut(fullDoc, output).as(ExitCode.Success)
}
}
def reportException(ex: Throwable): IO[Unit] =
mainExceptionToString(ex) match {
case Some(msg) =>
IO.consoleForIO.errorln(msg)
case None =>
IO.consoleForIO.errorln("unknown error:\n") *>
IO.blocking(ex.printStackTrace(System.err))
}
def pathPackage(roots: List[Path], packFile: Path): Option[PackageName] = {
import scala.jdk.CollectionConverters._
def getP(p: Path): Option[PackageName] = {
val subPath = p
.relativize(packFile)
.asScala
.map { part =>
part.toString.toLowerCase.capitalize
}
.mkString("/")
val dropExtension = """(.*)\.[^.]*$""".r
val toParse = subPath match {
case dropExtension(prefix) => prefix
case _ => subPath
}
PackageName.parse(toParse)
}
@annotation.tailrec
def loop(roots: List[Path]): Option[PackageName] =
roots match {
case Nil => None
case h :: _ if packFile.startsWith(h) => getP(h)
case _ :: t => loop(t)
}
if (packFile.toString.isEmpty) None
else loop(roots)
}
}