Skip to content

Commit

Permalink
Handle upcoming mill embedded BSP and fix mill BSP install
Browse files Browse the repository at this point in the history
  • Loading branch information
joan38 committed Oct 29, 2020
1 parent e1eba99 commit ec0332a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 87 deletions.
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)
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.

0 comments on commit ec0332a

Please sign in to comment.