11package mill .testkit
22
3+ enum Chunk derives upickle.default.ReadWriter {
4+ case Yaml (lines : Seq [String ])
5+ case Example (lines : Seq [String ])
6+ case See (path : String , lines : Seq [String ])
7+ case Scala (lines : Seq [String ])
8+ case Comment (lines : Seq [String ])
9+ }
10+
311object ExampleParser {
4- def apply (testRepoRoot : os.Path ): Seq [(String , String )] = {
12+ def apply (testRepoRoot : os.Path ): Seq [Chunk ] = {
13+ val result = collection.mutable.Buffer [Chunk ]()
514
6- val states = collection.mutable.Buffer (" yaml" )
7- val chunks = collection.mutable.Buffer (collection.mutable.Buffer .empty[String ])
15+ def appendLine (line : String ): Unit = {
16+ if (result.nonEmpty) {
17+ val last = result.last
18+ val updated = last match {
19+ case Chunk .Yaml (lines) => Chunk .Yaml (lines :+ line)
20+ case Chunk .Example (lines) => Chunk .Example (lines :+ line)
21+ case Chunk .See (path, lines) => Chunk .See (path, lines :+ line)
22+ case Chunk .Scala (lines) => Chunk .Scala (lines :+ line)
23+ case Chunk .Comment (lines) => Chunk .Comment (lines :+ line)
24+ }
25+ result(result.length - 1 ) = updated
26+ }
27+ }
828
929 val rootBuildFileNames = Seq (" build.mill" )
1030 val buildFile = rootBuildFileNames.map(testRepoRoot / _)
@@ -14,26 +34,47 @@ object ExampleParser {
1434 s " No build file named ${rootBuildFileNames.mkString(" /" )} found in $testRepoRoot"
1535 )
1636 )
37+
1738 for (line <- os.read.lines(buildFile)) {
18- val (newState, restOpt ) = line match {
19- case s " /** Usage " => (" example " , None )
39+ val (newChunkType, lineToAdd ) = line match {
40+ case s " /** Usage " => (Chunk . Example ( Vector ()) , None )
2041 case s " /** See Also: $path */ " =>
21- (s " see: $path" , Some (os.read(os.Path (path, testRepoRoot))))
22- case s " */ " => (" scala" , None )
23- case line @ s " //| $_" if states.last == " yaml" => (" yaml" , Some (line))
24- case s " // $rest" if ! rest.startsWith(" |" ) => (" comment" , Some (rest.stripPrefix(" " )))
42+ (Chunk .See (path, Vector ()), Some (os.read(os.Path (path, testRepoRoot))))
43+ case s " */ " => (Chunk .Scala (Vector ()), None )
44+ case line @ s " //| $_" if result.nonEmpty && result.last.isInstanceOf [Chunk .Yaml ] =>
45+ (Chunk .Yaml (Vector ()), Some (line))
46+ case s " // $rest" if ! rest.startsWith(" |" ) =>
47+ (Chunk .Comment (Vector ()), Some (rest.stripPrefix(" " )))
2548 case l =>
26- (if (Seq (" comment" , " yaml" ).contains(states.last)) " scala" else states.last, Some (l))
49+ val chunkType = if (result.nonEmpty) {
50+ result.last match {
51+ case _ : Chunk .Comment | _ : Chunk .Yaml => Chunk .Scala (Vector ())
52+ case other => other
53+ }
54+ } else Chunk .Yaml (Vector ()) // initial state
55+ (chunkType, Some (l))
2756 }
2857
29- if (newState != states.last) {
30- states.append(newState)
31- chunks.append(collection.mutable.Buffer .empty[String ])
58+ if (result.isEmpty || newChunkType.getClass != result.last.getClass) {
59+ result.append(newChunkType)
3260 }
3361
34- restOpt .foreach(r => chunks.last.append(r) )
62+ lineToAdd .foreach(appendLine )
3563 }
3664
37- states.zip(chunks.map(_.mkString(" \n " ).trim)).filter(_._2.nonEmpty).toSeq
65+ result.filter {
66+ case Chunk .Yaml (lines) => lines.mkString(" \n " ).trim.nonEmpty
67+ case Chunk .Example (lines) => lines.mkString(" \n " ).trim.nonEmpty
68+ case Chunk .See (_, lines) => lines.mkString(" \n " ).trim.nonEmpty
69+ case Chunk .Scala (lines) => lines.mkString(" \n " ).trim.nonEmpty
70+ case Chunk .Comment (lines) => lines.mkString(" \n " ).trim.nonEmpty
71+ }.map {
72+ case Chunk .Yaml (lines) => Chunk .Yaml (lines.mkString(" \n " ).trim.linesIterator.toVector)
73+ case Chunk .Example (lines) => Chunk .Example (lines.mkString(" \n " ).trim.linesIterator.toVector)
74+ case Chunk .See (path, lines) =>
75+ Chunk .See (path, lines.mkString(" \n " ).trim.linesIterator.toVector)
76+ case Chunk .Scala (lines) => Chunk .Scala (lines.mkString(" \n " ).trim.linesIterator.toVector)
77+ case Chunk .Comment (lines) => Chunk .Comment (lines.mkString(" \n " ).trim.linesIterator.toVector)
78+ }.toSeq
3879 }
3980}
0 commit comments