2024-05-28 16:51:42 +03:00
|
|
|
import sbt.*
|
|
|
|
import sbt.Keys.TaskStreams
|
|
|
|
|
|
|
|
import scala.sys.process.*
|
|
|
|
|
|
|
|
object Ydoc {
|
|
|
|
|
2024-07-05 14:13:04 +03:00
|
|
|
private val pnpmCommand =
|
|
|
|
if (Platform.isWindows) "corepack.cmd pnpm" else "corepack pnpm"
|
2024-05-28 16:51:42 +03:00
|
|
|
|
|
|
|
/** 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] = {
|
2024-11-14 18:25:15 +03:00
|
|
|
val store = streams.cacheStoreFactory.make("ydoc-server-npm-compile-cache")
|
2024-05-28 16:51:42 +03:00
|
|
|
val generator = Tracked.inputChanged[Seq[File], Seq[File]](store) {
|
|
|
|
case (changed, _) =>
|
|
|
|
val resourceYdocServerJs =
|
2024-08-08 15:12:05 +03:00
|
|
|
ydocServerResourceManaged / "org" / "enso" / "ydoc" / "ydoc.cjs"
|
2024-05-28 16:51:42 +03:00
|
|
|
|
|
|
|
if (changed) {
|
2024-08-08 15:12:05 +03:00
|
|
|
val command = s"$pnpmCommand -r compile"
|
2024-05-28 16:51:42 +03:00
|
|
|
streams.log.info(command)
|
2024-11-14 18:25:15 +03:00
|
|
|
val exitCode = command ! streams.log
|
|
|
|
|
|
|
|
if (exitCode != 0) {
|
|
|
|
throw new CommandFailed(command, exitCode)
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:51:42 +03:00
|
|
|
val generatedYdocServerJs =
|
2024-08-08 15:12:05 +03:00
|
|
|
base / "app" / "ydoc-server-polyglot" / "dist" / "main.cjs"
|
2024-05-28 16:51:42 +03:00
|
|
|
IO.copyFile(generatedYdocServerJs, resourceYdocServerJs)
|
|
|
|
}
|
|
|
|
|
|
|
|
Seq(resourceYdocServerJs)
|
|
|
|
}
|
|
|
|
|
2024-11-14 18:25:15 +03:00
|
|
|
val sourceFiles: PathFinder =
|
|
|
|
(base / "app") ** ("*.js" | "*.ts" | "*.json" | "*.rs" | "*.toml")
|
|
|
|
val nodeModulesFiles =
|
|
|
|
(base / "app") ** "node_modules" ** "*"
|
|
|
|
val ideDesktopFiles =
|
|
|
|
(base / "app") ** "ide-desktop" ** "*"
|
|
|
|
|
|
|
|
val inputFiles = sourceFiles --- nodeModulesFiles --- ideDesktopFiles
|
|
|
|
|
|
|
|
generator(inputFiles.get())
|
2024-05-28 16:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-07-05 14:13:04 +03:00
|
|
|
val command = s"$pnpmCommand i --frozen-lockfile"
|
2024-05-28 16:51:42 +03:00
|
|
|
streams.log.info(command)
|
2024-11-14 18:25:15 +03:00
|
|
|
val exitCode = command ! streams.log
|
|
|
|
if (exitCode != 0) {
|
|
|
|
throw new CommandFailed(command, exitCode)
|
|
|
|
}
|
2024-05-28 16:51:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 18:25:15 +03:00
|
|
|
val inputFile = base / "pnpm-lock.json"
|
2024-05-28 16:51:42 +03:00
|
|
|
|
|
|
|
generator(inputFile)
|
|
|
|
}
|
2024-11-14 18:25:15 +03:00
|
|
|
|
|
|
|
final private class CommandFailed(command: String, exitCode: Int)
|
|
|
|
extends FeedbackProvidedException {
|
|
|
|
|
|
|
|
override def toString: String = {
|
|
|
|
s"Command [$command] failed with exit code [$exitCode]"
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 16:51:42 +03:00
|
|
|
}
|