-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ide] Initialize a shared terminal on JetBrains IDEs when a workspace…
… starts
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...brains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodTerminalService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package io.gitpod.jetbrains.remote | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.application.runInEdt | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.wm.ToolWindowManager | ||
import com.intellij.openapi.wm.ex.ToolWindowManagerListener | ||
import com.intellij.remoteDev.util.onTerminationOrNow | ||
import com.intellij.util.application | ||
import com.jetbrains.rd.util.lifetime.Lifetime | ||
import com.jetbrains.rdserver.terminal.BackendTerminalManager | ||
import com.jetbrains.rdserver.unattendedHost.UnattendedHostManager | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.future.await | ||
import kotlinx.coroutines.launch | ||
import org.jetbrains.plugins.terminal.ShellTerminalWidget | ||
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory | ||
import org.jetbrains.plugins.terminal.TerminalView | ||
import java.util.concurrent.CompletableFuture | ||
|
||
@Suppress("UnstableApiUsage", "EXPERIMENTAL_IS_NOT_ENABLED", "OPT_IN_IS_NOT_ENABLED") | ||
@OptIn(DelicateCoroutinesApi::class) | ||
class GitpodTerminalService(private val project: Project) : Disposable { | ||
private val lifetime = Lifetime.Eternal.createNested() | ||
private val terminalView = TerminalView.getInstance(project) | ||
private val backendTerminalManager = BackendTerminalManager.getInstance(project) | ||
|
||
override fun dispose() { | ||
lifetime.terminate() | ||
} | ||
|
||
init { | ||
if (!application.isHeadlessEnvironment) { | ||
val job = launch() | ||
lifetime.onTerminationOrNow { job.cancel() } | ||
} | ||
} | ||
|
||
private fun launch() = GlobalScope.launch { | ||
getTerminalToolWindowRegisteredEvent().await() | ||
delayUntilControllerClientConnects() | ||
val widget = createNewSharedTerminal().await() | ||
printWelcomeMessage(widget) | ||
} | ||
|
||
private fun printWelcomeMessage(widget: ShellTerminalWidget) { | ||
widget.executeCommand( | ||
"clear; echo '\uD83D\uDC4B Welcome to Gitpod!\n" + | ||
"\t\t - Start by typing `gp --help` to see what you can do with Gitpod CLI.\n" + | ||
"\t\t - Run `gp tasks --help` to learn how to attach and watch tasks defined in .gitpod.yml!\n'; gp tasks attach" | ||
) | ||
} | ||
|
||
private fun getTerminalToolWindowRegisteredEvent(): CompletableFuture<Void> { | ||
val completableFuture = CompletableFuture<Void>() | ||
|
||
val messageBusConnection = project.messageBus.connect() | ||
|
||
val toolWindowManagerListener = object : ToolWindowManagerListener { | ||
override fun toolWindowsRegistered(ids: MutableList<String>, toolWindowManager: ToolWindowManager) { | ||
if (ids.contains(TerminalToolWindowFactory.TOOL_WINDOW_ID)) { | ||
completableFuture.complete(null) | ||
messageBusConnection.disconnect() | ||
} | ||
} | ||
} | ||
|
||
messageBusConnection.subscribe(ToolWindowManagerListener.TOPIC, toolWindowManagerListener) | ||
|
||
return completableFuture | ||
} | ||
|
||
private suspend fun delayUntilControllerClientConnects() { | ||
while (UnattendedHostManager.getInstance().controllerClientId == null) { | ||
delay(1000L) | ||
} | ||
} | ||
|
||
private fun createNewSharedTerminal(): CompletableFuture<ShellTerminalWidget> { | ||
val completableFuture = CompletableFuture<ShellTerminalWidget>() | ||
|
||
runInEdt { | ||
val shellTerminalWidget = terminalView.createLocalShellWidget(project.basePath, null) | ||
backendTerminalManager.shareTerminal(shellTerminalWidget, "Gitpod") | ||
completableFuture.complete(shellTerminalWidget) | ||
} | ||
|
||
return completableFuture | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters