Skip to content

Commit

Permalink
Wait the Backend Plugin to be ready and the Controller Client to be c…
Browse files Browse the repository at this point in the history
…onnected before executing "gp preview" on JetBrains IDEs
  • Loading branch information
felladrin committed May 26, 2022
1 parent 308601a commit 8c8cdb5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
12 changes: 11 additions & 1 deletion components/ide/jetbrains/backend-plugin/launch-dev-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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? {
Expand All @@ -88,4 +104,4 @@ class GitpodCLIService : RestService() {
companion object {
const val SERVICE_NAME = "gitpod/cli"
}
}
}
6 changes: 1 addition & 5 deletions components/ide/jetbrains/cli/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"path/filepath"

"github.com/spf13/cobra"
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions components/ide/jetbrains/cli/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ package cmd
import (
"log"
"net/http"
"net/url"

"github.com/spf13/cobra"
)

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])
Expand Down
16 changes: 16 additions & 0 deletions components/ide/jetbrains/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
package cmd

import (
"errors"
"log"
"net/url"
"os"
"strconv"

"github.com/spf13/cobra"
)
Expand All @@ -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() {}

0 comments on commit 8c8cdb5

Please sign in to comment.