/
ImportedBuild.scala
166 lines (153 loc) Β· 5.59 KB
/
ImportedBuild.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
package scala.meta.internal.metals
import java.{util => ju}
import scala.build.bsp.WrappedSourcesParams
import scala.build.bsp.WrappedSourcesResult
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.meta.internal.metals.MetalsEnrichments._
import ch.epfl.scala.bsp4j._
/**
* Metadata that we import from the build tool via BSP.
*/
case class ImportedBuild(
workspaceBuildTargets: WorkspaceBuildTargetsResult,
scalacOptions: ScalacOptionsResult,
javacOptions: JavacOptionsResult,
sources: SourcesResult,
dependencySources: DependencySourcesResult,
wrappedSources: WrappedSourcesResult,
dependencyModules: DependencyModulesResult,
) {
def ++(other: ImportedBuild): ImportedBuild = {
val updatedBuildTargets = new WorkspaceBuildTargetsResult(
(workspaceBuildTargets.getTargets.asScala ++ other.workspaceBuildTargets.getTargets.asScala).asJava
)
val updatedScalacOptions = new ScalacOptionsResult(
(scalacOptions.getItems.asScala ++ other.scalacOptions.getItems.asScala).asJava
)
val updatedJavacOptions = new JavacOptionsResult(
(javacOptions.getItems.asScala ++ other.javacOptions.getItems.asScala).asJava
)
val updatedSources = new SourcesResult(
(sources.getItems.asScala ++ other.sources.getItems.asScala).asJava
)
val updatedDependencySources = new DependencySourcesResult(
(dependencySources.getItems.asScala ++ other.dependencySources.getItems.asScala).asJava
)
val updatedWrappedSources = new WrappedSourcesResult(
(wrappedSources.getItems.asScala ++ other.wrappedSources.getItems.asScala).asJava
)
val updatedDependencyModules = new DependencyModulesResult(
(dependencyModules.getItems.asScala ++ other.dependencyModules.getItems.asScala).asJava
)
ImportedBuild(
updatedBuildTargets,
updatedScalacOptions,
updatedJavacOptions,
updatedSources,
updatedDependencySources,
updatedWrappedSources,
updatedDependencyModules,
)
}
lazy val targetUris: Set[String] =
workspaceBuildTargets.getTargets.asScala
.map(_.getId.getUri)
.toSet
}
object ImportedBuild {
def empty: ImportedBuild =
ImportedBuild(
new WorkspaceBuildTargetsResult(ju.Collections.emptyList()),
new ScalacOptionsResult(ju.Collections.emptyList()),
new JavacOptionsResult(ju.Collections.emptyList()),
new SourcesResult(ju.Collections.emptyList()),
new DependencySourcesResult(ju.Collections.emptyList()),
new WrappedSourcesResult(ju.Collections.emptyList()),
new DependencyModulesResult(ju.Collections.emptyList()),
)
def fromConnection(
conn: BuildServerConnection
)(implicit ec: ExecutionContext): Future[ImportedBuild] =
for {
allBuildTargets <- conn.workspaceBuildTargets()
workspaceBuildTargets = relevantBuildTargets(allBuildTargets)
ids = workspaceBuildTargets.getTargets.map(_.getId)
scalacOptions <- conn.buildTargetScalacOptions(
new ScalacOptionsParams(ids)
)
javacOptions <- conn.buildTargetJavacOptions(
new JavacOptionsParams(ids)
)
sources <- conn.buildTargetSources(new SourcesParams(ids))
bspProvidedDependencySources <- conn.buildTargetDependencySources(
new DependencySourcesParams(ids)
)
dependencySources <- resolveMissingDependencySources(
bspProvidedDependencySources,
javacOptions,
scalacOptions,
)
wrappedSources <- conn.buildTargetWrappedSources(
new WrappedSourcesParams(ids)
)
dependencyModules <- conn.buildTargetDependencyModules(
new DependencyModulesParams(ids)
)
} yield {
ImportedBuild(
workspaceBuildTargets,
scalacOptions,
javacOptions,
sources,
dependencySources,
wrappedSources,
dependencyModules,
)
}
private def relevantBuildTargets(
workspaceBuildTargets: WorkspaceBuildTargetsResult
): WorkspaceBuildTargetsResult = {
val targets = workspaceBuildTargets.getTargets.asScala.toList
val scalaJavaTargets = targets.filter { target =>
val langIds = target.getLanguageIds.asScala.toList
langIds.contains("scala") || langIds.contains("java")
}
workspaceBuildTargets.setTargets(scalaJavaTargets.asJava)
workspaceBuildTargets
}
private def resolveMissingDependencySources(
dependencySources: DependencySourcesResult,
javacOptions: JavacOptionsResult,
scalacOptions: ScalacOptionsResult,
)(implicit ec: ExecutionContext): Future[DependencySourcesResult] = {
val dependencySourcesItems = dependencySources.getItems().asScala.toList
val idsLookup = dependencySourcesItems.map(_.getTarget()).toSet
val classpaths = javacOptions
.getItems()
.asScala
.map(item => (item.getTarget(), item.getClasspath())) ++
scalacOptions
.getItems()
.asScala
.map(item => (item.getTarget(), item.getClasspath()))
val newItemsFuture =
Future.sequence {
classpaths.collect {
case (id, classpath) if !idsLookup(id) =>
for {
items <- JarSourcesProvider.fetchSources(
classpath.asScala.filter(_.endsWith(".jar")).toSeq
)
} yield new DependencySourcesItem(id, items.asJava)
}
}
newItemsFuture.map { newItems =>
new DependencySourcesResult((dependencySourcesItems ++ newItems).asJava)
}
}
def fromList(data: Seq[ImportedBuild]): ImportedBuild =
if (data.isEmpty) empty
else if (data.lengthCompare(1) == 0) data.head
else data.reduce(_ ++ _)
}