42 lines
867 B
Go
42 lines
867 B
Go
|
package codegen
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/sunboyy/repogen/internal/code"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
}
|