forked from joeblew99/gu
/
components.go
106 lines (96 loc) · 2.48 KB
/
components.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// +build !js
package generators
import (
"errors"
"fmt"
"strings"
"text/template"
"github.com/influx6/faux/fmtwriter"
"github.com/gu-io/gu/generators/data"
"github.com/influx6/moz/ast"
"github.com/influx6/moz/gen"
)
// ComponentPackageGenerator which defines a function for generating a type for receiving a giving
// struct type has a notification type which can then be wired as a notification.EventDistributor.
//
// Annotation: @notification:event
func ComponentPackageGenerator(an ast.AnnotationDeclaration, pkg ast.PackageDeclaration) ([]gen.WriteDirective, error) {
if len(an.Arguments) == 0 {
return nil, errors.New("Expected atleast one argument for annotation as component name")
}
componentName := an.Arguments[0]
componentNameLower := strings.ToLower(componentName)
typeGen := gen.Block(
gen.Package(
gen.Name(componentName),
gen.Imports(
gen.Import("github.com/gu-io/gu", ""),
gen.Import("github.com/gu-io/gu/trees", ""),
gen.Import("github.com/gu-io/gu/trees/elems", ""),
gen.Import("github.com/gu-io/gu/trees/property", ""),
),
gen.Block(
gen.SourceTextWith(
string(data.Must("scaffolds/component.gen")),
template.FuncMap{},
struct {
Name string
}{
Name: componentName,
},
),
),
),
)
generatorGen := gen.Block(
gen.SourceText(
string(data.Must("scaffolds/pack-bundle.gen")),
struct {
Name string
LessFile string
Package string
TargetDir string
TargetPackage string
}{
TargetDir: "./",
Name: componentName,
Package: componentNameLower,
TargetPackage: componentNameLower,
},
),
)
pipeGen := gen.Block(
gen.Package(
gen.Name(componentName),
gen.Block(
gen.Text("\n"),
gen.Text("//go:generate go run generate.go"),
gen.Text("\n"),
gen.SourceText(
string(data.Must("scaffolds/bundle.gen")),
nil,
),
),
),
)
return []gen.WriteDirective{
{
DontOverride: true,
Dir: componentNameLower,
FileName: fmt.Sprintf("%s.go", componentNameLower),
Writer: fmtwriter.New(typeGen, true, true),
},
{
DontOverride: true,
FileName: fmt.Sprintf("%s_bundle.go", componentNameLower),
Dir: componentNameLower,
Writer: fmtwriter.New(pipeGen, true, true),
},
{
DontOverride: true,
FileName: "generate.go",
Dir: componentNameLower,
Writer: fmtwriter.New(generatorGen, true, true),
},
}, nil
}