@@ -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
7182func (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 \n replace 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
114120func (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
124130func (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}
0 commit comments