Skip to content

Commit

Permalink
Merge pull request #92 from AdrasteonDev/master
Browse files Browse the repository at this point in the history
feat: Better recognition of the Godot command and support of Godot 4
  • Loading branch information
NathanLovato authored May 6, 2023
2 parents 5c033fb + dd807db commit 2bb1a99
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 66 deletions.
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ Although to use the shell script that simplifies creating the reference, `genera

Docstring or doc-comments in GDScript don't have any special markup.

You can document classes, properties, and functions with comment blocks placed on the line before their definition:
You can document classes, properties, and functions with comment blocks placed on the line before their definition.

Example of docstrings for Godot 3:

```gdscript
# A linear and angular amount of acceleration.
Expand All @@ -76,12 +78,31 @@ func reset() -> void:
angular = 0.0
```

If you need long docstrings, you can use multiple commented lines:
Example of docstrings for Godot 4:

```gdscript
## A linear and angular amount of acceleration.
class_name GSTTargetAcceleration
## Linear acceleration
var linear: = Vector3.ZERO
## Angular acceleration
var angular: = 0.0
## Resets the accelerations to zero
func reset() -> void:
linear = Vector3.ZERO
angular = 0.0
```
# A specialized steering agent that updates itself every frame so the user does
# not have to using a KinematicBody2D
# category: Specialized agents

If you need long docstrings, you can use multiple commented lines:

```gdscript
## A specialized steering agent that updates itself every frame so the user does
## not have to using a KinematicBody2D
## category: Specialized agents
extends GSAISpecializedAgent
class_name GSAIKinematicBody2DAgent
```
Expand Down Expand Up @@ -148,8 +169,10 @@ python3 -m gdscript_docs_maker $HOME/Repositories/godot-steering-toolkit/project
If you want to generate the JSON and convert it manually, there are three steps involved:
1. Copying the GDScript files `./godot-scripts/Collector.gd` and `./godot-scripts/ReferenceCollectorCLI.gd` or `./godot-scripts/ReferenceCollectorCLI.gd` to your Godot 3.2 project.
2. Running the GDScript code with Godot, either from the editor (`ReferenceCollector.gd`) or by calling Godot from the command line (`ReferenceCollectorCLI.gd`).
1. Copying the GDScript files to your Godot project:
- `./godot-scripts/Collector.gd` and `./godot-scripts/ReferenceCollectorCLI.gd` or `./godot-scripts/ReferenceCollectorCLI.gd` for Godot 3
- `./godot-scripts/CollectorGd4.gd` and `./godot-scripts/ReferenceCollectorCLIGd4.gd` or `./godot-scripts/ReferenceCollectorCLIGd4.gd` for Godot 4
2. Running the GDScript code with Godot, either from the editor (`ReferenceCollector.gd` / `ReferenceCollectorGd4.gd`) or by calling Godot from the command line (`ReferenceCollectorCLI.gd` / `ReferenceCollectorCLIGd4.gd`).
3. Running `gdscript_docs_maker` on the reference.json file that Godot generated in the previous step.
<!-- TODO: turn into a note block on the website. -->
Expand Down
206 changes: 152 additions & 54 deletions generate_reference
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,101 @@ 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

arguments=$(getopt --name "generate_reference" -o "h,o:,d:,f:,a:" -l "help,output-directory:,directories:" -- "$@")

eval set -- "$arguments"
while true; do
case "$1" in
-h | --help)
echo_help
shift
;;
-o | --output-directory)
output_directory=$2
shift 2
;;
-d | --directory)
directories_override="$directories_override $2"
shift 2
;;
-f | --format)
format=$2
shift 2
;;
-a | --author)
author=$2
shift 2
;;
--)
shift
break
;;
*)
echo "Missing arguments. Try 'generate_reference --help' for more information"
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
;;
esac
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"
Expand All @@ -96,32 +155,69 @@ if ! test -f "$godot_project_file"; then
fi



ERROR_LOG=$(mktemp)
LOG=$(mktemp)
godot_project_dir=$(dirname "$godot_project_file")
godot=$(get_godot_cmd)

path_ref_collector="godot-scripts/ReferenceCollectorCLI.gd"
path_collector="godot-scripts/Collector.gd"
$godot --version 2>"$ERROR_LOG" >/dev/null
test $? -eq 0 -o $? -eq 255
godot_exec_ok=$?

# 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/,$//')
sed -ri "s#^var directories.+#var directories := [$args]#" "$file_ref_collector"
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

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..."

ERROR_LOG=$(mktemp)

if ! godot --editor --quit --no-window --script ReferenceCollectorCLI.gd \
--path "$godot_project_dir" 2>"$ERROR_LOG" >/dev/null
if [ $godot_exec_ok -ne 0 ]
then
ERRORS=$(cat "$ERROR_LOG")
cat <<EOT
Expand All @@ -134,6 +230,7 @@ This was the error log:
$ERRORS
EOT
rm "$ERROR_LOG"
rm "$LOG"
exit 1
fi

Expand Down Expand Up @@ -162,6 +259,7 @@ 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
Expand Down
53 changes: 49 additions & 4 deletions generate_reference.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,44 @@ setlocal enabledelayedexpansion

where /q godot*
if ERRORLEVEL 1 goto no-godot else (
for /f "delims=" %%F in ('where godot*') do set godot=%%F
for /f "delims=" %%F in ('where godot*') do (
::Search a command called "godot" in path
echo.%%F | findstr "godot\." 1>nul
if !ERRORLEVEL! == 0 (
for /f %%i in ('%%F -q --version') do (
set v=%%i
set v=!v:~0,1!
if !v! == 3 (
set is_godot_3=0
set godot=%%F
)
if !v! == 4 (
set is_godot_3=1
set godot=%%F
)
)
)else (
echo.%%F | findstr "Godot_v3" 1>nul

::The console exe should not be used (especially when using Godot 3.5)
if !ERRORLEVEL! == 0 (
set is_godot_3=0
echo.%%F | findstr "_console.cmd" 1>nul
if !ERRORLEVEL! == 1 (
set godot=%%F
)
)else (
set is_godot_3=1
echo.%%F | findstr "_console.exe" 1>nul
if !ERRORLEVEL! == 1 (
set godot=%%F
)
)
)
)
)
if not defined godot goto no-godot

::Test for python
where /q python
if ERRORLEVEL 1 goto no-python
Expand Down Expand Up @@ -110,8 +146,13 @@ goto end

:tail
set gdscript_path=godot-scripts
set gdscript_1=ReferenceCollectorCLI.gd
set gdscript_2=Collector.gd
if %is_godot_3% == 0 (
set gdscript_1=ReferenceCollectorCLI.gd
set gdscript_2=Collector.gd
) else (
set gdscript_1=ReferenceCollectorCLIGd4.gd
set gdscript_2=CollectorGd4.gd
)

::Copy CLI scripts to project location to be found in res://
copy /Y "%gdscript_path%\%gdscript_1%" "%project_path%\%gdscript_1%" > nul
Expand All @@ -120,7 +161,11 @@ copy /Y "%gdscript_path%\%gdscript_2%" "%project_path%\%gdscript_2%" > nul
echo Generating reference...

::Run godot in editor mode and runs the collector script
%godot% -e -q -s --no-window --path "%project_path%" %gdscript_1% > nul
if %is_godot_3% == 0 (
%godot% -e -q -s --no-window --path "%project_path%" %gdscript_1% > nul
) else (
%godot% -e -q --quit -s --headless --path "%project_path%" %gdscript_1% 2> nul > nul
)

::Clean up
erase /Q "%project_path%\%gdscript_1%"
Expand Down
Loading

0 comments on commit 2bb1a99

Please sign in to comment.