Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(template): adjust the template storage location #133

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var genCmd = &cobra.Command{
home, _ := os.UserHomeDir()
config.C.Gen.Home, _ = homedir.Expand(config.C.Gen.Home)
if !pathx.FileExists(config.C.Gen.Home) {
config.C.Gen.Home = filepath.Join(home, ".jzero", Version)
config.C.Gen.Home = filepath.Join(home, ".jzero", "templates", Version)
}
embeded.Home = config.C.Gen.Home
return gen.Run(config.C)
Expand Down Expand Up @@ -205,7 +205,7 @@ var genSdkCmd = &cobra.Command{
return err
}
if embeded.Home == "" {
embeded.Home = filepath.Join(homeDir, ".jzero", Version)
embeded.Home = filepath.Join(homeDir, ".jzero", "templates", Version)
}
return gensdk.GenSdk(config.C, genModule)
},
Expand Down
24 changes: 12 additions & 12 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,29 @@ var newCmd = &cobra.Command{
// 使用远程仓库模板
if config.C.New.Remote != "" && config.C.New.Branch != "" {
// clone to local
_ = os.MkdirAll(filepath.Join(home, ".jzero"), 0o755)

fmt.Printf("%s templates into '%s', please wait...\n", color.WithColor("Cloning", color.FgGreen), filepath.Join(home, ".jzero", "templates", config.C.New.Branch))

_ = os.RemoveAll(filepath.Join(home, ".jzero", "templates", config.C.New.Branch))
fp := filepath.Join(home, ".jzero", "templates", "remote", config.C.New.Branch)
_ = os.MkdirAll(fp, 0o755)
fmt.Printf("%s templates into '%s', please wait...\n", color.WithColor("Cloning", color.FgGreen), fp)
_ = os.RemoveAll(fp)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

_, err := git.PlainCloneContext(ctx, filepath.Join(home, ".jzero", "templates", config.C.New.Branch), false, &git.CloneOptions{
_, err := git.PlainCloneContext(ctx, fp, false, &git.CloneOptions{
SingleBranch: true,
URL: config.C.New.Remote,
Depth: 0,
ReferenceName: plumbing.ReferenceName("refs/heads/" + config.C.New.Branch),
})
cobra.CheckErr(err)
_ = os.RemoveAll(filepath.Join(fp, ".git"))
fmt.Println(color.WithColor("Done", color.FgGreen))
embeded.Home = filepath.Join(home, ".jzero", "templates", config.C.New.Branch)
embeded.Home = fp
}

// 使用本地模板
if config.C.New.Local != "" {
embeded.Home = filepath.Join(home, ".jzero", "templates", config.C.New.Local)
embeded.Home = filepath.Join(home, ".jzero", "templates", "local", config.C.New.Local)
}

// 指定 home 时优先级最高
Expand All @@ -85,7 +85,7 @@ var newCmd = &cobra.Command{
}

if !pathx.FileExists(embeded.Home) {
embeded.Home = filepath.Join(home, ".jzero", Version)
embeded.Home = filepath.Join(home, ".jzero", "templates", Version)
}
return new.Run(config.C, args[0])
},
Expand All @@ -96,11 +96,11 @@ func init() {
rootCmd.AddCommand(newCmd)
newCmd.Flags().StringP("module", "m", "", "set go module")
newCmd.Flags().StringP("output", "o", "", "set output dir")
newCmd.Flags().StringP("home", "", "", "set templates path")
newCmd.Flags().StringP("home", "", "", "use the specified template.")
newCmd.Flags().StringP("frame", "", "api", "frame")
newCmd.Flags().StringP("remote", "r", "https://github.com/jzero-io/templates", "remote templates repo")
newCmd.Flags().StringP("branch", "b", "", "remote templates repo branch")
newCmd.Flags().StringP("local", "", "", "local templates")
newCmd.Flags().StringP("branch", "b", "", "use remote template repo branch")
newCmd.Flags().StringP("local", "", "", "use local template")
newCmd.Flags().StringP("style", "", "gozero", "The file naming format, see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
newCmd.Flags().StringSliceP("features", "", []string{}, "select features")
newCmd.Flags().BoolP("mono", "", false, "mono project under go mod project")
Expand Down
22 changes: 13 additions & 9 deletions cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ import (
// templateCmd represents the template command
var templateCmd = &cobra.Command{
Use: "template",
Short: `Used to save and build templates`,
Short: `Used to initialize or build templates`,
}

var templateInitCmd = &cobra.Command{
Use: "init",
Short: `Save template files on your disk`,
Short: `Initialize templates`,
Long: `Initialize specific remote template or embedded templates on your disk`,
PreRun: func(_ *cobra.Command, _ []string) {},

RunE: func(cmd *cobra.Command, args []string) error {
if config.C.Template.Init.Output == "" {
home, _ := os.UserHomeDir()
config.C.Template.Init.Output = filepath.Join(home, ".jzero", Version)
if config.C.Template.Init.Remote != "" && config.C.Template.Init.Branch != "" {
config.C.Template.Init.Output = filepath.Join(home, ".jzero", "templates", "remote")
} else {
config.C.Template.Init.Output = filepath.Join(home, ".jzero", "templates", Version)
}
}
return templateinit.Run(config.C)
},
Expand All @@ -39,12 +44,11 @@ var templateInitCmd = &cobra.Command{
var templateBuildCmd = &cobra.Command{
Use: "build",
Short: `Build your current project to template`,
Long: `Build your current project to template and save them into ${HOME}/.jzero/templates/local.`,
PreRun: func(cmd *cobra.Command, args []string) {
if config.C.Template.Build.Output == "" {
home, _ := os.UserHomeDir()
config.C.Template.Build.Output = filepath.Join(home, ".jzero", "templates", config.C.Template.Build.Name, "app")
} else {
config.C.Template.Build.Output = filepath.Join(config.C.Template.Build.Output, "app")
config.C.Template.Build.Output = filepath.Join(home, ".jzero", "templates", "local", config.C.Template.Build.Name)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -58,18 +62,18 @@ func init() {
{
templateCmd.AddCommand(templateBuildCmd)

templateBuildCmd.Flags().StringP("working-dir", "w", ".", "default working directory")
templateBuildCmd.Flags().StringP("working-dir", "w", ".", "working directory")
templateBuildCmd.Flags().StringP("name", "n", "", "template name")
_ = templateBuildCmd.MarkFlagRequired("name")
templateBuildCmd.Flags().StringP("output", "o", "", "default output directory")
templateBuildCmd.Flags().StringP("output", "o", "", "output directory")
templateBuildCmd.Flags().StringSliceP("ignore", "i", templatebuild.IgnoreDirs, "dir list for ignored files")
}

{
templateCmd.AddCommand(templateInitCmd)

templateInitCmd.Flags().StringP("remote", "r", "https://github.com/jzero-io/templates", "remote templates repo")
templateInitCmd.Flags().StringP("branch", "b", "", "remote templates repo branch")
templateInitCmd.Flags().StringP("branch", "b", "", "remote template repo branch. If not set, init the embedded templates.")
templateInitCmd.Flags().StringP("output", "o", "", "template output dir")
}
}
10 changes: 9 additions & 1 deletion internal/template/templatebuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/moby/patternmatcher"
"github.com/zeromicro/go-zero/core/color"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"golang.org/x/mod/modfile"

Expand All @@ -38,6 +39,8 @@ func Run(tc config.TemplateConfig) error {
if pathx.FileExists(tc.Build.Output) {
return errors.New("template build output already exists")
}
fmt.Printf("%s your project to templates into '%s', please wait...\n", color.WithColor("Building", color.FgGreen), tc.Build.Output)
tc.Build.Output = filepath.Join(tc.Build.Output, "app")
wd, _ := os.Getwd()

modfileBytes, err := os.ReadFile(filepath.Join(tc.Build.WorkingDir, "go.mod"))
Expand All @@ -50,7 +53,12 @@ func Run(tc config.TemplateConfig) error {
return err
}
tc.Build.WorkingDir = filepath.Join(wd, tc.Build.WorkingDir)
return build(tc, tc.Build.WorkingDir, mod)
err = build(tc, tc.Build.WorkingDir, mod)
if err != nil {
return err
}
fmt.Println(color.WithColor("Done", color.FgGreen))
return nil
}

func build(tc config.TemplateConfig, dirname string, mod *modfile.File) error {
Expand Down
17 changes: 11 additions & 6 deletions internal/template/templateinit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (

func Run(c config.Config) error {
if c.Template.Init.Remote != "" && c.Template.Init.Branch != "" {
_ = os.MkdirAll(c.Template.Init.Output, 0o755)
fmt.Printf("%s templates into '%s/templates/%s', please wait...\n", color.WithColor("Cloning", color.FgGreen), c.Template.Init.Output, c.Template.Init.Branch)
target := filepath.Join(c.Template.Init.Output, c.Template.Init.Branch)
_ = os.MkdirAll(target, 0o755)
fmt.Printf("%s templates into '%s', please wait...\n", color.WithColor("Cloning", color.FgGreen), target)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
_, err := git.PlainCloneContext(ctx, filepath.Join(c.Template.Init.Output), false, &git.CloneOptions{
_, err := git.PlainCloneContext(ctx, target, false, &git.CloneOptions{
SingleBranch: true,
URL: c.Template.Init.Remote,
Depth: 0,
Expand All @@ -31,11 +32,15 @@ func Run(c config.Config) error {
if err != nil {
return err
}
_ = os.RemoveAll(filepath.Join(target, ".git"))
fmt.Println(color.WithColor("Done", color.FgGreen))

return nil
}

fmt.Printf("%s templates into '%s', please wait...\n", color.WithColor("Initializing embedded", color.FgGreen), c.Template.Init.Output)
err := embeded.WriteTemplateDir("", c.Template.Init.Output)
return err
if err != nil {
return err
}
fmt.Println(color.WithColor("Done", color.FgGreen))
return nil
}
Loading