From c461c42058601cbb49dbea130bfe8dc6e6a43fd1 Mon Sep 17 00:00:00 2001 From: xhd2015 Date: Wed, 22 May 2024 09:56:42 +0800 Subject: [PATCH] add openGoland --- cmd/xgo/runtime_gen/core/version.go | 4 +- cmd/xgo/test-explorer/config.go | 3 +- cmd/xgo/test-explorer/index.html | 2 +- cmd/xgo/test-explorer/open.go | 57 ++++++++++++++++++++++++++ cmd/xgo/test-explorer/session.go | 3 +- cmd/xgo/test-explorer/test_explorer.go | 16 +------- cmd/xgo/version.go | 4 +- runtime/core/version.go | 4 +- support/cmd/cmd.go | 31 ++++++++++++++ 9 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 cmd/xgo/test-explorer/open.go diff --git a/cmd/xgo/runtime_gen/core/version.go b/cmd/xgo/runtime_gen/core/version.go index 0ece866c..669d461e 100755 --- a/cmd/xgo/runtime_gen/core/version.go +++ b/cmd/xgo/runtime_gen/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.36" -const REVISION = "fce3dfc3c3587abde57944d6b16030437a9d8ce9+1" -const NUMBER = 228 +const REVISION = "1e1b9419279db2f75b8619256fced2e8a84e9ee7+1" +const NUMBER = 229 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/cmd/xgo/test-explorer/config.go b/cmd/xgo/test-explorer/config.go index 1da470c8..08a72109 100644 --- a/cmd/xgo/test-explorer/config.go +++ b/cmd/xgo/test-explorer/config.go @@ -30,7 +30,6 @@ func (c *TestConfig) CmdEnv() []string { return nil } var env []string - env = append(env, os.Environ()...) for k, v := range c.Env { env = append(env, fmt.Sprintf("%s=%s", k, fmt.Sprint(v))) } @@ -147,7 +146,7 @@ func parseConfigAndMergeOptions(configFile string, opts *Options) (*TestConfig, } func validateGoVersion(testConfig *TestConfig) error { - if testConfig.Go != nil || (testConfig.Go.Min == "" && testConfig.Go.Max == "") { + if testConfig == nil || testConfig.Go == nil || (testConfig.Go.Min == "" && testConfig.Go.Max == "") { return nil } // check go version diff --git a/cmd/xgo/test-explorer/index.html b/cmd/xgo/test-explorer/index.html index 29f4a012..36f2373e 100644 --- a/cmd/xgo/test-explorer/index.html +++ b/cmd/xgo/test-explorer/index.html @@ -20,6 +20,6 @@ - + \ No newline at end of file diff --git a/cmd/xgo/test-explorer/open.go b/cmd/xgo/test-explorer/open.go new file mode 100644 index 00000000..32d75fd6 --- /dev/null +++ b/cmd/xgo/test-explorer/open.go @@ -0,0 +1,57 @@ +package test_explorer + +import ( + "context" + "fmt" + "net/http" + "runtime" + "strconv" + + "github.com/xhd2015/xgo/support/cmd" + "github.com/xhd2015/xgo/support/netutil" +) + +func setupOpenHandler(server *http.ServeMux) { + server.HandleFunc("/openVscode", func(w http.ResponseWriter, r *http.Request) { + handleOpenFile(w, r, func(file string, line int) error { + if line > 0 { + return cmd.Debug().Run("code", "--goto", fmt.Sprintf("%s:%d", file, line)) + } + return cmd.Debug().Run("code", file) + }) + }) + server.HandleFunc("/openGoland", func(w http.ResponseWriter, r *http.Request) { + handleOpenFile(w, r, func(file string, line int) error { + // see https://www.jetbrains.com/help/go/working-with-the-ide-features-from-command-line.html#standalone + // and https://www.jetbrains.com/help/go/opening-files-from-command-line.html#macos + // open -na "GoLand.app" --args "$@" + var args []string + if line > 0 { + args = append(args, "--line", strconv.Itoa(line)) + } + args = append(args, file) + switch runtime.GOOS { + case "darwin": + return cmd.Debug().Run("open", append([]string{"-na", "GoLand.app", "--args"}, args...)...) + case "linux": + return cmd.Debug().Run("goland.sh", args...) + case "windows": + return cmd.Debug().Run("goland64.exe", args...) + default: + return fmt.Errorf("open goland not supported on %s", runtime.GOOS) + } + }) + }) +} + +func handleOpenFile(w http.ResponseWriter, r *http.Request, callback func(file string, line int) error) { + netutil.SetCORSHeaders(w) + netutil.HandleJSON(w, r, func(ctx context.Context, r *http.Request) (interface{}, error) { + q := r.URL.Query() + file := q.Get("file") + line := q.Get("line") + + lineNum, _ := strconv.Atoi(line) + return nil, callback(file, lineNum) + }) +} diff --git a/cmd/xgo/test-explorer/session.go b/cmd/xgo/test-explorer/session.go index 94c26070..87e5447d 100644 --- a/cmd/xgo/test-explorer/session.go +++ b/cmd/xgo/test-explorer/session.go @@ -269,9 +269,8 @@ func (c *session) Start() error { } testFlags := append([]string{"test", "-json"}, c.testFlags...) testFlags = append(testFlags, testArgs...) - fmt.Printf("%s %v\n", goCmd, testFlags) - err := cmd.Env(c.env).Dir(c.dir). + err := cmd.Debug().Env(c.env).Dir(c.dir). Stdout(io.MultiWriter(os.Stdout, w)). Run(goCmd, testFlags...) if err != nil { diff --git a/cmd/xgo/test-explorer/test_explorer.go b/cmd/xgo/test-explorer/test_explorer.go index b202f8a9..2840717e 100644 --- a/cmd/xgo/test-explorer/test_explorer.go +++ b/cmd/xgo/test-explorer/test_explorer.go @@ -259,18 +259,7 @@ func handle(opts *Options) error { }) setupSessionHandler(server, opts.ProjectDir, getTestConfig) - - server.HandleFunc("/openVscode", func(w http.ResponseWriter, r *http.Request) { - netutil.SetCORSHeaders(w) - netutil.HandleJSON(w, r, func(ctx context.Context, r *http.Request) (interface{}, error) { - q := r.URL.Query() - file := q.Get("file") - line := q.Get("line") - - err := cmd.Run("code", "--goto", fmt.Sprintf("%s:%s", file, line)) - return nil, err - }) - }) + setupOpenHandler(server) return netutil.ServePortHTTP(server, 7070, true, 500*time.Millisecond, func(port int) { url = fmt.Sprintf("http://localhost:%d", port) @@ -467,8 +456,7 @@ func run(req *RunRequest, goCmd string, env []string, testFlags []string) (*RunR goCmd = "go" } - fmt.Printf("test: %s %v\n", goCmd, args) - runErr := cmd.Dir(filepath.Dir(req.File)). + runErr := cmd.Debug().Dir(filepath.Dir(req.File)). Env(env). Stderr(io.MultiWriter(os.Stderr, &buf)). Stdout(io.MultiWriter(os.Stdout, &buf)). diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index a8e7b8a3..7744059f 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.36" -const REVISION = "fce3dfc3c3587abde57944d6b16030437a9d8ce9+1" -const NUMBER = 228 +const REVISION = "1e1b9419279db2f75b8619256fced2e8a84e9ee7+1" +const NUMBER = 229 func getRevision() string { revSuffix := "" diff --git a/runtime/core/version.go b/runtime/core/version.go index 0ece866c..669d461e 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.36" -const REVISION = "fce3dfc3c3587abde57944d6b16030437a9d8ce9+1" -const NUMBER = 228 +const REVISION = "1e1b9419279db2f75b8619256fced2e8a84e9ee7+1" +const NUMBER = 229 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/support/cmd/cmd.go b/support/cmd/cmd.go index a8177020..cc5a85d6 100644 --- a/support/cmd/cmd.go +++ b/support/cmd/cmd.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "io" "os" "os/exec" @@ -18,6 +19,7 @@ func Run(cmd string, args ...string) error { type CmdBuilder struct { env []string dir string + debug bool stdout io.Writer stderr io.Writer } @@ -33,6 +35,11 @@ func Dir(dir string) *CmdBuilder { dir: dir, } } +func Debug() *CmdBuilder { + return &CmdBuilder{ + debug: true, + } +} func New() *CmdBuilder { return &CmdBuilder{} @@ -56,6 +63,10 @@ func (c *CmdBuilder) Stderr(stderr io.Writer) *CmdBuilder { c.stderr = stderr return c } +func (c *CmdBuilder) Debug() *CmdBuilder { + c.debug = true + return c +} func (c *CmdBuilder) Output(cmd string, args ...string) (string, error) { return cmdExecEnv(cmd, args, c.env, c.dir, false, c) @@ -70,6 +81,26 @@ func cmdExec(cmd string, args []string, dir string, pipeStdout bool) (string, er return cmdExecEnv(cmd, args, nil, dir, pipeStdout, nil) } func cmdExecEnv(cmd string, args []string, env []string, dir string, useStdout bool, c *CmdBuilder) (string, error) { + if c != nil && c.debug { + var lines []string + if len(env) > 0 { + for _, e := range env { + lines = append(lines, "# "+e) + } + } + if dir != "" { + lines = append(lines, "# cd "+dir) + } + cmdStr := cmd + if len(args) > 0 { + cmdStr += " " + strings.Join(args, " ") + } + lines = append(lines, cmdStr) + for _, line := range lines { + fmt.Fprintln(os.Stderr, line) + } + } + execCmd := exec.Command(cmd, args...) if c != nil && c.stderr != nil { execCmd.Stderr = c.stderr