-
Notifications
You must be signed in to change notification settings - Fork 4
/
menu.go
156 lines (143 loc) · 4.63 KB
/
menu.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package controller
import (
"fmt"
"slices"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/file"
"projectforge.dev/projectforge/app/lib/menu"
"projectforge.dev/projectforge/app/project/export/files/helper"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
"projectforge.dev/projectforge/app/util"
)
func Menu(args *model.Args, addHeader bool, linebreak string) (*file.File, error) {
g := golang.NewFile("cmenu", []string{"app", "controller", "cmenu"}, "generated")
g.AddImport(helper.ImpAppMenu)
groups, names, orphans := sortModels(args)
if vBlock := menuBlockV(args, groups, names); len(vBlock.Lines) > 0 {
g.AddBlocks(vBlock)
}
g.AddBlocks(menuBlockGM(args, orphans))
return g.Render(addHeader, linebreak)
}
func sortModels(args *model.Args) (map[string][]string, []string, []string) {
groups := map[string][]string{}
names := make([]string, 0, len(args.Models)+len(args.Groups))
orphans := make([]string, 0)
lo.ForEach(args.Models.SortedDisplay(), func(m *model.Model, _ int) {
n := m.ProperWithGroup(args.Acronyms)
names = append(names, n)
if len(m.Group) == 0 {
orphans = append(orphans, n)
} else {
gn := m.Group[len(m.Group)-1]
curr := groups[gn]
groups[gn] = append(curr, helper.TextMenuItem+n)
}
})
return groups, names, orphans
}
func menuBlockV(args *model.Args, groups map[string][]string, names []string) *golang.Block {
nameLength := util.StringArrayMaxLength(names)
lines := lo.Map(args.Models, func(m *model.Model, _ int) string {
n := util.StringPad(m.ProperWithGroup(args.Acronyms), nameLength)
i := menuSerialize(menuItemForModel(m, args.Models, args.Acronyms), "", true)
return fmt.Sprintf("\t%s%s = %s", helper.TextMenuItem, n, strings.Join(i, "\n"))
})
slices.Sort(lines)
flatGroups := args.Groups.Flatten()
maxGroupLength := util.StringArrayMaxLength(flatGroups.Strings(""))
if len(lines) > 0 && len(flatGroups) > 0 {
lines = append(lines, "")
}
for _, grp := range flatGroups {
if grp.Provided {
continue
}
k, g := grp.Key, groups[grp.Key]
n := util.StringToCamel(k, args.Acronyms...)
msg := fmt.Sprintf("\tmenuGroup%s = &menu.Item{Key: %q, Title: %q", util.StringPad(n, maxGroupLength), grp.Key, grp.String())
if grp.Icon != "" {
msg += fmt.Sprintf(", Icon: %q", grp.Icon)
}
if grp.Route != "" {
msg += fmt.Sprintf(", Route: %q", grp.Route)
}
if len(grp.Children) > 0 || len(g) > 0 {
items := append(slices.Clone(g), grp.Children.Strings("menuGroup")...)
msg += fmt.Sprintf(", Children: menu.Items{%s}", strings.Join(items, ", "))
}
msg += "}"
lines = append(lines, msg)
}
v := golang.NewBlock("items", "var")
switch len(lines) {
case 0:
// noop
case 1:
v.W("var " + strings.TrimSpace(lines[0]))
default:
v.W("var (")
lo.ForEach(lines, func(l string, _ int) {
v.W(l)
})
v.W(")")
}
return v
}
func menuBlockGM(args *model.Args, orphans []string) *golang.Block {
gm := golang.NewBlock("generatedMenu", "func")
// gm.Lints = append(gm.Lints, "unused")
gm.W("func generatedMenu() menu.Items {")
if len(args.Groups) == 0 && len(orphans) == 0 {
gm.W("\treturn menu.Items{}")
} else {
gm.W("\treturn menu.Items{")
for _, g := range args.Groups {
if !g.Provided {
gm.W("\t\tmenuGroup%s,", util.StringToCamel(g.Proper(), args.Acronyms...))
}
}
for _, o := range orphans {
gm.W("\t\t%s%s,", helper.TextMenuItem, o)
}
gm.W("\t}")
}
gm.W("}")
return gm
}
func menuItemForModel(m *model.Model, models model.Models, acronyms []string) *menu.Item {
w := m.ProperWithGroup(acronyms)
ret := &menu.Item{Key: m.Package, Title: m.TitlePlural(), Description: m.Description, Icon: m.Icon, Route: m.Route(), Warning: w}
lo.ForEach(models.ForGroup(append(slices.Clone(m.Group), m.Name)...), func(x *model.Model, _ int) {
kid := menuItemForModel(x, models, acronyms)
ret.Children = append(ret.Children, kid)
})
return ret
}
func menuSerialize(m *menu.Item, prefix string, top bool) []string {
ws := ""
if !top {
ws = "\t"
}
var out []string
var rt string
if m.Route != "" {
rt = fmt.Sprintf(", Route: %q", "/"+m.Route)
}
var desc string
if m.Description != "" {
desc = fmt.Sprintf("Description: %q, ", m.Description)
}
args := fmt.Sprintf("Key: %q, Title: %q, %sIcon: %q%s", m.Key, m.Title, desc, m.Icon, rt)
if len(m.Children) == 0 {
out = append(out, ws+prefix+fmt.Sprintf("&menu.Item{%s}", args))
} else {
kids := lo.Map(m.Children, func(kid *menu.Item, _ int) string {
return helper.TextMenuItem + kid.Warning
})
out = append(out, ws+prefix+"&menu.Item{"+args+", Children: menu.Items{"+strings.Join(kids, ", ")+"}}")
}
return out
}