fun runAsync()

in hot-reload-gradle-plugin/src/main/kotlin/org/jetbrains/compose/reload/gradle/hotAsyncRunTasks.kt [182:225]


    fun runAsync() {
        /**
         * If the app is currently running, then we'll kill it before launching another instance.
         */
        if (pidFile.get().asFile.toPath().isRegularFile()) run pid@{
            val pidFileInfo = PidFileInfo(pidFile.get().asFile.toPath()).leftOr { return@pid }
            val pid = pidFileInfo.pid ?: return@pid
            val processHandle = ProcessHandle.of(pid).getOrNull() ?: return@pid
            logger.info("A previous run ($pid) still running, killing...")
            processHandle.destroyWithDescendants()
            processHandle.onExit().get(15, TimeUnit.SECONDS)
            logger.info("Previous run ($pid) killed")
        }

        pidFile.get().asFile.toPath().createParentDirectories()
        stdoutFile.get().asFile.toPath().createParentDirectories()
        stderrFile.get().asFile.toPath().createParentDirectories()

        val additionalJvmArguments = listOfNotNull(
            stdinFile.orNull?.asFile?.let { file -> "-D${HotReloadProperty.StdinFile.key}=${file.absolutePath}" },
            "-D${HotReloadProperty.StdoutFile.key}=${stdoutFile.get().asFile.absolutePath}",
            "-D${HotReloadProperty.StderrFile.key}=${stderrFile.get().asFile.absolutePath}",
            "-D${HotReloadProperty.LaunchMode.key}=${LaunchMode.Detached.name}",
            "-D${HotReloadProperty.MainClass.key}=${mainClass.get()}",
        ) + orchestrationListenerPortSystemProperties.orNull.orEmpty()
            .map { (key, value) -> "-D$key=$value" }


        val processBuilder = ProcessBuilder(
            javaBinary.get().asFile.absolutePath,
            *issueNewDebugSessionJvmArguments(name, intellijDebuggerDispatchPort),
            *additionalJvmArguments.toTypedArray(),
            "@${argFile.asFile.get().absolutePath}",
        ).apply { environment().putAll(orchestrationListenerPortEnvironmentVariables.orNull.orEmpty()) }
            .redirectOutput(stdoutFile.get().asFile)
            .redirectError(stderrFile.get().asFile)

        if (stdinFile.isPresent) {
            processBuilder.redirectInput(stdinFile.get().asFile)
        }

        val process = processBuilder.start()
        logger.quiet("Started '${mainClass.get()}' in background (${process.pid()})")
    }