-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract connecting to build server
- Loading branch information
1 parent
d69e6dd
commit 00bbd5c
Showing
12 changed files
with
1,082 additions
and
931 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
metals/src/main/scala/scala/meta/internal/metals/BuildToolProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package scala.meta.internal.metals | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
import scala.meta.internal.builds.BuildTool | ||
import scala.meta.internal.builds.BuildToolSelector | ||
import scala.meta.internal.builds.BuildTools | ||
import scala.meta.internal.builds.VersionRecommendation | ||
import scala.meta.internal.metals.clients.language.MetalsLanguageClient | ||
import scala.meta.internal.semver.SemVer | ||
import scala.meta.io.AbsolutePath | ||
|
||
class BuildToolProvider( | ||
buildTools: BuildTools, | ||
tables: Tables, | ||
folder: AbsolutePath, | ||
warnings: ProjectWarnings, | ||
languageClient: MetalsLanguageClient, | ||
)(implicit ec: ExecutionContext) { | ||
private val buildToolSelector: BuildToolSelector = new BuildToolSelector( | ||
languageClient, | ||
tables, | ||
) | ||
|
||
def buildTool: Option[BuildTool] = | ||
for { | ||
name <- tables.buildTool.selectedBuildTool() | ||
buildTool <- buildTools.current().find(_.executableName == name) | ||
if isCompatibleVersion(buildTool) | ||
} yield buildTool | ||
|
||
def optProjectRoot: Option[AbsolutePath] = | ||
buildTool.map(_.projectRoot).orElse(buildTools.bloopProject) | ||
|
||
private def isCompatibleVersion(buildTool: BuildTool): Boolean = { | ||
buildTool match { | ||
case buildTool: VersionRecommendation => | ||
SemVer.isCompatibleVersion( | ||
buildTool.minimumVersion, | ||
buildTool.version, | ||
) | ||
case _ => true | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the version of the build tool is compatible with the version that | ||
* metals expects and returns the current digest of the build tool. | ||
*/ | ||
private def verifyBuildTool(buildTool: BuildTool): BuildTool.Verified = { | ||
buildTool match { | ||
case buildTool: VersionRecommendation | ||
if !isCompatibleVersion(buildTool) => | ||
BuildTool.IncompatibleVersion(buildTool) | ||
case _ => | ||
buildTool.digestWithRetry(folder) match { | ||
case Some(digest) => | ||
BuildTool.Found(buildTool, digest) | ||
case None => BuildTool.NoChecksum(buildTool, folder) | ||
} | ||
} | ||
} | ||
|
||
def supportedBuildTool(): Future[Option[BuildTool.Found]] = { | ||
buildTools.loadSupported() match { | ||
case Nil => { | ||
if (!buildTools.isAutoConnectable()) { | ||
warnings.noBuildTool() | ||
} | ||
Future(None) | ||
} | ||
case buildTools => | ||
for { | ||
buildTool <- buildToolSelector.checkForChosenBuildTool( | ||
buildTools | ||
) | ||
} yield { | ||
buildTool.flatMap { bt => | ||
verifyBuildTool(bt) match { | ||
case found: BuildTool.Found => Some(found) | ||
case warn @ BuildTool.IncompatibleVersion(buildTool) => | ||
scribe.warn(warn.message) | ||
languageClient.showMessage( | ||
Messages.IncompatibleBuildToolVersion.params(buildTool) | ||
) | ||
None | ||
case warn: BuildTool.NoChecksum => | ||
scribe.warn(warn.message) | ||
None | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def onNewBuildToolAdded( | ||
newBuildTool: BuildTool, | ||
currentBuildTool: BuildTool, | ||
): Future[Boolean] = | ||
buildToolSelector.onNewBuildToolAdded(newBuildTool, currentBuildTool) | ||
} |
Oops, something went wrong.