forked from VirtusLab/scala-cli
/
CrossSources.scala
486 lines (444 loc) · 19.1 KB
/
CrossSources.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package scala.build
import java.io.File
import scala.build.CollectionOps.*
import scala.build.EitherCps.{either, value}
import scala.build.Ops.*
import scala.build.Positioned
import scala.build.errors.{
BuildException,
CompositeBuildException,
ExcludeDefinitionError,
MalformedDirectiveError,
Severity
}
import scala.build.input.ElementsUtils.*
import scala.build.input.*
import scala.build.internal.Constants
import scala.build.internal.util.{RegexUtils, WarningMessages}
import scala.build.options.{
BuildOptions,
BuildRequirements,
MaybeScalaVersion,
Scope,
SuppressWarningOptions,
WithBuildRequirements
}
import scala.build.preprocessing.*
import scala.build.testrunner.DynamicTestRunner.globPattern
import scala.util.Try
import scala.util.chaining.*
/** Information gathered from preprocessing command inputs - sources (including unwrapped scripts)
* and build options from using directives
*
* @param paths
* paths and realtive paths to sources on disk, wrapped in their build requirements
* @param inMemory
* in memory sources (e.g. snippets) wrapped in their build requirements
* @param defaultMainClass
* @param resourceDirs
* @param buildOptions
* build options from sources
* @param unwrappedScripts
* in memory script sources, their code must be wrapped before compiling
*/
final case class CrossSources(
paths: Seq[WithBuildRequirements[(os.Path, os.RelPath)]],
inMemory: Seq[WithBuildRequirements[Sources.InMemory]],
defaultMainClass: Option[String],
resourceDirs: Seq[WithBuildRequirements[os.Path]],
buildOptions: Seq[WithBuildRequirements[BuildOptions]],
unwrappedScripts: Seq[WithBuildRequirements[Sources.UnwrappedScript]]
) {
def sharedOptions(baseOptions: BuildOptions): BuildOptions =
buildOptions
.filter(_.requirements.isEmpty)
.map(_.value)
.foldLeft(baseOptions)(_ orElse _)
private def needsScalaVersion =
paths.exists(_.needsScalaVersion) ||
inMemory.exists(_.needsScalaVersion) ||
resourceDirs.exists(_.needsScalaVersion) ||
buildOptions.exists(_.needsScalaVersion)
def scopedSources(baseOptions: BuildOptions): Either[BuildException, ScopedSources] = either {
val sharedOptions0 = sharedOptions(baseOptions)
// FIXME Not 100% sure the way we compute the intermediate and final BuildOptions
// is consistent (we successively filter out / retain options to compute a scala
// version and platform, which might not be the version and platform of the final
// BuildOptions).
val crossSources0 =
if (needsScalaVersion) {
val retainedScalaVersion = value(sharedOptions0.scalaParams)
.map(p => MaybeScalaVersion(p.scalaVersion))
.getOrElse(MaybeScalaVersion.none)
val buildOptionsWithScalaVersion = buildOptions
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
.filter(_.requirements.isEmpty)
.map(_.value)
.foldLeft(sharedOptions0)(_ orElse _)
val platform = buildOptionsWithScalaVersion.platform
copy(
paths = paths
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
.flatMap(_.withPlatform(platform.value).toSeq),
inMemory = inMemory
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
.flatMap(_.withPlatform(platform.value).toSeq),
resourceDirs = resourceDirs
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
.flatMap(_.withPlatform(platform.value).toSeq),
buildOptions = buildOptions
.filter(!_.requirements.isEmpty)
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
.flatMap(_.withPlatform(platform.value).toSeq),
unwrappedScripts = unwrappedScripts
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
.flatMap(_.withPlatform(platform.value).toSeq)
)
}
else {
val platform = sharedOptions0.platform
copy(
paths = paths
.flatMap(_.withPlatform(platform.value).toSeq),
inMemory = inMemory
.flatMap(_.withPlatform(platform.value).toSeq),
resourceDirs = resourceDirs
.flatMap(_.withPlatform(platform.value).toSeq),
buildOptions = buildOptions
.filter(!_.requirements.isEmpty)
.flatMap(_.withPlatform(platform.value).toSeq),
unwrappedScripts = unwrappedScripts
.flatMap(_.withPlatform(platform.value).toSeq)
)
}
val defaultScope: Scope = Scope.Main
ScopedSources(
crossSources0.paths.map(_.scopedValue(defaultScope)),
crossSources0.inMemory.map(_.scopedValue(defaultScope)),
defaultMainClass,
crossSources0.resourceDirs.map(_.scopedValue(defaultScope)),
crossSources0.buildOptions.map(_.scopedValue(defaultScope)),
crossSources0.unwrappedScripts.map(_.scopedValue(defaultScope))
)
}
}
object CrossSources {
private def withinTestSubDirectory(p: ScopePath, inputs: Inputs): Boolean =
p.root.exists { path =>
val fullPath = path / p.subPath
inputs.elements.exists {
case Directory(path) =>
// Is this file subdirectory of given dir and if we have a subdiretory 'test' on the way
fullPath.startsWith(path) &&
fullPath.relativeTo(path).segments.contains("test")
case _ => false
}
}
/** @return
* a CrossSources and Inputs which contains element processed from using directives
*/
def forInputs(
inputs: Inputs,
preprocessors: Seq[Preprocessor],
logger: Logger,
suppressWarningOptions: SuppressWarningOptions,
exclude: Seq[Positioned[String]] = Nil,
maybeRecoverOnError: BuildException => Option[BuildException] = e => Some(e)
)(using ScalaCliInvokeData): Either[BuildException, (CrossSources, Inputs)] = either {
def preprocessSources(elems: Seq[SingleElement])
: Either[BuildException, Seq[PreprocessedSource]] =
elems
.map { elem =>
preprocessors
.iterator
.flatMap(p =>
p.preprocess(
elem,
logger,
maybeRecoverOnError,
inputs.allowRestrictedFeatures,
suppressWarningOptions
).iterator
)
.take(1)
.toList
.headOption
.getOrElse(Right(Nil)) // FIXME Warn about unprocessed stuff?
}
.sequence
.left.map(CompositeBuildException(_))
.map(_.flatten)
val flattenedInputs = inputs.flattened()
val allExclude = { // supports only one exclude directive in one source file, which should be the project file.
val projectScalaFileOpt = flattenedInputs.collectFirst {
case f: ProjectScalaFile => f
}
val excludeFromProjectFile =
value(preprocessSources(projectScalaFileOpt.toSeq))
.flatMap(_.options).flatMap(_.internal.exclude)
exclude ++ excludeFromProjectFile
}
val preprocessedInputFromArgs: Seq[PreprocessedSource] =
value(
preprocessSources(value(excludeSources(flattenedInputs, inputs.workspace, allExclude)))
)
val sourcesFromDirectives =
preprocessedInputFromArgs
.flatMap(_.options)
.flatMap(_.internal.extraSourceFiles)
.distinct
val inputsElemFromDirectives: Seq[SingleFile] =
value(resolveInputsFromSources(sourcesFromDirectives, inputs.enableMarkdown))
val preprocessedSourcesFromDirectives: Seq[PreprocessedSource] =
value(preprocessSources(inputsElemFromDirectives.pipe(elements =>
value(excludeSources(elements, inputs.workspace, allExclude))
)))
warnAboutChainedUsingFileDirectives(preprocessedSourcesFromDirectives, logger)
val allInputs = inputs.add(inputsElemFromDirectives).pipe(inputs =>
val filteredElements = value(excludeSources(inputs.elements, inputs.workspace, allExclude))
inputs.withElements(elements = filteredElements)
)
val preprocessedSources: Seq[PreprocessedSource] =
(preprocessedInputFromArgs ++ preprocessedSourcesFromDirectives).distinct
.pipe { sources =>
val validatedSources: Seq[PreprocessedSource] =
value(validateExcludeDirectives(sources, allInputs.workspace))
val distinctSources = validatedSources.distinctBy(_.distinctPathOrSource)
val diff = validatedSources.diff(distinctSources)
if diff.nonEmpty then
val diffString = diff.map(_.distinctPathOrSource).mkString(s"${System.lineSeparator} ")
logger.message(
s"""[${Console.YELLOW}warn${Console.RESET}] Skipped duplicate sources:
| $diffString""".stripMargin
)
distinctSources
}
logger.flushExperimentalWarnings
val scopedRequirements = preprocessedSources.flatMap(_.scopedRequirements)
val scopedRequirementsByRoot = scopedRequirements.groupBy(_.path.root)
def baseReqs(path: ScopePath): BuildRequirements = {
val fromDirectives =
scopedRequirementsByRoot
.getOrElse(path.root, Nil)
.flatMap(_.valueFor(path).toSeq)
.foldLeft(BuildRequirements())(_ orElse _)
// Scala CLI treats all `.test.scala` files tests as well as
// files from within `test` subdirectory from provided input directories
// If file has `using target <scope>` directive this take precendeces.
if (
fromDirectives.scope.isEmpty &&
(path.subPath.last.endsWith(".test.scala") || withinTestSubDirectory(path, allInputs))
)
fromDirectives.copy(scope = Some(BuildRequirements.ScopeRequirement(Scope.Test)))
else fromDirectives
}
val buildOptions: Seq[WithBuildRequirements[BuildOptions]] = (for {
preprocessedSource <- preprocessedSources
opts <- preprocessedSource.options.toSeq
if opts != BuildOptions() || preprocessedSource.optionsWithTargetRequirements.nonEmpty
} yield {
val baseReqs0 = baseReqs(preprocessedSource.scopePath)
preprocessedSource.optionsWithTargetRequirements :+ WithBuildRequirements(
preprocessedSource.requirements.fold(baseReqs0)(_ orElse baseReqs0),
opts
)
}).flatten
val defaultMainClassOpt: Option[String] = for {
mainClassPath <- allInputs.defaultMainClassElement
.map(s => ScopePath.fromPath(s.path).subPath)
processedMainClass <- preprocessedSources.find(_.scopePath.subPath == mainClassPath)
mainClass <- processedMainClass.mainClassOpt
} yield mainClass
val pathsWithDirectivePositions
: Seq[(WithBuildRequirements[(os.Path, os.RelPath)], Option[Position.File])] =
preprocessedSources.collect {
case d: PreprocessedSource.OnDisk =>
val baseReqs0 = baseReqs(d.scopePath)
WithBuildRequirements(
d.requirements.fold(baseReqs0)(_ orElse baseReqs0),
(d.path, d.path.relativeTo(allInputs.workspace))
) -> d.directivesPositions
}
val inMemoryWithDirectivePositions
: Seq[(WithBuildRequirements[Sources.InMemory], Option[Position.File])] =
preprocessedSources.collect {
case m: PreprocessedSource.InMemory =>
val baseReqs0 = baseReqs(m.scopePath)
WithBuildRequirements(
m.requirements.fold(baseReqs0)(_ orElse baseReqs0),
Sources.InMemory(m.originalPath, m.relPath, m.content, m.wrapperParamsOpt)
) -> m.directivesPositions
}
val unwrappedScriptsWithDirectivePositions
: Seq[(WithBuildRequirements[Sources.UnwrappedScript], Option[Position.File])] =
preprocessedSources.collect {
case m: PreprocessedSource.UnwrappedScript =>
val baseReqs0 = baseReqs(m.scopePath)
WithBuildRequirements(
m.requirements.fold(baseReqs0)(_ orElse baseReqs0),
Sources.UnwrappedScript(m.originalPath, m.relPath, m.wrapScriptFun)
) -> m.directivesPositions
}
val resourceDirs: Seq[WithBuildRequirements[os.Path]] =
resolveResourceDirs(allInputs, preprocessedSources)
lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
.flatMap { (withBuildRequirements, directivesPositions) =>
val scope = withBuildRequirements.scopedValue(Scope.Main).scope
val path: os.Path = withBuildRequirements.value match
case im: Sources.InMemory =>
im.originalPath match
case Right((_, p: os.Path)) => p
case _ => inputs.workspace / im.generatedRelPath
case us: Sources.UnwrappedScript =>
us.originalPath match
case Right((_, p: os.Path)) => p
case _ => inputs.workspace / us.generatedRelPath
case (p: os.Path, _) => p
directivesPositions.map((path, scope, _))
}
.groupBy((_, scope, _) => scope)
.view
.mapValues(_.map((path, _, directivesPositions) => path -> directivesPositions))
.toMap
lazy val anyScopeHasMultipleSourcesWithDirectives =
Scope.all.exists(allPathsWithDirectivesByScope.get(_).map(_.length).getOrElse(0) > 1)
val shouldSuppressWarning =
suppressWarningOptions.suppressDirectivesInMultipleFilesWarning.getOrElse(false)
if !shouldSuppressWarning && anyScopeHasMultipleSourcesWithDirectives then {
val projectFilePath = inputs.elements.projectSettingsFiles.headOption match
case Some(s) => s.path
case _ => inputs.workspace / Constants.projectFileName
allPathsWithDirectivesByScope
.values
.flatten
.filter((path, _) => ScopePath.fromPath(path) != ScopePath.fromPath(projectFilePath))
.foreach { (_, directivesPositions) =>
logger.diagnostic(
s"Using directives detected in multiple files. It is recommended to keep them centralized in the $projectFilePath file.",
positions = Seq(directivesPositions)
)
}
}
val paths = pathsWithDirectivePositions.map(_._1)
val inMemory = inMemoryWithDirectivePositions.map(_._1)
val unwrappedScripts = unwrappedScriptsWithDirectivePositions.map(_._1)
(
CrossSources(
paths,
inMemory,
defaultMainClassOpt,
resourceDirs,
buildOptions,
unwrappedScripts
),
allInputs
)
}
/** @return
* the resource directories that should be added to the classpath
*/
private def resolveResourceDirs(
allInputs: Inputs,
preprocessedSources: Seq[PreprocessedSource]
): Seq[WithBuildRequirements[os.Path]] = {
val fromInputs = allInputs.elements
.collect { case r: ResourceDirectory => WithBuildRequirements(BuildRequirements(), r.path) }
val fromSources =
preprocessedSources.flatMap(_.options)
.flatMap(_.classPathOptions.resourcesDir)
.map(r => WithBuildRequirements(BuildRequirements(), r))
val fromSourcesWithRequirements = preprocessedSources
.flatMap(_.optionsWithTargetRequirements)
.flatMap(_.map(_.classPathOptions.resourcesDir).flatten)
fromInputs ++ fromSources ++ fromSourcesWithRequirements
}
private def resolveInputsFromSources(sources: Seq[Positioned[os.Path]], enableMarkdown: Boolean) =
sources.map { source =>
val sourcePath = source.value
lazy val dir = sourcePath / os.up
lazy val subPath = sourcePath.subRelativeTo(dir)
if (os.isDir(sourcePath))
Right(Directory(sourcePath).singleFilesFromDirectory(enableMarkdown))
else if (sourcePath == os.sub / Constants.projectFileName)
Right(Seq(ProjectScalaFile(dir, subPath)))
else if (sourcePath.ext == "scala") Right(Seq(SourceScalaFile(dir, subPath)))
else if (sourcePath.ext == "sc") Right(Seq(Script(dir, subPath, None)))
else if (sourcePath.ext == "java") Right(Seq(JavaFile(dir, subPath)))
else if (sourcePath.ext == "jar") Right(Seq(JarFile(dir, subPath)))
else if (sourcePath.ext == "md") Right(Seq(MarkdownFile(dir, subPath)))
else {
val msg =
if (os.exists(sourcePath))
s"$sourcePath: unrecognized source type (expected .scala, .sc, .java extension or directory) in using directive."
else s"$sourcePath: not found path defined in using directive."
Left(new MalformedDirectiveError(msg, source.positions))
}
}.sequence
.left.map(CompositeBuildException(_))
.map(_.flatten)
/** Filters out the sources from the input sequence based on the provided 'exclude' patterns. The
* exclude patterns can be absolute paths, relative paths, or glob patterns.
*
* @throws BuildException
* If multiple 'exclude' patterns are defined across the input sources.
*/
private def excludeSources[E <: Element](
elements: Seq[E],
workspaceDir: os.Path,
exclude: Seq[Positioned[String]]
): Either[BuildException, Seq[E]] = either {
val excludePatterns = exclude.map(_.value).flatMap { p =>
val maybeRelPath = Try(os.RelPath(p)).toOption
maybeRelPath match {
case Some(relPath) if os.isDir(workspaceDir / relPath) =>
// exclude relative directory paths, add * to exclude all files in the directory
Seq(p, (workspaceDir / relPath / "*").toString)
case Some(relPath) =>
Seq(p, (workspaceDir / relPath).toString) // exclude relative paths
case None => Seq(p)
}
}
def isSourceIncluded(path: String, excludePatterns: Seq[String]): Boolean =
excludePatterns
.forall(pattern => !RegexUtils.globPattern(pattern).matcher(path).matches())
elements.filter {
case e: OnDisk => isSourceIncluded(e.path.toString, excludePatterns)
case _ => true
}
}
/** Validates that exclude directives are defined only in the one source.
*/
def validateExcludeDirectives(
sources: Seq[PreprocessedSource],
workspaceDir: os.Path
): Either[BuildException, Seq[PreprocessedSource]] = {
val excludeDirectives = sources.flatMap(_.options).map(_.internal.exclude).toList.flatten
excludeDirectives match {
case Nil | Seq(_) =>
Right(sources)
case _ =>
val expectedProjectFilePath = workspaceDir / Constants.projectFileName
Left(new ExcludeDefinitionError(
excludeDirectives.flatMap(_.positions),
expectedProjectFilePath
))
}
}
/** When a source file added by a `using file` directive, itself, contains `using file` directives
* there should be a warning printed that transitive `using file` directives are not supported.
*/
def warnAboutChainedUsingFileDirectives(
sourcesAddedWithDirectives: Seq[PreprocessedSource],
logger: Logger
): Unit = for {
additionalSource <- sourcesAddedWithDirectives
buildOptions <- additionalSource.options
transitiveAdditionalSource <- buildOptions.internal.extraSourceFiles
} do
logger.diagnostic(
WarningMessages.chainingUsingFileDirective,
Severity.Warning,
transitiveAdditionalSource.positions
)
}