Skip to content

Commit

Permalink
add openGoland
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 22, 2024
1 parent 1e1b941 commit c461c42
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
3 changes: 1 addition & 2 deletions cmd/xgo/test-explorer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/xgo/test-explorer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
</body>
<!--after body because document.body may be null-->
<!--available in CN and Global-->
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].4/index.js"></script>
<!-- <script src="http://127.0.0.1:8080/npm-publish/index.js"></script> -->
</html>
57 changes: 57 additions & 0 deletions cmd/xgo/test-explorer/open.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
3 changes: 1 addition & 2 deletions cmd/xgo/test-explorer/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 2 additions & 14 deletions cmd/xgo/test-explorer/test_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)).
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
31 changes: 31 additions & 0 deletions support/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"io"
"os"
"os/exec"
Expand All @@ -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
}
Expand All @@ -33,6 +35,11 @@ func Dir(dir string) *CmdBuilder {
dir: dir,
}
}
func Debug() *CmdBuilder {
return &CmdBuilder{
debug: true,
}
}

func New() *CmdBuilder {
return &CmdBuilder{}
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit c461c42

Please sign in to comment.