From 392701ac6eed48d60f7f7e8dc93b7abbd25e7876 Mon Sep 17 00:00:00 2001 From: Luca Junge Date: Mon, 2 Sep 2024 12:27:57 +0200 Subject: [PATCH] adds hooks, newlines and github workflow --- .github/workflows/build-on-push.yml | 38 ++--- README.md | 2 +- hooks/README.md | 18 +++ hooks/canonicalize_filename.sh | 48 ++++++ hooks/pre-commit | 50 ++++++ hooks/pre-commit-clang-format | 150 ++++++++++++++++++ plugin/.gitignore | 2 +- .../ARCoreInterface.gd | 2 +- plugin/src/main/cpp/arcore_wrapper.cpp | 2 +- plugin/src/main/cpp/plugin_jni.cpp | 2 +- 10 files changed, 284 insertions(+), 30 deletions(-) create mode 100644 hooks/README.md create mode 100755 hooks/canonicalize_filename.sh create mode 100755 hooks/pre-commit create mode 100755 hooks/pre-commit-clang-format diff --git a/.github/workflows/build-on-push.yml b/.github/workflows/build-on-push.yml index 42b7fb9..b54e5a7 100644 --- a/.github/workflows/build-on-push.yml +++ b/.github/workflows/build-on-push.yml @@ -43,27 +43,15 @@ jobs: # for android, run generate bindings python script - name: Generate bindings (Android) run: | - cd $GITHUB_WORKSPACE/plugin/libs/godot-cpp - python ../../../generate.py + cd $GITHUB_WORKSPACE/godot-cpp + scons platform=android target=template_debug + scons platform=android target=template_release if: matrix.platform == 'android' - # then run gradlew to build aar - - name: Validate Gradle wrapper - uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b - - name: Run the build for godot_arcore run: | cd $GITHUB_WORKSPACE - ./gradlew :generatePluginBinary - - # for android, copy aar - - name: Upload build files (artifacts) - uses: actions/upload-artifact@v2 - with: - name: build-files-arcore - path: | - plugin/build/outputs/pluginBin/*.aar - plugin/gdarcore.gdap + ./gradlew :assemble # This job collects the build output and assembles the final asset (artifact) asset: @@ -80,15 +68,15 @@ jobs: path: godot_arcore - name: Download all workflow run artifacts uses: actions/download-artifact@v2 - - name: Copy files to destination - run: | - mkdir godot_arcore_plugin - mkdir godot_arcore_plugin/addons - mkdir godot_arcore_plugin/android - mkdir godot_arcore_plugin/android/plugins - cp -r godot_arcore/demo/addons/godot_arcore godot_arcore_plugin/addons - cp build-files-arcore/*.aar godot_arcore_plugin/android/plugins/ - cp build-files-arcore/gdarcore.gdap godot_arcore_plugin/android/plugins/godot_openxr.gdap + #- name: Copy files to destination + # run: | + # mkdir godot_arcore_plugin + # mkdir godot_arcore_plugin/addons + # mkdir godot_arcore_plugin/android + # mkdir godot_arcore_plugin/android/plugins + # cp -r godot_arcore/demo/addons/godot_arcore godot_arcore_plugin/addons + # cp build-files-arcore/*.aar godot_arcore_plugin/android/plugins/ + # cp build-files-arcore/gdarcore.gdap godot_arcore_plugin/android/plugins/godot_openxr.gdap - name: Calculate GIT short ref run: | cd godot_arcore diff --git a/README.md b/README.md index e21bac6..a94be0b 100644 --- a/README.md +++ b/README.md @@ -137,4 +137,4 @@ To fix that, navigate to the folder where the `gradlew` script is and enter the dos2unix ./gradlew ``` -This will convert the line endings to LF. \ No newline at end of file +This will convert the line endings to LF. diff --git a/hooks/README.md b/hooks/README.md new file mode 100644 index 0000000..6ec90fc --- /dev/null +++ b/hooks/README.md @@ -0,0 +1,18 @@ +# Git hooks for Godot Engine + +This folder contains git hooks meant to be installed locally by Godot Engine +contributors to make sure they comply with our requirements. + +## List of hooks + +- Pre-commit hook for clang-format: Applies clang-format to the staged files + before accepting a commit; blocks the commit and generates a patch if the + style is not respected. + Should work on Linux and macOS. You may need to edit the file if your + clang-format binary is not in the $PATH, or if you want to enable colored + output with pygmentize. + +## Installation + +Copy all the files from this folder into your .git/hooks folder, and make sure +the hooks and helper scripts are executable. diff --git a/hooks/canonicalize_filename.sh b/hooks/canonicalize_filename.sh new file mode 100755 index 0000000..5eecabf --- /dev/null +++ b/hooks/canonicalize_filename.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +# Provide the canonicalize filename (physical filename with out any symlinks) +# like the GNU version readlink with the -f option regardless of the version of +# readlink (GNU or BSD). + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + +########################################################### +# There should be no need to change anything below this line. + +# Canonicalize by recursively following every symlink in every component of the +# specified filename. This should reproduce the results of the GNU version of +# readlink with the -f option. +# +# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac +canonicalize_filename () { + local target_file="$1" + local physical_directory="" + local result="" + + # Need to restore the working directory after work. + local working_dir="`pwd`" + + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + + # Iterate down a (possible) chain of symlinks + while [ -L "$target_file" ] + do + target_file="$(readlink -- "$target_file")" + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + done + + # Compute the canonicalized name by finding the physical path + # for the directory we're in and appending the target file. + physical_directory="`pwd -P`" + result="$physical_directory/$target_file" + + # restore the working directory after work. + cd -- "$working_dir" + + echo "$result" +} diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..fc50ed7 --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,50 @@ +#!/bin/sh +# Git pre-commit hook that runs multiple hooks specified in $HOOKS. +# Make sure this script is executable. Bypass hooks with git commit --no-verify. + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + + +########################################################### +# CONFIGURATION: +# pre-commit hooks to be executed. They should be in the same .git/hooks/ folder +# as this script. Hooks should return 0 if successful and nonzero to cancel the +# commit. They are executed in the order in which they are listed. +#HOOKS="pre-commit-compile pre-commit-uncrustify" +HOOKS="pre-commit-clang-format" +########################################################### +# There should be no need to change anything below this line. + +. "$(dirname -- "$0")/canonicalize_filename.sh" + +# exit on error +set -e + +# Absolute path to this script, e.g. /home/user/bin/foo.sh +SCRIPT="$(canonicalize_filename "$0")" + +# Absolute path this script is in, thus /home/user/bin +SCRIPTPATH="$(dirname -- "$SCRIPT")" + + +for hook in $HOOKS +do + echo "Running hook: $hook" + # run hook if it exists + # if it returns with nonzero exit with 1 and thus abort the commit + if [ -f "$SCRIPTPATH/$hook" ]; then + "$SCRIPTPATH/$hook" + if [ $? != 0 ]; then + exit 1 + fi + else + echo "Error: file $hook not found." + echo "Aborting commit. Make sure the hook is in $SCRIPTPATH and executable." + echo "You can disable it by removing it from the list in $SCRIPT." + echo "You can skip all pre-commit hooks with --no-verify (not recommended)." + exit 1 + fi +done diff --git a/hooks/pre-commit-clang-format b/hooks/pre-commit-clang-format new file mode 100755 index 0000000..c5e025e --- /dev/null +++ b/hooks/pre-commit-clang-format @@ -0,0 +1,150 @@ +#!/usr/bin/env bash + +# git pre-commit hook that runs a clang-format stylecheck. +# Features: +# - abort commit when commit does not comply with the style guidelines +# - create a patch of the proposed style changes +# Modifications for clang-format by rene.milk@wwu.de + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + +# Some quality of life modifications made for Godot Engine. + +################################################################## +# SETTINGS +# Set path to clang-format binary +# CLANG_FORMAT="/usr/bin/clang-format" +CLANG_FORMAT=`which clang-format` + +# Remove any older patches from previous commits. Set to true or false. +# DELETE_OLD_PATCHES=false +DELETE_OLD_PATCHES=false + +# Only parse files with the extensions in FILE_EXTS. Set to true or false. +# If false every changed file in the commit will be parsed with clang-format. +# If true only files matching one of the extensions are parsed with clang-format. +# PARSE_EXTS=true +PARSE_EXTS=true + +# File types to parse. Only effective when PARSE_EXTS is true. +# FILE_EXTS=".c .h .cpp .hpp" +FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .m .mm .inc .java .glsl" + +# Use pygmentize instead of cat to parse diff with highlighting. +# Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac) +# READER="pygmentize -l diff" +READER=cat + +################################################################## +# There should be no need to change anything below this line. + +. "$(dirname -- "$0")/canonicalize_filename.sh" + +# exit on error +set -e + +# check whether the given file matches any of the set extensions +matches_extension() { + local filename=$(basename "$1") + local extension=".${filename##*.}" + local ext + + for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done + + return 1 +} + +# necessary check for initial commit +if git rev-parse --verify HEAD >/dev/null 2>&1 ; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +if [ ! -x "$CLANG_FORMAT" ] ; then + printf "Error: clang-format executable not found.\n" + printf "Set the correct path in $(canonicalize_filename "$0").\n" + exit 1 +fi + +# create a random filename to store our generated patch +prefix="pre-commit-clang-format" +suffix="$(date +%s)" +patch="/tmp/$prefix-$suffix.patch" + +# clean up any older clang-format patches +$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch + +# create one patch containing all changes to the files +git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; +do + # ignore thirdparty files + if grep -q "thirdparty" <<< $file; then + continue; + fi + if grep -q "openxr_loader" <<< $file; then + continue; + fi + + # ignore file if we do check for file extensions and the file + # does not match any of the extensions specified in $FILE_EXTS + if $PARSE_EXTS && ! matches_extension "$file"; then + continue; + fi + + # clang-format our sourcefile, create a patch with diff and append it to our $patch + # The sed call is necessary to transform the patch from + # --- $file timestamp + # +++ - timestamp + # to both lines working on the same file and having a/ and b/ prefix. + # Else it can not be applied with 'git apply'. + "$CLANG_FORMAT" -style=file "$file" | \ + diff -u "$file" - | \ + sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch" +done + +# if no patch has been generated all is ok, clean up the file stub and exit +if [ ! -s "$patch" ] ; then + printf "Files in this commit comply with the clang-format rules.\n" + rm -f "$patch" + exit 0 +fi + +# a patch has been created, notify the user and exit +printf "\nThe following differences were found between the code to commit " +printf "and the clang-format rules:\n\n" +$READER "$patch" +printf "\n" + +# Allows us to read user input below, assigns stdin to keyboard +exec < /dev/tty + +while true; do + read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn + case $yn in + [Yy] ) git apply $patch; + printf "The patch was applied. You can now stage the changes and commit again.\n\n"; + break + ;; + [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n"; + printf "(may need to be called from the root directory of your repository)\n"; + printf "Aborting commit. Apply changes and commit again or skip checking with"; + printf " --no-verify (not recommended).\n\n"; + break + ;; + [Ss] ) git apply $patch; + git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; + do git add $file; + done + printf "The patch was applied and the changed files staged. You can now commit.\n\n"; + break + ;; + * ) echo "Please answer yes or no." + ;; + esac +done +exit 1 # we don't commit in any case diff --git a/plugin/.gitignore b/plugin/.gitignore index 42afabf..796b96d 100644 --- a/plugin/.gitignore +++ b/plugin/.gitignore @@ -1 +1 @@ -/build \ No newline at end of file +/build diff --git a/plugin/export_scripts_template/ARCoreInterface.gd b/plugin/export_scripts_template/ARCoreInterface.gd index 6518e4a..da59384 100644 --- a/plugin/export_scripts_template/ARCoreInterface.gd +++ b/plugin/export_scripts_template/ARCoreInterface.gd @@ -24,4 +24,4 @@ func start(): get_viewport().use_xr = true func get_tracking_status(): - return arcore.get_tracking_status() \ No newline at end of file + return arcore.get_tracking_status() diff --git a/plugin/src/main/cpp/arcore_wrapper.cpp b/plugin/src/main/cpp/arcore_wrapper.cpp index a0f3693..16a24a5 100644 --- a/plugin/src/main/cpp/arcore_wrapper.cpp +++ b/plugin/src/main/cpp/arcore_wrapper.cpp @@ -78,4 +78,4 @@ jobject ARCoreWrapper::get_global_context() jmethodID getApplication = env->GetMethodID(activityThread, "getApplication", "()Landroid/app/Application;"); jobject context = env->CallObjectMethod(activityThreadObj, getApplication); return context; -} \ No newline at end of file +} diff --git a/plugin/src/main/cpp/plugin_jni.cpp b/plugin/src/main/cpp/plugin_jni.cpp index 96f1bcd..a7b5562 100644 --- a/plugin/src/main/cpp/plugin_jni.cpp +++ b/plugin/src/main/cpp/plugin_jni.cpp @@ -20,4 +20,4 @@ extern "C" { JNIEXPORT void JNICALL JNI_METHOD(uninitializeEnvironment)(JNIEnv *env, jobject) { ARCoreWrapper::uninitialize_environment(env); } -}; \ No newline at end of file +};