forked from VirtusLab/scala-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScalaPreprocessor.scala
255 lines (243 loc) · 8.35 KB
/
ScalaPreprocessor.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
package scala.build.preprocessing
import dependency.AnyDependency
import dependency.parser.DependencyParser
import java.nio.charset.StandardCharsets
import scala.build.EitherCps.{either, value}
import scala.build.Ops.*
import scala.build.directives.{
HasBuildOptions,
HasBuildOptionsWithRequirements,
HasBuildRequirements
}
import scala.build.errors.*
import scala.build.input.{Inputs, ScalaCliInvokeData, ScalaFile, SingleElement, VirtualScalaFile}
import scala.build.internal.Util
import scala.build.options.*
import scala.build.preprocessing.directives
import scala.build.preprocessing.directives.{
DirectiveHandler,
DirectiveUtil,
PreprocessedDirectives,
ScopedDirective
}
import scala.build.{Logger, Position, Positioned}
case object ScalaPreprocessor extends Preprocessor {
case class ProcessingOutput(
globalReqs: BuildRequirements,
scopedReqs: Seq[Scoped[BuildRequirements]],
opts: BuildOptions,
optsWithReqs: List[WithBuildRequirements[BuildOptions]],
updatedContent: Option[String],
directivesPositions: Option[Position.File]
)
object ProcessingOutput {
def empty: ProcessingOutput = ProcessingOutput(
BuildRequirements(),
Nil,
BuildOptions(),
List.empty,
None,
None
)
}
def preprocess(
input: SingleElement,
logger: Logger,
maybeRecoverOnError: BuildException => Option[BuildException] = e => Some(e),
allowRestrictedFeatures: Boolean,
suppressWarningOptions: SuppressWarningOptions
)(using ScalaCliInvokeData): Option[Either[BuildException, Seq[PreprocessedSource]]] =
input match {
case f: ScalaFile =>
val res = either {
val content = value(PreprocessingUtil.maybeRead(f.path))
val scopePath = ScopePath.fromPath(f.path)
val source =
value(
process(
content,
Right(f.path),
scopePath / os.up,
logger,
maybeRecoverOnError,
allowRestrictedFeatures,
suppressWarningOptions
)
) match {
case None =>
PreprocessedSource.OnDisk(f.path, None, List.empty, None, Nil, None, None)
case Some(ProcessingOutput(
requirements,
scopedRequirements,
options,
optionsWithReqs,
Some(updatedCode),
directivesPositions
)) =>
PreprocessedSource.InMemory(
originalPath = Right((f.subPath, f.path)),
relPath = f.subPath,
code = updatedCode,
ignoreLen = 0,
options = Some(options),
optionsWithTargetRequirements = optionsWithReqs,
requirements = Some(requirements),
scopedRequirements = scopedRequirements,
mainClassOpt = None,
scopePath = scopePath,
directivesPositions = directivesPositions
)
case Some(ProcessingOutput(
requirements,
scopedRequirements,
options,
optionsWithReqs,
None,
directivesPositions
)) =>
PreprocessedSource.OnDisk(
path = f.path,
options = Some(options),
optionsWithTargetRequirements = optionsWithReqs,
requirements = Some(requirements),
scopedRequirements = scopedRequirements,
mainClassOpt = None,
directivesPositions = directivesPositions
)
}
Seq(source)
}
Some(res)
case v: VirtualScalaFile =>
val res = either {
val relPath = v match {
case v if !v.isStdin && !v.isSnippet => v.subPath
case v => os.sub / v.generatedSourceFileName
}
val content = new String(v.content, StandardCharsets.UTF_8)
val (
requirements: BuildRequirements,
scopedRequirements: Seq[Scoped[BuildRequirements]],
options: BuildOptions,
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
updatedContentOpt: Option[String],
directivesPositions: Option[Position.File]
) =
value(
process(
content,
Left(v.source),
v.scopePath / os.up,
logger,
maybeRecoverOnError,
allowRestrictedFeatures,
suppressWarningOptions
)
).map {
case ProcessingOutput(
reqs,
scopedReqs,
opts,
optsWithReqs,
updatedContent,
dirsPositions
) =>
(reqs, scopedReqs, opts, optsWithReqs, updatedContent, dirsPositions)
}.getOrElse((
BuildRequirements(),
Nil,
BuildOptions(),
List(WithBuildRequirements(BuildRequirements(), BuildOptions())),
None,
None
))
val s = PreprocessedSource.InMemory(
originalPath = Left(v.source),
relPath = relPath,
updatedContentOpt.getOrElse(content),
ignoreLen = 0,
options = Some(options),
optionsWithTargetRequirements = optionsWithTargetRequirements,
requirements = Some(requirements),
scopedRequirements,
mainClassOpt = None,
scopePath = v.scopePath,
directivesPositions = directivesPositions
)
Seq(s)
}
Some(res)
case _ =>
None
}
def process(
content: String,
path: Either[String, os.Path],
scopeRoot: ScopePath,
logger: Logger,
maybeRecoverOnError: BuildException => Option[BuildException],
allowRestrictedFeatures: Boolean,
suppressWarningOptions: SuppressWarningOptions
)(using ScalaCliInvokeData): Either[BuildException, Option[ProcessingOutput]] = either {
val (contentWithNoShebang, _) = SheBang.ignoreSheBangLines(content)
val extractedDirectives: ExtractedDirectives = value(ExtractedDirectives.from(
contentWithNoShebang.toCharArray,
path,
logger,
maybeRecoverOnError
))
value {
processSources(
content,
extractedDirectives,
path,
scopeRoot,
logger,
allowRestrictedFeatures,
suppressWarningOptions,
maybeRecoverOnError
)
}
}
def processSources(
content: String,
extractedDirectives: ExtractedDirectives,
path: Either[String, os.Path],
scopeRoot: ScopePath,
logger: Logger,
allowRestrictedFeatures: Boolean,
suppressWarningOptions: SuppressWarningOptions,
maybeRecoverOnError: BuildException => Option[BuildException]
)(using ScalaCliInvokeData): Either[BuildException, Option[ProcessingOutput]] = either {
val (content0, isSheBang) = SheBang.ignoreSheBangLines(content)
val preprocessedDirectives: PreprocessedDirectives =
value(DirectivesPreprocessor.preprocess(
extractedDirectives,
path,
scopeRoot,
logger,
allowRestrictedFeatures,
suppressWarningOptions,
maybeRecoverOnError
))
if (preprocessedDirectives.isEmpty) None
else {
val allRequirements = Seq(preprocessedDirectives.globalReqs)
val summedRequirements = allRequirements.foldLeft(BuildRequirements())(_ orElse _)
val allOptions = Seq(preprocessedDirectives.globalUsings)
val summedOptions = allOptions.foldLeft(BuildOptions())(_ orElse _)
val lastContentOpt = preprocessedDirectives.strippedContent
.orElse(if (isSheBang) Some(content0) else None)
val directivesPositions = preprocessedDirectives.directivesPositions
val scopedRequirements = preprocessedDirectives.scopedReqs
Some(ProcessingOutput(
summedRequirements,
scopedRequirements,
summedOptions,
preprocessedDirectives.usingsWithReqs,
lastContentOpt,
directivesPositions
))
}
}
}