Skip to content

Commit

Permalink
feat: Add generate frontend project
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Dec 12, 2023
1 parent 6eff84d commit 4e53271
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build

RELEASE_VERSION = v10.1.0
RELEASE_VERSION = v10.2.0

APP = gin-admin-cli
BIN = ${APP}
Expand Down
28 changes: 28 additions & 0 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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())
},
Expand Down
96 changes: 93 additions & 3 deletions internal/actions/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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: "[email protected]",
Expand Down

0 comments on commit 4e53271

Please sign in to comment.