/
BuildTools.scala
280 lines (255 loc) Β· 9.24 KB
/
BuildTools.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
package scala.meta.internal.builds
import java.nio.file.Files
import java.util.Properties
import java.util.concurrent.atomic.AtomicReference
import scala.meta.internal.bsp.ScalaCliBspScope
import scala.meta.internal.io.PathIO
import scala.meta.internal.metals.BloopServers
import scala.meta.internal.metals.Directories
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.UserConfiguration
import scala.meta.internal.metals.scalacli.ScalaCli
import scala.meta.io.AbsolutePath
import ujson.ParsingFailedException
/**
* Detects what build tool is used in this workspace.
*
* Although we only support a limited set of build tools, knowing
* what build tool is used in the workspace helps to produce better errors
* for people using unsupported build tools. For example: "Gradle is not supported"
* instead of "Unsupported build tool".
*
* @param bspGlobalDirectories Directories for user and system installed BSP connection
* details according to BSP spec:
* https://build-server-protocol.github.io/docs/server-discovery.html#default-locations-for-bsp-connection-files
*/
final class BuildTools(
workspace: AbsolutePath,
bspGlobalDirectories: List[AbsolutePath],
userConfig: () => UserConfiguration,
explicitChoiceMade: () => Boolean,
) {
private val lastDetectedBuildTools = new AtomicReference(Set.empty[String])
// NOTE: We do a couple extra check here before we say a workspace with a
// `.bsp` is auto-connectable, and we ensure that a user has explicitly chosen
// to use another build server besides Bloop or it's a BSP server for a build
// tool we don't support. If this isn't done, it causes unexpected warnings
// since if a `.bsp/<something>.json` exists before a `.bloop` one does in a
// workspace with a build tool we support, we will attempt to autoconnect to
// Bloop since Metals thinks it's in state that's auto-connectable before the
// user is even prompted.
def isAutoConnectable(
maybeProjectRoot: Option[AbsolutePath] = None
): Boolean = {
maybeProjectRoot
.map(isBloop)
.getOrElse(
isBloop
) || (isBsp && all.isEmpty) || (isBsp && explicitChoiceMade())
}
def isBloop(root: AbsolutePath): Boolean = hasJsonFile(root.resolve(".bloop"))
def bloopProject: Option[AbsolutePath] = searchForBuildTool(isBloop)
def isBloop: Boolean = bloopProject.isDefined
def isBsp: Boolean = {
hasJsonFile(workspace.resolve(Directories.bsp)) ||
customProjectRoot.exists(root =>
hasJsonFile(root.resolve(Directories.bsp))
) ||
bspGlobalDirectories.exists(hasJsonFile)
}
private def hasJsonFile(dir: AbsolutePath): Boolean = {
dir.list.exists(_.extension == "json")
}
def isBazelBsp: Boolean = {
workspace.resolve(Directories.bazelBsp).isDirectory &&
BazelBuildTool.existingProjectView(workspace).nonEmpty &&
isBsp
}
// Returns true if there's a build.sbt file or project/build.properties with sbt.version
def sbtProject: Option[AbsolutePath] = searchForBuildTool { root =>
root.resolve("build.sbt").isFile || {
val buildProperties =
root.resolve("project").resolve("build.properties")
buildProperties.isFile && {
val props = new Properties()
val in = Files.newInputStream(buildProperties.toNIO)
try props.load(in)
finally in.close()
props.getProperty("sbt.version") != null
}
}
}
def isSbt: Boolean = sbtProject.isDefined
def millProject: Option[AbsolutePath] = searchForBuildTool(
_.resolve("build.sc").isFile
)
def isMill: Boolean = millProject.isDefined
def isMillBsp(path: AbsolutePath): Boolean =
isInBsp(path) && path.filename.contains("mill") &&
path.filename.endsWith(".json")
def scalaCliProject: Option[AbsolutePath] =
searchForBuildTool(_.resolve("project.scala").isFile)
.orElse {
try {
ScalaCliBspScope.scalaCliBspRoot(workspace) match {
case Nil => None
case path :: Nil if path.isFile => Some(path.parent)
case path :: Nil => Some(path)
case _ => Some(workspace)
}
} catch {
case _: ParsingFailedException =>
scribe.warn(s"could not parse scala-cli build server configuration")
None
}
}
def gradleProject: Option[AbsolutePath] = {
val defaultGradlePaths = List(
"settings.gradle",
"settings.gradle.kts",
"build.gradle",
"build.gradle.kts",
)
searchForBuildTool(root =>
defaultGradlePaths.exists(root.resolve(_).isFile)
)
}
def isGradle: Boolean = gradleProject.isDefined
def mavenProject: Option[AbsolutePath] = searchForBuildTool(
_.resolve("pom.xml").isFile
)
def isMaven: Boolean = mavenProject.isDefined
def bazelProject: Option[AbsolutePath] = searchForBuildTool(
_.resolve("WORKSPACE").isFile
)
def isBazel: Boolean = bazelProject.isDefined
def isInBsp(path: AbsolutePath): Boolean =
path.isFile && path.parent.filename == ".bsp" &&
path.filename.endsWith(".json")
private def customBsps: List[BspOnly] = {
val bspFolders =
(workspace :: customProjectRoot.toList).distinct
.map(_.resolve(Directories.bsp)) ++ bspGlobalDirectories
val root = customProjectRoot.getOrElse(workspace)
for {
bspFolder <- bspFolders
if (bspFolder.exists && bspFolder.isDirectory)
buildTool <- bspFolder.toFile
.listFiles()
.collect {
case file
if file.isFile() && file.getName().endsWith(".json") &&
!knownBsps(file.getName().stripSuffix(".json")) =>
BspOnly(
file.getName().stripSuffix(".json"),
root,
AbsolutePath(file.toPath()),
)
}
.toList
} yield buildTool
}
private def knownBsps =
Set(
SbtBuildTool.name,
MillBuildTool.bspName,
BazelBuildTool.bspName,
) ++ ScalaCli.names
private def customProjectRoot = userConfig().getCustomProjectRoot(workspace)
private def searchForBuildTool(
isProjectRoot: AbsolutePath => Boolean
): Option[AbsolutePath] = {
customProjectRoot match {
case Some(projectRoot) => Some(projectRoot).filter(isProjectRoot)
case None =>
if (isProjectRoot(workspace)) Some(workspace)
else
workspace.toNIO
.toFile()
.listFiles()
.collectFirst {
case file
if file.isDirectory &&
!file.getName.startsWith(".") &&
isProjectRoot(AbsolutePath(file.toPath())) =>
AbsolutePath(file.toPath())
}
}
}
def allAvailable: List[BuildTool] = {
List(
SbtBuildTool(workspaceVersion = None, workspace, userConfig),
GradleBuildTool(userConfig, workspace),
MavenBuildTool(userConfig, workspace),
MillBuildTool(userConfig, workspace),
ScalaCliBuildTool(workspace, workspace, userConfig),
BazelBuildTool(userConfig, workspace),
)
}
def all: List[String] = {
val buf = List.newBuilder[String]
if (isBloop) buf += BloopServers.name
if (isSbt) buf += "sbt"
if (isMill) buf += "Mill"
if (isGradle) buf += "Gradle"
if (isMaven) buf += "Maven"
if (isBazel) buf += "Bazel"
buf.result()
}
def isEmpty: Boolean = {
all.isEmpty
}
def loadSupported(): List[BuildTool] = {
val buf = List.newBuilder[BuildTool]
sbtProject.foreach(buf += SbtBuildTool(_, userConfig))
gradleProject.foreach(buf += GradleBuildTool(userConfig, _))
mavenProject.foreach(buf += MavenBuildTool(userConfig, _))
millProject.foreach(buf += MillBuildTool(userConfig, _))
scalaCliProject.foreach(buf += ScalaCliBuildTool(workspace, _, userConfig))
bazelProject.foreach(buf += BazelBuildTool(userConfig, _))
buf.addAll(customBsps)
buf.result()
}
override def toString: String = {
val names = all.mkString("+")
if (names.isEmpty) "<no build tool>"
else names
}
def isBuildRelated(
path: AbsolutePath
): Option[String] = {
if (sbtProject.exists(SbtBuildTool.isSbtRelatedPath(_, path)))
Some(SbtBuildTool.name)
else if (gradleProject.exists(GradleBuildTool.isGradleRelatedPath(_, path)))
Some(GradleBuildTool.name)
else if (mavenProject.exists(MavenBuildTool.isMavenRelatedPath(_, path)))
Some(MavenBuildTool.name)
else if (isMill && MillBuildTool.isMillRelatedPath(path))
Some(MillBuildTool.name)
else if (bazelProject.exists(BazelBuildTool.isBazelRelatedPath(_, path)))
Some(BazelBuildTool.name)
else if (isInBsp(path)) {
val name = path.filename.stripSuffix(".json")
if (knownBsps(name) && !ScalaCli.names(name)) None
else Some(name)
} else None
}
def initialize(): Set[String] = {
lastDetectedBuildTools.getAndSet(
loadSupported().map(_.executableName).toSet
)
}
def newBuildTool(buildTool: String): Boolean = {
val before = lastDetectedBuildTools.getAndUpdate(_ + buildTool)
!before.contains(buildTool)
}
}
object BuildTools {
def default(workspace: AbsolutePath = PathIO.workingDirectory): BuildTools =
new BuildTools(
workspace,
Nil,
() => UserConfiguration(),
explicitChoiceMade = () => false,
)
}