|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "io/ioutil" |
6 | | - "os" |
7 | | - "path/filepath" |
8 | | - "sort" |
9 | | - "strconv" |
10 | | - "strings" |
11 | | - |
12 | | - "github.com/gertd/go-pluralize" |
13 | | - "github.com/invopop/jsonschema" |
14 | 4 | "github.com/loft-sh/devspace/docs/hack/util" |
15 | | - "github.com/loft-sh/devspace/pkg/devspace/config/versions" |
16 | 5 | "github.com/loft-sh/devspace/pkg/devspace/config/versions/latest" |
17 | 6 | ) |
18 | 7 |
|
19 | 8 | const configPartialBasePath = "docs/pages/configuration/_partials/" |
20 | | -const nameFieldName = "name" |
21 | | -const versionFieldName = "version" |
22 | | -const groupKey = "group" |
23 | | -const groupNameKey = "group_name" |
24 | | -const prefixSeparator = "/" |
25 | | -const anchorSeparator = "-" |
26 | | - |
27 | | -var pluralizeClient = pluralize.NewClient() |
28 | 9 |
|
29 | 10 | func main() { |
30 | | - r := new(jsonschema.Reflector) |
31 | | - r.AllowAdditionalProperties = true |
32 | | - r.PreferYAMLSchema = true |
33 | | - r.RequiredFromJSONSchemaTags = true |
34 | | - r.YAMLEmbeddedStructs = false |
35 | | - r.ExpandedStruct = true |
36 | | - |
37 | | - err := r.AddGoComments("github.com/loft-sh/devspace", "./pkg/devspace/config/versions/latest") |
38 | | - if err != nil { |
39 | | - panic(err) |
40 | | - } |
41 | | - |
42 | | - schema := r.Reflect(&latest.Config{}) |
43 | | - |
44 | | - versionField, ok := schema.Properties.Get(versionFieldName) |
45 | | - if ok { |
46 | | - if fieldSchema, ok := versionField.(*jsonschema.Schema); ok { |
47 | | - versionEnum := []string{} |
48 | | - for version := range versions.VersionLoader { |
49 | | - versionEnum = append(versionEnum, version) |
50 | | - } |
51 | | - |
52 | | - sort.SliceStable(versionEnum, func(a, b int) bool { |
53 | | - majorA, _ := strconv.Atoi(string(versionEnum[a][1])) |
54 | | - majorB, _ := strconv.Atoi(string(versionEnum[b][1])) |
55 | | - minorA, _ := strconv.Atoi(string(versionEnum[a][6:])) |
56 | | - minorB, _ := strconv.Atoi(string(versionEnum[b][6:])) |
57 | | - |
58 | | - if majorA == majorB { |
59 | | - return minorA > minorB |
60 | | - } else { |
61 | | - return majorA > majorB |
62 | | - } |
63 | | - }) |
64 | | - |
65 | | - fieldSchema.Enum = []interface{}{} |
66 | | - for _, version := range versionEnum { |
67 | | - fieldSchema.Enum = append(fieldSchema.Enum, version) |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - createSections("", schema, schema.Definitions, 1, false) |
73 | | -} |
74 | | - |
75 | | -func createSections(prefix string, schema *jsonschema.Schema, definitions jsonschema.Definitions, depth int, parentIsNameObjectMap bool) string { |
76 | | - partialImports := &[]string{} |
77 | | - content := "" |
78 | | - headlinePrefix := strings.Repeat("#", depth+1) + " " |
79 | | - anchorPrefix := strings.TrimPrefix(strings.ReplaceAll(prefix, prefixSeparator, anchorSeparator), anchorSeparator) |
80 | | - |
81 | | - groups := map[string]*util.Group{} |
82 | | - |
83 | | - for _, fieldName := range schema.Properties.Keys() { |
84 | | - if parentIsNameObjectMap && fieldName == nameFieldName { |
85 | | - continue |
86 | | - } |
87 | | - |
88 | | - field, ok := schema.Properties.Get(fieldName) |
89 | | - if ok { |
90 | | - if fieldSchema, ok := field.(*jsonschema.Schema); ok { |
91 | | - fieldContent := "" |
92 | | - fieldFile := fmt.Sprintf(configPartialBasePath+"%s/%s%s.mdx", latest.Version, prefix, fieldName) |
93 | | - fieldType := "object" |
94 | | - isNameObjectMap := false |
95 | | - groupID, _ := fieldSchema.Extras[groupKey].(string) |
96 | | - |
97 | | - var patternPropertySchema *jsonschema.Schema |
98 | | - var nestedSchema *jsonschema.Schema |
99 | | - |
100 | | - ref := "" |
101 | | - if fieldSchema.Type == "array" { |
102 | | - ref = fieldSchema.Items.Ref |
103 | | - fieldType = "object[]" |
104 | | - } else if patternPropertySchema, ok = fieldSchema.PatternProperties[".*"]; ok { |
105 | | - ref = patternPropertySchema.Ref |
106 | | - isNameObjectMap = true |
107 | | - } else if fieldSchema.Ref != "" { |
108 | | - ref = fieldSchema.Ref |
109 | | - } |
110 | | - |
111 | | - if ref != "" { |
112 | | - refSplit := strings.Split(ref, "/") |
113 | | - nestedSchema, ok = definitions[refSplit[len(refSplit)-1]] |
114 | | - |
115 | | - if ok { |
116 | | - newPrefix := prefix + fieldName + prefixSeparator |
117 | | - createSections(newPrefix, nestedSchema, definitions, depth+1, isNameObjectMap) |
118 | | - } |
119 | | - } else { |
120 | | - required := contains(schema.Required, fieldName) |
121 | | - fieldDefault := "" |
122 | | - |
123 | | - fieldType = fieldSchema.Type |
124 | | - if fieldType == "" && fieldSchema.OneOf != nil { |
125 | | - for _, oneOfType := range fieldSchema.OneOf { |
126 | | - if fieldType != "" { |
127 | | - fieldType = fieldType + "|" |
128 | | - } |
129 | | - fieldType = fieldType + oneOfType.Type |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - if isNameObjectMap { |
134 | | - fieldNameSingular := pluralizeClient.Singular(fieldName) |
135 | | - fieldType = "<" + fieldNameSingular + "_name>:" |
136 | | - |
137 | | - if patternPropertySchema != nil && patternPropertySchema.Type != "" { |
138 | | - fieldType = fieldType + patternPropertySchema.Type |
139 | | - } else { |
140 | | - fieldType = fieldType + "object" |
141 | | - } |
142 | | - } |
143 | | - |
144 | | - if fieldType == "array" { |
145 | | - fieldType = fieldSchema.Items.Type + "[]" |
146 | | - } |
147 | | - |
148 | | - if fieldType == "boolean" { |
149 | | - fieldDefault = "false" |
150 | | - if required { |
151 | | - fieldDefault = "true" |
152 | | - required = false |
153 | | - } |
154 | | - } else { |
155 | | - fieldDefault, ok = fieldSchema.Default.(string) |
156 | | - if !ok { |
157 | | - fieldDefault = "" |
158 | | - } |
159 | | - } |
| 11 | + repository := "github.com/loft-sh/devspace" |
| 12 | + configGoPackage := "./pkg/devspace/config/versions/latest" |
| 13 | + versionedConfigBasePath := configPartialBasePath + latest.Version + "/" |
160 | 14 |
|
161 | | - enumValues := GetEumValues(fieldSchema, required, &fieldDefault) |
162 | | - |
163 | | - anchorName := anchorPrefix + fieldName |
164 | | - fieldContent = fmt.Sprintf(util.TemplateConfigField, false, " open", headlinePrefix, fieldName, required, fieldType, fieldDefault, enumValues, anchorName, fieldSchema.Description, "") |
165 | | - |
166 | | - err := os.MkdirAll(filepath.Dir(fieldFile), os.ModePerm) |
167 | | - if err != nil { |
168 | | - panic(err) |
169 | | - } |
170 | | - |
171 | | - err = ioutil.WriteFile(fieldFile, []byte(fieldContent), os.ModePerm) |
172 | | - if err != nil { |
173 | | - panic(err) |
174 | | - } |
175 | | - } |
176 | | - |
177 | | - fieldPartial := fmt.Sprintf(util.TemplatePartialUse, util.GetPartialImportName(fieldFile)) |
178 | | - if ref != "" { |
179 | | - if isNameObjectMap && nestedSchema != nil { |
180 | | - nameField, ok := nestedSchema.Properties.Get(nameFieldName) |
181 | | - if ok { |
182 | | - if nameFieldSchema, ok := nameField.(*jsonschema.Schema); ok { |
183 | | - fieldNameSingular := pluralizeClient.Singular(fieldName) |
184 | | - nameFieldRequired := true |
185 | | - nameFieldDefault := "" |
186 | | - nameFieldEnumValues := GetEumValues(nameFieldSchema, nameFieldRequired, &nameFieldDefault) |
187 | | - |
188 | | - anchorName := anchorPrefix + nameFieldName |
189 | | - fieldPartial = fmt.Sprintf(util.TemplateConfigField, true, "open", headlinePrefix, "<"+fieldNameSingular+"_"+nameFieldName+">", nameFieldRequired, "string", nameFieldDefault, nameFieldEnumValues, anchorName, nameFieldSchema.Description, fieldPartial) |
190 | | - fieldType = "<" + fieldNameSingular + "_name>:object" |
191 | | - } |
192 | | - } |
193 | | - } |
194 | | - |
195 | | - anchorName := anchorPrefix + fieldName |
196 | | - fieldPartial = fmt.Sprintf(util.TemplateConfigField, true, "", headlinePrefix, fieldName, false, fieldType, "", "", anchorName, fieldSchema.Description, fieldPartial) |
197 | | - } |
198 | | - |
199 | | - if groupID != "" { |
200 | | - group, ok := groups[groupID] |
201 | | - if !ok { |
202 | | - group = &util.Group{ |
203 | | - File: fmt.Sprintf(configPartialBasePath+"%s/%sgroup_%s.mdx", latest.Version, prefix, groupID), |
204 | | - Imports: &[]string{}, |
205 | | - } |
206 | | - groups[groupID] = group |
207 | | - |
208 | | - groupPartial := fmt.Sprintf(util.TemplatePartialUse, util.GetPartialImportName(group.File)) |
209 | | - |
210 | | - content = content + "\n\n" + groupPartial |
211 | | - *partialImports = append(*partialImports, group.File) |
212 | | - } |
213 | | - |
214 | | - if groupName, ok := fieldSchema.Extras[groupNameKey]; ok { |
215 | | - group.Name = groupName.(string) |
216 | | - } |
217 | | - |
218 | | - group.Content = group.Content + fieldPartial |
219 | | - *group.Imports = append(*group.Imports, fieldFile) |
220 | | - } else { |
221 | | - content = content + "\n\n" + fieldPartial |
222 | | - *partialImports = append(*partialImports, fieldFile) |
223 | | - } |
224 | | - } |
225 | | - } |
226 | | - } |
227 | | - |
228 | | - util.ProcessGroups(groups) |
229 | | - |
230 | | - if prefix == "" { |
231 | | - prefix = "reference" |
232 | | - } |
233 | | - |
234 | | - pageFile := fmt.Sprintf(configPartialBasePath+"%s/%s.mdx", latest.Version, strings.TrimSuffix(prefix, "/")) |
235 | | - |
236 | | - importContent := "" |
237 | | - for _, partialFile := range *partialImports { |
238 | | - importContent = importContent + util.GetPartialImport(partialFile, pageFile) |
239 | | - } |
240 | | - |
241 | | - content = fmt.Sprintf("%s%s", importContent, content) |
242 | | - |
243 | | - err := ioutil.WriteFile(pageFile, []byte(content), os.ModePerm) |
244 | | - if err != nil { |
245 | | - panic(err) |
246 | | - } |
247 | | - |
248 | | - //fmt.Println(content) |
249 | | - |
250 | | - return content |
251 | | -} |
252 | | - |
253 | | -func GetEumValues(fieldSchema *jsonschema.Schema, required bool, fieldDefault *string) string { |
254 | | - enumValues := "" |
255 | | - if fieldSchema.Enum != nil { |
256 | | - for i, enumVal := range fieldSchema.Enum { |
257 | | - enumValString, ok := enumVal.(string) |
258 | | - if ok { |
259 | | - if i == 0 && !required && *fieldDefault == "" { |
260 | | - *fieldDefault = enumValString |
261 | | - } |
262 | | - |
263 | | - if enumValues != "" { |
264 | | - enumValues = enumValues + "<br/>" |
265 | | - } |
266 | | - enumValues = enumValues + enumValString |
267 | | - } |
268 | | - } |
269 | | - enumValues = fmt.Sprintf("<span>%s</span>", enumValues) |
270 | | - } |
271 | | - return enumValues |
272 | | -} |
| 15 | + schema := util.GenerateSchema(latest.Config{}, repository, configGoPackage) |
| 16 | + util.GenerateReference(schema, versionedConfigBasePath) |
273 | 17 |
|
274 | | -func contains(s []string, e string) bool { |
275 | | - for _, a := range s { |
276 | | - if a == e { |
277 | | - return true |
278 | | - } |
279 | | - } |
280 | | - return false |
| 18 | + schema = util.GenerateSchema(latest.ComponentConfig{}, repository, configGoPackage) |
| 19 | + util.GenerateReference(schema, versionedConfigBasePath+"deployments/helm/componentChart/") |
281 | 20 | } |
0 commit comments