From 6f392ca0ee478e99e275dbeaa61fdbdbb4f70ec8 Mon Sep 17 00:00:00 2001 From: Hariom Verma Date: Wed, 25 Jan 2023 23:35:13 +0530 Subject: [PATCH] Make necessary changes before writing go.mod --- cmd/gnodev/mod.go | 5 +---- pkgs/gnomod/gnomod.go | 50 +++++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/cmd/gnodev/mod.go b/cmd/gnodev/mod.go index b799ae9f99bc..0c35cdd5ae7c 100644 --- a/cmd/gnodev/mod.go +++ b/cmd/gnodev/mod.go @@ -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 diff --git a/pkgs/gnomod/gnomod.go b/pkgs/gnomod/gnomod.go index bee39b8d4a8a..f6dffb462b2c 100644 --- a/pkgs/gnomod/gnomod.go +++ b/pkgs/gnomod/gnomod.go @@ -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 { @@ -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 +}