Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package plushhelpers
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gobuffalo/plush/v4"
|
|
|
|
"github.com/ignite/cli/v29/ignite/pkg/xstrings"
|
|
"github.com/ignite/cli/v29/ignite/templates/field"
|
|
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
|
|
)
|
|
|
|
// ExtendPlushContext sets available field helpers on the provided context.
|
|
func ExtendPlushContext(ctx *plush.Context) {
|
|
ctx.Set("mergeGoImports", mergeGoImports)
|
|
ctx.Set("mergeProtoImports", mergeProtoImports)
|
|
ctx.Set("mergeCustomImports", mergeCustomImports)
|
|
ctx.Set("appendFieldsAndMergeCustomImports", appendFieldsAndMergeCustomImports)
|
|
ctx.Set("title", xstrings.Title)
|
|
ctx.Set("toLower", strings.ToLower)
|
|
}
|
|
|
|
func appendFieldsAndMergeCustomImports(f field.Field, fields ...field.Fields) []string {
|
|
return mergeCustomImports(append(fields, field.Fields{f})...)
|
|
}
|
|
|
|
func mergeCustomImports(fields ...field.Fields) []string {
|
|
allImports := make([]string, 0)
|
|
exist := make(map[string]struct{})
|
|
for _, field := range fields {
|
|
for _, customImport := range field.Custom() {
|
|
if _, ok := exist[customImport]; ok {
|
|
continue
|
|
}
|
|
exist[customImport] = struct{}{}
|
|
allImports = append(allImports, customImport)
|
|
}
|
|
}
|
|
return allImports
|
|
}
|
|
|
|
func mergeGoImports(fields ...field.Fields) []datatype.GoImport {
|
|
allImports := make([]datatype.GoImport, 0)
|
|
exist := make(map[string]struct{})
|
|
for _, fields := range fields {
|
|
for _, goImport := range fields.GoCLIImports() {
|
|
if _, ok := exist[goImport.Name]; ok {
|
|
continue
|
|
}
|
|
exist[goImport.Name] = struct{}{}
|
|
allImports = append(allImports, goImport)
|
|
}
|
|
}
|
|
return allImports
|
|
}
|
|
|
|
func mergeProtoImports(fields ...field.Fields) []string {
|
|
allImports := make([]string, 0)
|
|
exist := make(map[string]struct{})
|
|
for _, fields := range fields {
|
|
for _, protoImport := range fields.ProtoImports() {
|
|
if _, ok := exist[protoImport]; ok {
|
|
continue
|
|
}
|
|
exist[protoImport] = struct{}{}
|
|
allImports = append(allImports, protoImport)
|
|
}
|
|
}
|
|
return allImports
|
|
}
|