@@ -25,28 +25,30 @@ func GetGnoModPath() (string, error) {
2525 return filepath .Join (goPath , "pkg" , "gnomod" ), nil
2626}
2727
28- func writePackage (remote , basePath , pkgPath string ) error {
28+ func writePackage (remote , basePath , pkgPath string ) ( requirements [] string , err error ) {
2929 res , err := queryChain (remote , queryPathFile , []byte (pkgPath ))
3030 if err != nil {
31- return fmt .Errorf ("querychain: %w" , err )
31+ return nil , fmt .Errorf ("querychain: %w" , err )
3232 }
3333
3434 dirPath , fileName := std .SplitFilepath (pkgPath )
3535 if fileName == "" {
3636 // Is Dir
3737 // Create Dir if not exists
3838 dirPath := filepath .Join (basePath , dirPath )
39- if _ , err : = os .Stat (dirPath ); os .IsNotExist (err ) {
40- if err : = os .MkdirAll (dirPath , 0o755 ); err != nil {
41- return fmt .Errorf ("mkdir %q: %w" , dirPath , err )
39+ if _ , err = os .Stat (dirPath ); os .IsNotExist (err ) {
40+ if err = os .MkdirAll (dirPath , 0o755 ); err != nil {
41+ return nil , fmt .Errorf ("mkdir %q: %w" , dirPath , err )
4242 }
4343 }
4444
4545 files := strings .Split (string (res .Data ), "\n " )
4646 for _ , file := range files {
47- if err := writePackage (remote , basePath , filepath .Join (pkgPath , file )); err != nil {
48- return fmt .Errorf ("writepackage: %w" , err )
47+ reqs , err := writePackage (remote , basePath , filepath .Join (pkgPath , file ))
48+ if err != nil {
49+ return nil , fmt .Errorf ("writepackage: %w" , err )
4950 }
51+ requirements = append (requirements , reqs ... )
5052 }
5153 } else {
5254 // Is File
@@ -55,17 +57,21 @@ func writePackage(remote, basePath, pkgPath string) error {
5557 targetFilename , _ := gnolang .GetPrecompileFilenameAndTags (filePath )
5658 precompileRes , err := gnolang .Precompile (string (res .Data ), "" , fileName )
5759 if err != nil {
58- return fmt .Errorf ("precompile: %w" , err )
60+ return nil , fmt .Errorf ("precompile: %w" , err )
61+ }
62+
63+ for _ , i := range precompileRes .Imports {
64+ requirements = append (requirements , i .Path .Value )
5965 }
6066
6167 fileNameWithPath := filepath .Join (basePath , dirPath , targetFilename )
6268 err = os .WriteFile (fileNameWithPath , []byte (precompileRes .Translated ), 0o644 )
6369 if err != nil {
64- return fmt .Errorf ("writefile %q: %w" , fileNameWithPath , err )
70+ return nil , fmt .Errorf ("writefile %q: %w" , fileNameWithPath , err )
6571 }
6672 }
6773
68- return nil
74+ return removeDuplicateStr ( requirements ), nil
6975}
7076
7177// GnoToGoMod make necessary modifications in the gno.mod
@@ -76,9 +82,9 @@ func GnoToGoMod(f File) (*File, error) {
7682 return nil , err
7783 }
7884
79- if strings .HasPrefix (f .Module .Mod .Path , "gno.land/r/" ) ||
80- strings .HasPrefix (f .Module .Mod .Path , "gno.land/p/demo/" ) {
81- f .Module .Mod .Path = "github.com/ gnolang/gno /examples/" + f .Module .Mod .Path
85+ if strings .HasPrefix (f .Module .Mod .Path , gnolang . GnoRealmPkgsPrefixBefore ) ||
86+ strings .HasPrefix (f .Module .Mod .Path , gnolang . GnoPackagePrefixBefore ) {
87+ f .Module .Mod .Path = gnolang . ImportPrefix + " /examples/" + f .Module .Mod .Path
8288 }
8389
8490 for i := range f .Require {
@@ -89,9 +95,9 @@ func GnoToGoMod(f File) (*File, error) {
8995 }
9096 }
9197 path := f .Require [i ].Mod .Path
92- if strings .HasPrefix (f .Require [i ].Mod .Path , "gno.land/r/" ) ||
93- strings .HasPrefix (f .Require [i ].Mod .Path , "gno.land/p/demo/" ) {
94- f .Require [i ].Mod .Path = "github.com/ gnolang/gno /examples/" + f .Require [i ].Mod .Path
98+ if strings .HasPrefix (f .Require [i ].Mod .Path , gnolang . GnoRealmPkgsPrefixBefore ) ||
99+ strings .HasPrefix (f .Require [i ].Mod .Path , gnolang . GnoPackagePrefixBefore ) {
100+ f .Require [i ].Mod .Path = gnolang . ImportPrefix + " /examples/" + f .Require [i ].Mod .Path
95101 }
96102
97103 f .Replace = append (f .Replace , & modfile.Replace {
@@ -153,3 +159,14 @@ func isReplaced(module module.Version, repl []*modfile.Replace) (*module.Version
153159 }
154160 return nil , false
155161}
162+
163+ func removeDuplicateStr (str []string ) (res []string ) {
164+ m := make (map [string ]struct {}, len (str ))
165+ for _ , s := range str {
166+ if _ , ok := m [s ]; ! ok {
167+ m [s ] = struct {}{}
168+ res = append (res , s )
169+ }
170+ }
171+ return
172+ }
0 commit comments