Skip to content

Commit

Permalink
Add goctl version to code header
Browse files Browse the repository at this point in the history
  • Loading branch information
kesonan committed Aug 3, 2024
1 parent dedba17 commit c7c0c38
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gogen

import (
"fmt"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"os"
"path"
"sort"
Expand All @@ -22,6 +23,9 @@ const (
jwtTransKey = "jwtTransition"
routesFilename = "routes"
routesTemplate = `// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: {{.version}}
package handler
import (
Expand Down Expand Up @@ -203,6 +207,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 @@ -3,6 +3,7 @@ package gogen
import (
_ "embed"
"fmt"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"io"
"os"
"path"
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
3 changes: 3 additions & 0 deletions tools/goctl/api/gogen/types.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: {{.version}}

package types{{if .containsTime}}
import (
"time"
Expand Down
3 changes: 3 additions & 0 deletions tools/goctl/api/javagen/component.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: {{.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 @@ -6,6 +6,7 @@ import (
_ "embed"
"errors"
"fmt"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"io"
"path"
"strings"
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
2 changes: 2 additions & 0 deletions tools/goctl/api/tsgen/components.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: {{.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 @@ -2,6 +2,7 @@ package tsgen

import (
_ "embed"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"path"
"strings"
"text/template"
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 @@ -2,6 +2,7 @@ package generate

import (
"errors"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"path/filepath"

"github.com/zeromicro/go-zero/tools/goctl/config"
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
3 changes: 3 additions & 0 deletions tools/goctl/model/mongo/template/model.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: {{.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 @@ -3,6 +3,7 @@ package template
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
7 changes: 6 additions & 1 deletion tools/goctl/util/head.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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 + `
// versions:
// goctl version: {{.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 c7c0c38

Please sign in to comment.