-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCommunityBuildPlugin.scala
More file actions
871 lines (807 loc) · 33.3 KB
/
CommunityBuildPlugin.scala
File metadata and controls
871 lines (807 loc) · 33.3 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
import sbt._
import sbt.Keys._
import sjsonnew._, LList.:*:
import sjsonnew.BasicJsonProtocol._
import sjsonnew.support.scalajson.unsafe.{Converter, Parser}
import sbt.protocol.testing.TestResult
import xsbti.compile.{CompileAnalysis, FileAnalysisStore}
import xsbti.{CompileFailed, Problem, Severity}
import scala.collection.JavaConverters._
import scala.collection.mutable
import Scala3CommunityBuild.{Utils => _, _}
import Scala3CommunityBuild.Utils.{LibraryDependency, logOnce}
import TaskEvaluator.EvalResult
class SbtTaskEvaluator(val project: ProjectRef, private var state: State)
extends TaskEvaluator[TaskKey] {
override def eval[T](task: TaskKey[T]): EvalResult[T] = {
val evalStart = System.currentTimeMillis()
val scopedTask = project / task
sbt.Project
.runTask(scopedTask, state)
.fold[EvalResult[T]] {
val reason = TaskEvaluator.UnkownTaskException(scopedTask.toString())
EvalResult.Failure(reason :: Nil, 0)
} { case (newState, result) =>
val tookMs = (System.currentTimeMillis() - evalStart).toInt
this.state = newState
result.toEither match {
case Right(value) => EvalResult.Value(value, tookMs)
case Left(incomplete) =>
val causes = getAllDirectCauses(incomplete)
EvalResult.Failure(causes, tookMs)
}
}
}
private def getAllDirectCauses(incomplete: Incomplete): List[Throwable] = {
val Limit = 10
@scala.annotation.tailrec
def loop(
incomplete: List[Incomplete],
acc: List[Throwable]
): List[Throwable] = {
incomplete match {
case Nil => acc
case _ if acc.length > Limit => acc
case head :: tail =>
loop(
incomplete = tail ::: head.causes.toList,
acc = acc ::: head.directCause.toList
)
}
}
loop(incomplete :: Nil, Nil)
}
}
object WithExtractedScala3Suffix {
def unapply(s: String): Option[(String, String)] = {
val parts = s.split("_")
if (parts.length > 1 && parts.last.startsWith("3")) {
Some((parts.init.mkString("_"), parts.last))
} else {
None
}
}
}
object CommunityBuildPlugin extends AutoPlugin {
override def trigger = allRequirements
val runBuild = inputKey[Unit]("")
val moduleMappings = inputKey[Unit]("")
import complete.DefaultParsers._
lazy val customMavenRepoRepository = sys.env
.get("CB_MVN_REPO_URL")
.filter(_.nonEmpty)
.map("Community Build Repo".at(_))
.toSeq
private lazy val scala3NightliesRepo = "The Scala Nightly Repository"
.at("https://repo.scala-lang.org/artifactory/maven-nightlies/")
private lazy val extraOpenCBMavenRepos =
customMavenRepoRepository ++
Seq(scala3NightliesRepo)
override def projectSettings = Seq(
externalResolvers ++= extraOpenCBMavenRepos,
// Fix for cyclic dependency when trying to use crossScalaVersion ~= ???
crossScalaVersions := (thisProjectRef / crossScalaVersions).value,
// Use explicitly required scala version, otherwise we might stumble onto the default projects Scala versions
libraryDependencies ++= extraLibraryDependencies(
buildScalaVersion = sys.props.get("communitybuild.scala").filter(_.nonEmpty),
projectScalaVersion = (thisProjectRef / scalaVersion).value
)
)
private def extraLibraryDependencies(
buildScalaVersion: Option[String],
projectScalaVersion: String
): Seq[ModuleID] =
if (!projectScalaVersion.startsWith("3.")) Nil
else
Scala3CommunityBuild.Utils
.extraLibraryDependencies(
buildScalaVersion.getOrElse(projectScalaVersion)
)
.map {
case LibraryDependency(
org,
artifact,
version,
scalaCrossVersion
) =>
if (scalaCrossVersion) org %% artifact % version
else org % artifact % version
}
private def stripScala3Suffix(s: String) = s match {
case WithExtractedScala3Suffix(prefix, _) => prefix; case _ => s
}
val disableFatalWarnings =
keyTransformCommand("disableFatalWarnings", Keys.scalacOptions) { (_, _) =>
val flags = Seq("-Xfatal-warnings", "-Werror")
(_: Scope, currentSettings: Seq[String]) => currentSettings.filterNot(flags.contains)
}
private val projectCrossScalaVersions = mutable.Map.empty[String, Seq[String]]
/** Helper command used to update crossScalaVersion It's needed for sbt 1.7.x, which does force
* exact match in `++ <scalaVersion>` command for defined crossScalaVersions,
*/
val setCrossScalaVersions =
projectBasedKeyTransformCommand(
"setCrossScalaVersions",
Keys.crossScalaVersions
) { (args, extracted) =>
val scalaVersion = args.head
val partialVersion = CrossVersion.partialVersion(scalaVersion)
val targetsScala3 = partialVersion.exists(_._1 == 3)
(ref: ProjectRef, currentCrossVersions: Seq[String]) => {
val currentScalaVersion = extracted.get(ref / Keys.scalaVersion)
if (!projectCrossScalaVersions.contains(ref.project)) {
projectCrossScalaVersions(ref.project) = currentCrossVersions
.diff(Seq(scalaVersion)) ++ // exclude current version
sys.env
.get("OVERRIDEN_SCALA_VERSION")
.filterNot(_.isEmpty) // overriden before start of built tool
}
def updateVersion(fromVersion: String) = {
logOnce(
s"Changing crossVersion $fromVersion -> $scalaVersion in ${ref.project}/crossScalaVersions"
)
scalaVersion
}
def withCrossVersion(version: String) =
(version -> CrossVersion.partialVersion(version))
val crossVersionsWithPartial =
currentCrossVersions.map(withCrossVersion).toMap
val currentScalaWithPartial =
Seq(currentScalaVersion).map(withCrossVersion).toMap
val partialCrossVersions = crossVersionsWithPartial.values.toSet
val allPartialVersions =
crossVersionsWithPartial ++ currentScalaWithPartial
type PartialVersion = Option[(Long, Long)]
def mapVersions(versionsWithPartial: Map[String, PartialVersion]) =
versionsWithPartial
.map {
case (version, Some((3, _))) if targetsScala3 =>
updateVersion(version)
case (version, `partialVersion`) => updateVersion(version)
case (version, _) => version // not changed
}
.toSeq
.distinct
allPartialVersions(currentScalaVersion) match {
// Check currently used version of given project
// Some projects only set scalaVersion, while leaving crossScalaVersions default, eg. softwaremill/tapir in xxx3, xxx2_13 projects
case pv if partialCrossVersions.contains(pv) =>
mapVersions(allPartialVersions)
case _ => // if version is not a part of cross version allow only current version
val allowedCrossVersions = mapVersions(currentScalaWithPartial)
logOnce(
s"Limitting incorrect crossVersions $currentCrossVersions -> $allowedCrossVersions in ${ref.project}/crossScalaVersions"
)
allowedCrossVersions
}
}
}
val mapScalacOptions =
keyTransformCommand("mapScalacOptions", Keys.scalacOptions) {
(args, extracted) => (scope: Scope, currentScalacOptions: Seq[String]) =>
val scalaVersion = extracted.get(scope / Keys.scalaVersion)
val safeArgs = args.map(Scala3CommunityBuild.Utils.splitScalacOptionArgs)
val ignoredVersions = safeArgs.lift(2).getOrElse(Nil)
val shouldIgnore = ignoredVersions.exists(scalaVersion.startsWith)
val append = safeArgs.lift(0).getOrElse(Nil)
lazy val (appendScala3Exclusive, appendScala3Inclusive) =
append.partition { opt =>
scala3ExclusiveFlags.exists(opt.contains(_))
}
// Make sure to not modify Scala 2 project scalacOptions
// these can compiled as transitive dependency of custom startup task
val filteredAppend =
if (shouldIgnore) Nil
else if (scalaVersion.startsWith("3.")) append
else {
appendScala3Exclusive.foreach { setting =>
logOnce(
s"Exclude Scala3 specific scalacOption `$setting` in Scala ${scalaVersion} module $scope"
)
}
appendScala3Inclusive
}
val remove = safeArgs.lift(1).getOrElse(Nil)
val filteredRemove =
if (shouldIgnore) Nil
else if (scalaVersion.startsWith("3.")) remove
else remove ++ appendScala3Exclusive
Scala3CommunityBuild.Utils.mapScalacOptions(
scalaVersion = Some(scalaVersion),
current = currentScalacOptions,
append = filteredAppend,
remove = filteredRemove
)
}
// format: off
val scala3ExclusiveFlags = Seq(
"-source", "-rewrite", "-from-tasty", "-experimental",
"-new-syntax", "-old-syntax", "-indent", "-no-indent",
"-print-tasty", "-print-lines", "-uniqid", "-semanticdb-text", "-semanticdb-target",
"-coverage-out", "-explain-types", "-scalajs",
"-Vprofile" ,"-Vprofile-details" ,"-Vprofile-sorted-by", "-Vrepl-max-print-characters", "-Vrepl-max-print-elements",
"-Wimplausible-patterns",
"-Xcheck-macros", "-Xignore-scala2-macros", "-Ximplicit-search-limit",
"-Ximport-suggestion-timeout", "-Xmax-inlined-trees", "-Xmax-inlines",
"-Xprint-diff", "-Xprint-diff-del", "-Xprint-inline", "-Xprint-suspension",
"-Xrepl-disable-display", "-Xsemanticdb",
"-Xtarget", "-Xunchecked-java-output-version",
"-Xverify-signatures", "-Xwiki-syntax",
"-Ycc-debug", "-Ycc-log", "-Ycc-new", "-Ycc-print-setup",
"-Ycheck-all-patmat", "-Ycheck-constraint-deps", "-Ycheck-mods", "-Ycheck-reentrant",
"-Ycompile-scala2-library", "-Ycook-comments", "-Ycook-docs",
"-Ydebug-error", "-Ydebug-flags", "-Ydebug-macros", "-Ydebug-missing-refs", "-Ydebug-names", "-Ydebug-pos", "-Ydebug-trace", "-Ydebug-tree-with-id", "-Ydebug-unpickling",
"-Ydetailed-stats", "-Ydrop-comments", "-Ydrop-docs", "-Ydump-sbt-inc",
"-Yexplain-lowlevel", "-Yexplicit-nulls",
"-Yforce-inline-while-typing", "-Yforce-sbt-phases", "-Yforce-sbt-phases.", "-Yfrom-tasty-ignore-list",
"-Yinstrument", "-Yinstrument-defs",
"-Ykind-projector", "-Ylegacy-lazy-vals",
"-Yno-decode-stacktraces", "-Yno-deep-subtypes", "-Yno-double-bindings", "-Yno-enrich-error-messages", "-Yno-experimental",
"-Yno-kind-polymorphism", "-Yno-patmat-opt",
"-Youtput-only-tasty", "-Yplain-printer",
"-Yprint-debug", "-Yprint-debug-owners", "-Yprint-level", "-Yprint-pos", "-Yprint-pos-syms", "-Yprint-syms", "-Yprint-tasty",
"-Yread-docs", "-Yrecheck-test", "-Yrequire-targetName", "-Yretain-trees",
"-Ysafe-init", "-Ysafe-init-global", "-Yscala2-unpickler", "-Ysemanticdb", "-Yshow-print-errors",
"-Yshow-suppressed-errors", "-Yshow-tree-ids", "-Yshow-var-bounds", "-Ytest-pickler",
// Different semantics / input type
"-opt",
)
// format: on
import sbt.librarymanagement.InclExclRule
val excludeLibraryDependency =
keyTransformCommand(
"excludeLibraryDependency",
Keys.allExcludeDependencies
) { (args, extracted) => (scope, currentExcludeDependencies: Seq[InclExclRule]) =>
val scalaVersion = extracted.get(scope / Keys.scalaVersion)
val newRules = args
.map(_.replace("{scalaVersion}", scalaVersion))
.map {
_.split(":").zipWithIndex.foldLeft(InclExclRule()) {
case (rule, (org, 0)) => rule.withOrganization(org)
case (rule, (name, 1)) => rule.withName(name)
case (rule, (artifact, 2)) => rule.withArtifact(artifact)
case (_, (unexpected, idx)) =>
sys.error(s"unexpected argument $unexpected at idx: $idx")
}
}
currentExcludeDependencies ++ newRules.filterNot(
currentExcludeDependencies.contains
)
}
val removeScalacOptionsStartingWith =
keyTransformCommand("removeScalacOptionsStartingWith", Keys.scalacOptions) {
(args, _) => (_, currentScalacOptions: Seq[String]) =>
currentScalacOptions.filterNot(opt => args.exists(opt.startsWith))
}
val commands = Seq(
disableFatalWarnings,
setCrossScalaVersions,
mapScalacOptions,
excludeLibraryDependency,
removeScalacOptionsStartingWith
)
type SettingMapping[T] = (Seq[String], Extracted) => (Scope, T) => T
def keyTransformCommand[T](name: String, task: ScopedTaskable[T])(
mapping: SettingMapping[T]
) =
Command.args(name, "args") { case (state, args) =>
println(s"Execute $name: ${args.mkString(" ")}")
val extracted = sbt.Project.extract(state)
import extracted._
val withArgs = mapping(args, extracted)
val r = sbt.Project.relation(extracted.structure, true)
val allDefs = r._1s.toSeq
val projectScopes =
allDefs.filter(_.key == task.key).map(_.scope).distinct
val globalScopes = Seq(Scope.Global)
def reapply(scopes: Seq[Scope]): State = {
val redefined = task match {
case setting: SettingKey[T] =>
scopes.map(scope => (scope / setting) ~= (v => withArgs(scope, v)))
case task: TaskKey[T] =>
scopes.map(scope => (scope / task) ~= (v => withArgs(scope, v)))
}
val session = extracted.session.appendRaw(redefined)
BuiltinCommands.reapply(session, structure, state)
}
try reapply(globalScopes ++ projectScopes)
catch {
case ex: Throwable =>
logOnce(
s"Failed to reapply settings in $name: ${ex.getMessage}, retry without global scopes"
)
reapply(projectScopes)
}
}
type ProjectBasedSettingMapping[T] =
(Seq[String], Extracted) => (ProjectRef, T) => T
def projectBasedKeyTransformCommand[T](name: String, task: ScopedTaskable[T])(
mapping: ProjectBasedSettingMapping[T]
) =
Command.args(name, "args") { case (state, args) =>
println(s"Execute $name: ${args.mkString(" ")}")
val extracted = sbt.Project.extract(state)
val withArgs = mapping(args, extracted)
val refs = extracted.structure.allProjectRefs
state.appendWithSession(
task match {
case setting: SettingKey[T] =>
refs.map(ref => (ref / setting) ~= (v => withArgs(ref, v)))
case task: TaskKey[T] =>
refs.map(ref => (ref / task) ~= (v => withArgs(ref, v)))
}
)
}
// Create mapping from org%artifact_name to project name
val mkMappings = Def.task {
val cState = state.value
val s = sbt.Project.extract(cState).structure
val refs = s.allProjectRefs
refs.map { r =>
val current: ModuleID = (r / projectID).get(s.data).get
val sv = (r / scalaVersion).get(s.data).get
val sbv = (r / scalaBinaryVersion).get(s.data).get
val name = CrossVersion(current.crossVersion, sv, sbv)
.fold(current.name)(_(current.name))
val mappingKey = s"${current.organization}%${stripScala3Suffix(name)}"
mappingKey -> r
}
}
override def globalSettings = Seq(
resolvers ++= extraOpenCBMavenRepos,
moduleMappings := { // Store settings in file to capture its original scala versions
val moduleIds = mkMappings.value
IO.write(
file("community-build-mappings.txt"),
moduleIds.map(v => v._1 + " " + v._2.project).mkString("\n")
)
},
runBuild := {
val scalaVersionArg :: configJson :: ids =
spaceDelimited("<arg>").parsed.toList
println(s"Build config: ${configJson}")
val config = {
val parsed = Parser
.parseFromString(configJson)
.flatMap(
Converter.fromJson[ProjectBuildConfig](_)(ProjectBuildConfigFormat)
)
println(s"Parsed config: ${parsed}")
parsed.getOrElse(ProjectBuildConfig())
}
val cState = state.value
val extracted = sbt.Project.extract(cState)
val s = extracted.structure
val refsByName = s.allProjectRefs.map(r => r.project -> r).toMap
val scalaBinaryVersionUsed =
CrossVersion.binaryScalaVersion(scalaVersionArg)
val scalaBinaryVersionSuffix = "_" + scalaBinaryVersionUsed
val scalaVersionSuffix = "_" + scalaVersionArg
val rootDirName = file(".").getCanonicalFile().getName()
// Ignore projects for which crossScalaVersion does not contain any binary version
// of currently used Scala version. This is important in case of usage projectMatrix and
// Scala.js / Scala Native, which can define multiple projects for different major Scala versions
// but with common target, eg. foo2_13, foo3
def projectSupportsScalaBinaryVersion(
projectRef: ProjectRef
): Boolean = {
val hasCrossVersionSet = extracted
.get(projectRef / crossScalaVersions)
.exists {
// Do not make exact check to handle projects using 3.0.0-RC* versions
CrossVersion
.binaryScalaVersion(_)
.startsWith(scalaBinaryVersionUsed)
}
// Workaround for scalatest/circe which does not set crossScalaVersions correctly
def matchesName =
scalaBinaryVersionUsed.startsWith("3") && projectRef.project.contains(
"Dotty"
)
hasCrossVersionSet || matchesName
}
def selectProject(projects: Seq[(String, ProjectRef)]): ProjectRef = {
require(projects.nonEmpty, "selectProject with empty projects argument")
projects.map(_._2) match {
case Seq(project) => project
case projects =>
projects
.find(projectSupportsScalaBinaryVersion)
.getOrElse {
val selected = projects.head
System.err.println(
s"None of projects in group ${projects.map(_.project)} uses current Scala binary version, using random: ${selected.project}"
)
selected
}
}
}
val originalModuleIds: Map[String, ProjectRef] = IO
.readLines(file("community-build-mappings.txt"))
.map(_.split(' '))
.map(d => d(0) -> refsByName(d(1)))
.groupBy(_._1)
.mapValues(selectProject)
.toMap
val moduleIds: Map[String, ProjectRef] = mkMappings.value
.groupBy(_._1)
.mapValues(selectProject)
.toMap
def simplifiedModuleId(id: String) =
// Drop first part of mapping (organization%)
id.substring(id.indexOf('%') + 1)
val simplifiedModuleIds = moduleIds.map { case (key, value) =>
simplifiedModuleId(key) -> value
}
val idsToUse = ids match {
case "*%*" :: _ =>
originalModuleIds.keys.toSeq
.filterNot { id => id.contains("_sjs") || id.contains("_native") }
case ids => ids
}
val filteredIds = Scala3CommunityBuild.Utils
.filterTargets(idsToUse, config.projects.exclude.map(_.r))
println("Starting build...")
// Find projects that matches maven
val topLevelProjects = {
var haveUsedRootModule = false
val idsWithMissingMappings =
scala.collection.mutable.ListBuffer.empty[String]
val mappedProjects =
for {
id <- filteredIds
testedSuffixes = Seq(
"",
scalaVersionSuffix,
scalaBinaryVersionSuffix
) ++
Option("Dotty").filter(_ => scalaBinaryVersionUsed.startsWith("3"))
testedFullIds = testedSuffixes.map(id + _)
candidates = for (fullId <- testedFullIds)
yield Stream(
refsByName.get(fullId),
originalModuleIds.get(fullId),
moduleIds.get(fullId),
simplifiedModuleIds.get(simplifiedModuleId(fullId)),
// Single, top level, unnamed project
refsByName.headOption
.map(_._2)
.filter(_ => refsByName.size == 1)
).flatten
} yield candidates.flatten.headOption
.orElse {
// Try use root project, it might not be explicitly declared or named
refsByName
.get(rootDirName)
.collect {
case rootProject if !haveUsedRootModule =>
haveUsedRootModule = true
rootProject
}
}
.orElse {
System.err.println(s"""Module mapping missing:
| id: $id
| testedIds: $testedFullIds
| scalaVersionSuffix: $scalaVersionSuffix
| scalaBinaryVersionSuffix: $scalaBinaryVersionSuffix
| refsByName: ${refsByName.keySet}
| originalModuleIds: ${originalModuleIds.keySet}
| moduleIds: ${moduleIds.keySet}
|""".stripMargin)
idsWithMissingMappings += id
None
}
if (idsWithMissingMappings.nonEmpty) {
val msg =
s"Failed to resolve mappings for ${idsWithMissingMappings.size}:${filteredIds.size} targets: ${idsWithMissingMappings.toSeq
.mkString(", ")}"
if (idsWithMissingMappings.size >= filteredIds.size) sys.error(msg)
else System.err.println(msg)
}
mappedProjects.flatten.toSet
}
val projectDeps = s.allProjectPairs.map { case (rp, ref) =>
ref -> rp.dependencies.map(_.project)
}.toMap
@annotation.tailrec
def flatten(
soFar: Set[ProjectRef],
toCheck: Set[ProjectRef]
): Set[ProjectRef] =
toCheck match {
case e if e.isEmpty => soFar
case pDeps =>
val deps = projectDeps(pDeps.head).filterNot(soFar.contains)
flatten(soFar ++ deps, pDeps.tail ++ deps)
}
val allToBuild = flatten(topLevelProjects, topLevelProjects)
println("Projects: " + allToBuild.map(_.project))
val projectsBuildResults = allToBuild.toList.zipWithIndex.map { case (r, idx) =>
val evaluator = new SbtTaskEvaluator(r, cState)
val s = sbt.Project.extract(cState).structure
val projectName = (r / moduleName).get(s.data).get
println(s"Starting build for $r ($projectName)... [$idx/${allToBuild.size}]")
val overrideSettings = {
val overrides = config.projects.overrides
overrides
.get(projectName)
.orElse {
overrides.collectFirst {
// No Regex.matches in Scala 2.12
// Exclude cases when excluded name is a prefix of other projects
case (key, value)
if key.r.findFirstIn(projectName).isDefined &&
!projectName.startsWith(key) =>
value
}
}
}
val testingMode =
overrideSettings.flatMap(_.tests).getOrElse(config.tests)
import evaluator._
val scalacOptions = eval(Compile / Keys.scalacOptions) match {
case EvalResult.Value(settings, _) => settings
case _ => Nil
}
println(s"Compile scalacOptions: ${scalacOptions.mkString(", ")}")
def mayRetry[T](task: TaskKey[T])(
evaluate: TaskKey[T] => EvalResult[T]
): EvalResult[T] = evaluate(task) match {
case EvalResult.Failure(reasons, _) if reasons.exists {
case ex: AssertionError =>
ex.getMessage.contains("overlapping patches")
case _ => false
} =>
evaluate(task)
case result => result
}
val compileResult = mayRetry(Compile / compile)(eval)
val shouldBuildDocs = eval(Compile / doc / skip) match {
case EvalResult.Value(skip, _) => skip
case _ => false
}
val docsResult = mayRetry(Compile / doc) {
evalWhen(shouldBuildDocs, compileResult)
}
val testsCompileResult = mayRetry(Test / compile) {
evalWhen(testingMode != TestingMode.Disabled, compileResult)
}
// Introduced to fix publishing artifact locally in scala-debug-adapter
lazy val testOptionsResult = eval(Test / testOptions)
val testsExecuteResult =
evalWhen(
testingMode == TestingMode.Full,
testsCompileResult,
testOptionsResult
)(
Test / executeTests
)
val shouldPublish = eval(Compile / publish / skip) match {
case EvalResult.Value(skip, _) => skip
case _ => false
}
val publishResult = PublishResult(
evalWhen(shouldPublish, compileResult)(Compile / publishLocal)
)
ModuleBuildResults(
artifactName = projectName,
compile = collectCompileResult(compileResult, scalacOptions),
doc = DocsResult(docsResult),
testsCompile = collectCompileResult(testsCompileResult, scalacOptions),
testsExecute = collectTestResults(
testsExecuteResult,
eval(Test / definedTests),
eval(Test / loadedTestFrameworks)
),
publish = publishResult,
metadata = ModuleMetadata(
crossScalaVersions = projectCrossScalaVersions.getOrElse(r.project, Nil)
)
)
}
val buildSummary = BuildSummary(projectsBuildResults)
println(s"""
|************************
|Build summary:
|${buildSummary.toJson}
|************************""".stripMargin)
IO.write(file("..") / "build-summary.txt", buildSummary.toJson)
val failedModules = projectsBuildResults
.filter(_.hasFailedStep)
.map(_.artifactName)
val hasFailedSteps = failedModules.nonEmpty
val buildStatus =
if (hasFailedSteps) "failure"
else "success"
IO.write(file("..") / "build-status.txt", buildStatus)
if (hasFailedSteps) throw new ProjectBuildFailureException(failedModules)
},
(runBuild / aggregate) := false
)
private def collectCompileResult(
evalResult: EvalResult[CompileAnalysis],
scalacOptions: Seq[String]
): CompileResult = {
def countProblems(severity: Severity, problems: Iterable[Problem]) =
problems.count(_.severity == severity)
val sourceVersion = scalacOptions.collectFirst {
case s if s.contains("-source:") => s.split(":").last
}
evalResult match {
case EvalResult.Failure(reasons, tookTime) =>
reasons
.collectFirst { case ctx: CompileFailed => ctx }
.foldLeft(
CompileResult(
Status.Failed,
failureContext = evalResult.toBuildError,
warnings = 0,
errors = 0,
tookMs = tookTime,
sourceVersion = sourceVersion
)
) { case (result, ctx) =>
result.copy(
warnings = countProblems(Severity.Warn, ctx.problems()),
errors = countProblems(Severity.Error, ctx.problems())
)
}
case EvalResult.Value(compileAnalysis, tookTime) =>
case class CompileStats(warnings: Int, errors: Int)
val stats = compileAnalysis
.readSourceInfos()
.getAllSourceInfos()
.asScala
.values
.foldLeft(CompileStats(0, 0)) { case (CompileStats(accWarnings, accErrors), sourceInfo) =>
def countAll(severity: Severity) =
countProblems(severity, sourceInfo.getReportedProblems()) +
countProblems(severity, sourceInfo.getUnreportedProblems())
CompileStats(
warnings = accWarnings + countAll(Severity.Warn),
errors = accErrors + countAll(Severity.Error)
)
}
CompileResult(
Status.Ok,
warnings = stats.warnings,
errors = stats.errors,
tookMs = tookTime,
sourceVersion = sourceVersion
)
case EvalResult.Skipped =>
CompileResult(Status.Skipped, warnings = 0, errors = 0, tookMs = 0)
}
}
def collectTestResults(
evalResult: EvalResult[sbt.Tests.Output],
definedTests: EvalResult[Seq[sbt.TestDefinition]],
loadedTestFrameworks: EvalResult[
Map[sbt.TestFramework, sbt.testing.Framework]
]
): TestsResult = {
val default = TestsResult(
evalResult.toStatus,
failureContext = evalResult.toBuildError,
tookMs = evalResult.evalTime
)
evalResult match {
case EvalResult.Value(value, _) =>
val status = value.overall match {
case TestResult.Passed => Status.Ok
case _ => Status.Failed
}
def sum(results: Iterable[SuiteResult]) =
results.foldLeft(TestStats.empty) { case (state, result) =>
state.copy(
passed = state.passed + result.passedCount,
failed =
state.failed + result.failureCount + result.errorCount + result.canceledCount,
ignored = state.ignored + result.ignoredCount,
skipped = state.skipped + result.skippedCount
)
}
val byFrameworkStats: Map[String, TestStats] =
(definedTests, loadedTestFrameworks) match {
case (
EvalResult.Value(definedTests, _),
EvalResult.Value(loadedTestFrameworks, _)
) =>
val frameworkByFingerprint = loadedTestFrameworks.values.flatMap { framework =>
val name = framework.name()
framework.fingerprints().map(_ -> name)
}.toMap
val testFrameworks = definedTests.map { test =>
val framework = frameworkByFingerprint
.get(test.fingerprint)
.orElse {
// In case if overwrites toString but not equals (see munit)
frameworkByFingerprint.collectFirst {
case (fingerprint, name)
if test.fingerprint
.toString() == fingerprint.toString =>
name
}
}
.getOrElse("unknown")
test.name -> framework
}.toMap
value.events
.groupBy { case (testName, _) =>
testFrameworks.get(testName).getOrElse("unknown")
}
.map { case (frameworkName, results) =>
frameworkName -> sum(results.values)
}
case _ => Map.empty
}
val overallStats = sum(value.events.values)
default.copy(
status = status,
overall = overallStats,
byFramework = byFrameworkStats
)
case _ => default
}
}
// Serialization
implicit object TestingModeEnumJsonFormat extends JsonFormat[TestingMode] {
def write[J](x: TestingMode, builder: Builder[J]): Unit = ???
def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): TestingMode =
jsOpt.fold(deserializationError("Missing string")) { js =>
unbuilder.readString(js) match {
case "disabled" => TestingMode.Disabled
case "compile-only" => TestingMode.CompileOnly
case "full" => TestingMode.Full
}
}
}
implicit object ProjectOverridesFormat extends JsonFormat[ProjectOverrides] {
def write[J](obj: ProjectOverrides, builder: Builder[J]): Unit = ???
def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): ProjectOverrides =
jsOpt.fold(deserializationError("Empty object")) { js =>
implicit val _unbuilder: Unbuilder[J] = unbuilder
unbuilder.beginObject(js)
val testsMode = readOrDefault("tests", Option.empty[TestingMode])
unbuilder.endObject
ProjectOverrides(tests = testsMode)
}
}
implicit object ProjectsConfigFormat extends JsonFormat[ProjectsConfig] {
def write[J](obj: ProjectsConfig, builder: Builder[J]): Unit = ???
def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): ProjectsConfig =
jsOpt.fold(deserializationError("Empty object")) { js =>
implicit val _unbuilder: Unbuilder[J] = unbuilder
unbuilder.beginObject(js)
val excluded = readOrDefault("exclude", Array.empty[String])
val overrides =
readOrDefault("overrides", Map.empty[String, ProjectOverrides])
unbuilder.endObject()
ProjectsConfig(excluded.toList, overrides)
}
}
implicit object ProjectBuildConfigFormat extends JsonFormat[ProjectBuildConfig] {
def write[J](v: ProjectBuildConfig, builder: Builder[J]): Unit = ???
def read[J](
optValue: Option[J],
unbuilder: Unbuilder[J]
): ProjectBuildConfig =
optValue.fold(deserializationError("Empty object")) { v =>
implicit val _unbuilder: Unbuilder[J] = unbuilder
unbuilder.beginObject(v)
val projects = readOrDefault("projects", ProjectsConfig())
val testsMode = readOrDefault[TestingMode, J]("tests", TestingMode.Full)
unbuilder.endObject()
ProjectBuildConfig(projects, testsMode)
}
}
private def readOrDefault[T, J](field: String, default: T)(implicit
format: JsonReader[T],
unbuilder: Unbuilder[J]
) =
unbuilder
.lookupField(field)
.fold(default)(v => format.read(Some(v), unbuilder))
}