Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle upcoming mill embedded BSP and fix mill BSP install #572

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ class BspProjectResolver extends ExternalSystemProjectResolver[BspExecutionSetti
case BspProjectSettings.AutoPreImport =>
if (executionSettings.config == AutoConfig && bloopConfigDir(workspace).isDefined && isSbtProject)
runBloopInstall(workspace)
else if (MillProjectImportProvider.canImport(workspace))
MillProjectImportProvider.bspInstall(workspace)
else emptySuccess
case BspProjectSettings.BloopSbtPreImport =>
runBloopInstall(workspace)
case BspProjectSettings.MillBspPreImport =>
MillProjectImportProvider.bspInstall(workspace)
}
} else emptySuccess
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jetbrains.bsp.project.importing

import java.io.File
import org.jetbrains.plugins.scala.build.BuildMessages
import scala.io.Source
import scala.sys.process._
import scala.util.{Failure, Try, Using}

object MillProjectImportProvider {
def canImport(workspace: File): Boolean =
Option(workspace) match {
case Some(directory) if directory.isDirectory => isBspCompatible(directory) || isLegacyBspCompatible(directory)
case _ => false
}

def bspInstall(workspace: File): Try[BuildMessages] = {
val work =
if (isBspCompatible(workspace)) Try(Process("./mill mill.bsp.BSP/install", workspace).!!)
else if (isLegacyBspCompatible(workspace)) Try(Process("./mill -i mill.contrib.BSP/install", workspace).!!)
else Failure(new IllegalStateException("Unable to install BSP as this is not a Mill project"))

work.transform(
_ => Try(BuildMessages.empty.status(BuildMessages.OK)),
e => Try(BuildMessages.empty.status(BuildMessages.Error).addError(e.getMessage))
)
}

private def isBspCompatible(workspace: File) = workspace.listFiles().exists { buildScript =>
!buildScript.isDirectory && buildScript.getName == "mill" &&
Using.resource(Source.fromFile(buildScript))(_.getLines().exists(!_.matches("""^.*(0\.8\.0|0\.7.+|0\.6.+)$""")))
}

// Legacy Mill =< 0.8.0
private def isLegacyBspCompatible(workspace: File) = workspace.listFiles().exists { buildScript =>
!buildScript.isDirectory && buildScript.getName == "build.sc" &&
Using.resource(Source.fromFile(buildScript))(
_.getLines().contains("import $ivy.`com.lihaoyi::mill-contrib-bsp:$MILL_VERSION`")
)
}
}
11 changes: 6 additions & 5 deletions bsp/src/org/jetbrains/bsp/project/importing/bspConfigSteps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import org.jetbrains.annotations.Nls
import org.jetbrains.bsp.{BspBundle, BspUtil}
import org.jetbrains.bsp.project.importing.BspSetupConfigStep.ConfigSetupTask
import org.jetbrains.bsp.project.importing.bspConfigSteps._
import org.jetbrains.bsp.project.importing.setup.{BspConfigSetup, FastpassConfigSetup, MillConfigSetup, NoConfigSetup, SbtConfigSetup}
import org.jetbrains.bsp.project.importing.setup.{BspConfigSetup, FastpassConfigSetup, NoConfigSetup, SbtConfigSetup}
import org.jetbrains.bsp.protocol.BspConnectionConfig
import org.jetbrains.bsp.settings.BspProjectSettings._
import org.jetbrains.plugins.scala.build.IndicatorReporter
import org.jetbrains.plugins.scala.project.Version
import org.jetbrains.sbt.SbtUtil._
import org.jetbrains.sbt.project.{MillProjectImportProvider, SbtProjectImportProvider}
import org.jetbrains.sbt.project.SbtProjectImportProvider

object bspConfigSteps {

Expand Down Expand Up @@ -104,8 +104,9 @@ object bspConfigSteps {
// server config to be set in next step
SbtConfigSetup(workspace)
case bspConfigSteps.MillSetup =>
builder.setPreImportConfig(NoPreImport)
MillConfigSetup(workspace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking this case out causes a compile error since we have fatal warnings now. Should it just be an empty case, or the MillSetup object removed?

[error] /Users/jast/workspace/scalaUltimate/community/bsp/src/org/jetbrains/bsp/project/importing/bspConfigSteps.scala:89:12: match may not be exhaustive.
[error] It would fail on the following input: MillSetup
[error]     } else configSetup match {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a new implementation which is much better actually.

builder.setPreImportConfig(MillBspPreImport)
builder.setServerConfig(AutoConfig)
NoConfigSetup
case bspConfigSteps.FastpassSetup =>
builder.setPreImportConfig(NoPreImport)
val bspWorkspace = FastpassConfigSetup.computeBspWorkspace(workspace)
Expand All @@ -129,7 +130,7 @@ object bspConfigSteps {
} else Nil

val millChoice =
if (MillProjectImportProvider.canImport(vfile)) List(MillSetup)
if (MillProjectImportProvider.canImport(vfile.toNioPath.toFile)) List(MillSetup)
else Nil

val bloopChoice =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.jetbrains.bsp._
import org.jetbrains.bsp.protocol.BspConnectionConfig
import org.jetbrains.bsp.settings.BspProjectSettings._
import org.jetbrains.bsp.settings._
import org.jetbrains.sbt.project.{MillProjectImportProvider, SbtProjectImportProvider}
import org.jetbrains.sbt.project.SbtProjectImportProvider

class BspProjectImportBuilder
extends AbstractExternalProjectImportBuilder[BspImportControl](
Expand Down Expand Up @@ -251,7 +251,8 @@ object BspProjectOpenProcessor {
// val sbtProject = SbtProjectImportProvider.canImport(workspace)
// temporarily disable sbt importing via bloop from welcome screen (SCL-17359)
val sbtProject = false
val millProject = MillProjectImportProvider.canImport(workspace)

val millProject = MillProjectImportProvider.canImport(workspace.toNioPath.toFile)

bspConnectionProtocolSupported || bloopProject || bspConnectionProtocolSupported || sbtProject || millProject
}
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion bsp/src/org/jetbrains/bsp/protocol/BspCommunication.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BspCommunication private[protocol](base: File, config: BspServerConfig) ex

private val session: AtomicReference[Option[BspSession]] = new AtomicReference[Option[BspSession]](None)

val exitCommands: List[List[String]] = {
lazy val exitCommands: List[List[String]] = {
val workspace = new File(base.getAbsolutePath)
val files = BspConnectionConfig.workspaceBspConfigs(workspace)
val argvExitCommands = files.flatMap { file =>
Expand Down
6 changes: 5 additions & 1 deletion bsp/src/org/jetbrains/bsp/settings/settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,24 @@ object BspProjectSettings {
case object AutoPreImport extends PreImportConfig
/** Preimport with Bloop from sbt project */
case object BloopSbtPreImport extends PreImportConfig
/** Preimport with BSP from Mill project */
case object MillBspPreImport extends PreImportConfig

class PreImportConfigConverter extends Converter[PreImportConfig] {
override def fromString(value: String): PreImportConfig =
value match {
case "NoPreImport" => NoPreImport
case "AutoPreImport" => AutoPreImport
case "BloopBspPreImport" => BloopSbtPreImport
case "MillBspPreImport" => MillBspPreImport
}

override def toString(value: PreImportConfig): String =
value match {
case NoPreImport => "NoPreImport"
case AutoPreImport => "AutoPreImport"
case BloopSbtPreImport => "BloopBspPreImport"
case MillBspPreImport => "MillBspPreImport"
}
}

Expand All @@ -101,7 +105,7 @@ object BspProjectSettings {
override def toString(value: BspServerConfig): String =
value match {
case AutoConfig => "AutoConfig"
case BloopConfig =>"BloopConfig"
case BloopConfig => "BloopConfig"
case BspConfigFile(path) => s"BspConfigFile:$path"
}
}
Expand Down

This file was deleted.