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 25, 2022
1 parent b053dae commit 6bbdd06
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
5 changes: 4 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,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
# 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
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.runInEdt
import com.intellij.openapi.client.ClientSession
import com.intellij.openapi.client.ClientSessionsManager
import com.intellij.openapi.diagnostic.thisLogger
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -88,4 +105,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
15 changes: 15 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,10 @@
package cmd

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

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

0 comments on commit 6bbdd06

Please sign in to comment.