Skip to content

Commit

Permalink
Merge pull request #24 from shenghui0779/feature_cmd
Browse files Browse the repository at this point in the history
Feature cmd
  • Loading branch information
shenghui0779 authored Jan 2, 2025
2 parents ca49711 + 7a95b20 commit 6535f2c
Show file tree
Hide file tree
Showing 97 changed files with 682 additions and 757 deletions.
122 changes: 101 additions & 21 deletions cmd/internal/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package internal
import (
"embed"
"fmt"
"io/fs"
"log"
"path/filepath"
"strings"
"text/template"

"github.com/shenghui0779/yiigo"
Expand All @@ -15,74 +18,151 @@ type Params struct {
Module string
AppPkg string
AppName string
DockerF string
}

func InitHttpProject(root, mod string, apps ...string) {
// 创建项目
initProject(root, mod, http.Project, http.FS)
initProject(root, mod, http.FS)
// 创建App(单应用)
if len(apps) == 0 {
initApp(root, mod, "", http.App, http.FS)
initApp(root, mod, "", http.FS)
return
}
// 创建App(多应用)
for _, name := range apps {
initApp(root, mod, name, http.App, http.FS)
initApp(root, mod, name, http.FS)
}
}

func InitHttpApp(root, mod, name string) {
initApp(root, mod, name, http.App, http.FS)
initApp(root, mod, name, http.FS)
}

func InitGrpcProject(root, mod string, apps ...string) {
// 创建项目
initProject(root, mod, grpc.Project, grpc.FS)
initProject(root, mod, grpc.FS)
// 创建App(单应用)
if len(apps) == 0 {
initApp(root, mod, "", grpc.App, grpc.FS)
initApp(root, mod, "", grpc.FS)
return
}
// 创建App(多应用)
for _, name := range apps {
initApp(root, mod, name, grpc.App, grpc.FS)
initApp(root, mod, name, grpc.FS)
}
}

func InitGrpcApp(root, mod, name string) {
initApp(root, mod, name, grpc.App, grpc.FS)
initApp(root, mod, name, grpc.FS)
}

func initProject(root, mod string, tmpls []map[string]string, fs embed.FS) {
func initProject(root, mod string, fsys embed.FS) {
params := &Params{Module: mod}
// 创建项目
for _, tmpl := range tmpls {
output := root + "/" + tmpl["output"]
buildTmpl(fs, tmpl["name"], tmpl["path"], output, params)
// 项目根目录文件
files, _ := fs.ReadDir(fsys, ".")
for _, v := range files {
if v.IsDir() || filepath.Ext(v.Name()) == ".go" {
continue
}
output := genOutput(root, v.Name(), "")
buildTmpl(fsys, v.Name(), output, params)
}
// lib目录文件
_ = fs.WalkDir(fsys, "pkg/internal", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() || filepath.Ext(path) == ".go" {
return nil
}
output := genOutput(root, path, "")
buildTmpl(fsys, path, output, params)
return nil
})
}

func initApp(root, mod, name string, tmpls []map[string]string, fs embed.FS) {
prefix := root + "/pkg/app"
func initApp(root, mod, name string, fsys embed.FS) {
params := &Params{
Module: mod,
AppPkg: "app",
AppName: root,
DockerF: "Dockerfile",
}
if len(name) != 0 {
prefix += "/" + name
params.AppPkg = "app/" + name
params.AppName = name
params.DockerF = name + ".dockerfile"
}
// app目录文件
_ = fs.WalkDir(fsys, "pkg/app", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
if filepath.Ext(path) == ".go" {
return nil
}
output := genOutput(root, path, name)
buildTmpl(fsys, path, output, params)
return nil
})
}

func genOutput(root, path, appName string) string {
var builder strings.Builder
// 项目根目录
builder.WriteString(root)
builder.WriteString("/")
// 解析path
dir, name := filepath.Split(path)
// dockerfile
switch name {
case "Dockerfile":
if len(appName) != 0 {
builder.WriteString(appName)
builder.WriteString(".dockerfile")
} else {
builder.WriteString("Dockerfile")
}
return filepath.Clean(builder.String())
case "dockerun.sh":
if len(appName) != 0 {
builder.WriteString(appName)
builder.WriteString("_dockerun.sh")
} else {
builder.WriteString("dockerun.sh")
}
return filepath.Clean(builder.String())
}
// 文件目录
if len(dir) != 0 {
builder.WriteString(dir)
}
for _, tmpl := range tmpls {
output := prefix + "/" + tmpl["output"]
buildTmpl(fs, tmpl["name"], tmpl["path"], output, params)
// 文件名称
switch ext := filepath.Ext(path); ext {
case ".yiigo":
builder.WriteString(name[:len(name)-6])
builder.WriteString(".go")
case "":
if strings.Contains(name, "ignore") {
builder.WriteString(".")
}
builder.WriteString(name)
default:
builder.WriteString(name)
}
// 新的文件路径
output := builder.String()
if len(appName) != 0 {
output = strings.Replace(output, "/app", "/app/"+appName, 1)
}
return filepath.Clean(output)
}

func buildTmpl(fs embed.FS, name, path, output string, params *Params) {
func buildTmpl(fsys embed.FS, path, output string, params *Params) {
b, err := fsys.ReadFile(path)
if err != nil {
log.Fatalln(err)
}
// 模板解析
t, err := template.New(name).ParseFS(fs, path)
t, err := template.New(path).Parse(string(b))
if err != nil {
log.Fatalln(err)
}
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6535f2c

Please sign in to comment.