From 5c60b7a7c9034ab4bf4c937b91af655c4db62d84 Mon Sep 17 00:00:00 2001 From: Joan Goyeau Date: Mon, 26 Oct 2020 12:25:20 -0700 Subject: [PATCH] Handle upcoming mill embedded BSP and fix mill BSP install --- .../importing/MillProjectImportProvider.scala | 34 +++++++++++++ .../project/importing/bspConfigSteps.scala | 7 +-- .../bsp/project/importing/projectImport.scala | 2 +- .../importing/setup/MillConfigSetup.scala | 51 ------------------- .../project/MillProjectImportProvider.scala | 27 ---------- 5 files changed, 37 insertions(+), 84 deletions(-) create mode 100644 bsp/src/org/jetbrains/bsp/project/importing/MillProjectImportProvider.scala delete mode 100644 bsp/src/org/jetbrains/bsp/project/importing/setup/MillConfigSetup.scala delete mode 100644 scala/scala-impl/src/org/jetbrains/sbt/project/MillProjectImportProvider.scala diff --git a/bsp/src/org/jetbrains/bsp/project/importing/MillProjectImportProvider.scala b/bsp/src/org/jetbrains/bsp/project/importing/MillProjectImportProvider.scala new file mode 100644 index 00000000000..dc1da679b12 --- /dev/null +++ b/bsp/src/org/jetbrains/bsp/project/importing/MillProjectImportProvider.scala @@ -0,0 +1,34 @@ +package org.jetbrains.bsp.project.importing + +import com.intellij.openapi.vfs.VirtualFile +import scala.io.Source +import scala.sys.process._ +import scala.util.Try + +object MillProjectImportProvider { + private val buildFile = "build.sc" + + def canImport(workspace: VirtualFile): Boolean = { + workspace match { + case null => false + case directory if directory.isDirectory => + lazy val bspBuiltIn = directory.getChildren.exists { vBuildScript => + !vBuildScript.isDirectory && vBuildScript.getName == "mill" && + Source.fromInputStream(vBuildScript.getInputStream) + .getLines() + .exists(!_.matches("""^.*(0\.8\.0|0\.7.+|0\.6.+)$""")) + } && Try(Process("./mill mill.bsp.BSP/install", workspace.toNioPath.toFile).!!).isSuccess + + // Legacy support for Mill =< 0.8.0 + lazy val buildWithBspPlugin = directory.getChildren.exists { vBuildScript => + !vBuildScript.isDirectory && vBuildScript.getName == "build.sc" && + Source.fromInputStream(vBuildScript.getInputStream) + .getLines() + .contains("import $ivy.`com.lihaoyi::mill-contrib-bsp:$MILL_VERSION`") + } && Try(Process("./mill -i mill.contrib.BSP/install", workspace.toNioPath.toFile).!!).isSuccess + + bspBuiltIn || buildWithBspPlugin + case file => file.getName == buildFile + } + } +} diff --git a/bsp/src/org/jetbrains/bsp/project/importing/bspConfigSteps.scala b/bsp/src/org/jetbrains/bsp/project/importing/bspConfigSteps.scala index 2b63c3c1f42..8527888068f 100644 --- a/bsp/src/org/jetbrains/bsp/project/importing/bspConfigSteps.scala +++ b/bsp/src/org/jetbrains/bsp/project/importing/bspConfigSteps.scala @@ -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 { @@ -103,9 +103,6 @@ object bspConfigSteps { builder.setPreImportConfig(NoPreImport) // server config to be set in next step SbtConfigSetup(workspace) - case bspConfigSteps.MillSetup => - builder.setPreImportConfig(NoPreImport) - MillConfigSetup(workspace) case bspConfigSteps.FastpassSetup => builder.setPreImportConfig(NoPreImport) val bspWorkspace = FastpassConfigSetup.computeBspWorkspace(workspace) diff --git a/bsp/src/org/jetbrains/bsp/project/importing/projectImport.scala b/bsp/src/org/jetbrains/bsp/project/importing/projectImport.scala index 73974ff9e86..8dc2a34b64c 100644 --- a/bsp/src/org/jetbrains/bsp/project/importing/projectImport.scala +++ b/bsp/src/org/jetbrains/bsp/project/importing/projectImport.scala @@ -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]( diff --git a/bsp/src/org/jetbrains/bsp/project/importing/setup/MillConfigSetup.scala b/bsp/src/org/jetbrains/bsp/project/importing/setup/MillConfigSetup.scala deleted file mode 100644 index 93a4a87c0ad..00000000000 --- a/bsp/src/org/jetbrains/bsp/project/importing/setup/MillConfigSetup.scala +++ /dev/null @@ -1,51 +0,0 @@ -package org.jetbrains.bsp.project.importing.setup - -import java.io.File -import java.util.concurrent.atomic.AtomicBoolean - -import org.jetbrains.plugins.scala.build.{BuildMessages, BuildReporter} - -import scala.jdk.CollectionConverters._ -import scala.concurrent.duration._ -import scala.util.{Failure, Success, Try} - -object MillConfigSetup { - private val MillProcessCheckTimeout = 100.millis - - def apply(baseDir: File): MillConfigSetup = { - val processBuilder = new ProcessBuilder(Seq("./mill", "-i", "mill.contrib.BSP/install").asJava) - processBuilder.directory(baseDir) - new MillConfigSetup(processBuilder) - } -} - -class MillConfigSetup(processBuilder: ProcessBuilder) extends BspConfigSetup { - import MillConfigSetup._ - - private val cancellationFlag: AtomicBoolean = new AtomicBoolean(false) - - def cancel(): Unit = cancellationFlag.set(true) - - private def waitFinish(process: Process): Option[BuildMessages] = - Iterator.continually(process.waitFor(MillProcessCheckTimeout.length, MillProcessCheckTimeout.unit)) - .map { end => - if (! end && cancellationFlag.get()) { - process.destroy() - true - } else end - } - .find(identity) - .map(_ => BuildMessages.empty.status(BuildMessages.OK)) - - override def run(implicit reporter: BuildReporter): Try[BuildMessages] = { - // TODO send process output to reporter / messages - reporter.start() - val process = processBuilder.start() - val result = Try(waitFinish(process).get) - result match { - case Failure(err) => reporter.finishWithFailure(err) - case Success(bm) => reporter.finish(bm) - } - result - } -} diff --git a/scala/scala-impl/src/org/jetbrains/sbt/project/MillProjectImportProvider.scala b/scala/scala-impl/src/org/jetbrains/sbt/project/MillProjectImportProvider.scala deleted file mode 100644 index 2fc6e1c2310..00000000000 --- a/scala/scala-impl/src/org/jetbrains/sbt/project/MillProjectImportProvider.scala +++ /dev/null @@ -1,27 +0,0 @@ -package org.jetbrains.sbt.project - -import com.intellij.openapi.vfs.VirtualFile -import java.io.File -import scala.io.Source - -object MillProjectImportProvider { - private val buildFile = "build.sc" - - def canImport(vFile: VirtualFile): Boolean = vFile match { - case null => false - case directory if directory.isDirectory => - val buildWithBspPlugin = directory.getChildren.exists { vBuildScript => - vBuildScript.getName == "build.sc" && - Source.fromInputStream(vBuildScript.getInputStream) - .getLines() - .contains("import $ivy.`com.lihaoyi::mill-contrib-bsp:$MILL_VERSION`") - } - val bootstrap = directory.getChildren.exists { child => - val file = new File(child.getPath) - file.getName == "mill" && file.canExecute - } - - buildWithBspPlugin && bootstrap - case file => file.getName == buildFile - } -}