diff --git a/components/ide/jetbrains/backend-plugin/launch-dev-server.sh b/components/ide/jetbrains/backend-plugin/launch-dev-server.sh index 6e02a8996bae32..7c658fb4ac8717 100755 --- a/components/ide/jetbrains/backend-plugin/launch-dev-server.sh +++ b/components/ide/jetbrains/backend-plugin/launch-dev-server.sh @@ -39,4 +39,14 @@ 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 idea-cli, then overwrite environment variables initially defined by `components/ide/jetbrains/image/leeway.Dockerfile` +IDEA_CLI_DEV_PATH=/ide-desktop/bin/idea-cli-dev +(cd ../cli && go build -o $IDEA_CLI_DEV_PATH) +export EDITOR="$IDEA_CLI_DEV_PATH open" +export VISUAL="$EDITOR" +export GP_OPEN_EDITOR="$EDITOR" +export GIT_EDITOR="$EDITOR --wait" +export GP_PREVIEW_BROWSER="$IDEA_CLI_DEV_PATH preview" +export GP_EXTERNAL_BROWSER="$IDEA_CLI_DEV_PATH preview" + +$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..77c36c0d5958f0 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.ApplicationManager import com.intellij.openapi.client.ClientSession import com.intellij.openapi.client.ClientSessionsManager import com.intellij.openapi.diagnostic.thisLogger @@ -20,7 +21,7 @@ import org.jetbrains.ide.RestService import java.nio.file.InvalidPathException import java.nio.file.Path - +@Suppress("UnstableApiUsage") class GitpodCLIService : RestService() { override fun getServiceName() = SERVICE_NAME @@ -54,22 +55,37 @@ class GitpodCLIService : RestService() { } private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: (project: Project?) -> Unit): String? { + ApplicationManager.getApplication().executeOnPooledThread { + getClientSessionAndProjectAsync().let { + ClientId.withClientId(it.session.clientId) { + action(it.project) + sendOk(request, context) + } + } + } + return null + } + + private data class ClientSessionAndProject(val session: ClientSession, val project: Project?) + + private tailrec fun getClientSessionAndProjectAsync(): ClientSessionAndProject { 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) { + ClientSessionAndProject ( + session = session, + project = project + ) + } else { + Thread.sleep(1000L) + getClientSessionAndProjectAsync() } - ClientId.withClientId(session.clientId) { - action(project) - } - sendOk(request, context) - return null } private fun parseFilePath(path: String): Path? { @@ -88,4 +104,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..aa10341de094de 100644 --- a/components/ide/jetbrains/cli/cmd/root.go +++ b/components/ide/jetbrains/cli/cmd/root.go @@ -5,7 +5,11 @@ package cmd import ( + "errors" + "log" + "net/url" "os" + "strconv" "github.com/spf13/cobra" ) @@ -21,4 +25,16 @@ func Execute() { } } +func getCliApiUrl() *url.URL { + var backendPort = 63342 + if _, fileStatError := os.Stat("/ide-desktop/bin/idea-cli-dev"); !errors.Is(fileStatError, os.ErrNotExist) { + 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() {}