Skip to content

Commit 37b3569

Browse files
authored
Merge pull request #747 from JiepengTan/pr_support_ai_pack
Spx cmd tool supports custom Go modules
2 parents 688177b + a07f199 commit 37b3569

6 files changed

Lines changed: 59 additions & 71 deletions

File tree

cmd/gox/pkg/command/build.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ func (pself *CmdTool) BuildDll() error {
8585
}
8686
func (pself *CmdTool) genGo() string {
8787
rawdir, _ := os.Getwd()
88-
projectDir, _ := filepath.Abs(pself.ProjectDir)
8988
spxProjPath, _ := filepath.Abs(pself.ProjectDir + "/..")
9089

90+
// Generate code in spx project root directory
9191
os.Chdir(spxProjPath)
9292
envVars := []string{""}
9393
tagStr := ""
@@ -100,10 +100,16 @@ func (pself *CmdTool) genGo() string {
100100
} else {
101101
util.RunXGo(envVars, "go", tagStr)
102102
}
103+
104+
// Re-add replace directive if in spx development environment
105+
// Copy generated file to project/go/main.go
103106
os.MkdirAll(pself.GoDir, 0755)
104107
os.Rename(path.Join(spxProjPath, "xgo_autogen.go"), path.Join(pself.GoDir, "main.go"))
105-
os.Chdir(projectDir)
108+
109+
// Run go mod tidy in root directory
110+
os.Chdir(spxProjPath)
106111
util.RunGolang(nil, "mod", "tidy")
112+
107113
os.Chdir(rawdir)
108114
return tagStr
109115
}

cmd/gox/pkg/command/env.go

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,60 +60,66 @@ func (pself *CmdTool) PrepareEnv(fsRelDir, dstDir string) {
6060
// Handle go.mod file adaptively
6161
pself.adaptGoMod()
6262

63+
// create a temp go file to run go mod tidy
64+
tempFile, _ := filepath.Abs(path.Join(pself.TargetDir, "xgo_autogen.go"))
65+
if _, err := os.Stat(tempFile); os.IsNotExist(err) {
66+
tmp := `
67+
package main
68+
import "github.com/goplus/spx/v2"
69+
func main() {print(&spx.Game{})}
70+
`
71+
os.WriteFile(tempFile, []byte(tmp), 0644)
72+
}
6373
rawDir, _ := os.Getwd()
64-
os.Chdir(pself.GoDir)
74+
os.Chdir(pself.TargetDir)
6575
util.RunGolang(nil, "mod", "tidy")
66-
76+
// delete temp go file
77+
os.Remove(tempFile)
6778
os.Chdir(rawDir)
6879
}
6980

7081
// adaptGoMod adapts the go.mod file for different directory structures
7182
func (pself *CmdTool) adaptGoMod() {
72-
projDir := path.Join(pself.GoDir, "../")
73-
goModPath := path.Join(projDir, "go.mod")
74-
// Check if go.mod exists
75-
if _, err := os.Stat(goModPath); os.IsNotExist(err) {
76-
return
77-
}
78-
79-
// Read go.mod content
80-
content, err := os.ReadFile(goModPath)
81-
if err != nil {
82-
return
83+
// Check if go.mod exists in project root
84+
rootGoModPath, _ := filepath.Abs(path.Join(pself.TargetDir, "go.mod"))
85+
if _, err := os.Stat(rootGoModPath); os.IsNotExist(err) {
86+
// No go.mod in root, create one
87+
pself.createDefaultGoMod(pself.TargetDir, false)
8388
}
84-
85-
strContent := string(content)
86-
87-
// Check if we're in a spx directory by looking for spx/gop.mod
88-
currentDir, _ := os.Getwd()
89-
spxPath := pself.findSpxRoot(currentDir)
89+
// Check if we need to add replace directive for local spx development
90+
absTargetDir, _ := filepath.Abs(pself.TargetDir)
91+
spxPath := pself.findSpxRoot(absTargetDir)
9092

9193
if spxPath != "" {
92-
// We found spx root, calculate relative path
93-
relPath, err := filepath.Rel(projDir, spxPath)
94+
// We're in spx development environment, add replace directive
95+
content, err := os.ReadFile(rootGoModPath)
9496
if err != nil {
95-
relPath = "../../../" // fallback to original
97+
return
9698
}
9799

98-
// Update the replace directive
99-
oldPattern := `replace github.com/goplus/spx/v2 => ../../../`
100-
newReplace := fmt.Sprintf("replace github.com/goplus/spx/v2 => %s", relPath)
101-
replacedContent := strings.ReplaceAll(strContent, oldPattern, newReplace)
100+
strContent := string(content)
101+
// Check if replace directive already exists
102+
if !strings.Contains(strContent, "replace github.com/goplus/spx/v2") {
103+
// Calculate relative path from project to spx root
104+
relPath, err := filepath.Rel(absTargetDir, spxPath)
105+
if err != nil {
106+
return
107+
}
102108

103-
// Write back the modified content
104-
os.WriteFile(goModPath, []byte(replacedContent), 0644)
105-
} else {
106-
// create default go mod file if not exist
107-
pself.createDefaultGoMod(pself.TargetDir, false)
108-
// copy root mod to project dir
109-
util.CopyFile(path.Join(pself.TargetDir, "go.mod"), path.Join(projDir, "go.mod"))
109+
// Add replace directive
110+
replaceDir := fmt.Sprintf("\n\nreplace github.com/goplus/spx/v2 => %s\n", relPath)
111+
strContent += replaceDir
112+
113+
// Write back the modified content
114+
os.WriteFile(rootGoModPath, []byte(strContent), 0644)
115+
}
110116
}
111117
}
112118

113119
// createDefaultGoMod ensures gop.mod exists if not in spx directory
114120
func (pself *CmdTool) createDefaultGoMod(dir string, forceWrite bool) {
115121
// Not in spx directory, create gop.mod in target directory
116-
gopModPath := path.Join(dir, "go.mod")
122+
gopModPath, _ := filepath.Abs(path.Join(dir, "go.mod"))
117123
if _, err := os.Stat(gopModPath); os.IsNotExist(err) || forceWrite {
118124
gopModContent := pself.GoModTemplate
119125
os.WriteFile(gopModPath, []byte(gopModContent), 0644)
@@ -122,7 +128,7 @@ func (pself *CmdTool) createDefaultGoMod(dir string, forceWrite bool) {
122128

123129
// findSpxRoot finds the spx root directory by looking for gop.mod
124130
func (pself *CmdTool) findSpxRoot(startDir string) string {
125-
currentDir := startDir
131+
currentDir := filepath.Dir(startDir)
126132
for {
127133
gopModPath := path.Join(currentDir, "gop.mod")
128134
if _, err := os.Stat(gopModPath); err == nil {
@@ -332,6 +338,15 @@ func (pself *CmdTool) Clear() error {
332338
// Only return an error if the file exists and couldn't be removed
333339
return fmt.Errorf("failed to remove gitignore file: %w", err)
334340
}
341+
// Remove go.mod
342+
if err := os.RemoveAll(path.Join(pself.TargetDir, "go.sum")); err != nil {
343+
return fmt.Errorf("failed to remove project directory: %w", err)
344+
}
345+
346+
// Remove go.mod
347+
if err := os.RemoveAll(path.Join(pself.TargetDir, "xgo_autogen.go")); err != nil {
348+
return fmt.Errorf("failed to remove project directory: %w", err)
349+
}
335350

336351
return nil
337352
}

cmd/gox/template/go.mod.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module github.com/goplus/spxdemo
33

44
go 1.23.0
55

6-
require github.com/goplus/spx/v2 v2.0.0-pre.1 //gop:class
6+
require github.com/goplus/spx/v2 v2.0.0-pre.2 //gop:class
77

cmd/gox/template/project/go.mod.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

cmd/gox/template/project/go.sum

Whitespace-only changes.

cmd/gox/template/project/main.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)