From 1ec3616f81b605344c772f240a7c6f0419ad8c94 Mon Sep 17 00:00:00 2001 From: Jan Rada <31016933+ZelvaMan@users.noreply.github.com> Date: Mon, 29 Jan 2024 12:18:16 +0100 Subject: [PATCH] Pass build information to templates --- src/generator.go | 11 ++++--- src/types.go | 11 ++++--- src/version.go | 47 +++++++++++++++++------------- testing/templates/dbcontext.gotmpl | 2 +- testing/templates/model.gotmpl | 2 +- testing/templates/processor.gotmpl | 2 +- 6 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/generator.go b/src/generator.go index de10006..2dd105f 100644 --- a/src/generator.go +++ b/src/generator.go @@ -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) @@ -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) @@ -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) diff --git a/src/types.go b/src/types.go index 5000324..dc05d7b 100644 --- a/src/types.go +++ b/src/types.go @@ -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 } diff --git a/src/version.go b/src/version.go index bae73e0..3186eb5 100644 --- a/src/version.go +++ b/src/version.go @@ -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 } diff --git a/testing/templates/dbcontext.gotmpl b/testing/templates/dbcontext.gotmpl index 1f3aff2..4eb4385 100644 --- a/testing/templates/dbcontext.gotmpl +++ b/testing/templates/dbcontext.gotmpl @@ -1,4 +1,4 @@ -// Autogenerated using db-gen +// Autogenerated using db-gen version: {{.BuildInfo.Version}} using Database.Common; using Database.Generated; diff --git a/testing/templates/model.gotmpl b/testing/templates/model.gotmpl index 26a8309..c9a57c4 100644 --- a/testing/templates/model.gotmpl +++ b/testing/templates/model.gotmpl @@ -1,4 +1,4 @@ -// Autogenerated using db-gen +// Autogenerated using db-gen version: {{.BuildInfo.Version}} using Database.Common; diff --git a/testing/templates/processor.gotmpl b/testing/templates/processor.gotmpl index 36cc497..e0f421b 100644 --- a/testing/templates/processor.gotmpl +++ b/testing/templates/processor.gotmpl @@ -1,4 +1,4 @@ -// Autogenerated using db-gen +// Autogenerated using db-gen version: {{.BuildInfo.Version}} using Database.Common; using Npgsql;