Skip to content

Commit

Permalink
Make necessary changes before writing go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Jan 27, 2023
1 parent 4e4a535 commit 6f392ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
5 changes: 1 addition & 4 deletions cmd/gnodev/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ func runModDownload(opts *modFlags) error {
return err
}

gnoModPath, err := gnomod.GetGnoModPath()
if err != nil {
if err := gnomod.Sanitize(gnoMod); err != nil {
return err
}

gnomod.ReplaceModuleAll(gnoMod, gnoModPath)
err = gnomod.WriteGoMod(path, gnoMod)
if err != nil {
return err
Expand Down
50 changes: 34 additions & 16 deletions pkgs/gnomod/gnomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,6 @@ func WriteGoMod(absPath string, f *File) error {
return nil
}

// ReplaceModuleAll replaces all the required modules with
// the modules in given path.
func ReplaceModuleAll(f *File, path string) {
for _, req := range f.Require {
f.Replace = append(f.Replace, &modfile.Replace{
Old: module.Version{
Path: req.Mod.Path,
Version: req.Mod.Version,
},
New: module.Version{
Path: filepath.Join(path, req.Mod.Path),
},
})
}
}

func ResolvePrecompileName(gnoFilePath string) (tags, targetFilename string) {
nameNoExtension := strings.TrimSuffix(filepath.Base(gnoFilePath), ".gno")
switch {
Expand All @@ -190,3 +174,37 @@ func ResolvePrecompileName(gnoFilePath string) (tags, targetFilename string) {
}
return
}

// Sanitize make necessary modifications in the gno.mod
// before writing it to go.mod file.
func Sanitize(f *File) error {
gnoModPath, err := GetGnoModPath()
if err != nil {
return err
}

if strings.HasPrefix(f.Module.Mod.Path, "gno.land/r/") ||
strings.HasPrefix(f.Module.Mod.Path, "gno.land/p/demo/") {
f.Module.Mod.Path = "github.com/gnolang/gno/examples/" + f.Module.Mod.Path
}

for i := range f.Require {
path := f.Require[i].Mod.Path
if strings.HasPrefix(f.Require[i].Mod.Path, "gno.land/r/") ||
strings.HasPrefix(f.Require[i].Mod.Path, "gno.land/p/demo/") {
f.Require[i].Mod.Path = "github.com/gnolang/gno/examples/" + f.Require[i].Mod.Path
}

f.Replace = append(f.Replace, &modfile.Replace{
Old: module.Version{
Path: f.Require[i].Mod.Path,
Version: f.Require[i].Mod.Version,
},
New: module.Version{
Path: filepath.Join(gnoModPath, path),
},
})
}

return nil
}

0 comments on commit 6f392ca

Please sign in to comment.