From 069eec643a58daaefc585f5616eea687f3d258fb Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 1 Jul 2024 20:05:01 -0300 Subject: [PATCH] chore: use clang-format (#232) --- .clang-format | 4 ++++ .clang-format-ignore | 11 +++++++++++ .husky/pre-commit | 1 + README.md | 3 +++ package.json | 8 ++++++++ update_version.sh | 4 ++-- 6 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-format-ignore create mode 100644 .husky/pre-commit diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..df5ce85b --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +# uses the same styling as V8 +BasedOnStyle: Google +DerivePointerAlignment: false +MaxEmptyLinesToKeep: 1 \ No newline at end of file diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 00000000..0e38bbc5 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1,11 @@ +# clang format doesn't work properly with * and ** +NativeScript/include/* +NativeScript/include/*/* +NativeScript/include/*/*/* +NativeScript/include/*/*/*/* +NativeScript/include/*/*/*/*/* +NativeScript/ada/* +NativeScript/inspector/third_party/* +NativeScript/inspector/third_party/*/* +NativeScript/inspector/third_party/*/*/* +NativeScript/inspector/third_party/*/*/*/* diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 00000000..cc28745c --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1 @@ +npx lint-staged || echo "Linting failed, verify your clang-format installation" \ No newline at end of file diff --git a/README.md b/README.md index d7136cf6..60a25cd4 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ To start diving into the v8 iOS runtime make sure you have Xcode and [Homebrew]( # Install CMake brew install cmake +# (Optional) Install clang-format to format the code +brew install clang-format + # To avoid errors, you might need to link cmake to: /usr/local/bin/cmake # xcode doesn't read your profile during the build step, which causes it to ignore the PATH sudo ln -s /usr/local/bin/cmake $(which cmake) diff --git a/package.json b/package.json index 8031f11b..9e058141 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "**/*" ], "scripts": { + "prepare": "husky", "apply-patches": "./apply_patches.sh", "build-v8-source": "./build_v8_source.sh", "build-v8-source-catalyst": "./build_v8_source_catalyst.sh", @@ -34,6 +35,13 @@ "devDependencies": { "conventional-changelog-cli": "^2.1.1", "dayjs": "^1.11.7", + "husky": "^9.0.11", + "lint-staged": "^15.2.7", "semver": "^7.5.0" + }, + "lint-staged": { + "{NativeScript,metadata-generator/src,TestFixtures}/**/*.{h,hpp,c,cpp,mm,m}": [ + "clang-format -i" + ] } } diff --git a/update_version.sh b/update_version.sh index 5948e55a..f9bfb52d 100755 --- a/update_version.sh +++ b/update_version.sh @@ -3,9 +3,9 @@ set -e # If a parameter is passed to this script then replace the version in package.json by appending $PACKAGE_VERSION if [ -n "$1" ]; then - sed -i.bak "s/\"version\"[[:space:]]*:[[:space:]]*\"\(.*\)\"[[:space:]]*,/\"version\": \"\1-$PACKAGE_VERSION\",/g" package.json && rm package.json.bak + jq ".version = \"$PACKAGE_VERSION\"" package.json > package.json.tmp && rm package.json && mv package.json.tmp package.json fi # Read the version from package.json and replace it inside the NativeScript-Prefix.pch precompiled header -FULL_VERSION=$(sed -nE "s/.*(\"version\"[[:space:]]*:[[:space:]]*\"(.+)\"[[:space:]]*,).*/\2/p" package.json) +FULL_VERSION=$(jq -r .version package.json) sed -i.bak "s/#define[[:space:]]*NATIVESCRIPT_VERSION[[:space:]]*\"\(.*\)\"/#define NATIVESCRIPT_VERSION \"$FULL_VERSION\"/g" NativeScript/NativeScript-Prefix.pch && rm NativeScript/NativeScript-Prefix.pch.bak \ No newline at end of file