-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmain-generateschema.go
More file actions
219 lines (194 loc) · 7.94 KB
/
Copy pathmain-generateschema.go
File metadata and controls
219 lines (194 loc) · 7.94 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"github.com/invopop/jsonschema"
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wconfig"
)
const WaveSchemaSettingsFileName = "schema/settings.json"
const WaveSchemaConnectionsFileName = "schema/connections.json"
const WaveSchemaAiPresetsFileName = "schema/aipresets.json"
const WaveSchemaWidgetsFileName = "schema/widgets.json"
const WaveSchemaBackgroundsFileName = "schema/backgrounds.json"
const WaveSchemaWaveAIFileName = "schema/waveai.json"
// ViewNameType is a string type whose JSON Schema offers enum suggestions for the most
// common widget view names while still accepting any arbitrary string value.
type ViewNameType string
func (ViewNameType) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
{
Enum: []any{"term", "preview", "web", "sysinfo", "launcher"},
},
{
Type: "string",
},
},
}
}
// ControllerNameType is a string type whose JSON Schema offers enum suggestions for the
// known block controller names while still accepting any arbitrary string value.
type ControllerNameType string
func (ControllerNameType) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
{
Enum: []any{"shell", "cmd"},
},
{
Type: "string",
},
},
}
}
// WidgetsMetaSchemaHints provides schema hints for the blockdef.meta field in widget configs.
// It covers the most common keys used when defining widgets: view, file, url, controller,
// cmd and cmd:* options, and term:* options.
type WidgetsMetaSchemaHints struct {
View ViewNameType `json:"view,omitempty"`
File string `json:"file,omitempty"`
Url string `json:"url,omitempty"`
Controller ControllerNameType `json:"controller,omitempty"`
Cmd string `json:"cmd,omitempty"`
CmdInteractive bool `json:"cmd:interactive,omitempty"`
CmdLogin bool `json:"cmd:login,omitempty"`
CmdPersistent bool `json:"cmd:persistent,omitempty"`
CmdRunOnStart bool `json:"cmd:runonstart,omitempty"`
CmdClearOnStart bool `json:"cmd:clearonstart,omitempty"`
CmdRunOnce bool `json:"cmd:runonce,omitempty"`
CmdCloseOnExit bool `json:"cmd:closeonexit,omitempty"`
CmdCloseOnExitForce bool `json:"cmd:closeonexitforce,omitempty"`
CmdCloseOnExitDelay float64 `json:"cmd:closeonexitdelay,omitempty"`
CmdNoWsh bool `json:"cmd:nowsh,omitempty"`
CmdArgs []string `json:"cmd:args,omitempty"`
CmdShell bool `json:"cmd:shell,omitempty"`
CmdAllowConnChange bool `json:"cmd:allowconnchange,omitempty"`
CmdEnv map[string]string `json:"cmd:env,omitempty"`
CmdCwd string `json:"cmd:cwd,omitempty"`
CmdInitScript string `json:"cmd:initscript,omitempty"`
CmdInitScriptSh string `json:"cmd:initscript.sh,omitempty"`
CmdInitScriptBash string `json:"cmd:initscript.bash,omitempty"`
CmdInitScriptZsh string `json:"cmd:initscript.zsh,omitempty"`
CmdInitScriptPwsh string `json:"cmd:initscript.pwsh,omitempty"`
CmdInitScriptFish string `json:"cmd:initscript.fish,omitempty"`
TermFontSize int `json:"term:fontsize,omitempty"`
TermFontFamily string `json:"term:fontfamily,omitempty"`
TermMode string `json:"term:mode,omitempty"`
TermTheme string `json:"term:theme,omitempty"`
TermLocalShellPath string `json:"term:localshellpath,omitempty"`
TermLocalShellOpts []string `json:"term:localshellopts,omitempty"`
TermScrollback *int `json:"term:scrollback,omitempty"`
TermTransparency *float64 `json:"term:transparency,omitempty"`
TermAllowBracketedPaste *bool `json:"term:allowbracketedpaste,omitempty"`
TermShiftEnterNewline *bool `json:"term:shiftenternewline,omitempty"`
TermCtrlEnter *bool `json:"term:ctrlenter,omitempty"`
TermMacOptionIsMeta *bool `json:"term:macoptionismeta,omitempty"`
TermBellSound *bool `json:"term:bellsound,omitempty"`
TermBellIndicator *bool `json:"term:bellindicator,omitempty"`
TermDurable *bool `json:"term:durable,omitempty"`
}
// allowNullValues wraps the top-level additionalProperties of a map schema with
// anyOf: [originalSchema, {type: "null"}] so that setting a key to null is valid
// (e.g. "bg@foo": null to remove a default entry).
func allowNullValues(schema *jsonschema.Schema) {
if schema.AdditionalProperties != nil && schema.AdditionalProperties != jsonschema.TrueSchema && schema.AdditionalProperties != jsonschema.FalseSchema {
original := schema.AdditionalProperties
schema.AdditionalProperties = &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
original,
{Type: "null"},
},
}
}
}
func generateSchema(template any, dir string, allowNull bool) error {
settingsSchema := jsonschema.Reflect(template)
if allowNull {
allowNullValues(settingsSchema)
}
jsonSettingsSchema, err := json.MarshalIndent(settingsSchema, "", " ")
if err != nil {
return fmt.Errorf("failed to parse local schema: %w", err)
}
written, err := utilfn.WriteFileIfDifferent(dir, jsonSettingsSchema)
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", dir)
}
if err != nil {
return fmt.Errorf("failed to write local schema: %w", err)
}
return nil
}
func generateWidgetsSchema(dir string) error {
metaT := reflect.TypeOf(waveobj.MetaMapType(nil))
// Build the hints schema once using an expanded reflector
hr := &jsonschema.Reflector{
DoNotReference: true,
ExpandedStruct: true,
AllowAdditionalProperties: true,
}
hintSchema := hr.Reflect(&WidgetsMetaSchemaHints{})
r := &jsonschema.Reflector{}
r.Mapper = func(t reflect.Type) *jsonschema.Schema {
if t == metaT {
return &jsonschema.Schema{
Type: "object",
Properties: hintSchema.Properties,
AdditionalProperties: jsonschema.TrueSchema,
}
}
return nil
}
widgetsTemplate := make(map[string]wconfig.WidgetConfigType)
widgetsSchema := r.Reflect(&widgetsTemplate)
allowNullValues(widgetsSchema)
jsonWidgetsSchema, err := json.MarshalIndent(widgetsSchema, "", " ")
if err != nil {
return fmt.Errorf("failed to parse widgets schema: %w", err)
}
written, err := utilfn.WriteFileIfDifferent(dir, jsonWidgetsSchema)
if !written {
fmt.Fprintf(os.Stderr, "no changes to %s\n", dir)
}
if err != nil {
return fmt.Errorf("failed to write widgets schema: %w", err)
}
return nil
}
func main() {
err := generateSchema(&wconfig.SettingsType{}, WaveSchemaSettingsFileName, false)
if err != nil {
log.Fatalf("settings schema error: %v", err)
}
connectionTemplate := make(map[string]wconfig.ConnKeywords)
err = generateSchema(&connectionTemplate, WaveSchemaConnectionsFileName, false)
if err != nil {
log.Fatalf("connections schema error: %v", err)
}
aiPresetsTemplate := make(map[string]wconfig.AiSettingsType)
err = generateSchema(&aiPresetsTemplate, WaveSchemaAiPresetsFileName, false)
if err != nil {
log.Fatalf("ai presets schema error: %v", err)
}
err = generateWidgetsSchema(WaveSchemaWidgetsFileName)
if err != nil {
log.Fatalf("widgets schema error: %v", err)
}
backgroundsTemplate := make(map[string]wconfig.BackgroundConfigType)
err = generateSchema(&backgroundsTemplate, WaveSchemaBackgroundsFileName, true)
if err != nil {
log.Fatalf("backgrounds schema error: %v", err)
}
waveAITemplate := make(map[string]wconfig.AIModeConfigType)
err = generateSchema(&waveAITemplate, WaveSchemaWaveAIFileName, false)
if err != nil {
log.Fatalf("waveai schema error: %v", err)
}
}