-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerate_reference
executable file
·267 lines (220 loc) · 6.82 KB
/
generate_reference
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
#!/usr/bin/env sh
project_directory="$1"
output_directory="export"
directories_override=""
format="markdown"
author="developer"
echo_help() {
cat <<'EOT'
Generate a code reference from GDScript
Usage:
generate_reference $project_directory [options]
Required arguments:
$project_directory -- path to your Godot project directory.
This directory or one of its subdirectories should contain a
project.godot file.
Options:
-h/--help -- Display this help message.
-o/--output-directory -- directory path to output the documentation into.
-d/--directory -- Name of a directory to find files and generate the code reference in the Godot project.
You can use the option multiple times to generate a reference for multiple directories.
-f/--format -- Either `markdown` or `hugo`. If `hugo`, the output document includes a TOML front-matter
at the top. Default: `markdown`.
-a/--author -- If --format is `hugo`, controls the author property in the TOML front-matter.
Usage example:
generate_reference ~/Repositories/other/nakama-godot/project/ -o export-nakama -d addons
This command walks files in the res://addons directory of the Godot Nakama project, and converts it
to markdown files output in ./export-nakama.
EOT
exit 0
}
get_godot_cmd() {
if command -v godot > /dev/null
then
echo godot
else
godotcmd=""
if [ "$(echo $OSTYPE | head -c 6)" = "darwin" ]
then
godotcmd=$(find $(echo $PATH | tr ":" " ") -name "Godot*.app" -maxdepth 1 2>/dev/null | head -n 1 | tr -d '\n')
if [ "$(echo $godotcmd | tr -d '\n' | tail -c 4)" = ".app" ]
then
godotcmd="$godotcmd/Contents/MacOS/Godot"
fi
fi
if [ "$godotcmd" = "" ]
then
if command -v zsh > /dev/null
then
godotcmd=$(zsh -c "whence -ap -m 'Godot*' | head -n 1")
elif command -v bash > /dev/null
then
godotcmd=$(bash -c "compgen -c Godot | head -n 1")
fi
fi
if [ "$godotcmd" = "" ]
then
echo godot
else
echo $godotcmd
fi
fi
}
is_gnu_sed(){
sed --version >/dev/null 2>&1
}
# Interpret arguments
if [ $(echo $1 | head -c 1) != "-" ]
then
shift 1
fi
while getopts ':hodfa-:' OPTION; do
# Support of long options with this syntax: --format=hugo
if [ "$OPTION" = "-" ]; then
OPTION="${OPTARG%%=*}"
OPTARG="${OPTARG#$OPTION}"
OPTARG="${OPTARG#=}"
fi
# Support of long options with this syntax: --format hugo
if [ "$OPTARG" = "" ]
then
OPTARG="$(eval echo \${$OPTIND})"; OPTIND=$(( $OPTIND + 1 ))
fi
if [ "$(echo $OPTARG | cut -c1)" = "-" ]
then
OPTARG=""
OPTIND=$(( $OPTIND - 1 ))
fi
# Option processing
if [ "$OPTION" = "h" -o "$OPTION" = "help" ]
then
echo_help
elif [ "$OPTARG" = "" ]
then
echo "Missing value for option $OPTION. Try 'generate_reference --help' for more information"
exit 1
else
case "$OPTION" in
o | output-directory)
output_directory="$OPTARG"
;;
d | directory)
directories_override="$directories_override $OPTARG"
;;
f | format)
format="$OPTARG"
;;
a | author)
author="$OPTARG"
;;
?)
echo "Missing arguments. Try 'generate_reference --help' for more information"
exit 1
;;
esac
fi
done
echo "Checking parameters"
if test -z "$project_directory"; then
echo "Missing first parameter: project_directory."
exit 1
fi
if ! test -d "$project_directory"; then
echo "Directory $project_directory does not exist, exiting."
exit 1
fi
godot_project_file=$(find "$project_directory" -iname project.godot -print -quit)
if ! test -f "$godot_project_file"; then
echo "Could not find a project.godot file in $project_directory. This program needs a Godot project to work."
exit 1
fi
ERROR_LOG=$(mktemp)
LOG=$(mktemp)
godot_project_dir=$(dirname "$godot_project_file")
godot=$(get_godot_cmd)
$godot --version 2>"$ERROR_LOG" >/dev/null
test $? -eq 0 -o $? -eq 255
godot_exec_ok=$?
if [ $godot_exec_ok -eq 0 ]
then
version=$($godot --version | tail -n 1 | cut -c1-1)
if [ "$version" = "3" ]
then
ref_collector="ReferenceCollectorCLI.gd"
path_collector="godot-scripts/Collector.gd"
path_ref_collector="godot-scripts/ReferenceCollectorCLI.gd"
else
ref_collector="ReferenceCollectorCLIGd4.gd"
path_collector="godot-scripts/CollectorGd4.gd"
path_ref_collector="godot-scripts/ReferenceCollectorCLIGd4.gd"
fi
# Override the content of the directories variable in ReferenceCollectorCLI.gd if we got --directory arguments
file_ref_collector=$(mktemp)
cat $path_ref_collector > "$file_ref_collector"
if test "$directories_override" != ""; then
echo "Setting directories"
args=$(echo "$directories_override" | sed -r 's#([-/._a-zA-Z0-9]+)#"res://\1",#g' | sed -r 's/,$//')
if is_gnu_sed
then
sed -ri "s#^var directories.+#var directories := [$args]#" "$file_ref_collector"
else
sed -i "" -r "s#^var directories.+#var directories := [$args]#" "$file_ref_collector"
fi
fi
echo "Copying collectors to project directory"
cp "$file_ref_collector" "$godot_project_dir/$(basename $path_ref_collector)" >/dev/null
cp $path_collector "$godot_project_dir" >/dev/null
echo "Generating reference json data..."
if [ "$version" = "3" ]
then
$godot --editor --quit --no-window --script $ref_collector \
--path "$godot_project_dir" 2>"$ERROR_LOG" >"$LOG"
else
$godot --editor --quit --headless --script $ref_collector \
--path "$godot_project_dir" 2>"$ERROR_LOG" >"$LOG"
fi
godot_exec_ok=1
if grep -q -F "Saved data to res://reference.json" "$LOG" >/dev/null 2>/dev/null
then
godot_exec_ok=0
fi
fi
if [ $godot_exec_ok -ne 0 ]
then
ERRORS=$(cat "$ERROR_LOG")
cat <<EOT
There was an error running 'godot'.
The program 'godot' must be available on the system '\$PATH' variable for this program to work.
For more information, see https://en.wikipedia.org/wiki/PATH_(variable).
This was the error log:
$ERRORS
EOT
rm "$ERROR_LOG"
rm "$LOG"
exit 1
fi
echo "Done."
if ! [ -f "$godot_project_dir/reference.json" ]
then
echo "There was an error generating the reference from Godot. The file $godot_project_dir/reference.json was not found."
exit 1
fi
echo "Generating markdown files in $output_directory"
if [ ! -d "$output_directory" ]
then
mkdir -v "$output_directory" >/dev/null
fi
if ! python3 -m gdscript_docs_maker "$godot_project_dir/reference.json" --path "$output_directory" --format "$format" \
--author "$author" 2>"$ERROR_LOG"
then
echo "Error running gdscript_docs_maker. This is the log:"
cat "$ERROR_LOG"
exit 1
fi
echo "Cleaning up..."
rm "$ERROR_LOG" >/dev/null
rm "$LOG" >/dev/null
rm "$godot_project_dir/$(basename $path_ref_collector)" >/dev/null
rm "$godot_project_dir/$(basename $path_collector)" >/dev/null
rm "$godot_project_dir/reference.json" >/dev/null
exit 0