Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

feat(templates): Use user-provided templates #32

Merged
merged 1 commit into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions blox/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,53 @@ func (m Model) New(slug string, destination string) error {
if err != nil {
return err
}

// check for user installed templates first
cfg, err := config.Load()
cobra.CheckErr(err)

templatePath := path.Join(cfg.Base, cfg.Templates, m.Folder, m.ID+cfg.DefaultExtension)

joined := path.Join(destination, slug)
// check to see if we're creating the templates
// from the `init` command
fmt.Println("creating:", joined)
fmt.Println("template path:", templatePath)
var bb []byte
if templatePath == joined {
bb, err = m.defaultTemplate()
cobra.CheckErr(err)
} else {
bb, err = os.ReadFile(templatePath)
cobra.CheckErr(err)
}

// create the destination file
f, err := os.Create(joined)
if err != nil {
return err
}
defer f.Close()

f.Write(bb)

return nil
}

func (m Model) defaultTemplate() ([]byte, error) {

switch m.ID {
case "article":
f.Write([]byte(ArticleTemplate))
return []byte(ArticleTemplate), nil
case "category":
f.Write([]byte(CategoryTemplate))
return []byte(CategoryTemplate), nil
case "profile":
f.Write([]byte(ProfileTemplate))
return []byte(ProfileTemplate), nil
case "page":
f.Write([]byte(PageTemplate))
return []byte(PageTemplate), nil
default:
return fmt.Errorf("generator doesn't support %s yet", m.ID)
return []byte{}, fmt.Errorf("generator doesn't support %s yet", m.ID)
}

return nil
}

// baseModel defines fields used by all drb
Expand Down
2 changes: 1 addition & 1 deletion blox/profile.cue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
first_name: "David" | "Brian"
first_name: string
last_name: string
age?: int
company?: string
Expand Down
2 changes: 0 additions & 2 deletions blox/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package blox

import (
_ "embed"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
Expand Down Expand Up @@ -65,7 +64,6 @@ func FromYAML(path string, modelName string, cue string) (map[string]interface{}
slug := strings.Replace(filepath.Base(path), ext, "", -1)

model["id"] = slug
fmt.Printf("Model %s '%s' validated successfully\n", modelName, model["id"])

return model, nil
}
9 changes: 7 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/devrel-blox/drb/blox"
"github.com/devrel-blox/drb/config"
Expand All @@ -40,6 +41,7 @@ to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
cobra.CheckErr(err)
fmt.Println("Preparing data output...")

// convert markdown to yaml
cobra.CheckErr(convertModels(cfg))
Expand All @@ -61,16 +63,17 @@ to quickly create a Cobra application.`,
_, err = f.Write(bb)
cobra.CheckErr(err)

fmt.Printf("output data to %s\n", path.Join(cfg.Base, cfg.Destination, "data.json"))
fmt.Printf("wrote: %s\n", path.Join(cfg.Base, cfg.Destination, "data.json"))
},
}

func aggregateModels(cfg *config.BloxConfig) (map[string][]interface{}, error) {
data := make(map[string][]interface{})
fmt.Printf("Loading YAML files...\n")

for _, model := range blox.Models {
// Attempt to decode all the YAML files with this directory as model
fmt.Printf("Loading %s YAML files in %s\n", model.ID, path.Join(cfg.Base, cfg.Destination, model.Folder))
fmt.Printf("\t model %s: \n\t\tsource: %s\n", model.ID, path.Join(cfg.Base, cfg.Destination, model.Folder))

filepath.Walk(path.Join(cfg.Base, cfg.Destination, model.Folder),
func(path string, info os.FileInfo, err error) error {
Expand All @@ -84,6 +87,7 @@ func aggregateModels(cfg *config.BloxConfig) (map[string][]interface{}, error) {
}

ext := filepath.Ext(path)
slug := strings.Replace(filepath.Base(path), ext, "", -1)

// if ext != cfg.DefaultExtension {
// Should be SupportedExtensions?
Expand All @@ -101,6 +105,7 @@ func aggregateModels(cfg *config.BloxConfig) (map[string][]interface{}, error) {
return err
}
data[model.Folder] = append(data[model.ID], entity)
fmt.Printf("\t\t\t%s '%s' loaded\n", model.ID, slug)

return nil

Expand Down
5 changes: 3 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ to quickly create a Cobra application.`,

func convertModels(cfg *config.BloxConfig) error {
var errors error
fmt.Printf("Reading Markdown files...\n")

for _, model := range blox.Models {
// Attempt to decode all the YAML files with this directory as model
fmt.Printf("Checking for %s markdown files in %s\n", model.ID, path.Join(cfg.Base, cfg.Source, model.Folder))
fmt.Printf("\tmodel: %s \n\t\tsource: %s\n", model.Name, path.Join(cfg.Base, cfg.Source, model.Folder))

filepath.Walk(path.Join(cfg.Base, cfg.Source, model.Folder),
func(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -112,7 +113,7 @@ func convertModels(cfg *config.BloxConfig) error {

*/

fmt.Println(fmt.Sprintf("Markdown file '%s' converted", slug))
fmt.Printf("\t\t\t%s '%s' converted\n", model.ID, slug)

return nil

Expand Down
8 changes: 6 additions & 2 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/devrel-blox/drb/blox"
"github.com/devrel-blox/drb/config"
Expand All @@ -46,11 +47,13 @@ to quickly create a Cobra application.`,

func validateModels(cfg *config.BloxConfig) error {
var errors error
fmt.Printf("Validating YAML files...\n")

// We want to validate all the YAML for the models that we're aware of.
for _, model := range blox.Models {
// Attempt to decode all the YAML files with this directory as model
fmt.Printf("Validating %s YAML files in %s\n", model.ID, path.Join(cfg.Base, cfg.Destination, model.Folder))

fmt.Printf("\t model: %s\n\t\t in: %s\n", model.ID, path.Join(cfg.Base, cfg.Destination, model.Folder))

filepath.Walk(path.Join(cfg.Base, cfg.Destination, model.Folder),
func(path string, info os.FileInfo, err error) error {
Expand All @@ -64,7 +67,7 @@ func validateModels(cfg *config.BloxConfig) error {
}

ext := filepath.Ext(path)

slug := strings.Replace(filepath.Base(path), ext, "", -1)
// if ext != cfg.DefaultExtension {
// Should be SupportedExtensions?
if ext != ".yaml" && ext != ".yml" {
Expand All @@ -81,6 +84,7 @@ func validateModels(cfg *config.BloxConfig) error {
errors = multierror.Append(errors, multierror.Prefix(err, path))
return err
}
fmt.Printf("\t\t\t%s '%s' validated\n", model.ID, slug)

return err

Expand Down