-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_helper.nf
97 lines (77 loc) · 2.84 KB
/
schema_helper.nf
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
// modules/config/schema_helper.nf
import groovy.json.JsonSlurper
def showSchemaHelp(String schemaPath) {
def schemaFile = new File("$projectDir/$schemaPath")
def jsonSlurper = new JsonSlurper()
def schema = jsonSlurper.parse(schemaFile)
// ANSI escape codes for styling
def ANSI_BOLD = "\u001B[1m"
def ANSI_UNDERLINE = "\u001B[4m"
def ANSI_RESET = "\u001B[0m"
def ANSI_GREY = "\u001B[90m"
// Title and Description
def title = schema['title'] ?: ''
def description = schema['description'] ?: ''
def output = "\n"
// Add Title and Description
if (title) {
output += "${ANSI_BOLD}${ANSI_UNDERLINE}${title}${ANSI_RESET}\n"
}
if (description) {
output += "${description}\n\n"
}
def properties = schema['items']['properties']
def requiredFields = schema['items']['required'] ?: []
def allColumns = []
properties.each { key, value ->
def column = [:]
column.key = key
column.type = extractType(value)
column.description = value['description'] ?: ''
column.defaultValue = value['default'] ?: ''
column.required = requiredFields.contains(key)
allColumns << column
}
def maxKeyLength = allColumns.collect { it.key.size() }.max() ?: 10
def maxTypeLength = allColumns.collect { it.type.size() }.max() ?: 10
// Underlined Headers
output += "${ANSI_UNDERLINE}Required columns:${ANSI_RESET}\n"
allColumns.findAll { it.required }.each { col ->
def typeStr = "${ANSI_GREY}[${col.type}]${ANSI_RESET}"
def desc = col.description
if (col.defaultValue) {
desc += " ${ANSI_GREY}[default: ${col.defaultValue}]${ANSI_RESET}"
}
output += String.format(" %-${maxKeyLength}s %s %s\n", col.key, typeStr, desc)
}
output += "\n${ANSI_UNDERLINE}Optional columns:${ANSI_RESET}\n"
allColumns.findAll { !it.required }.each { col ->
def typeStr = "${ANSI_GREY}[${col.type}]${ANSI_RESET}"
def desc = col.description
if (col.defaultValue) {
desc += " ${ANSI_GREY}[default: ${col.defaultValue}]${ANSI_RESET}"
}
output += String.format(" %-${maxKeyLength}s %s %s\n", col.key, typeStr, desc)
}
output += "-${ANSI_GREY}----------------------------------------------------${ANSI_RESET}-"
}
def extractType(value) {
def types = []
if (value['type']) {
types << value['type']
} else if (value['format']) {
types << value['format']
}
if (value['anyOf']) {
value['anyOf'].each { subValue ->
if (subValue['type']) {
types << subValue['type']
} else if (subValue['format']) {
types << subValue['format']
}
}
}
types = types.unique()
def typeStr = types.join('/')
return typeStr ?: 'unknown'
}