1 Star 0 Fork 0

micro-tools/gen-project

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
project.go 3.15 KB
一键复制 编辑 原始数据 按行查看 历史
xushenghao 提交于 2022-10-19 13:47 +08:00 . 更新文件
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/gobuffalo/packr/v2"
)
// project project config
type project struct {
// project name
Name string
UName string
// mod prefix
ModPrefix string
// project dir
path string
none bool
onlyGRPC bool
onlyHTTP bool
onlyPure bool
}
var p project
var _replace = "micro-replace"
var _change = ""
//go:generate packr2
func create() (err error) {
box := packr.New("all", "./templates/all")
if p.onlyHTTP {
box = packr.New("http", "./templates/http")
} else if p.onlyGRPC {
box = packr.New("grpc", "./templates/grpc")
} else if p.onlyPure {
box = packr.New("pure", "./templates/pure")
}
if err = os.MkdirAll(p.path, 0755); err != nil {
return
}
for _, name := range box.List() {
tmpl, _ := box.FindString(name)
i := strings.LastIndex(name, string(os.PathSeparator))
if i > 0 {
dir := name[:i]
if err = os.MkdirAll(filepath.Join(p.path, dir), 0755); err != nil {
return
}
}
if strings.HasSuffix(name, ".tmpl") {
name = strings.TrimSuffix(name, ".tmpl")
}
if err = write(filepath.Join(p.path, name), tmpl); err != nil {
return
}
}
_change = p.Name
for {
if err = changeFileName(p.path); err == nil {
break
}
}
if err = generate("./..."); err != nil {
return
}
return
}
func generate(path string) error {
cmd := exec.Command("go", "generate", "-x", path)
cmd.Dir = p.path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func write(path, tpl string) (err error) {
data, err := parse(tpl)
if err != nil {
return
}
return ioutil.WriteFile(path, data, 0644)
}
func parse(s string) ([]byte, error) {
t, err := template.New("").Parse(s)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err = t.Execute(&buf, p); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func changeFileName(path string) error {
err := filepath.Walk(path, walkFunc)
if err != nil {
return err
}
return nil
}
func walkFunc(path string, info os.FileInfo, err error) error {
if info == nil {
return errors.New("can't find:(" + path + ")")
}
if info.IsDir() {
if info.Name() == _replace {
os.Rename(path, fmt.Sprintf("%s\\%s", filepath.Dir(path), _change))
}
return nil
} else {
if info.Name() == fmt.Sprintf("%s.go", _replace) {
os.Rename(path, fmt.Sprintf("%s\\%s.go", filepath.Dir(path), _change))
} else if info.Name() == fmt.Sprintf("%s-common.proto", _replace) {
os.Rename(path, fmt.Sprintf("%s\\%s_common.proto", filepath.Dir(path), _change))
} else if info.Name() == fmt.Sprintf("%s.proto", _replace) {
os.Rename(path, fmt.Sprintf("%s\\%s.proto", filepath.Dir(path), _change))
} else if info.Name() == fmt.Sprintf("%s.pb.go", _replace) {
os.Rename(path, fmt.Sprintf("%s\\%s.pb.go", filepath.Dir(path), _change))
} else if info.Name() == fmt.Sprintf("%s_mysql.sql", _replace) {
os.Rename(path, fmt.Sprintf("%s\\%s_mysql.sql", filepath.Dir(path), _change))
} else if info.Name() == fmt.Sprintf("%s_postgres.sql", _replace) {
os.Rename(path, fmt.Sprintf("%s\\%s_postgres.sql", filepath.Dir(path), _change))
}
return nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/micro-tools/gen-project.git
git@gitee.com:micro-tools/gen-project.git
micro-tools
gen-project
gen-project
v1.0.0

搜索帮助