diff --git a/embed/embed.go b/embed/embed.go deleted file mode 100644 index 0cae6e2..0000000 --- a/embed/embed.go +++ /dev/null @@ -1,7 +0,0 @@ -package embed - -import "embed" - -//go:embed templates/*.tmpl templates/**/* -var Templates embed.FS - diff --git a/flake.nix b/flake.nix index 81207eb..c47b7fe 100644 --- a/flake.nix +++ b/flake.nix @@ -39,7 +39,7 @@ default = pkgs.callPackage ( { buildGoModule }: buildGoModule { - pname = "bubblewand"; + pname = "wand-templater"; version = "0.0.1"; src = builtins.path { name = "source"; diff --git a/internal/render/render.go b/internal/render/render.go index 730a867..4eea7c2 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -1,100 +1,71 @@ package render import ( - "io/fs" - "os" - "path/filepath" - "strings" - "text/template" - - "specCon18/bubblewand/embed" // Import embedded template files - "specCon18/bubblewand/internal/logger" // Import logger for logging output + "os" + "path/filepath" + "strings" + "text/template" + "specCon18/bubblewand/internal/logger" ) -// ProgramData holds user-supplied values for template substitution. +// ProgramData holds user-supplied template values type ProgramData struct { - ModName string // Module name - PackageName string // Go package name - ProgramVersion string // Version string - ProgramDesc string // Description of the program - OutputDir string // Target output directory for rendered files + ModName string + PackageName string + ProgramVersion string + ProgramDesc string + OutputDir string } -// RenderTemplates renders embedded .tmpl files into outputDir +// RenderTemplates renders all .tmpl files from the templates/ directory into outputDir func RenderTemplates(data ProgramData, outputDir string, verbose bool) error { - var renderedFiles int // Count of successfully rendered files + var renderedFiles int - // Walk through the embedded templates filesystem starting at "templates" - err := fs.WalkDir(embed.Templates, "templates", func(path string, d fs.DirEntry, err error) error { - if err != nil { - // Log and return error encountered while walking the filesystem - logger.Log.Errorf("Error walking path %q: %v", path, err) - return err - } + err := filepath.Walk("templates", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() || !strings.HasSuffix(info.Name(), ".tmpl") { + return nil + } - // Skip directories and files that do not end with ".tmpl" - if d.IsDir() || !strings.HasSuffix(d.Name(), ".tmpl") { - return nil - } + relPath, err := filepath.Rel("templates", path) + if err != nil { + return err + } + outputPath := filepath.Join(outputDir, strings.TrimSuffix(relPath, ".tmpl")) - // Strip the "templates/" prefix from path and remove ".tmpl" extension - relPath := strings.TrimPrefix(path, "templates/") - outputPath := filepath.Join(outputDir, strings.TrimSuffix(relPath, ".tmpl")) + if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil { + return err + } - // Ensure the parent directory for the output file exists - if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil { - logger.Log.Errorf("Failed to create directory for %s: %v", outputPath, err) - return err - } + tmpl, err := template.ParseFiles(path) + if err != nil { + return err + } - // Read the embedded template file - tmplBytes, err := embed.Templates.ReadFile(path) - if err != nil { - logger.Log.Errorf("Failed to read template %s: %v", path, err) - return err - } + outFile, err := os.Create(outputPath) + if err != nil { + return err + } + defer outFile.Close() - // Parse the template content - tmpl, err := template.New(d.Name()).Parse(string(tmplBytes)) - if err != nil { - logger.Log.Errorf("Failed to parse template %s: %v", path, err) - return err - } + if verbose { + logger.Log.Infof("Rendering %s → %s", path, outputPath) + } - // Create the output file for writing the rendered content - outFile, err := os.Create(outputPath) - if err != nil { - logger.Log.Errorf("Failed to create output file %s: %v", outputPath, err) - return err - } - defer outFile.Close() + renderedFiles++ + return tmpl.Execute(outFile, data) + }) - // Log the render operation if verbose mode is enabled - if verbose { - logger.Log.Infof("Rendering %s → %s", path, outputPath) - } + if err != nil { + return err + } - // Execute the template using the provided data and write to file - if err := tmpl.Execute(outFile, data); err != nil { - logger.Log.Errorf("Failed to execute template %s: %v", path, err) - return err - } + if !verbose && renderedFiles > 0 { + logger.Log.Info("Rendering templates") + } - renderedFiles++ - return nil - }) - - // If any error occurred during the walk/render process, log and return it - if err != nil { - logger.Log.Errorf("Template rendering failed: %v", err) - return err - } - - // If not verbose and at least one file was rendered, print a summary log - if !verbose && renderedFiles > 0 { - logger.Log.Info("Rendering templates") - } - - return nil // Success + return nil } diff --git a/embed/templates/.envrc.tmpl b/templates/.envrc.tmpl similarity index 100% rename from embed/templates/.envrc.tmpl rename to templates/.envrc.tmpl diff --git a/embed/templates/README.md.tmpl b/templates/README.md.tmpl similarity index 100% rename from embed/templates/README.md.tmpl rename to templates/README.md.tmpl diff --git a/embed/templates/cmd/root.go.tmpl b/templates/cmd/root.go.tmpl similarity index 100% rename from embed/templates/cmd/root.go.tmpl rename to templates/cmd/root.go.tmpl diff --git a/embed/templates/cmd/version.go.tmpl b/templates/cmd/version.go.tmpl similarity index 100% rename from embed/templates/cmd/version.go.tmpl rename to templates/cmd/version.go.tmpl diff --git a/embed/templates/config/config.go.tmpl b/templates/config/config.go.tmpl similarity index 98% rename from embed/templates/config/config.go.tmpl rename to templates/config/config.go.tmpl index 7167440..66e4d2c 100644 --- a/embed/templates/config/config.go.tmpl +++ b/templates/config/config.go.tmpl @@ -1,6 +1,7 @@ package config import ( + "fmt" "github.com/spf13/viper" "{{.ModName}}/internal/logger" ) diff --git a/embed/templates/flake.nix.tmpl b/templates/flake.nix.tmpl similarity index 100% rename from embed/templates/flake.nix.tmpl rename to templates/flake.nix.tmpl diff --git a/embed/templates/go.mod.tmpl b/templates/go.mod.tmpl similarity index 100% rename from embed/templates/go.mod.tmpl rename to templates/go.mod.tmpl diff --git a/embed/templates/go.sum.tmpl b/templates/go.sum.tmpl similarity index 100% rename from embed/templates/go.sum.tmpl rename to templates/go.sum.tmpl diff --git a/embed/templates/internal/logger/logger.go.tmpl b/templates/internal/logger/logger.go.tmpl similarity index 100% rename from embed/templates/internal/logger/logger.go.tmpl rename to templates/internal/logger/logger.go.tmpl diff --git a/embed/templates/justfile.tmpl b/templates/justfile.tmpl similarity index 100% rename from embed/templates/justfile.tmpl rename to templates/justfile.tmpl diff --git a/embed/templates/main.go.tmpl b/templates/main.go.tmpl similarity index 100% rename from embed/templates/main.go.tmpl rename to templates/main.go.tmpl diff --git a/embed/templates/tui/model.go.tmpl b/templates/tui/model.go.tmpl similarity index 100% rename from embed/templates/tui/model.go.tmpl rename to templates/tui/model.go.tmpl diff --git a/embed/templates/tui/run.go.tmpl b/templates/tui/run.go.tmpl similarity index 100% rename from embed/templates/tui/run.go.tmpl rename to templates/tui/run.go.tmpl diff --git a/embed/templates/tui/update.go.tmpl b/templates/tui/update.go.tmpl similarity index 100% rename from embed/templates/tui/update.go.tmpl rename to templates/tui/update.go.tmpl diff --git a/embed/templates/tui/view.go.tmpl b/templates/tui/view.go.tmpl similarity index 100% rename from embed/templates/tui/view.go.tmpl rename to templates/tui/view.go.tmpl