Skip to content

Commit

Permalink
Merge pull request #49 from ASUWebPlatforms/WS2-1607
Browse files Browse the repository at this point in the history
WS2 1607: Investigate code linting and static analysis options to implement in before commiting.
  • Loading branch information
mlsamuelson authored Jun 13, 2023
2 parents a90ba83 + 471ce1c commit 1730c6a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"jcalderonzumba/mink-phantomjs-driver": "0.3.3",
"mikey179/vfsstream": "1.6.11",
"phpunit/phpunit": "9.6.8",
"squizlabs/php_codesniffer": "3.7.2"
"squizlabs/php_codesniffer": "3.7.2",
"sirbrillig/phpcs-changed": "v2.11.0-beta.2"
},
"conflict": {
"drupal/drupal": "*"
Expand Down
18 changes: 18 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

Steps to install this code validation tool in your local :

1) The 'pre-commit' file, located in the 'scripts/' directory, should be relocated to the './.git/hooks/' directory.
Run the following command to make the hook executable:
chmod +x .git/hooks/pre-commit

2) Please, ensure that PHP version 8.2.6 is installed on your system.

3) As last step, you have to run the next command:
```
composer install
```
Steps to use this tool:

1) Once you finish your implementation(code changes), please move your files to the staging area.
2) When you commit your files, you may encounter errors if there are issues in your staged files. Also, you can run this validation **manually** by executing : ./.git/hooks/pre-commit (Files that needs to be evaluated must be in the staging area).
3) Please fix the errors and commit your changes again.
41 changes: 41 additions & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Run Drupal code sniffing before committing
PROJECT_PATH=$(git rev-parse --show-toplevel)
PHPCS_BIN="$PROJECT_PATH/vendor/bin/phpcs"
PHPCSCHANGED="$PROJECT_PATH/vendor/bin/phpcs-changed"
DRUPAL_STANDARD="$PROJECT_PATH/vendor/drupal/coder/coder_sniffer/Drupal"
WEBSPARK_MODULE_PATH="$PROJECT_PATH/web/modules/webspark/"

# Get the list of files with changes to be committed
STAGED_FILES=$(git diff-index --name-only --cached HEAD --diff-filter=ACM $WEBSPARK_MODULE_PATH | grep -E '\.(php|module|inc|install|test|profile|theme|css|info|txt|md)$')

# Run the code sniffing on each staged file
echo "Running Drupal code standard validation on staged files..."
# Initialize FLAG to zero. If there are any errors, it will be set to 1.
FLAG=0
for FILE in $STAGED_FILES
do
OUTPUT=$($PHPCSCHANGED --git --git-staged --phpcs-path $PHPCS_BIN --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --standard $DRUPAL_STANDARD $FILE)

# Check if there are any errors
if [ -n "$OUTPUT" ]; then
echo ""
echo "PHP CodeSniffer found coding standards violations in file: $FILE"
echo "Staged code that needs to be fixed:"
echo "------------------------------------"
echo ""
echo "$OUTPUT"
echo ""
echo "Please fix the violations before committing."
echo ""
FLAG=1
fi
done

# When FLAG is set to 1, we need to stop the commit action and check/fix errors.
if [[ $FLAG -eq 1 ]]; then
exit 1
fi

exit 0

0 comments on commit 1730c6a

Please sign in to comment.