Repository: goplus/gop
Pull request #2314 (2025-05-06 00:08:27)

add gox run command


Patch:
diff --git a/cl/classfile.go b/cl/classfile.go
index f33a50a8..0b44709f 100644
--- a/cl/classfile.go
+++ b/cl/classfile.go
@@ -205,36 +205,17 @@ var (
 	repl = strings.NewReplacer(":", "", "#", "", "-", "_", ".", "_")
 )
 
-func exClassNameAndExt(file string, keepMain bool) (name, clsfile, ext string) {
+func ClassNameAndExt(file string) (name, clsfile, ext string) {
 	fname := filepath.Base(file)
 	clsfile, ext = modfile.SplitFname(fname)
 	name = clsfile
 	if strings.ContainsAny(name, ":#-.") {
 		name = repl.Replace(name)
-	} else {
-		switch name {
-		case "main":
-			if !keepMain {
-				name = "_main"
-			}
-		case "init":
-			name = "_init"
-		}
 	}
 	return
 }
 
-// ClassNameAndExt func
-// TODO(xsw): to refactor
-//
-// Deprecated: Don't use it
-func ClassNameAndExt(file string) (name, clsfile, ext string) {
-	return exClassNameAndExt(file, true)
-}
-
 // GetFileClassType get ast.File classType
-//
-// Deprecated: Don't use it (to refactor)
 func GetFileClassType(file *ast.File, filename string, lookupClass func(ext string) (c *Project, ok bool)) (classType string, isTest bool) {
 	if file.IsClass {
 		var ext string
@@ -265,7 +246,7 @@ func isGoxTestFile(ext string) bool {
 }
 
 func loadClass(ctx *pkgCtx, pkg *gogen.Package, file string, f *ast.File, conf *Config) *gmxProject {
-	tname, clsfile, ext := exClassNameAndExt(file, true)
+	tname, clsfile, ext := ClassNameAndExt(file)
 	gt, ok := conf.LookupClass(ext)
 	if !ok {
 		panic("class not found: " + ext)
diff --git a/cl/compile.go b/cl/compile.go
index 20b3a69d..b3400f06 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -733,7 +733,10 @@ func preloadGopFile(p *gogen.Package, ctx *blockCtx, file string, f *ast.File, c
 	var parent = ctx.pkgCtx
 	if f.IsClass {
 		if f.IsNormalGox {
-			classType, _, _ = exClassNameAndExt(file, false)
+			classType, _, _ = ClassNameAndExt(file)
+			if classType == "main" {
+				classType = "_main"
+			}
 			if f.ShadowEntry != nil {
 				parent.goxMainClass = classType
 				parent.goxMain++
diff --git a/cmd/xgo/run_cmd.gox b/cmd/xgo/run_cmd.gox
new file mode 100644
index 00000000..e09cac95
--- /dev/null
+++ b/cmd/xgo/run_cmd.gox
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+
+ import (
+	self "github.com/goplus/gop/cmd/internal/run"
+)
+
+short "Compile and run a Go+ program"
+
+flagOff
+
+run args => {
+	self.Cmd.Run self.Cmd, args
+}


Test patch:
diff --git a/cl/_testspx/init/init.tspx b/cl/_testspx/init/init.tspx
deleted file mode 100644
index e69de29b..00000000
diff --git a/cl/_testspx/init/out.go b/cl/_testspx/init/out.go
deleted file mode 100644
index 56ae55c6..00000000
--- a/cl/_testspx/init/out.go
+++ /dev/null
@@ -1,20 +0,0 @@
-package main
-
-import "github.com/goplus/gop/cl/internal/spx"
-
-type _init struct {
-	spx.Sprite
-	*MyGame
-}
-type MyGame struct {
-	*spx.MyGame
-}
-
-func (this *MyGame) Main() {
-	spx.Gopt_MyGame_Main(this)
-}
-func (this *_init) Main() {
-}
-func main() {
-	new(MyGame).Main()
-}