forked from VirtusLab/scala-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreprocessedSource.scala
106 lines (96 loc) · 3.7 KB
/
PreprocessedSource.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
package scala.build.preprocessing
import scala.build.internal.CodeWrapper
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}
import scala.build.Position
sealed abstract class PreprocessedSource extends Product with Serializable {
def options: Option[BuildOptions]
def optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]]
def requirements: Option[BuildRequirements]
def mainClassOpt: Option[String]
def scopedRequirements: Seq[Scoped[BuildRequirements]]
def scopePath: ScopePath
def directivesPositions: Option[Position.File]
}
object PreprocessedSource {
final case class OnDisk(
path: os.Path,
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
directivesPositions: Option[Position.File]
) extends PreprocessedSource {
def scopePath: ScopePath =
ScopePath.fromPath(path)
}
final case class InMemory(
originalPath: Either[String, (os.SubPath, os.Path)],
relPath: os.RelPath,
code: String,
ignoreLen: Int,
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
scopePath: ScopePath,
directivesPositions: Option[Position.File]
) extends PreprocessedSource {
def reportingPath: Either[String, os.Path] =
originalPath.map(_._2)
}
final case class UnwrappedScript(
originalPath: Either[String, (os.SubPath, os.Path)],
relPath: os.RelPath,
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
scopePath: ScopePath,
directivesPositions: Option[DirectivesPositions],
wrapScriptFun: CodeWrapper => (String, Int)
) extends PreprocessedSource
final case class NoSourceCode(
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
path: os.Path
) extends PreprocessedSource {
def mainClassOpt: None.type = None
def scopePath: ScopePath =
ScopePath.fromPath(path)
def directivesPositions: None.type = None
}
private def index(s: PreprocessedSource): Int =
s match {
case _: NoSourceCode => 0
case _: InMemory => 1
case _: OnDisk => 2
}
implicit val ordering: Ordering[PreprocessedSource] =
new Ordering[PreprocessedSource] {
def compare(a: PreprocessedSource, b: PreprocessedSource): Int = {
val aIdx = index(a)
val bIdx = index(b)
val idxCmp = aIdx.compare(bIdx)
if (idxCmp == 0)
(a, b) match {
case (a0: NoSourceCode, b0: NoSourceCode) =>
a0.path.toString.compareTo(b0.path.toString)
case (a0: InMemory, b0: InMemory) =>
(a0.reportingPath, b0.reportingPath) match {
case (Left(ap), Left(bp)) => ap.compareTo(bp)
case (Left(_), Right(_)) => -1
case (Right(ap), Right(bp)) => ap.toString.compareTo(bp.toString)
case (Right(_), Left(_)) => 1
}
case (a0: OnDisk, b0: OnDisk) => a0.path.toString.compareTo(b0.path.toString)
case _ => ???
}
else idxCmp
}
}
}