From 4e5327198796c9beedb84aeaa41ac5ae22e8f44b Mon Sep 17 00:00:00 2001 From: LyricTian Date: Tue, 12 Dec 2023 20:12:05 +0800 Subject: [PATCH] feat: Add generate frontend project --- Makefile | 2 +- cmd/new.go | 28 ++++++++++++ internal/actions/new.go | 96 +++++++++++++++++++++++++++++++++++++++-- main.go | 4 +- 4 files changed, 124 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index e5ff925..166f441 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: build -RELEASE_VERSION = v10.1.0 +RELEASE_VERSION = v10.2.0 APP = gin-admin-cli BIN = ${APP} diff --git a/cmd/new.go b/cmd/new.go index af69bbc..4ed90bc 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -24,6 +24,10 @@ func New() *cli.Command { Usage: "The project name", Required: true, }, + &cli.StringFlag{ + Name: "app-name", + Usage: "The application name (default: project name)", + }, &cli.StringFlag{ Name: "desc", Usage: "The project description", @@ -46,16 +50,40 @@ func New() *cli.Command { Usage: "Use git branch to initialize the project (default: main)", Value: "main", }, + &cli.StringFlag{ + Name: "fe-dir", + Usage: "The frontend directory to generate the project (if empty, the frontend project will not be generated)", + }, + &cli.StringFlag{ + Name: "fe-name", + Usage: "The frontend project name (default: frontend)", + Value: "frontend", + }, + &cli.StringFlag{ + Name: "fe-git-url", + Usage: "Use git repository to initialize the frontend project (default: https://github.com/gin-admin/gin-admin-frontend.git)", + Value: "https://github.com/gin-admin/gin-admin-frontend.git", + }, + &cli.StringFlag{ + Name: "fe-git-branch", + Usage: "Use git branch to initialize the frontend project (default: main)", + Value: "main", + }, }, Action: func(c *cli.Context) error { n := actions.New(actions.NewConfig{ Dir: c.String("dir"), Name: c.String("name"), + AppName: c.String("app-name"), Description: c.String("desc"), PkgName: c.String("pkg"), Version: c.String("version"), GitURL: c.String("git-url"), GitBranch: c.String("git-branch"), + FeDir: c.String("fe-dir"), + FeName: c.String("fe-name"), + FeGitURL: c.String("fe-git-url"), + FeGitBranch: c.String("fe-git-branch"), }) return n.Run(context.Background()) }, diff --git a/internal/actions/new.go b/internal/actions/new.go index 518a00b..72718df 100644 --- a/internal/actions/new.go +++ b/internal/actions/new.go @@ -18,6 +18,9 @@ const ( ) func New(cfg NewConfig) *NewAction { + if cfg.AppName == "" { + cfg.AppName = strings.ToLower(strings.ReplaceAll(cfg.Name, "-", "")) + } if cfg.GitURL == "" { cfg.GitURL = defaultGitURL } @@ -28,7 +31,9 @@ func New(cfg NewConfig) *NewAction { cfg.Version = "v1.0.0" } if cfg.Description == "" { - cfg.Description = fmt.Sprintf("%s API service", cfg.Name) + name := []byte(cfg.AppName) + name[0] = name[0] - 32 + cfg.Description = fmt.Sprintf("%s API service", name) } return &NewAction{ @@ -40,11 +45,16 @@ func New(cfg NewConfig) *NewAction { type NewConfig struct { Dir string Name string + AppName string PkgName string Description string Version string GitURL string GitBranch string + FeDir string + FeName string + FeGitURL string + FeGitBranch string } type NewAction struct { @@ -67,7 +77,7 @@ func (a *NewAction) Run(ctx context.Context) error { return err } - cleanFiles := []string{".git", "CHANGELOG.md", "LICENSE", "README.md", "README_CN.md", "internal/swagger/v3", "internal/wirex/wire_gen.go"} + cleanFiles := []string{".git", "CHANGELOG.md", "LICENSE", "README.md", "README_CN.md", "internal/swagger/v3", "internal/wirex/wire_gen.go", "swagger.jpeg"} for _, f := range cleanFiles { if err := os.RemoveAll(filepath.Join(projectDir, f)); err != nil { return err @@ -84,7 +94,7 @@ func (a *NewAction) Run(ctx context.Context) error { return err } - appName := strings.ToLower(strings.ReplaceAll(a.cfg.Name, "-", "")) + appName := a.cfg.AppName err = filepath.WalkDir(projectDir, func(path string, d fs.DirEntry, err error) error { if err != nil { return err @@ -148,6 +158,11 @@ func (a *NewAction) Run(ctx context.Context) error { fmt.Printf("cd %s\n", projectDir) fmt.Println("make start") fmt.Println("------------------------------------------------------------") + + if err := a.generateFE(ctx); err != nil { + return err + } + return nil } @@ -224,3 +239,78 @@ func (a *NewAction) getReadme() string { return sb.String() } + +func (a *NewAction) generateFE(ctx context.Context) error { + if a.cfg.FeDir == "" { + return nil + } + + a.logger.Infof("Create frontend project %s in %s", a.cfg.FeName, a.cfg.FeDir) + feDir, err := filepath.Abs(filepath.Join(a.cfg.FeDir, a.cfg.FeName)) + if err != nil { + return err + } + + if exists, err := utils.ExistsFile(feDir); err != nil { + return err + } else if exists { + a.logger.Warnf("Frontend project %s already exists", a.cfg.FeName) + return nil + } + + _ = os.MkdirAll(a.cfg.FeDir, os.ModePerm) + err = utils.ExecGitClone(a.cfg.FeDir, a.cfg.FeGitURL, a.cfg.FeGitBranch, a.cfg.FeName) + if err != nil { + return err + } + + cleanFiles := []string{".git", "LICENSE", "README.md", "demo.png"} + for _, file := range cleanFiles { + if err := os.RemoveAll(filepath.Join(feDir, file)); err != nil { + return err + } + } + + err = utils.WriteFile(filepath.Join(feDir, "README.md"), []byte(a.getFeReadme())) + if err != nil { + return err + } + + a.logger.Infof("🎉 Frontend project %s has been created successfully", a.cfg.FeName) + fmt.Println("------------------------------------------------------------") + fmt.Printf("Git repository: %s\n", a.cfg.FeGitURL) + fmt.Printf("Branch: %s\n", a.cfg.FeGitBranch) + fmt.Printf("Directory: %s\n", feDir) + fmt.Println("------------------------------------------------------------") + + return nil +} + +func (a *NewAction) getFeReadme() string { + var sb strings.Builder + sb.WriteString("# " + a.cfg.FeName + "\n\n") + sb.WriteString("> " + a.cfg.Description + "\n\n") + + sb.WriteString("## Environment Prepare\n\n") + sb.WriteString("> You can use [nvm](https://github.com/nvm-sh/nvm) to manage node version.\n\n") + sb.WriteString("- Node.js v16.20.2\n\n") + + sb.WriteString("## Quick Start\n\n") + + sb.WriteString("### Install dependencies\n\n") + sb.WriteString("```bash\n") + sb.WriteString("npm install\n") + sb.WriteString("```\n\n") + + sb.WriteString("### Start project\n\n") + sb.WriteString("```bash\n") + sb.WriteString("npm start\n") + sb.WriteString("```\n\n") + + sb.WriteString("### Build project\n\n") + sb.WriteString("```bash\n") + sb.WriteString("npm run build\n") + sb.WriteString("```\n\n") + + return sb.String() +} diff --git a/main.go b/main.go index 71018d3..84c2d1b 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( //go:embed tpls var f embed.FS -var VERSION = "v10.1.0" +var VERSION = "v10.2.0" func main() { defer func() { @@ -32,7 +32,7 @@ func main() { app := cli.NewApp() app.Name = "gin-admin-cli" app.Version = VERSION - app.Usage = "A command line tool for gin-admin." + app.Usage = "A command line tool for [gin-admin](https://github.com/LyricTian/gin-admin)." app.Authors = append(app.Authors, &cli.Author{ Name: "LyricTian", Email: "tiannianshou@gmail.com",