forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularity: module builders generator (prebid#2491)
- Loading branch information
Showing
4 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package modules | ||
|
||
{{if .}} | ||
import ( | ||
{{- range .}} | ||
{{.Vendor}}{{.Module | Title}} "github.com/prebid/prebid-server/modules/{{.Vendor}}/{{.Module}}" | ||
{{- end}} | ||
) | ||
{{end}} | ||
|
||
// builders returns mapping between module name and its builder | ||
// vendor and module names are chosen based on the module directory name | ||
func builders() ModuleBuilders { | ||
return ModuleBuilders{ | ||
{{- range .}} | ||
"{{.Vendor}}": { | ||
"{{.Module}}": {{.Vendor}}{{.Module | Title}}.Builder, | ||
}, | ||
{{- end}} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//go:build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/format" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var ( | ||
r = regexp.MustCompile("^([^/]+)/([^/]+)/module.go$") | ||
tmplName = "builder.tmpl" | ||
outName = "builder.go" | ||
) | ||
|
||
type Module struct { | ||
Vendor string | ||
Module string | ||
} | ||
|
||
func main() { | ||
var modules []Module | ||
|
||
filepath.WalkDir("./", func(path string, d fs.DirEntry, err error) error { | ||
if !r.MatchString(path) { | ||
return nil | ||
} | ||
match := r.FindStringSubmatch(path) | ||
modules = append(modules, Module{ | ||
Vendor: match[1], | ||
Module: match[2], | ||
}) | ||
return nil | ||
}) | ||
|
||
funcMap := template.FuncMap{"Title": strings.Title} | ||
t, err := template.New(tmplName).Funcs(funcMap).ParseFiles(fmt.Sprintf("generator/%s", tmplName)) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to parse builder template: %s", err)) | ||
} | ||
|
||
f, err := os.Create(outName) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to create %s file: %s", outName, err)) | ||
} | ||
defer f.Close() | ||
|
||
var buf bytes.Buffer | ||
if err = t.Execute(&buf, modules); err != nil { | ||
panic(fmt.Sprintf("failed to generate %s file content: %s", outName, err)) | ||
} | ||
|
||
content, err := format.Source(buf.Bytes()) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to format generated code: %s", err)) | ||
} | ||
|
||
if _, err = f.Write(content); err != nil { | ||
panic(fmt.Sprintf("failed to write file content: %s", err)) | ||
} | ||
|
||
fmt.Printf("%s file successfully generated\n", outName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters