generate

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cobra.Command{
	Use:   "generate",
	Short: "generate gone loading code and import code",
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		if len(scanDirs) == 0 {
			return errors.New("scan dir is empty")
		}

		cwd, _ := os.Getwd()
		fmt.Printf("current work dir: %s\n", cwd)

		for i := range scanDirs {
			absolutePath, err := filepath.Abs(scanDirs[i])
			if err != nil {
				return err
			}
			scanDirs[i] = absolutePath
		}

		if mainPackageDir == "" {
			mainPackageDir, err = findFirstMainPackageDir(scanDirs)
			if err != nil {
				return err
			}
		}

		fmt.Printf("main package dir: %s\n", mainPackageDir)
		fmt.Printf("scan dirs: %v\n", scanDirs)

		moduleInfo, err := utils.FindModuleInfo(mainPackageDir)
		if err != nil {
			return err
		}

		var needImportPackages []string

		for _, dir := range scanDirs {
			packages, err := scanDirGenCode(dir, moduleInfo, mainPackageDir)
			if err != nil {
				return err
			}

			needImportPackages = append(needImportPackages, packages...)
		}

		if len(needImportPackages) > 0 {
			return genImportCode(mainPackageDir, needImportPackages)
		}

		err = os.Chdir(moduleInfo.ModulePath)
		if err != nil {
			return err
		}

		return utils.Command("go", []string{"mod", "tidy"})
	},
}

Functions

This section is empty.

Types

type ModuleInfo

type ModuleInfo struct {
	ModuleName string
	ModulePath string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL