Skip to content

Commit

Permalink
support .gsh
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Apr 26, 2024
1 parent 71e41ed commit c4deefe
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 23 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
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ go 1.16

require (
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00
github.com/goplus/gogen v1.15.1
github.com/goplus/gop v1.2.5
github.com/goplus/mod v0.13.9
github.com/goplus/gogen v1.15.2
github.com/goplus/gop v1.2.6
github.com/goplus/mod v0.13.10
github.com/goplus/reflectx v1.2.2
github.com/modern-go/reflect2 v1.0.2
github.com/peterh/liner v1.2.2
github.com/qiniu/x v1.13.9
github.com/qiniu/x v1.13.10
github.com/visualfc/funcval v0.1.4
github.com/visualfc/gid v0.1.0
github.com/visualfc/goembed v0.3.2
github.com/visualfc/xtype v0.2.0
golang.org/x/mod v0.16.0
golang.org/x/mod v0.17.0
golang.org/x/tools v0.19.0
)
22 changes: 11 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/gopherjs/gopherjs v0.0.0-20180708170036-38b413be4187/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/goplus/c2go v0.7.25/go.mod h1:e9oe4jDVhGFMJLEGmPSrVkLuXbLZAEmAu0/uD6fSz5E=
github.com/goplus/gogen v1.15.0/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gogen v1.15.1 h1:iz/fFpOeldjwmnjLzEdNsZF2mCf+sOHJavbAvV3o7sY=
github.com/goplus/gogen v1.15.1/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.5 h1:kBCcvJ7ONt3IqTUTNGb+n9JO1nwTp5STg1UUSEfvz/k=
github.com/goplus/gop v1.2.5/go.mod h1:5rLlryvlZhWLIBw1Rok8s0uvyO54vcYw/7sFLhqtNTc=
github.com/goplus/mod v0.13.9 h1:B9zZoHi2AzMltTSOFqZNVjqGlSMlhhNTWwEzVqhTQzg=
github.com/goplus/mod v0.13.9/go.mod h1:MibsLSftGmxaQq78YzUzNviyFwB9RtpMaoscufvEKH4=
github.com/goplus/c2go v0.7.26/go.mod h1:ePAStubV/ls8mmdPGQo6VfADTVd46rKuBemE4zzBDnA=
github.com/goplus/gogen v1.15.2 h1:Q6XaSx/Zi5tWnjfAziYsQI6Jv6MgODRpFtOYqNkiiqM=
github.com/goplus/gogen v1.15.2/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.6 h1:kog3c5Js+8EopqmI4+CwueXsqibnBwYVt5q5N7juRVY=
github.com/goplus/gop v1.2.6/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/goplus/reflectx v1.2.2 h1:T1p20OIH/HcnAvQQNnDLwl6AZOjU34icsfc6migD6L8=
github.com/goplus/reflectx v1.2.2/go.mod h1:wHOS9ilbB4zrecI0W1dMmkW9JMcpXV7VjALVbNU9xfM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand All @@ -26,8 +25,8 @@ github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qiniu/x v1.13.9 h1:OUcQZSze1oTO5pzrhsepTUvEb9K9WtOiqZomWffFGWE=
github.com/qiniu/x v1.13.9/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
github.com/qiniu/x v1.13.10/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down Expand Up @@ -56,8 +55,9 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
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()
}
`)
}
48 changes: 48 additions & 0 deletions pkg/github.com/qiniu/x/gsh/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// export by github.com/goplus/igop/cmd/qexp

package gsh

import (
q "github.com/qiniu/x/gsh"

"go/constant"
"reflect"

"github.com/goplus/igop"
)

func init() {
igop.RegisterPackage(&igop.Package{
Name: "gsh",
Path: "github.com/qiniu/x/gsh",
Deps: map[string]string{
"bytes": "bytes",
"errors": "errors",
"io": "io",
"os": "os",
"os/exec": "exec",
"strings": "strings",
},
Interfaces: map[string]reflect.Type{
"OS": reflect.TypeOf((*q.OS)(nil)).Elem(),
},
NamedTypes: map[string]reflect.Type{
"App": reflect.TypeOf((*q.App)(nil)).Elem(),
},
AliasTypes: map[string]reflect.Type{},
Vars: map[string]reflect.Value{
"Sys": reflect.ValueOf(&q.Sys),
},
Funcs: map[string]reflect.Value{
"Getenv": reflect.ValueOf(q.Getenv),
"Gopt_App_Main": reflect.ValueOf(q.Gopt_App_Main),
"Setenv__0": reflect.ValueOf(q.Setenv__0),
"Setenv__1": reflect.ValueOf(q.Setenv__1),
"Setenv__2": reflect.ValueOf(q.Setenv__2),
},
TypedConsts: map[string]igop.TypedConst{},
UntypedConsts: map[string]igop.UntypedConst{
"GopPackage": {"untyped bool", constant.MakeBool(bool(q.GopPackage))},

Check warning on line 45 in pkg/github.com/qiniu/x/gsh/export.go

View check run for this annotation

qiniu-x / golangci-lint

pkg/github.com/qiniu/x/gsh/export.go#L45

composites: github.com/goplus/igop.UntypedConst struct literal uses unkeyed fields (govet)
},
})
}
5 changes: 3 additions & 2 deletions rtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@ func (r *TypesLoader) ToType(rt reflect.Type) types.Type {
case reflect.Interface:
n := rt.NumMethod()
imethods = make([]*types.Func, n)
pkg := r.GetPackage(rt.PkgPath())
for i := 0; i < n; i++ {
im := rt.Method(i)
sig := typesDummySig
pkg := r.GetPackage(im.PkgPath)
imethods[i] = types.NewFunc(token.NoPos, pkg, im.Name, sig)
}
typ = types.NewInterfaceType(imethods, nil)
Expand Down Expand Up @@ -533,10 +533,11 @@ func (r *TypesLoader) ToType(rt reflect.Type) types.Type {
}
} else if kind == reflect.Interface {
n := rt.NumMethod()
pkg := named.Obj().Pkg()
pkg := r.GetPackage(rt.PkgPath())
recv := types.NewVar(token.NoPos, pkg, "", typ)
for i := 0; i < n; i++ {
im := rt.Method(i)
pkg := r.GetPackage(im.PkgPath)
sig := r.toMethod(pkg, recv, 0, im.Type)
imethods[i] = types.NewFunc(token.NoPos, pkg, im.Name, sig)
}
Expand Down

0 comments on commit c4deefe

Please sign in to comment.