Skip to content

Commit

Permalink
Add template sections
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed May 4, 2024
1 parent fbbd30f commit e0cb512
Show file tree
Hide file tree
Showing 326 changed files with 5,105 additions and 8 deletions.
69 changes: 61 additions & 8 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"unicode"
Expand Down Expand Up @@ -874,6 +875,31 @@ func augmentGenericConfig(config *YamlConfig, type_ string) {
}
}

func getTemplateSection(content, name string) string {
scanner := bufio.NewScanner(strings.NewReader(content))
result := ""
foundSection := false
beginRegex := regexp.MustCompile(`\/\/template:begin\s` + name + `$`)
endRegex := regexp.MustCompile(`\/\/template:end\s` + name + `$`)
for scanner.Scan() {
line := scanner.Text()
if !foundSection {
match := beginRegex.MatchString(line)
if match {
foundSection = true
result += line + "\n"
}
} else {
result += line + "\n"
match := endRegex.MatchString(line)
if match {
foundSection = false
}
}
}
return result
}

func renderTemplate(templatePath, outputPath string, config interface{}) {
if c, ok := config.(YamlConfig); ok {
for _, s := range c.SkipTemplates {
Expand Down Expand Up @@ -904,20 +930,47 @@ func renderTemplate(templatePath, outputPath string, config interface{}) {
log.Fatalf("Error parsing template: %v", err)
}

// create output file
outputFile := filepath.Join(outputPath)
os.MkdirAll(filepath.Dir(outputFile), 0755)
f, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Error creating output file: %v", err)
}

output := new(bytes.Buffer)
err = template.Execute(output, config)
if err != nil {
log.Fatalf("Error executing template: %v", err)
}

outputFile := filepath.Join(outputPath)
existingFile, err := os.Open(outputPath)
if err != nil {
os.MkdirAll(filepath.Dir(outputFile), 0755)
} else if strings.HasSuffix(templatePath, ".go") {
existingScanner := bufio.NewScanner(existingFile)
var newContent string
currentSectionName := ""
beginRegex := regexp.MustCompile(`\/\/template:begin\s(.*?)$`)
endRegex := regexp.MustCompile(`\/\/template:end\s(.*?)$`)
for existingScanner.Scan() {
line := existingScanner.Text()
if currentSectionName == "" {
matches := beginRegex.FindStringSubmatch(line)
if len(matches) > 1 && matches[1] != "" {
currentSectionName = matches[1]
} else {
newContent += line + "\n"
}
} else {
matches := endRegex.FindStringSubmatch(line)
if len(matches) > 1 && matches[1] == currentSectionName {
currentSectionName = ""
newSection := getTemplateSection(string(output.Bytes()), matches[1])
newContent += newSection
}
}
}
output = bytes.NewBufferString(newContent)
}
// write to output file
f, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Error creating output file: %v", err)
}
f.Write(output.Bytes())
}

Expand Down
7 changes: 7 additions & 0 deletions gen/templates/generic/data_source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions gen/templates/generic/data_source_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions gen/templates/generic/model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions gen/templates/generic/resource.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions gen/templates/generic/resource_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e0cb512

Please sign in to comment.