Skip to content

Commit

Permalink
Add goctl version to code header (#4293)
Browse files Browse the repository at this point in the history
  • Loading branch information
kesonan authored Aug 3, 2024
1 parent dedba17 commit 64e3aed
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/vars"
Expand All @@ -22,6 +23,8 @@ const (
jwtTransKey = "jwtTransition"
routesFilename = "routes"
routesTemplate = `// Code generated by goctl. DO NOT EDIT.
// goctl {{.version}}
package handler
import (
Expand Down Expand Up @@ -203,6 +206,7 @@ rest.WithPrefix("%s"),`, g.prefix)
"hasTimeout": hasTimeout,
"importPackages": genRouteImports(rootPkg, api),
"routesAdditions": strings.TrimSpace(builder.String()),
"version": version.BuildVersion,
},
})
}
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)
Expand Down Expand Up @@ -64,6 +65,7 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
data: map[string]any{
"types": val,
"containsTime": false,
"version": version.BuildVersion,
},
})
}
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/api/gogen/types.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Code generated by goctl. DO NOT EDIT.
// goctl {{.version}}

package types{{if .containsTime}}
import (
"time"
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/api/javagen/component.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Code generated by goctl. DO NOT EDIT.
// goctl {{.version}}

package com.xhb.logic.http.packet.{{.packet}}.model;

import org.jetbrains.annotations.NotNull;
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/api/javagen/gencomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/zeromicro/go-zero/core/stringx"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
Expand Down Expand Up @@ -131,6 +132,7 @@ func (c *componentsContext) createComponent(dir, packetName string, ty spec.Type
"className": util.Title(defineStruct.Name()),
"superClassName": superClassName,
"HasProperty": len(strings.TrimSpace(propertiesString)) > 0,
"version": version.BuildVersion,
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions tools/goctl/api/tsgen/components.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by goctl. DO NOT EDIT.
// goctl {{.version}}

{{.componentTypes}}
2 changes: 2 additions & 0 deletions tools/goctl/api/tsgen/gencomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/zeromicro/go-zero/tools/goctl/api/spec"
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)

Expand Down Expand Up @@ -61,5 +62,6 @@ func genComponents(dir string, api *spec.ApiSpec) error {
t := template.Must(template.New("componentsTemplate").Parse(componentsTemplate))
return t.Execute(fp, map[string]string{
"componentTypes": val,
"version": version.BuildVersion,
})
}
2 changes: 2 additions & 0 deletions tools/goctl/model/mongo/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/zeromicro/go-zero/tools/goctl/model/mongo/template"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
Expand Down Expand Up @@ -59,6 +60,7 @@ func generateModel(ctx *Context) error {
"Type": stringx.From(t).Title(),
"lowerType": stringx.From(t).Untitle(),
"Cache": ctx.Cache,
"version": version.BuildVersion,
}, output, true); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/model/mongo/template/model.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Code generated by goctl. DO NOT EDIT.
// goctl {{.version}}

package model

import (
Expand Down
5 changes: 4 additions & 1 deletion tools/goctl/model/sql/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"

"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/zeromicro/go-zero/tools/goctl/util"
)

Expand Down Expand Up @@ -44,6 +45,8 @@ var ModelCustom string

// ModelGen defines a template for model
var ModelGen = fmt.Sprintf(`%s
// versions:
// goctl version: %s
package {{.pkg}}
{{.imports}}
Expand All @@ -57,7 +60,7 @@ package {{.pkg}}
{{.extraMethod}}
{{.tableName}}
{{.customized}}
`, util.DoNotEditHead)
`, util.DoNotEditHead, version.BuildVersion)

// Insert defines a template for insert code in model
//
Expand Down
6 changes: 5 additions & 1 deletion tools/goctl/util/head.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package util

import "github.com/zeromicro/go-zero/tools/goctl/internal/version"

const (
// DoNotEditHead added to the beginning of a file to prompt the user not to edit
DoNotEditHead = "// Code generated by goctl. DO NOT EDIT."

headTemplate = DoNotEditHead + `
// goctl {{.version}}
// Source: {{.source}}`
)

// GetHead returns a code head string with source filename
func GetHead(source string) string {
buffer, _ := With("head").Parse(headTemplate).Execute(map[string]any{
"source": source,
"source": source,
"version": version.BuildVersion,
})
return buffer.String()
}

0 comments on commit 64e3aed

Please sign in to comment.