-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from ASUWebPlatforms/WS2-1607
WS2 1607: Investigate code linting and static analysis options to implement in before commiting.
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |