github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/cmd/gno/clean.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"io/fs"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/gnolang/gno/gnovm/pkg/gnoenv"
    13  	"github.com/gnolang/gno/gnovm/pkg/gnomod"
    14  	"github.com/gnolang/gno/tm2/pkg/commands"
    15  )
    16  
    17  type cleanCfg struct {
    18  	dryRun   bool // clean -n flag
    19  	verbose  bool // clean -x flag
    20  	modCache bool // clean -modcache flag
    21  }
    22  
    23  func newCleanCmd(io commands.IO) *commands.Command {
    24  	cfg := &cleanCfg{}
    25  
    26  	return commands.NewCommand(
    27  		commands.Metadata{
    28  			Name:       "clean",
    29  			ShortUsage: "clean [flags]",
    30  			ShortHelp:  "removes generated files and cached data",
    31  		},
    32  		cfg,
    33  		func(ctx context.Context, args []string) error {
    34  			return execClean(cfg, args, io)
    35  		},
    36  	)
    37  }
    38  
    39  func (c *cleanCfg) RegisterFlags(fs *flag.FlagSet) {
    40  	fs.BoolVar(
    41  		&c.dryRun,
    42  		"n",
    43  		false,
    44  		"print remove commands it would execute, but not run them",
    45  	)
    46  
    47  	fs.BoolVar(
    48  		&c.verbose,
    49  		"x",
    50  		false,
    51  		"print remove commands as it executes them",
    52  	)
    53  
    54  	fs.BoolVar(
    55  		&c.modCache,
    56  		"modcache",
    57  		false,
    58  		"remove the entire module download cache",
    59  	)
    60  }
    61  
    62  func execClean(cfg *cleanCfg, args []string, io commands.IO) error {
    63  	if len(args) > 0 {
    64  		return flag.ErrHelp
    65  	}
    66  
    67  	path, err := os.Getwd()
    68  	if err != nil {
    69  		return err
    70  	}
    71  	modDir, err := gnomod.FindRootDir(path)
    72  	if err != nil {
    73  		return fmt.Errorf("not a gno module: %w", err)
    74  	}
    75  
    76  	if path != modDir && (cfg.dryRun || cfg.verbose) {
    77  		io.Println("cd", modDir)
    78  	}
    79  	err = clean(modDir, cfg, io)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	if cfg.modCache {
    85  		modCacheDir := filepath.Join(gnoenv.HomeDir(), "pkg", "mod")
    86  		if !cfg.dryRun {
    87  			if err := os.RemoveAll(modCacheDir); err != nil {
    88  				return err
    89  			}
    90  		}
    91  		if cfg.dryRun || cfg.verbose {
    92  			io.Println("rm -rf", modCacheDir)
    93  		}
    94  	}
    95  	return nil
    96  }
    97  
    98  // clean removes generated files from a directory.
    99  func clean(dir string, cfg *cleanCfg, io commands.IO) error {
   100  	return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
   101  		if err != nil {
   102  			return err
   103  		}
   104  		if d.IsDir() {
   105  			return nil
   106  		}
   107  		// Ignore if not a generated file
   108  		if !strings.HasSuffix(path, ".gno.gen.go") && !strings.HasSuffix(path, ".gno.gen_test.go") {
   109  			return nil
   110  		}
   111  		if !cfg.dryRun {
   112  			if err := os.Remove(path); err != nil {
   113  				return err
   114  			}
   115  		}
   116  		if cfg.dryRun || cfg.verbose {
   117  			io.Println("rm", strings.TrimPrefix(path, dir+"/"))
   118  		}
   119  
   120  		return nil
   121  	})
   122  }