@@ -56,12 +56,93 @@ func (cmd *CmdTool) setupPaths(dstRelDir string) error {
5656// PrepareEnv prepares the environment for the command
5757func (pself * CmdTool ) PrepareEnv (fsRelDir , dstDir string ) {
5858 util .CopyDir (pself .ProjectFS , fsRelDir , dstDir , false )
59+
60+ // Handle go.mod file adaptively
61+ pself .adaptGoMod ()
62+
5963 rawDir , _ := os .Getwd ()
6064 os .Chdir (pself .GoDir )
6165 util .RunGolang (nil , "mod" , "tidy" )
66+
6267 os .Chdir (rawDir )
6368}
6469
70+ // adaptGoMod adapts the go.mod file for different directory structures
71+ 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+ }
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 )
90+
91+ if spxPath != "" {
92+ // We found spx root, calculate relative path
93+ relPath , err := filepath .Rel (projDir , spxPath )
94+ if err != nil {
95+ relPath = "../../../" // fallback to original
96+ }
97+
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 )
102+
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" ))
110+ }
111+ }
112+
113+ // createDefaultGoMod ensures gop.mod exists if not in spx directory
114+ func (pself * CmdTool ) createDefaultGoMod (dir string , forceWrite bool ) {
115+ // Not in spx directory, create gop.mod in target directory
116+ gopModPath := path .Join (dir , "go.mod" )
117+ if _ , err := os .Stat (gopModPath ); os .IsNotExist (err ) || forceWrite {
118+ gopModContent := pself .GoModTemplate
119+ os .WriteFile (gopModPath , []byte (gopModContent ), 0644 )
120+ }
121+ }
122+
123+ // findSpxRoot finds the spx root directory by looking for gop.mod
124+ func (pself * CmdTool ) findSpxRoot (startDir string ) string {
125+ currentDir := startDir
126+ for {
127+ gopModPath := path .Join (currentDir , "gop.mod" )
128+ if _ , err := os .Stat (gopModPath ); err == nil {
129+ // Check if this gop.mod contains spx project definition
130+ content , err := os .ReadFile (gopModPath )
131+ if err == nil && strings .Contains (string (content ), "github.com/goplus/spx/v2" ) {
132+ return currentDir
133+ }
134+ }
135+
136+ parent := filepath .Dir (currentDir )
137+ if parent == currentDir {
138+ // Reached root directory
139+ break
140+ }
141+ currentDir = parent
142+ }
143+ return ""
144+ }
145+
65146// SetupEnv sets up the environment for the command
66147func (pself * CmdTool ) SetupEnv (version string , fs embed.FS , fsRelDir string , projectRelPath string ) (err error ) {
67148 // Update the CmdTool struct fields
0 commit comments