-
Notifications
You must be signed in to change notification settings - Fork 0
/
drupal_arch.go
301 lines (241 loc) · 8.99 KB
/
drupal_arch.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"github.com/ghodss/yaml"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
var write_dir *string
var read_dir *string
func main() {
help := flag.String("help", "", "This listing.")
read_dir = flag.String("read_dir", ".", "Directory containing config files. Do not include trailing slash. Defaults to current directory.")
write_dir = flag.String("write_dir", ".", "Directory to write CSVs to. Do not include trailing slash. Defaults to current directory")
flag.Parse()
if *help == "help" {
flag.PrintDefaults()
os.Exit(1)
}
var contentTypes []string
var taxonomies []string
var views []string
var paragraphs []string
contentTypes = filterDirectoryList("node\\.type*")
taxonomies = filterDirectoryList("taxonomy\\.vocabulary*")
views = filterDirectoryList("views\\.view*")
paragraphs = filterDirectoryList("paragraphs\\.paragraphs_type*")
handleContentTypes(contentTypes)
handleParagraphs(paragraphs)
handleTaxonomies(taxonomies)
handleViews(views)
}
// Do all the work to write out the content_types csv and the individual content types
func handleContentTypes(contentTypes []string) {
// Header row for the CSV
var header = []string{"Name", "Type", "Description"}
var typeHeader = []string{"Label", "Machine Name", "Type", "Description", "Required", "Default Value", "Cardinality", "Translatable"}
var fields []string
var fieldName string
var fieldType string
// Create the file
file, err := os.Create(*write_dir + "/content_types.csv")
checkError(err)
defer file.Close()
// Open it for writing
writer := csv.NewWriter(file)
defer writer.Flush()
// Write the header
err = writer.Write(header)
checkError(err)
// Iterate content types array
for _, contentTypeFile := range contentTypes {
// Write a record to the main content types file for this content type
fmt.Println(contentTypeFile)
configData := getConfigData(*read_dir + "/" + contentTypeFile)
record := []string{configData["name"].(string), configData["type"].(string), fmt.Sprintf("%v", configData["description"])}
err = writer.Write(record)
checkError(err)
// Extract the actual content type machine name from the file name
parts := strings.Split(contentTypeFile, ".")
contentTypeName := parts[2]
// Create the type-specific file
typeFile, err := os.Create(*write_dir + "/content_type_" + contentTypeName + ".csv")
checkError(err)
defer typeFile.Close()
// Open it for writing
typeWriter := csv.NewWriter(typeFile)
defer typeWriter.Flush()
// Write the header
err = typeWriter.Write(typeHeader)
checkError(err)
// Find the fields for this content type
fields = filterDirectoryList("field.field.node." + contentTypeName + "*")
for _, field := range fields {
// Extract the actual field machine name from the file name
parts = strings.Split(field, ".")
fieldName = parts[4]
// Get the content-type-specific config data from this field
typeData := getConfigData(*read_dir + "/" + field)
// Get the storage-specific config data for this field
storageData := getConfigData(*read_dir + "/" + "field.storage.node." + fieldName + ".yml")
// Cardinality -1 = Unlimited
cardinality := fmt.Sprintf("%v", storageData["cardinality"])
if cardinality == "-1" {
cardinality = "Unlimited"
}
// Write the row
fieldType = fmt.Sprintf("%v", storageData["type"])
if storageData["type"].(string) == "entity_reference" {
storageSettings := storageData["settings"].(map[string]interface{})
if typeSettings, ok := typeData["settings"].(map[string]interface{}); ok {
if typeHandlerSettings, ok := typeSettings["handler_settings"].(map[string]interface{}); ok {
if targetBundles, ok := typeHandlerSettings["target_bundles"]; ok {
fieldType = fieldType + ", " + storageSettings["target_type"].(string) + " (" + fmt.Sprintf("%v", targetBundles) + ")"
}
}
}
}
record := []string{typeData["label"].(string), fieldName, fieldType, typeData["description"].(string), fmt.Sprintf("%v", typeData["required"]), fmt.Sprintf("%v", typeData["default_value"]), cardinality, fmt.Sprintf("%v", typeData["translatable"])}
err = typeWriter.Write(record)
checkError(err)
}
typeWriter.Flush()
}
}
// Do all the work to write out the paragraphs csv and the individual paragraphs types
func handleParagraphs(paragraphs []string) {
// Header row for the CSV
var header = []string{"Name", "Type", "Description"}
var typeHeader = []string{"Label", "Machine Name", "Type", "Description", "Required", "Default Value", "Cardinality", "Translatable"}
var fields []string
var fieldName string
// Create the file
file, err := os.Create(*write_dir + "/paragraphs_types.csv")
checkError(err)
defer file.Close()
// Open it for writing
writer := csv.NewWriter(file)
defer writer.Flush()
// Write the header
err = writer.Write(header)
checkError(err)
// Iterate content types array
for _, paragraphFile := range paragraphs {
// Write a record to the main content types file for this content type
configData := getConfigData(*read_dir + "/" + paragraphFile)
record := []string{configData["label"].(string), configData["id"].(string), fmt.Sprintf("%v", configData["description"])}
err = writer.Write(record)
checkError(err)
// Extract the actual content type machine name from the file name
parts := strings.Split(paragraphFile, ".")
paragraphName := parts[2]
// Create the type-specific file
typeFile, err := os.Create(*write_dir + "/paragraph_" + paragraphName + ".csv")
checkError(err)
defer typeFile.Close()
// Open it for writing
typeWriter := csv.NewWriter(typeFile)
defer typeWriter.Flush()
// Write the header
err = typeWriter.Write(typeHeader)
checkError(err)
// Find the fields for this content type
fields = filterDirectoryList("field.field.paragraph." + paragraphName + "*")
for _, field := range fields {
// Extract the actual field machine name from the file name
parts = strings.Split(field, ".")
fieldName = parts[4]
// Get the content-type-specific config data from this field
typeData := getConfigData(*read_dir + "/" + field)
// Get the storage-specific config data for this field
storageData := getConfigData(*read_dir + "/" + "field.storage.paragraph." + fieldName + ".yml")
// Cardinality -1 = Unlimited
cardinality := fmt.Sprintf("%v", storageData["cardinality"])
if cardinality == "-1" {
cardinality = "Unlimited"
}
// Write the row
record := []string{typeData["label"].(string), fieldName, storageData["type"].(string), typeData["description"].(string), fmt.Sprintf("%v", typeData["required"]), fmt.Sprintf("%v", typeData["default_value"]), cardinality, fmt.Sprintf("%v", typeData["translatable"])}
err = typeWriter.Write(record)
checkError(err)
}
}
}
// Do all the work to write out the taxonomies csv
func handleTaxonomies(taxonomies []string) {
var header = []string{"Type", "Name", "Description"}
file, err := os.Create(*write_dir + "/taxonomies.csv")
checkError(err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
err = writer.Write(header)
checkError(err)
// Parse content type files and create array of records
for _, file := range taxonomies {
configData := getConfigData(*read_dir + "/" + file)
record := []string{configData["vid"].(string), configData["name"].(string), fmt.Sprintf("%v", configData["description"])}
err = writer.Write(record)
checkError(err)
}
}
// Do all the work to write out the views csv
func handleViews(views []string) {
var header = []string{"Label", "Description"}
file, err := os.Create(*write_dir + "/views.csv")
checkError(err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
err = writer.Write(header)
checkError(err)
// Parse content type files and create array of records
for _, file := range views {
configData := getConfigData(*read_dir + "/" + file)
record := []string{configData["label"].(string), configData["description"].(string)}
err = writer.Write(record)
checkError(err)
}
}
// Given a filename (with full path), open the file, parse the yaml
// and return a map.
func getConfigData(file string) map[string]interface{} {
var configData map[string]interface{}
data, err := ioutil.ReadFile(file)
checkError(err)
jsonDoc, err := yaml.YAMLToJSON(data)
checkError(err)
err = json.Unmarshal(jsonDoc, &configData)
checkError(err)
return configData
}
// Simple and dumb error handler
func checkError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Given a regular expression, return a slice containing the files that match in readDir.
func filterDirectoryList(regex string) []string {
var results []string
// Walk the read_dir and check each file to see if it belongs in one of our slices.
filepath.Walk(*read_dir, func(path string, file os.FileInfo, _ error) error {
if !file.IsDir() {
r, err := regexp.MatchString(regex, file.Name())
if err == nil && r {
results = append(results, file.Name())
} else {
checkError(err)
}
}
return nil
})
return results
}