Adding scripts to setup linting as a pre-commit hook

This commit is contained in:
Blake McAnally 2024-07-09 19:41:35 -05:00
parent cd1b25e0c5
commit 1c74be8f36
4 changed files with 90 additions and 0 deletions

View file

@ -13,12 +13,14 @@ SwiftUI client applications for iOS, iPadOS and macOS.
This project is currently using **Xcode 15.4**.
1. Clone the repo.
2. Set up git hooks to automatically lint the project when you commit changes.
2. Open `Meshtastic.xcworkspace`
2. Build and run the `Meshtastic` target.
```sh
git clone git@github.com:meshtastic/Meshtastic-Apple.git
cd Meshtastic-Apple
./scripts/setup-hooks.sh
open Meshtastic.xcworkspace
```

3
scripts/hooks/pre-commit Normal file
View file

@ -0,0 +1,3 @@
#!/bin/bash
./scripts/lint/lint-fix-changes.sh

View file

@ -0,0 +1,64 @@
#!/bin/bash
# Path to swiftlint
SWIFT_LINT=$(which swiftlint)
# Check if SwiftLint is installed
if [[ -e "${SWIFT_LINT}" ]]; then
count=0
for file_path in $(git ls-files -m --exclude-from=.gitignore | grep ".swift$"); do
export SCRIPT_INPUT_FILE_$count=$file_path
count=$((count + 1))
done
##### Check for modified files in unstaged/Staged area #####
for file_path in $(git diff --name-only --cached | grep ".swift$"); do
export SCRIPT_INPUT_FILE_$count=$file_path
count=$((count + 1))
done
##### Make the count available as global variable #####
export SCRIPT_INPUT_FILE_COUNT=$count
##### Fix files or exit if no files found for fixing #####
if [ "$count" -ne 0 ]; then
echo "Found files to fix! Running swiftLint --fix..."
# Run SwiftLint --fix on each file
for ((i = 0; i < count; i++)); do
file_var="SCRIPT_INPUT_FILE_$i"
file_path=${!file_var}
echo "Fixing $file_path"
$SWIFT_LINT --fix --path "$file_path"
done
# Add the fixed files back to staging
for ((i = 0; i < count; i++)); do
file_var="SCRIPT_INPUT_FILE_$i"
file_path=${!file_var}
git add "$file_path"
done
echo "swiftLint --fix completed and files re-staged."
# Optionally lint the fixed files
echo "Linting fixed files..."
$SWIFT_LINT lint --use-script-input-files
else
exit 0
fi
RESULT=$?
if [ $RESULT -eq 0 ]; then
exit 0
else
echo ""
echo "⛔️ Violation found of the type ERROR! Please fix these issues before continuing!"
fi
exit $RESULT
else
echo "SwiftLint not installed. Please install from https://github.com/realm/SwiftLint"
exit -1
fi

21
scripts/setup-hooks.sh Executable file
View file

@ -0,0 +1,21 @@
#!/bin/bash
set -e
# Define the source and destination paths
SOURCE_PATH="./scripts/hooks/pre-commit"
HOOKS_DIR=".git/hooks"
DEST_PATH="$HOOKS_DIR/pre-commit"
# Check if the hooks directory exists
if [ ! -d "$HOOKS_DIR" ]; then
echo "Error: .git/hooks directory not found. Make sure you're in the root of a Git repository."
exit 1
fi
# Copy the script to the hooks directory
cp "$SOURCE_PATH" "$DEST_PATH"
# Make the hook script executable
chmod +x "$DEST_PATH"
echo "Pre-commit hooks have been set up successfully."