2022-10-18 12:37:50 +00:00
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-24 11:01:50 +00:00
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
2022-10-18 12:37:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const baseTemplate = `// Code generated by {{.Program}}. DO NOT EDIT.
|
|
|
|
package {{.PackageName}}
|
|
|
|
|
|
|
|
import (
|
|
|
|
{{.GenImports}}
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
|
|
|
type baseTemplateData struct {
|
|
|
|
Program string
|
|
|
|
PackageName string
|
|
|
|
Imports [][]code.Import
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data baseTemplateData) GenImports() string {
|
|
|
|
var sections []string
|
|
|
|
for _, importSection := range data.Imports {
|
|
|
|
var section []string
|
|
|
|
for _, imp := range importSection {
|
|
|
|
section = append(section, data.generateImportLine(imp))
|
|
|
|
}
|
|
|
|
sections = append(sections, strings.Join(section, "\n"))
|
|
|
|
}
|
|
|
|
return strings.Join(sections, "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data baseTemplateData) generateImportLine(imp code.Import) string {
|
|
|
|
if imp.Name == "" {
|
|
|
|
return fmt.Sprintf("\t\"%s\"", imp.Path)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("\t%s \"%s\"", imp.Name, imp.Path)
|
|
|
|
}
|