-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.sh
executable file
·197 lines (185 loc) · 6.89 KB
/
main.sh
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
#!/bin/bash
# env MAPPING_PATH: yaml(s) path
# env WARNINGS:
# print more visible notice if "notice"
# fail if "error"
# skip if "skip"
# env WARNING_NO_CID
# env WARNING_NO_CONNECTORS
# env WARNING_NO_IMAGE
# env WARNING_NO_PINS
# env WARNING_DUPE
# same options as WARNINGS
if [ "$DEBUG" = "true" ]; then
echo "WARNINGS: $WARNINGS"
echo "WARNING_NO_CID: $WARNING_NO_CID"
echo "WARNING_NO_CONNECTORS: $WARNING_NO_CONNECTORS"
echo "WARNING_NO_IMAGE: $WARNING_NO_IMAGE"
echo "WARNING_NO_PINS: $WARNING_NO_PINS"
echo "WARNING_DUPE: $WARNING_DUPE"
fi
SCRIPTDIR=$(dirname "$0")
handle_warning ()
{
if [ "$WARNINGS" = "error" ] && [ "$1" = "unset" ] || [ "$1" = "error" ]; then
echo "::error:: $2"
exit 1;
elif [ "$WARNINGS" = "notice" ] && [ "$1" = "unset" ] || [ "$1" = "notice" ]; then
echo "::notice:: $2"
elif [ "$WARNINGS" = "skip" ] && [ "$1" = "unset" ] || [ "$1" = "skip" ]; then
echo "$2"
return 1
else
echo "$2"
fi
return 0
}
# Sort all the yaml files by the order field they may contain.
CONNECTORS=$(echo "$MAPPING_PATH" | while read p; do find . -path "./$p"; done)
if [ "$DEBUG" = "true" ]; then
echo "Search Path: $MAPPING_PATH"
echo "Found YAMLs: $CONNECTORS"
fi
if [ $(echo "$CONNECTORS" | grep -v ^$ | wc -l) -eq 0 ]; then
handle_warning "$WARNING_NO_CONNECTORS" "WARNING: No connectors found"
exit
fi
FILES=$(for f in $CONNECTORS; do
ORDER=$(yq e '.info.order' "$f")
echo "$f $ORDER"
done)
CONNECTORS=$(echo "$FILES" | sort -k2 | cut -d ' ' -f 1)
# Make a temp directory for symlinks to actual files.
mkdir -p pinoutstmp
for c in $CONNECTORS; do
echo "Processing: $c"
if [ $(yq e '.pins.[].pin' "$c" | wc -c) -lt 1 ]; then
if ! handle_warning "$WARNING_NO_PINS" "WARNING: No pins found in definition $c"; then continue; fi
fi
DUPES=$(yq e '.pins.[].pin' "$c" | grep -v "null" | sort | uniq -d | tr -d '\n')
if [ -n "$DUPES" ]; then
if ! handle_warning "$WARNING_DUPE" "WARNING: Duplicate pins in $c: $DUPES"; then continue; fi
fi
POSDUPES=$(yq e '.info.pins.[].pin' "$c" | grep -v "null" | sort | uniq -d | tr -d '\n')
POSDUPES+=$(yq e '.info.image.pins.[].pin' "$c" | grep -v "null" | sort | uniq -d | tr -d '\n')
if [ -n "$POSDUPES" ]; then
if ! handle_warning "$WARNING_DUPE" "WARNING: Duplicate pin positionss in $c: $POSDUPES"; then continue; fi
fi
# Get the directory and title, if they exist
DIRECTORY=$(yq e '.info.directory' "$c")
TITLE=$(yq e '.info.title' "$c")
# Build the temp path, removing leading ./ and /
DIR="pinoutstmp/"$(dirname "$c" | sed -e 's/^\.\///' -e 's/^\///')
# If we have a directory field
if [ "$DIRECTORY" != "null" ]; then
if [ "$DEBUG" = "true" ]; then
echo "Found Directory: $DIRECTORY"
fi
# If temp dir exists
if [ -d "$DIR" ]; then
# If temp dir isn't symbolic link
if [ ! -L "$DIR" ]; then
if [ "$DEBUG" = "true" ]; then
echo "Found Directory is normal directory"
fi
# It must be a real directory, move it to the final dir and link it
mkdir -p "pinouts/$DIRECTORY"
mv "$DIR"/* "pinouts/$DIRECTORY"
rmdir "$DIR"
ln -rs "pinouts/$DIRECTORY" "$DIR"
# If temp dir is a link, but not to the specified directory
elif TARGET=$(readlink -f "$DIR") && [ "$TARGET" != "$(realpath "pinouts/$DIRECTORY")" ]; then
if [ "$DEBUG" = "true" ]; then
echo "Found Directory is link to another directory: $TARGET vs $(realpath "pinouts/$DIRECTORY")"
fi
# Make specified directory
mkdir -p "pinouts/$DIRECTORY"
# Move the contents of the directory the link points to, and remove that directory
mv "$(readlink -f "$DIR")"/* "pinouts/$DIRECTORY"
rmdir "$(readlink -f "$DIR")"
# Link to specified directory
rm "$DIR"
ln -rs "pinouts/$DIRECTORY" "$DIR"
fi
# If it doesn't exist, create specified directory and link it
else
mkdir -p "pinouts/$DIRECTORY"
mkdir -p "$(dirname "$DIR")"
ln -rs "pinouts/$DIRECTORY" "$DIR"
fi
# If we have a title field but not directory
elif [ "$TITLE" != "null" ]; then
if [ "$DEBUG" = "true" ]; then
echo "Found Title: $TITLE"
fi
# If temp dir exists
if [ -d "$DIR" ]; then
# If temp dir isn't symbolic link
# If it is a symbolic link, we just assume that it points to the directory field
if [ ! -L "$DIR" ]; then
# It must be a real directory, move it to the final dir and link it
mkdir -p "pinouts/$TITLE"
mv "$DIR"/* "pinouts/$TITLE"
rmdir "$DIR"
ln -rs "pinouts/$TITLE" "$DIR"
fi
# If it doesn't exist, create it at title field and link it
else
mkdir -p "pinouts/$TITLE"
mkdir -p "$(dirname "$DIR")"
ln -rs "pinouts/$TITLE" "$DIR"
fi
else
# Fallback to creating normal directory
mkdir -p "$DIR"
fi
if [ "$DEBUG" = "true" ]; then
echo "Target Directory: $DIR"
fi
NAME=$(basename "$c" .yaml)
if [ "$DEBUG" = "true" ]; then
echo "File Name: $NAME"
fi
if [ "$(yq e '.info.cid' "$c")" == "null" ]; then
if ! handle_warning "$WARNING_NO_CID" "WARNING: Missing yaml cid field in info section of $c"; then continue; fi
fi
IMG=$(yq e '.info.image.file' "$c")
if [ $? -ne 0 ] || [ "$IMG" = "null" ]; then
IMP=$(yq e '.info.image.import' "$c")
if [ $? -ne 0 ] || [ "$IMP" = "null" ]; then
if ! handle_warning "$WARNING_NO_IMAGE" "WARNING: $c missing image"; then continue; fi
else
IMG=$(yq e '.image.file' "$(dirname "$c")/$IMP")
echo "Image: $IMG"
cp "$(dirname $(dirname "$c")/$IMP)/$IMG" "$DIR"
yq --inplace ea '.info.image = .image | select(fi == 0)' "$c" "$(dirname "$c")/$IMP"
fi
else
echo "Image: $IMG"
cp "$(dirname "$c")/$IMG" "$DIR"
# Patch legacy YAMLs by moving .info.pins to .info.image.pins
yq --inplace e '.info.image.pins = .info.pins' "$c"
yq --inplace e 'del(.info.pins)' "$c"
fi
if [ -f "$DIR/index.html" ]; then
bash "$SCRIPTDIR"/append.sh "$(yq -o=json e "$c")" "$DIR/index.html"
else
bash "$SCRIPTDIR"/gen.sh "$(yq -o=json e "$c")" "$DIR/index.html"
fi
if [ $? -ne 0 ]; then
if ! handle_warning "unset" "WARNING: Failed to generate or append to pinout"; then continue; fi
fi
done
# Delete all symbolic links and empty directories from the temp dir.
find pinoutstmp -type l -delete
find pinoutstmp -type d -empty -delete
# Copy everything left over in the temp dir.
# This will get everything that didn't have a directory or title specified.
[ -d pinoutstmp ] && cp -r pinoutstmp/* pinouts/ && rm -r pinoutstmp
find pinouts/ -type f -name 'index.html' -print0 | while IFS= read -r -d '' f; do
DUPES=$(grep "cid\": " "$f" | uniq -d | tr -d '\n')
if [ -n "$DUPES" ]; then
if ! handle_warning "$WARNING_DUPE" "WARNING: Duplicate cids in $f: $DUPES"; then continue; fi
fi
done
echo "Completed processing $(echo "$CONNECTORS" | grep -v ^$ | wc -l) mappings"