diff --git a/components/ide/jetbrains/backend-plugin/launch-dev-server.sh b/components/ide/jetbrains/backend-plugin/launch-dev-server.sh index 6e02a8996bae32..53c8ae1b5dbc11 100755 --- a/components/ide/jetbrains/backend-plugin/launch-dev-server.sh +++ b/components/ide/jetbrains/backend-plugin/launch-dev-server.sh @@ -39,4 +39,7 @@ export IJ_HOST_SYSTEM_BASE_DIR=/workspace/.cache/JetBrains # Enable host status endpoint export CWM_HOST_STATUS_OVER_HTTP_TOKEN=gitpod -$TEST_BACKEND_DIR/bin/remote-dev-server.sh run $TEST_DIR \ No newline at end of file +# Build and move ide-cli to its respective place. +(cd ../cli && go build && mv cli /ide-desktop/bin/idea-cli) + +$TEST_BACKEND_DIR/bin/remote-dev-server.sh run $TEST_DIR diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt index a8fd8be4145cf6..2fd44ea938da83 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodCLIService.kt @@ -7,6 +7,7 @@ package io.gitpod.jetbrains.remote import com.intellij.codeWithMe.ClientId import com.intellij.ide.BrowserUtil import com.intellij.ide.CommandLineProcessor +import com.intellij.openapi.application.runInEdt import com.intellij.openapi.client.ClientSession import com.intellij.openapi.client.ClientSessionsManager import com.intellij.openapi.diagnostic.thisLogger @@ -16,11 +17,16 @@ import com.intellij.util.application import io.netty.channel.ChannelHandlerContext import io.netty.handler.codec.http.FullHttpRequest import io.netty.handler.codec.http.QueryStringDecoder +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import org.jetbrains.ide.RestService import java.nio.file.InvalidPathException import java.nio.file.Path - +@Suppress("OPT_IN_IS_NOT_ENABLED", "UnstableApiUsage") +@OptIn(DelicateCoroutinesApi::class) class GitpodCLIService : RestService() { override fun getServiceName() = SERVICE_NAME @@ -53,22 +59,33 @@ class GitpodCLIService : RestService() { return "invalid operation" } - private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: (project: Project?) -> Unit): String? { + private tailrec suspend fun getClientSessionAsync(): ClientSession { val project = getLastFocusedOrOpenedProject() var session: ClientSession? = null if (project != null) { - session = ClientSessionsManager.getProjectSessions(project, false).first() + session = ClientSessionsManager.getProjectSessions(project, false).firstOrNull() } if (session == null) { - session = ClientSessionsManager.getAppSessions(false).first() + session = ClientSessionsManager.getAppSessions(false).firstOrNull() } - if (session == null) { - return "no client" + return if (session != null) { + session + } else { + delay(1000L) + getClientSessionAsync() } - ClientId.withClientId(session.clientId) { - action(project) + } + + private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: (project: Project?) -> Unit): String? { + GlobalScope.launch { + val session = getClientSessionAsync() + runInEdt { + ClientId.withClientId(session.clientId) { + action(getLastFocusedOrOpenedProject()) + sendOk(request, context) + } + } } - sendOk(request, context) return null } @@ -88,4 +105,4 @@ class GitpodCLIService : RestService() { companion object { const val SERVICE_NAME = "gitpod/cli" } -} \ No newline at end of file +} diff --git a/components/ide/jetbrains/cli/cmd/open.go b/components/ide/jetbrains/cli/cmd/open.go index 8d637f42574bc7..e49a72fbef3792 100644 --- a/components/ide/jetbrains/cli/cmd/open.go +++ b/components/ide/jetbrains/cli/cmd/open.go @@ -8,7 +8,6 @@ import ( "fmt" "log" "net/http" - "net/url" "path/filepath" "github.com/spf13/cobra" @@ -25,10 +24,7 @@ var openCmd = &cobra.Command{ log.Fatal(err) } - url, err := url.Parse("http://localhost:63342/api/gitpod/cli") - if err != nil { - log.Fatal(err) - } + url := getCliApiUrl() query := url.Query() query.Add("op", "open") query.Add("file", file) diff --git a/components/ide/jetbrains/cli/cmd/preview.go b/components/ide/jetbrains/cli/cmd/preview.go index fc78be97fad3d3..77f89f47ebbbfc 100644 --- a/components/ide/jetbrains/cli/cmd/preview.go +++ b/components/ide/jetbrains/cli/cmd/preview.go @@ -7,7 +7,6 @@ package cmd import ( "log" "net/http" - "net/url" "github.com/spf13/cobra" ) @@ -15,10 +14,7 @@ import ( var previewCmd = &cobra.Command{ Use: "preview", Run: func(cmd *cobra.Command, args []string) { - url, err := url.Parse("http://localhost:63342/api/gitpod/cli") - if err != nil { - log.Fatal(err) - } + url := getCliApiUrl() query := url.Query() query.Add("op", "preview") query.Add("url", args[0]) diff --git a/components/ide/jetbrains/cli/cmd/root.go b/components/ide/jetbrains/cli/cmd/root.go index 9f44d048853be5..e6590c81893d1b 100644 --- a/components/ide/jetbrains/cli/cmd/root.go +++ b/components/ide/jetbrains/cli/cmd/root.go @@ -5,7 +5,10 @@ package cmd import ( + "log" + "net/url" "os" + "strconv" "github.com/spf13/cobra" ) @@ -21,4 +24,16 @@ func Execute() { } } +func getCliApiUrl() *url.URL { + var backendPort = 63342 + if os.Getenv("JB_DEV") == "true" { + backendPort = backendPort + 1 + } + parsedUrl, urlParseError := url.Parse("http://localhost:" + strconv.Itoa(backendPort) + "/api/gitpod/cli") + if urlParseError != nil { + log.Fatal(urlParseError) + } + return parsedUrl +} + func init() {}