Skip to content

Commit adeeb42

Browse files
committed
Be better at starting Code on various OS
- On OS X, use "open -a" so that code does not need to be added to the PATH - On Windows, use "cmd.exe /C" since that seems to be required to look in the PATH On every OS, pass "-n" to Code to start a new instance, this makes things more reproducible.
1 parent 380be44 commit adeeb42

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

docs/docs/usage/ide-support.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ Dotty, please follow the instructions at https://github.com/lampepfl/dotty-examp
1717
Usage
1818
=====
1919
1. Install [Visual Studio Code](https://code.visualstudio.com/).
20-
2. Make sure `code`, the binary for Visual Studio Code, is on your `$PATH`, this
21-
is the case if you can start the IDE by running `code` in a terminal.
22-
3. In your project, run:
20+
2. In your project, run:
2321
```shell
2422
sbt launchIDE
2523
```

project/Build.scala

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import sbt.Package.ManifestAttributes
1515
import com.typesafe.sbteclipse.plugin.EclipsePlugin._
1616

1717
import dotty.tools.sbtplugin.DottyPlugin.autoImport._
18+
import dotty.tools.sbtplugin.DottyIDEPlugin.runProcess
1819
import dotty.tools.sbtplugin.DottyIDEPlugin.autoImport._
1920
import org.scalajs.sbtplugin.ScalaJSPlugin
2021
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
@@ -766,8 +767,13 @@ object Build {
766767
val mainClass = "dotty.tools.languageserver.Main"
767768
val extensionPath = (baseDirectory in `vscode-dotty`).value.getAbsolutePath
768769

769-
val codeArgs = if (inputArgs.isEmpty) List((baseDirectory.value / "..").getAbsolutePath) else inputArgs
770-
val allArgs = List("-client_command", "code", s"--extensionDevelopmentPath=$extensionPath") ++ codeArgs
770+
val codeArgs =
771+
s"--extensionDevelopmentPath=$extensionPath" +:
772+
(if (inputArgs.isEmpty) List((baseDirectory.value / "..").getAbsolutePath) else inputArgs)
773+
774+
val clientCommand = codeCommand.value ++ codeArgs
775+
776+
val allArgs = "-client_command" +: clientCommand
771777

772778
runTask(Runtime, mainClass, allArgs: _*)
773779
}.dependsOn(compile in (`vscode-dotty`, Compile)).evaluated
@@ -893,7 +899,7 @@ object Build {
893899

894900

895901
sbtPlugin := true,
896-
version := "0.1.2",
902+
version := "0.1.3-SNAPSHOT",
897903
ScriptedPlugin.scriptedSettings,
898904
ScriptedPlugin.sbtTestDirectory := baseDirectory.value / "sbt-test",
899905
ScriptedPlugin.scriptedBufferLog := false,
@@ -944,12 +950,7 @@ object Build {
944950
// Currently, vscode-dotty depends on daltonjorge.scala for syntax highlighting,
945951
// this is not automatically installed when starting the extension in development mode
946952
// (--extensionDevelopmentPath=...)
947-
val exitCodeInstall = new java.lang.ProcessBuilder("code", "--install-extension", "daltonjorge.scala")
948-
.inheritIO()
949-
.start()
950-
.waitFor()
951-
if (exitCodeInstall != 0)
952-
throw new MessageOnlyException("Installing dependency daltonjorge.scala failed")
953+
runProcess(codeCommand.value ++ Seq("--install-extension", "daltonjorge.scala"), wait = true)
953954

954955
sbt.inc.Analysis.Empty
955956
},
@@ -986,14 +987,9 @@ object Build {
986987
val inputArgs = spaceDelimited("<arg>").parsed
987988
val codeArgs = if (inputArgs.isEmpty) List((baseDirectory.value / "..").getAbsolutePath) else inputArgs
988989
val extensionPath = baseDirectory.value.getAbsolutePath
989-
val processArgs = List("code", s"--extensionDevelopmentPath=${extensionPath}") ++ codeArgs
990+
val processArgs = List(s"--extensionDevelopmentPath=${extensionPath}") ++ codeArgs
990991

991-
val exitCode = new java.lang.ProcessBuilder(processArgs: _*)
992-
.inheritIO()
993-
.start()
994-
.waitFor()
995-
if (exitCode != 0)
996-
throw new MessageOnlyException("Running Visual Studio Code failed")
992+
runProcess(codeCommand.value ++ processArgs, wait = true)
997993
}.dependsOn(compile in Compile).evaluated
998994
)
999995

sbt-dotty/src/dotty/tools/sbtplugin/DottyIDEPlugin.scala

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sbt.Keys._
55
import java.io._
66
import java.lang.ProcessBuilder
77
import scala.collection.mutable
8+
import scala.util.Properties.{ isWin, isMac }
89

910
import dotty.tools.languageserver.config.ProjectConfig
1011

@@ -123,9 +124,24 @@ object DottyIDEPlugin extends AutoPlugin {
123124
runTask(joinedTask, state)
124125
}
125126

127+
def runProcess(cmd: Seq[String], wait: Boolean = false, directory: File = null): Unit = {
128+
val pb0 = new ProcessBuilder(cmd: _*).inheritIO()
129+
val pb = if (directory != null) pb0.directory(directory) else pb0
130+
if (wait) {
131+
val exitCode = pb.start().waitFor()
132+
if (exitCode != 0) {
133+
val cmdString = cmd.mkString(" ")
134+
throw new MessageOnlyException("""Running command "${cmdString}" failed.""")
135+
}
136+
}
137+
else
138+
pb.start()
139+
}
140+
126141
private val projectConfig = taskKey[Option[ProjectConfig]]("")
127142

128143
object autoImport {
144+
val codeCommand = taskKey[Seq[String]]("Command to start VSCode")
129145
val runCode = taskKey[Unit]("Start VSCode, usually called from launchIDE")
130146
val launchIDE = taskKey[Unit]("Configure and run VSCode on this project")
131147
}
@@ -203,17 +219,18 @@ object DottyIDEPlugin extends AutoPlugin {
203219
override def buildSettings: Seq[Setting[_]] = Seq(
204220
commands ++= Seq(configureIDE, compileForIDE),
205221

222+
codeCommand := {
223+
if (isWin)
224+
Seq("cmd.exe", "/C", "code", "-n")
225+
else if (isMac)
226+
Seq("open", "-a", "Visual Studio Code", "--args", "-n")
227+
else
228+
Seq("code", "-n")
229+
},
230+
206231
runCode := {
207-
val exitCode = new ProcessBuilder("code", "--install-extension", "lampepfl.dotty")
208-
.inheritIO()
209-
.start()
210-
.waitFor()
211-
if (exitCode != 0)
212-
throw new MessageOnlyException("Installing the Dotty support for VSCode failed")
213-
214-
new ProcessBuilder("code", baseDirectory.value.getAbsolutePath)
215-
.inheritIO()
216-
.start()
232+
runProcess(codeCommand.value ++ Seq("--install-extension", "lampepfl.dotty"), wait = true)
233+
runProcess(codeCommand.value ++ Seq("."), directory = baseDirectory.value)
217234
}
218235

219236
) ++ addCommandAlias("launchIDE", ";configureIDE;runCode")

vscode-dotty/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ To use this in your own Scala project, you must first get it to compile with
55
Dotty, please follow the instructions at https://github.com/lampepfl/dotty-example-project
66

77
## Starting Visual Studio Code from sbt
8-
First, make sure `code`, the binary for Visual Studio Code, is on your `$PATH`,
9-
this is the case if you can start the IDE by running `code` in a terminal.
10-
11-
If this is the case and your project succesfully compiles with dotty, you can
8+
Once your project succesfully compiles with dotty, you can
129
simply use the `launchIDE` command provided by the sbt-dotty plugin:
1310

1411
```shell

0 commit comments

Comments
 (0)