Skip to content

Commit

Permalink
gopbuild: support .gsh
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Apr 27, 2024
1 parent 9677e65 commit b9aeb7b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/internal/load/gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func BuildGopDir(ctx *igop.Context, path string) error {
}

func IsGopProject(dir string) bool {
return ContainsExt(dir, ".gop")
return ContainsExt(dir, ".gop", ".gox")
}
11 changes: 8 additions & 3 deletions cmd/internal/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ func IsDir(target string) (bool, error) {
return fi.IsDir(), nil
}

func ContainsExt(srcDir string, ext string) bool {
func ContainsExt(srcDir string, exts ...string) bool {
if f, err := os.Open(srcDir); err == nil {
defer f.Close()
fis, _ := f.Readdir(-1)
for _, fi := range fis {
if !fi.IsDir() && filepath.Ext(fi.Name()) == ext {
return true
if !fi.IsDir() {
ext := filepath.Ext(fi.Name())
for _, v := range exts {
if v == ext {
return true
}
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion gopbuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package gopbuild
//go:generate go run ../cmd/qexp -outdir ../pkg github.com/goplus/gop/builtin/ng
//go:generate go run ../cmd/qexp -outdir ../pkg github.com/goplus/gop/builtin/iox
//go:generate go run ../cmd/qexp -outdir ../pkg github.com/qiniu/x/errors
//go:generate go run ../cmd/qexp -outdir ../pkg github.com/qiniu/x/gsh

import (
"bytes"
Expand All @@ -37,16 +38,23 @@ import (
"github.com/goplus/mod/modfile"

_ "github.com/goplus/igop/pkg/bufio"
_ "github.com/goplus/igop/pkg/context"
_ "github.com/goplus/igop/pkg/errors"
_ "github.com/goplus/igop/pkg/fmt"
_ "github.com/goplus/igop/pkg/github.com/goplus/gop/builtin"
_ "github.com/goplus/igop/pkg/github.com/goplus/gop/builtin/iox"
_ "github.com/goplus/igop/pkg/github.com/goplus/gop/builtin/ng"
_ "github.com/goplus/igop/pkg/github.com/qiniu/x/errors"
_ "github.com/goplus/igop/pkg/github.com/qiniu/x/gsh"
_ "github.com/goplus/igop/pkg/io"
_ "github.com/goplus/igop/pkg/log"
_ "github.com/goplus/igop/pkg/math"
_ "github.com/goplus/igop/pkg/math/big"
_ "github.com/goplus/igop/pkg/math/bits"
_ "github.com/goplus/igop/pkg/os"
_ "github.com/goplus/igop/pkg/os/exec"
_ "github.com/goplus/igop/pkg/path/filepath"
_ "github.com/goplus/igop/pkg/runtime"
_ "github.com/goplus/igop/pkg/strconv"
_ "github.com/goplus/igop/pkg/strings"
)
Expand Down Expand Up @@ -76,7 +84,9 @@ func init() {
cl.SetDebug(cl.FlagNoMarkAutogen)
igop.RegisterFileProcess(".gop", BuildFile)
igop.RegisterFileProcess(".gox", BuildFile)
igop.RegisterFileProcess(".gsh", BuildFile)
RegisterClassFileType(".gmx", "Game", []*Class{{Ext: ".spx", Class: "Sprite"}}, "github.com/goplus/spx", "math")
RegisterClassFileType(".gsh", "App", nil, "github.com/qiniu/x/gsh", "math")
}

func BuildFile(ctx *igop.Context, filename string, src interface{}) (data []byte, err error) {
Expand Down Expand Up @@ -151,7 +161,7 @@ type Context struct {
func ClassKind(fname string) (isProj, ok bool) {
ext := modfile.ClassExt(fname)
switch ext {
case ".gmx":
case ".gmx", ".gsh":
return true, true
case ".spx":
return fname == "main.spx", true
Expand Down
43 changes: 43 additions & 0 deletions gopbuild/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,46 @@ func main() {
}
`)
}

func TestGsh(t *testing.T) {
gopClTestEx(t, "exec.gsh", `
gop "run", "./foo"
exec "gop run ./foo"
exec "FOO=100 gop run ./foo"
exec {"FOO": "101"}, "gop", "run", "./foo"
exec "gop", "run", "./foo"
exec "ls $HOME"
ls "${HOME}"
`, `package main
import "github.com/qiniu/x/gsh"
type exec struct {
gsh.App
}
//line exec.gsh:2
func (this *exec) MainEntry() {
//line exec.gsh:2:1
this.Gop_Exec("gop", "run", "./foo")
//line exec.gsh:3:1
this.Exec__1("gop run ./foo")
//line exec.gsh:4:1
this.Exec__1("FOO=100 gop run ./foo")
//line exec.gsh:5:1
this.Exec__0(map[string]string{"FOO": "101"}, "gop", "run", "./foo")
//line exec.gsh:6:1
this.Exec__2("gop", "run", "./foo")
//line exec.gsh:7:1
this.Exec__1("ls $HOME")
//line exec.gsh:8:1
this.Gop_Exec("ls", this.Gop_Env("HOME"))
}
func (this *exec) Main() {
gsh.Gopt_App_Main(this)
}
func main() {
new(exec).Main()
}
`)
}

0 comments on commit b9aeb7b

Please sign in to comment.