enso/project/Ydoc.scala
Dmitry Bushev 858e646328
Start Ydoc with the language server (#9862)
- related #7954

Changelog:
- update: Ydoc starts with the language server on the `localhost:1234` by default. The hostname and ports can be configured by setting environment variables `LANGUAGE_SERVER_YDOC_HOSTNAME` and `LANGUAGE_SERVER_YDOC_PORT`
- update: by default `npm dev run` uses the node Ydoc server. You can control it with `POLYGLOT_YDOC_SERVER` env variable. For example,
```
env POLYGLOT_YDOC_SERVER='true' npm --workspace=enso-gui2 run dev
```
To connect to the Ydoc server running on the 1234 port (the one started with the language server)
⠀
```
env POLYGLOT_YDOC_SERVER='ws://127.0.0.1:1235' npm --workspace=enso-gui2 run dev
```
To connect to the provided URL. Can be useful for debugging when you start a separate Ydoc process.
- update: run `npm install` before the engine build. It is required to create the Ydoc JS bundle.
2024-05-28 13:51:42 +00:00

92 lines
3.0 KiB
Scala

import sbt.*
import sbt.Keys.TaskStreams
import scala.sys.process.*
object Ydoc {
private val npmCommand = if (Platform.isWindows) "npm.cmd" else "npm"
/** Generates the bundled JS source of the Ydoc server.
*
* @param base the path to the base directory of this build
* @param ydocServerBase the path to the base directory of the ydoc-server project
* @param ydocServerResourceManaged the paht to the managed resources directory
* @param streams the build streams
* @return the list of generated files
*/
def generateJsBundle(
base: File,
ydocServerBase: File,
ydocServerResourceManaged: File,
streams: TaskStreams
): Seq[File] = {
runNpmInstallCached(base, streams)
generateJsBundleCached(
base,
ydocServerBase,
ydocServerResourceManaged,
streams
)
}
/** Cached JS bundle generator. Invokes the `npm` build only the input files have been changed.
*
* @param base the path to the base directory of this build
* @param ydocServerBase the path to the base directory of the ydoc-server project
* @param ydocServerResourceManaged the path the managed resources directory
* @param streams the build streams
* @return the list of generated files
*/
private def generateJsBundleCached(
base: File,
ydocServerBase: File,
ydocServerResourceManaged: File,
streams: TaskStreams
): Seq[File] = {
val store = streams.cacheStoreFactory.make("ydoc-server-cache")
val generator = Tracked.inputChanged[Seq[File], Seq[File]](store) {
case (changed, _) =>
val resourceYdocServerJs =
ydocServerResourceManaged / "org" / "enso" / "ydoc" / "ydocServer.js"
if (changed) {
val command =
s"$npmCommand --workspace=enso-gui2 run build-ydoc-server-polyglot"
streams.log.info(command)
command ! streams.log
val generatedYdocServerJs =
ydocServerBase / "target" / "ydoc-server-bundle" / "assets" / "ydocServer.js"
IO.copyFile(generatedYdocServerJs, resourceYdocServerJs)
}
Seq(resourceYdocServerJs)
}
val ydocServerSrc = (base / "app" / "gui2" / "ydoc-server") ** "*.ts"
val sharedSrc = (base / "app" / "gui2" / "shared") ** "*.ts"
val buildCfg = (base / "app" / "gui2") * ("*.ts" || "*.json")
val inputFiles = ydocServerSrc +++ sharedSrc +++ buildCfg
generator(inputFiles.get)
}
private def runNpmInstallCached(base: File, streams: TaskStreams): Unit = {
val store = streams.cacheStoreFactory.make("ydoc-server-npm-install-cache")
val generator = Tracked.inputChanged[File, Unit](store) {
case (changed, _) =>
val nodeModules = base / "node_modules"
if (changed || !nodeModules.isDirectory) {
val command = s"$npmCommand install"
streams.log.info(command)
command ! streams.log
}
}
val inputFile = base / "package-lock.json"
generator(inputFile)
}
}