Skip to content

Commit

Permalink
Pass build information to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Jan 29, 2024
1 parent 8b411f8 commit 1ec3616
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
11 changes: 7 additions & 4 deletions src/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func generateDbContext(routines []Routine, hashMap *map[string]string, config *C
data := &DbContextData{
Config: config,
Functions: routines,
BuildInfo: GetBuildInfo(),
}

filename := changeCase("DbContext"+config.GeneratedFileExtension, config.GeneratedFileCase)
Expand Down Expand Up @@ -107,8 +108,9 @@ func generateModels(routines []Routine, hashMap *map[string]string, config *Conf
filePath := filepath.Join(config.OutputFolder, relPath)

data := &ModelTemplateData{
Config: config,
Routine: routine,
Config: config,
Routine: routine,
BuildInfo: GetBuildInfo(),
}

changed, err := generateFile(data, moduleTemplate, filePath, hashMap)
Expand Down Expand Up @@ -149,8 +151,9 @@ func generateProcessors(routines []Routine, hashMap *map[string]string, config *
filePath := filepath.Join(config.OutputFolder, relPath)

data := &ProcessorTemplateData{
Config: config,
Routine: routine,
Config: config,
Routine: routine,
BuildInfo: GetBuildInfo(),
}

changed, err := generateFile(data, processorTemplate, filePath, hashMap)
Expand Down
11 changes: 7 additions & 4 deletions src/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ type Routine struct {
type DbContextData struct {
Config *Config
Functions []Routine
BuildInfo *BuildInformation
}

type ProcessorTemplateData struct {
Config *Config
Routine Routine
Config *Config
Routine Routine
BuildInfo *BuildInformation
}

type ModelTemplateData struct {
Config *Config
Routine Routine
Config *Config
Routine Routine
BuildInfo *BuildInformation
}
47 changes: 27 additions & 20 deletions src/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,55 @@ import (
)

type BuildInformation struct {
builder string
version string
commitHash string
Builder string
Version string
CommitHash string
}

var info *BuildInformation = nil
const localVersion = "LOCAL"
const localBuilder = "LOCAL"

var info BuildInformation = BuildInformation{
Builder: localBuilder,
Version: localVersion,
CommitHash: "",
}

func ParseBuildInformation(versionFileText string) error {
if strings.HasPrefix(versionFileText, "LOCAL") {
common.LogWarn("Running locally build version, be careful")
common.LogWarn("Running locally build Version, be careful")
return nil
}

splitted := strings.Split(versionFileText, " ")

if len(splitted) < 2 {
return fmt.Errorf("error with build, version file has incorrect format ")
return fmt.Errorf("error with build, Version file has incorrect format ")
}

info = &BuildInformation{
builder: splitted[0],
version: splitted[1],
commitHash: splitted[2],
info = BuildInformation{
Builder: splitted[0],
Version: splitted[1],
CommitHash: splitted[2],
}

return nil
}

func PrintVersion() {
if info == nil {
log.Printf("Locally build version")
if info.Builder == localBuilder {
log.Printf("Locally build Version")
return
}
log.Printf("db-gen build by %s ", info.builder)
log.Printf("version %s ", info.version)
log.Printf("last commit hash %s ", info.commitHash)
log.Printf("db-gen build by %s ", info.Builder)
log.Printf("Version %s ", info.Version)
log.Printf("last commit hash %s ", info.CommitHash)
}

func GetVersion() string {
if info == nil {
return "LOCAL"
} else {
return info.version
}
return info.Version
}

func GetBuildInfo() *BuildInformation {
return &info
}
2 changes: 1 addition & 1 deletion testing/templates/dbcontext.gotmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated using db-gen
// Autogenerated using db-gen version: {{.BuildInfo.Version}}

using Database.Common;
using Database.Generated;
Expand Down
2 changes: 1 addition & 1 deletion testing/templates/model.gotmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated using db-gen
// Autogenerated using db-gen version: {{.BuildInfo.Version}}

using Database.Common;

Expand Down
2 changes: 1 addition & 1 deletion testing/templates/processor.gotmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated using db-gen
// Autogenerated using db-gen version: {{.BuildInfo.Version}}

using Database.Common;
using Npgsql;
Expand Down

0 comments on commit 1ec3616

Please sign in to comment.