-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathsort-tomlfiles.sh
More file actions
executable file
·139 lines (118 loc) · 3.52 KB
/
sort-tomlfiles.sh
File metadata and controls
executable file
·139 lines (118 loc) · 3.52 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
#!/bin/sh
# Sort all tab_data.toml files in the core/tabs directory
# - Sorts [[data]] blocks alphabetically by name
# - Sorts [[data.entries]] within each block alphabetically by name
# - Places blocks with entries before blocks without entries
set -e
echo "Sorting all tab_data.toml files..."
find core/tabs -name "tab_data.toml" | while IFS= read -r file; do
echo "Processing: $file"
awk '
BEGIN {
in_data = 0
in_entry = 0
data_count = 0
entry_count = 0
}
# Capture file header (lines before first [[data]])
!in_data && !/^\[\[data\]\]$/ {
header = header $0 "\n"
next
}
# Start of new [[data]] block
/^\[\[data\]\]$/ {
save_entry_block()
save_data_block()
data_header = $0 "\n"
in_data = 1
in_entry = 0
data_name = ""
next
}
# Start of new [[data.entries]] block
in_data && /^\[\[data\.entries\]\]$/ {
save_entry_block()
entry_block = $0 "\n"
in_entry = 1
entry_name = ""
next
}
# Lines inside [[data.entries]]
in_entry {
entry_block = entry_block $0 "\n"
if ($0 ~ /^name = / && !entry_name) {
entry_name = extract_name($0)
}
next
}
# Lines inside [[data]] but outside [[data.entries]]
in_data {
data_header = data_header $0 "\n"
if ($0 ~ /^name = / && !data_name) {
data_name = extract_name($0)
}
next
}
END {
save_entry_block()
save_data_block()
print_sorted_output()
}
# Helper function to extract and normalize name field
function extract_name(line) {
gsub(/^name = "/, "", line)
gsub(/".*$/, "", line)
return tolower(line)
}
# Save current entry block
function save_entry_block() {
if (entry_block && entry_name) {
entry_blocks[entry_name] = entry_block
entry_names[++entry_count] = entry_name
entry_block = ""
entry_name = ""
}
}
# Save current data block with sorted entries
function save_data_block() {
if (data_name) {
# Sort and append entries
if (entry_count > 0) {
n = asort(entry_names, sorted)
for (i = 1; i <= n; i++) {
data_header = data_header entry_blocks[sorted[i]]
}
has_entries[data_name] = 1
}
data_blocks[data_name] = data_header
data_names[++data_count] = data_name
# Reset for next block
entry_count = 0
delete entry_blocks
delete entry_names
data_header = ""
data_name = ""
}
}
# Print sorted output
function print_sorted_output() {
printf "%s", header
if (data_count > 0) {
n = asort(data_names, sorted)
# First: blocks with entries
for (i = 1; i <= n; i++) {
if (has_entries[sorted[i]]) {
printf "%s", data_blocks[sorted[i]]
}
}
# Second: blocks without entries
for (i = 1; i <= n; i++) {
if (!has_entries[sorted[i]]) {
printf "%s", data_blocks[sorted[i]]
}
}
}
}
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
done
echo "Done!"