forked from scalameta/metals
/
BuildTargetInfo.scala
249 lines (224 loc) · 7.66 KB
/
BuildTargetInfo.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
package scala.meta.internal.metals
import java.nio.file.Path
import scala.collection.mutable.ListBuffer
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.mtags.URIEncoderDecoder
import scala.meta.io.AbsolutePath
import ch.epfl.scala.bsp4j.BuildTarget
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
class BuildTargetInfo(buildTargets: BuildTargets) {
def buildTargetDetails(targetName: String, uri: String): String = {
buildTargets.all
.find(_.getName == targetName)
.orElse(buildTargets.all.find(_.getId.getUri.toString == uri))
.orElse(
buildTargets.all.find(
_.getDisplayName().bazelEscapedDisplayName == targetName
)
)
.map(target => buildTargetDetail(target.getId()))
.getOrElse(s"Build target $targetName not found")
}
private def buildTargetDetail(
targetId: BuildTargetIdentifier
): String = {
val commonInfo = buildTargets.info(targetId)
val javaInfo = buildTargets.javaTarget(targetId)
val scalaInfo = buildTargets.scalaTarget(targetId)
val output = ListBuffer[String]()
commonInfo.foreach(info => {
output += "Target"
output += s" ${info.getName}"
if (!info.getTags.isEmpty)
output ++= getSection("Tags", info.getTags.asScala.toList)
if (!info.getLanguageIds.isEmpty)
output ++= getSection("Languages", info.getLanguageIds.asScala.toList)
val capabilities =
translateCapability("Debug", info.getCapabilities().getCanDebug) ::
translateCapability("Run", info.getCapabilities().getCanRun) ::
translateCapability("Test", info.getCapabilities().getCanTest) ::
translateCapability(
"Compile",
info.getCapabilities().getCanCompile,
) :: Nil
output ++= getSection("Capabilities", capabilities)
val dependencies = getDependencies(info)
if (dependencies.nonEmpty)
output ++= getSection("Dependencies", dependencies)
val dependentTargets = getDependentTargets(info)
if (dependentTargets.nonEmpty)
output ++= getSection("Dependent Targets", dependentTargets)
})
javaInfo.foreach(info => {
output += ""
output += "Javac Options"
output += " compile - https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#options"
output += " runtime - https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#standard-options-for-java"
output += " "
if (info.options.nonEmpty)
info.options.foreach(f =>
output += s" ${if (f.isEmpty) "[BLANK]" else f}"
)
else
output += " [BLANK]"
})
scalaInfo.foreach(info => {
output += ""
output += "Scalac Options"
if (info.scalaBinaryVersion.startsWith("3"))
output += " compile - https://docs.scala-lang.org/scala3/guides/migration/options-new.html"
else
output += " compile - https://docs.scala-lang.org/overviews/compiler-options/index.html#Standard_Settings"
output += " "
if (info.options.nonEmpty)
info.options.foreach(scalacOption =>
output += s" ${if (scalacOption.isEmpty) "[BLANK]" else scalacOption}"
)
else
output += " [BLANK]"
output ++= getSection("Scala Version", List(info.scalaVersion))
output ++= getSection(
"Scala Binary Version",
List(info.scalaBinaryVersion),
)
output ++= getSection(
"Scala Platform",
List(info.scalaPlatform.toString()),
)
info.jvmVersion.foreach(jvmVersion =>
output ++= getSection("JVM Version", List(jvmVersion))
)
info.jvmHome.foreach(jvmHome =>
output ++= getSection("JVM Home", List(jvmHome))
)
})
commonInfo.foreach(info => {
output ++= getSection(
"Base Directory",
List(URIEncoderDecoder.decode(info.baseDirectory)),
)
output ++= getSection("Sources", getSources(info))
})
val scalaClassesDir = scalaInfo.map(_.classDirectory)
val javaClassesDir = javaInfo.map(_.classDirectory)
if (scalaClassesDir == javaClassesDir)
scalaClassesDir.foreach(classesDir =>
output ++= getSection("Classes Directory", List(classesDir))
)
else {
javaClassesDir.foreach(classesDir =>
output ++= getSection("Java Classes Directory", List(classesDir))
)
scalaClassesDir.foreach(classesDir =>
output ++= getSection("Scala Classes Directory", List(classesDir))
)
}
val scalaClassPath = scalaInfo.map(_.fullClasspath).getOrElse(Nil)
val javaClassPath = javaInfo.map(_.fullClasspath).getOrElse(Nil)
if (scalaClassPath == javaClassPath)
output ++= getSection("Classpath", getClassPath(scalaClassPath, targetId))
else {
output ++= getSection(
"Java Classpath",
getClassPath(javaClassPath, targetId),
)
output ++= getSection(
"Scala Classpath",
getClassPath(scalaClassPath, targetId),
)
}
output += ""
output.mkString(System.lineSeparator())
}
private def getSection(
sectionName: String,
sectionText: List[String],
): List[String] =
"" :: sectionName :: {
if (sectionText.isEmpty) List(" NONE")
else
sectionText.map(text => s" ${if (text.isEmpty) "[BLANK]" else text}")
}
private def translateCapability(
capability: String,
hasCapability: Boolean,
): String =
if (hasCapability) capability else s"$capability <- NOT SUPPORTED"
private def getSingleClassPathInfo(
path: Path,
shortPath: Path,
maxFilenameSize: Int,
buildTargetId: BuildTargetIdentifier,
): String = {
val filename = shortPath.toString()
val padding = " " * (maxFilenameSize - filename.size)
val status = if (path.toFile.exists) {
val blankWarning = " " * 9
if (
path.toFile().isDirectory() || buildTargets
.sourceJarFor(buildTargetId, AbsolutePath(path))
.nonEmpty
)
blankWarning
else
"NO SOURCE"
} else " MISSING "
val fullName = if (path.toFile.isFile) s" $path" else ""
s" $filename$padding $status$fullName"
}
private def getClassPath(
classPath: List[Path],
buildTargetId: BuildTargetIdentifier,
): List[String] = {
def shortenPath(path: Path): Path = {
if (path.toFile.isFile)
path.getFileName()
else
path
}
if (classPath.nonEmpty) {
val maxFilenameSize =
classPath.map(shortenPath(_).toString.length()).max + 5
classPath.map(path =>
getSingleClassPathInfo(
path,
shortenPath(path),
maxFilenameSize,
buildTargetId,
)
)
} else
List("NONE")
}
private def getDependencies(target: BuildTarget): List[String] = {
target.getDependencies.asScala
.map(f =>
buildTargets
.info(f)
.map(_.getName())
.getOrElse("Unknown target")
)
.toList
}
private def getDependentTargets(target: BuildTarget): List[String] = {
buildTargets.all
.filter(dependentTarget =>
dependentTarget.getDependencies.contains(target.getId())
)
.map(_.getName())
.toList
}
private def getSources(target: BuildTarget): List[String] = {
buildTargets.sourceItemsToBuildTargets
.filter(_._2.iterator.asScala.contains(target.getId()))
.toList
.map {
case (path, _) if buildTargets.checkIfGeneratedDir(path) =>
s"${path}/* (generated)"
case (path, _) if path.isDirectory => s"${path}/*"
case (path, _) if !path.exists => s"${path}/* (empty)"
case (path, _) => path.toString()
}
.sorted
}
}