@@ -3,8 +3,9 @@ package tools
3
3
4
4
import java .io .File
5
5
import java .nio .charset .StandardCharsets .UTF_8
6
+ import java .nio .file .{Files , Path => JPath }
6
7
7
- import scala .io .Source
8
+ import scala .io .{ Codec , Source }
8
9
import scala .reflect .ClassTag
9
10
import scala .util .Using .resource
10
11
import scala .util .chaining .given
@@ -25,11 +26,10 @@ extension (f: File) def absPath =
25
26
extension (str : String ) def dropExtension =
26
27
str.reverse.dropWhile(_ != '.' ).drop(1 ).reverse
27
28
28
- private def withFile [T ](file : File )(action : Source => T ): T =
29
- resource(Source .fromFile(file, UTF_8 .name))(action)
30
-
31
- def readLines (f : File ): List [String ] = withFile(f)(_.getLines.toList)
32
- def readFile (f : File ): String = withFile(f)(_.mkString)
29
+ private
30
+ def withFile [T ](file : File )(action : Source => T )(using Codec ): T = resource(Source .fromFile(file))(action)
31
+ def readLines (f : File )(using codec : Codec = Codec .UTF8 ): List [String ] = withFile(f)(_.getLines.toList)
32
+ def readFile (f : File )(using codec : Codec = Codec .UTF8 ): String = withFile(f)(_.mkString)
33
33
34
34
private object Unthrown extends ControlThrowable
35
35
@@ -43,3 +43,25 @@ def assertThrows[T <: Throwable: ClassTag](p: T => Boolean)(body: => Any): Unit
43
43
case failed : T => throw AssertionError (s " Exception failed check: $failed" ).tap(_.addSuppressed(failed))
44
44
case NonFatal (other) => throw AssertionError (s " Wrong exception: expected ${implicitly[ClassTag [T ]]} but was ${other.getClass.getName}" ).tap(_.addSuppressed(other))
45
45
end assertThrows
46
+
47
+ def toolArgsFor (files : List [JPath ])(using codec : Codec = Codec .UTF8 ): List [String ] =
48
+ files.flatMap(path => toolArgsParse(readLines(path.toFile)))
49
+
50
+ // Inspect the first 10 of the given lines for compiler options of the form
51
+ // `// scalac: args`, `/* scalac: args`, ` * scalac: args`.
52
+ // If args string ends in close comment, drop the `*` `/`.
53
+ // If split, parse the args string as a command line.
54
+ // (from scala.tools.partest.nest.Runner#toolArgsFor)
55
+ def toolArgsParse (lines : List [String ]): List [String ] = {
56
+ val tag = " scalac:"
57
+ val endc = " *" + " /" // be forgiving of /* scalac: ... */
58
+ def stripped (s : String ) = s.substring(s.indexOf(tag) + tag.length).stripSuffix(endc)
59
+ val args = lines.to(LazyList ).take(10 ).filter { s =>
60
+ s.contains(" //" + tag)
61
+ || s.contains(" // " + tag)
62
+ || s.contains(" /* " + tag)
63
+ || s.contains(" * " + tag)
64
+ // but avoid picking up comments like "% scalac ./a.scala" and "$ scalac a.scala"
65
+ }.map(stripped).headOption
66
+ args.map(dotc.config.CommandLineParser .tokenize).getOrElse(Nil )
67
+ }
0 commit comments