commit-code-check.sh

Same filename and directory in other branches
  1. 10 core/scripts/dev/commit-code-check.sh
  2. 11.x core/scripts/dev/commit-code-check.sh
  3. 9 core/scripts/dev/commit-code-check.sh
  4. 8.9.x core/scripts/dev/commit-code-check.sh
#!/bin/bash
#
# This script performs code quality checks.
#
# @internal
#   This script is not covered by Drupal core's backwards compatibility promise.
#   It exists only for core development purposes.
#
# The script makes the following checks:
# - Spell checking.
# - File modes.
# - No changes to core/node_modules directory.
# - PHPCS checks PHP and YAML files.
# - PHPStan checks PHP files.
# - ESLint checks JavaScript and YAML files.
# - Stylelint checks CSS files.
# - Checks .pcss.css and .css files are equivalent.

# cSpell:disable

# Searches an array.
contains_element() {
  local e
  for e in ${@:2}; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

MEMORY_UNLIMITED=0
CACHED=0
BRANCH=""
while test $# -gt 0; do
  case "$1" in
    -h|--help)
      echo "Drupal code quality checks"
      echo " "
      echo "options:"
      echo "-h, --help                show brief help"
      echo "--branch BRANCH           creates list of files to check by comparing against a branch"
      echo "--cached                  checks staged files"
      echo "--memory-unlimited        bypass PHP memory limit for PHPStan and PHPCS"
      echo " "
      echo "Example usage: sh ./core/scripts/dev/commit-code-check.sh --branch 9.2.x"
      exit 0
      ;;
    --branch)
      BRANCH="$2"
      if [[ "$BRANCH" == "" ]]; then
        printf "The --branch option requires a value. For example: --branch 9.2.x\n"
        exit;
      fi
      shift 2
      ;;
    --cached)
      CACHED=1
      shift
      ;;
    --memory-unlimited)
      MEMORY_UNLIMITED=1
      shift
      ;;
    *)
      break
      ;;
  esac
done

memory_limit=""
phpcs_memory_limit=""

if [[ "$MEMORY_UNLIMITED" == "1" ]]; then
  memory_limit="--memory-limit=-1"
  phpcs_memory_limit="-d memory_limit=-1"
fi

# Set up variables to make colored output simple.
red=$(tput setaf 1 && tput bold)
blue=$(tput setaf 4 && tput bold)
green=$(tput setaf 2)
reset=$(tput sgr0)
GIT="git"

# Gets list of files to check.
if [[ "$BRANCH" != "" ]]; then
  FILES=$($GIT diff --name-only $BRANCH HEAD);
elif [[ "$CACHED" == "0" ]]; then
  # List of all changes in the working directory.
  FILES=$($GIT ls-files --other --modified --exclude-standard --exclude=vendor)
else
  # Check staged files only.
  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
  FILES=$($GIT diff --cached --name-only $AGAINST);
fi

TOP_LEVEL=$($GIT rev-parse --show-toplevel)

# This variable will be set to one when the file core/phpcs.xml.dist is changed.
PHPCS_XML_DIST_FILE_CHANGED=0

# This variable will be set to one when the files core/.phpstan-baseline.php or
# core/phpstan.neon.dist are changed.
PHPSTAN_DIST_FILE_CHANGED=0

# This variable will be set to one when one of the eslint config file is
# changed:
#  - core/.eslintrc.passing.json
#  - core/.eslintrc.json
#  - core/.eslintrc.jquery.json
ESLINT_CONFIG_PASSING_FILE_CHANGED=0

# This variable will be set to one when the stylelint config file is changed.
# changed:
#  - core/.stylelintignore
#  - core/.stylelintrc.json
STYLELINT_CONFIG_FILE_CHANGED=0

# This variable will be set to one when JavaScript packages files are changed.
# changed:
#  - core/package.json
#  - core/yarn.lock
JAVASCRIPT_PACKAGES_CHANGED=0

# This variable will be set when a Drupal-specific CKEditor 5 plugin has changed
# it is used to make sure the compiled JS is valid.
CKEDITOR5_PLUGINS_CHANGED=0

# This variable will be set to one when either of the core dictionaries or the
# .cspell.json config has changed.
CSPELL_DICTIONARY_FILE_CHANGED=0

# Build up a list of absolute file names.
ABS_FILES=
for FILE in $FILES; do
  if [ -f "$TOP_LEVEL/$FILE" ]; then
    ABS_FILES="$ABS_FILES $TOP_LEVEL/$FILE"
  fi

  if [[ $FILE == "core/phpcs.xml.dist" ]]; then
    PHPCS_XML_DIST_FILE_CHANGED=1;
  fi;

  if [[ $FILE == "core/.phpstan-baseline.php" || $FILE == "core/phpstan.neon.dist" ]]; then
    PHPSTAN_DIST_FILE_CHANGED=1;
  fi;

  if [[ $FILE == "core/.eslintrc.json" || $FILE == "core/.eslintrc.passing.json" || $FILE == "core/.eslintrc.jquery.json" ]]; then
    ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  fi;

  if [[ $FILE == "core/.stylelintignore" || $FILE == "core/.stylelintrc.json" ]]; then
    STYLELINT_CONFIG_FILE_CHANGED=1;
  fi;

  # If JavaScript packages change, then rerun all JavaScript style checks.
  if [[ $FILE == "core/package.json" || $FILE == "core/yarn.lock" ]]; then
    ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
    STYLELINT_CONFIG_FILE_CHANGED=1;
    JAVASCRIPT_PACKAGES_CHANGED=1;
  fi;

  if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.js$ ]] && [[ $FILE =~ ^core/modules/ckeditor5/js/build || $FILE =~ ^core/modules/ckeditor5/js/ckeditor5_plugins ]]; then
    CKEDITOR5_PLUGINS_CHANGED=1;
  fi;

  if [[ $FILE == "core/misc/cspell/dictionary.txt" || $FILE == "core/misc/cspell/drupal-dictionary.txt" || $FILE == "core/.cspell.json" ]]; then
    CSPELL_DICTIONARY_FILE_CHANGED=1;
  fi

  if [[ $FILE == "core/MAINTAINERS.txt" ]]; then
    MAINTAINERS_TXT_CHANGED=1;
  fi

done

# Exit early if there are no files.
if [[ "$ABS_FILES" == "" ]]; then
  printf "There are no files to check. If you have staged a commit use the --cached option.\n"
  exit;
fi;

# This script assumes that composer install and yarn install have already been
# run and all dependencies are updated.
FINAL_STATUS=0

DEPENDENCIES_NEED_INSTALLING=0
# Ensure PHP development dependencies are installed.
# @todo https://github.com/composer/composer/issues/4497 Improve this to
#  determine if dependencies in the lock file match the installed versions.
#  Using composer install --dry-run is not valid because it would depend on
#  user-facing strings in Composer.
if ! [[ -f 'vendor/bin/phpcs' ]]; then
  printf "Drupal's PHP development dependencies are not installed. Run 'composer install' from the root directory.\n"
  DEPENDENCIES_NEED_INSTALLING=1;
fi

cd "$TOP_LEVEL/core"

# Ensure JavaScript development dependencies are installed.
yarn --version
yarn >/dev/null

# Check all files for spelling in one go for better performance.
if [[ $CSPELL_DICTIONARY_FILE_CHANGED == "1" ]] ; then
  printf "\nRunning spellcheck on *all* files.\n"
  yarn run spellcheck:core --no-must-find-files --no-progress
else
  # Check all files for spelling in one go for better performance. We pipe the
  # list files in so we obey the globs set on the spellcheck:core command in
  # core/package.json.
  echo "${ABS_FILES}" | tr ' ' '\n' | yarn run spellcheck:core --no-must-find-files --file-list stdin
fi

if [ "$?" -ne "0" ]; then
  # If there are failures set the status to a number other than 0.
  FINAL_STATUS=1
  printf "\nCSpell: ${red}failed${reset}\n"
else
  printf "\nCSpell: ${green}passed${reset}\n"
fi
cd "$TOP_LEVEL"

# Add a separator line to make the output easier to read.
printf "\n"
printf -- '-%.0s' {1..100}
printf "\n"

# Run PHPStan on all files when phpstan files are changed.
# APCu is disabled to ensure that the composer classmap is not corrupted.
if [[ $PHPSTAN_DIST_FILE_CHANGED == "1" ]]; then
  printf "\nRunning PHPStan on *all* files.\n"
  php -d apc.enabled=0 -d apc.enable_cli=0 vendor/bin/phpstan analyze --no-progress --configuration="$TOP_LEVEL/core/phpstan.neon.dist" $memory_limit
else
  # Only run PHPStan on changed files locally.
  printf "\nRunning PHPStan on changed files.\n"
  php -d apc.enabled=0 -d apc.enable_cli=0 vendor/bin/phpstan analyze --no-progress --configuration="$TOP_LEVEL/core/phpstan-partial.neon" $ABS_FILES $memory_limit
fi

if [ "$?" -ne "0" ]; then
  # If there are failures set the status to a number other than 0.
  FINAL_STATUS=1
  printf "\nPHPStan: ${red}failed${reset}\n"
else
  printf "\nPHPStan: ${green}passed${reset}\n"
fi

# Add a separator line to make the output easier to read.
printf "\n"
printf -- '-%.0s' {1..100}
printf "\n"

# Run PHPCS on all files when phpcs files are changed.
if [[ $PHPCS_XML_DIST_FILE_CHANGED == "1" ]]; then
  # Test all files with phpcs rules.
  vendor/bin/phpcs $phpcs_memory_limit -ps --parallel="$( (nproc || sysctl -n hw.logicalcpu || echo 4) 2>/dev/null)" --standard="$TOP_LEVEL/core/phpcs.xml.dist"
  PHPCS=$?
  if [ "$PHPCS" -ne "0" ]; then
    # If there are failures set the status to a number other than 0.
    FINAL_STATUS=1
    printf "\nPHPCS: ${red}failed${reset}\n"
  else
    printf "\nPHPCS: ${green}passed${reset}\n"
  fi
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

# When the eslint config has been changed, then eslint must check all files.
if [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "1" ]]; then
  cd "$TOP_LEVEL/core"
  yarn run lint:core-js-passing "$TOP_LEVEL/core"
  CORRECTJS=$?
  if [ "$CORRECTJS" -ne "0" ]; then
    # If there are failures set the status to a number other than 0.
    FINAL_STATUS=1
    printf "\neslint: ${red}failed${reset}\n"
  else
    printf "\neslint: ${green}passed${reset}\n"
  fi
  cd $TOP_LEVEL
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

# When the stylelint config has been changed, then stylelint must check all files.
if [[ $STYLELINT_CONFIG_FILE_CHANGED == "1" ]]; then
  cd "$TOP_LEVEL/core"
  yarn run lint:css
  if [ "$?" -ne "0" ]; then
    # If there are failures set the status to a number other than 0.
    FINAL_STATUS=1
    printf "\nstylelint: ${red}failed${reset}\n"
  else
    printf "\nstylelint: ${green}passed${reset}\n"
  fi
  cd $TOP_LEVEL
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

# When JavaScript packages change, then rerun all JavaScript style checks.
if [[ "$JAVASCRIPT_PACKAGES_CHANGED" == "1" ]]; then
  cd "$TOP_LEVEL/core"
  yarn run build:css --check
  CORRECTCSS=$?
  if [ "$CORRECTCSS" -ne "0" ]; then
    FINAL_STATUS=1
    printf "\n${red}ERROR: The compiled CSS from the PCSS files"
    printf "\n       does not match the current CSS files. Some added"
    printf "\n       or updated JavaScript package made changes."
    printf "\n       Recompile the CSS with: yarn run build:css${reset}\n\n"
  fi
  cd $TOP_LEVEL
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

# Build file type lists for batch checks.
PHP_FILES=""
JS_FILES=""
CSS_FILES=""
for FILE in $FILES; do
  if [[ -f "$TOP_LEVEL/$FILE" ]]; then
    if [[ $FILE =~ \.(inc|install|module|php|profile|test|theme|yml)$ ]]; then
      PHP_FILES="$PHP_FILES $TOP_LEVEL/$FILE"
    fi
    if [[ $FILE =~ \.(yml|js)$ ]]; then
      JS_FILES="$JS_FILES $TOP_LEVEL/$FILE"
    fi
    if [[ $FILE =~ \.css$ ]]; then
      BASENAME=${FILE%.css}
      if [[ $FILE =~ \.pcss\.css$ ]] || [[ ! -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
        CSS_FILES="$CSS_FILES $TOP_LEVEL/$FILE"
      fi
    fi
  fi
done

# Run PHPCS on changed PHP and YAML files.
if [[ "$PHP_FILES" != "" ]] && [[ $PHPCS_XML_DIST_FILE_CHANGED == "0" ]]; then
  vendor/bin/phpcs $phpcs_memory_limit --standard="$TOP_LEVEL/core/phpcs.xml.dist" $PHP_FILES
  if [ "$?" -ne "0" ]; then
    FINAL_STATUS=1
    printf "\nPHPCS: ${red}failed${reset}\n"
  else
    printf "\nPHPCS: ${green}passed${reset}\n"
  fi
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

# Run ESLint on changed YAML and JavaScript files.
if [[ "$JS_FILES" != "" ]] && [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "0" ]]; then
  cd "$TOP_LEVEL/core"
  node ./node_modules/eslint/bin/eslint.js --quiet --resolve-plugins-relative-to . --config=.eslintrc.passing.json $JS_FILES
  if [ "$?" -ne "0" ]; then
    FINAL_STATUS=1
    printf "\nESLint: ${red}failed${reset}\n"
  else
    printf "\nESLint: ${green}passed${reset}\n"
  fi
  cd "$TOP_LEVEL"
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

# Run Stylelint on changed CSS files.
if [[ "$CSS_FILES" != "" ]] && [[ $STYLELINT_CONFIG_FILE_CHANGED == "0" ]] && [[ -f "core/node_modules/.bin/stylelint" ]]; then
  cd "$TOP_LEVEL/core"
  node_modules/.bin/stylelint --allow-empty-input $CSS_FILES
  if [ "$?" -ne "0" ]; then
    FINAL_STATUS=1
    printf "\nStylelint: ${red}failed${reset}\n"
  else
    printf "\nStylelint: ${green}passed${reset}\n"
  fi
  cd "$TOP_LEVEL"
  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

for FILE in $FILES; do
  # Ensure the file still exists (i.e. is not being deleted).
  if [ -a $FILE ]; then
    if [ ${FILE: -3} != ".sh" ]; then
      if [ -x $FILE ]; then
        printf "${red}check failed:${reset} file $FILE should not be executable\n"
        FINAL_STATUS=1
      fi
    fi
  fi

  # Don't commit changes to vendor.
  if [[ "$FILE" =~ ^vendor/ ]]; then
    printf "${red}check failed:${reset} file in vendor directory being committed ($FILE)\n"
    FINAL_STATUS=1
  fi

  # Don't commit changes to core/node_modules.
  if [[ "$FILE" =~ ^core/node_modules/ ]]; then
    printf "${red}check failed:${reset} file in core/node_modules directory being committed ($FILE)\n"
    FINAL_STATUS=1
  fi

  ############################################################################
  ### CSS FILES
  ############################################################################
  if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.css$ ]]; then
    # Work out the root name of the CSS so we can ensure that the PostCSS
    # version has been compiled correctly.
    if [[ $FILE =~ \.pcss\.css$ ]]; then
      BASENAME=${FILE%.pcss.css}
      COMPILE_CHECK=1
    else
      BASENAME=${FILE%.css}
      # We only need to compile check if the .pcss.css file is not also
      # changing. This is because the compile check will occur for the
      # .pcss.css file. This might occur if the compiled stylesheets have
      # changed.
      contains_element "$BASENAME.pcss.css" "${FILES[@]}"
      HASPOSTCSS=$?
      if [ "$HASPOSTCSS" -ne "0" ]; then
        COMPILE_CHECK=1
      else
        COMPILE_CHECK=0
      fi
    fi
    # PostCSS
    if [[ "$COMPILE_CHECK" == "1" ]] && [[ -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
      cd "$TOP_LEVEL/core"
      yarn run build:css --check --file "$TOP_LEVEL/$BASENAME.pcss.css"
      CORRECTCSS=$?
      if [ "$CORRECTCSS" -ne "0" ]; then
        # If the CSS does not match the PCSS, set the status to a number other
        # than 0.
        FINAL_STATUS=1
        printf "\n${red}ERROR: The compiled CSS from"
        printf "\n       ${BASENAME}.pcss.css"
        printf "\n       does not match its CSS file. Recompile the CSS with:"
        printf "\n       yarn run build:css${reset}\n\n"
      fi
      cd $TOP_LEVEL
    fi
  fi
done

if [[ "$MAINTAINERS_TXT_CHANGED" == "1" ]]; then
  printf "\n${blue}INFO: MAINTAINERS.TXT changed"
  printf "\n      Make sure follow up changes are made to documentation, Slack channel, email group etc."
  printf "\n      See https://www.drupal.org/about/core/policies/maintainers/add-or-remove-a-subsystem-or-topic-maintainer.${reset}\n\n"

  # Add a separator line to make the output easier to read.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
fi

exit $FINAL_STATUS

File

core/scripts/dev/commit-code-check.sh

View source
  1. #!/bin/bash
  2. #
  3. # This script performs code quality checks.
  4. #
  5. # @internal
  6. # This script is not covered by Drupal core's backwards compatibility promise.
  7. # It exists only for core development purposes.
  8. #
  9. # The script makes the following checks:
  10. # - Spell checking.
  11. # - File modes.
  12. # - No changes to core/node_modules directory.
  13. # - PHPCS checks PHP and YAML files.
  14. # - PHPStan checks PHP files.
  15. # - ESLint checks JavaScript and YAML files.
  16. # - Stylelint checks CSS files.
  17. # - Checks .pcss.css and .css files are equivalent.
  18. # cSpell:disable
  19. # Searches an array.
  20. contains_element() {
  21. local e
  22. for e in ${@:2}; do [[ "$e" == "$1" ]] && return 0; done
  23. return 1
  24. }
  25. MEMORY_UNLIMITED=0
  26. CACHED=0
  27. BRANCH=""
  28. while test $# -gt 0; do
  29. case "$1" in
  30. -h|--help)
  31. echo "Drupal code quality checks"
  32. echo " "
  33. echo "options:"
  34. echo "-h, --help show brief help"
  35. echo "--branch BRANCH creates list of files to check by comparing against a branch"
  36. echo "--cached checks staged files"
  37. echo "--memory-unlimited bypass PHP memory limit for PHPStan and PHPCS"
  38. echo " "
  39. echo "Example usage: sh ./core/scripts/dev/commit-code-check.sh --branch 9.2.x"
  40. exit 0
  41. ;;
  42. --branch)
  43. BRANCH="$2"
  44. if [[ "$BRANCH" == "" ]]; then
  45. printf "The --branch option requires a value. For example: --branch 9.2.x\n"
  46. exit;
  47. fi
  48. shift 2
  49. ;;
  50. --cached)
  51. CACHED=1
  52. shift
  53. ;;
  54. --memory-unlimited)
  55. MEMORY_UNLIMITED=1
  56. shift
  57. ;;
  58. *)
  59. break
  60. ;;
  61. esac
  62. done
  63. memory_limit=""
  64. phpcs_memory_limit=""
  65. if [[ "$MEMORY_UNLIMITED" == "1" ]]; then
  66. memory_limit="--memory-limit=-1"
  67. phpcs_memory_limit="-d memory_limit=-1"
  68. fi
  69. # Set up variables to make colored output simple.
  70. red=$(tput setaf 1 && tput bold)
  71. blue=$(tput setaf 4 && tput bold)
  72. green=$(tput setaf 2)
  73. reset=$(tput sgr0)
  74. GIT="git"
  75. # Gets list of files to check.
  76. if [[ "$BRANCH" != "" ]]; then
  77. FILES=$($GIT diff --name-only $BRANCH HEAD);
  78. elif [[ "$CACHED" == "0" ]]; then
  79. # List of all changes in the working directory.
  80. FILES=$($GIT ls-files --other --modified --exclude-standard --exclude=vendor)
  81. else
  82. # Check staged files only.
  83. if $GIT rev-parse --verify HEAD >/dev/null 2>&1
  84. then
  85. AGAINST=HEAD
  86. else
  87. # Initial commit: diff against an empty tree object
  88. AGAINST=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  89. fi
  90. FILES=$($GIT diff --cached --name-only $AGAINST);
  91. fi
  92. TOP_LEVEL=$($GIT rev-parse --show-toplevel)
  93. # This variable will be set to one when the file core/phpcs.xml.dist is changed.
  94. PHPCS_XML_DIST_FILE_CHANGED=0
  95. # This variable will be set to one when the files core/.phpstan-baseline.php or
  96. # core/phpstan.neon.dist are changed.
  97. PHPSTAN_DIST_FILE_CHANGED=0
  98. # This variable will be set to one when one of the eslint config file is
  99. # changed:
  100. # - core/.eslintrc.passing.json
  101. # - core/.eslintrc.json
  102. # - core/.eslintrc.jquery.json
  103. ESLINT_CONFIG_PASSING_FILE_CHANGED=0
  104. # This variable will be set to one when the stylelint config file is changed.
  105. # changed:
  106. # - core/.stylelintignore
  107. # - core/.stylelintrc.json
  108. STYLELINT_CONFIG_FILE_CHANGED=0
  109. # This variable will be set to one when JavaScript packages files are changed.
  110. # changed:
  111. # - core/package.json
  112. # - core/yarn.lock
  113. JAVASCRIPT_PACKAGES_CHANGED=0
  114. # This variable will be set when a Drupal-specific CKEditor 5 plugin has changed
  115. # it is used to make sure the compiled JS is valid.
  116. CKEDITOR5_PLUGINS_CHANGED=0
  117. # This variable will be set to one when either of the core dictionaries or the
  118. # .cspell.json config has changed.
  119. CSPELL_DICTIONARY_FILE_CHANGED=0
  120. # Build up a list of absolute file names.
  121. ABS_FILES=
  122. for FILE in $FILES; do
  123. if [ -f "$TOP_LEVEL/$FILE" ]; then
  124. ABS_FILES="$ABS_FILES $TOP_LEVEL/$FILE"
  125. fi
  126. if [[ $FILE == "core/phpcs.xml.dist" ]]; then
  127. PHPCS_XML_DIST_FILE_CHANGED=1;
  128. fi;
  129. if [[ $FILE == "core/.phpstan-baseline.php" || $FILE == "core/phpstan.neon.dist" ]]; then
  130. PHPSTAN_DIST_FILE_CHANGED=1;
  131. fi;
  132. if [[ $FILE == "core/.eslintrc.json" || $FILE == "core/.eslintrc.passing.json" || $FILE == "core/.eslintrc.jquery.json" ]]; then
  133. ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  134. fi;
  135. if [[ $FILE == "core/.stylelintignore" || $FILE == "core/.stylelintrc.json" ]]; then
  136. STYLELINT_CONFIG_FILE_CHANGED=1;
  137. fi;
  138. # If JavaScript packages change, then rerun all JavaScript style checks.
  139. if [[ $FILE == "core/package.json" || $FILE == "core/yarn.lock" ]]; then
  140. ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  141. STYLELINT_CONFIG_FILE_CHANGED=1;
  142. JAVASCRIPT_PACKAGES_CHANGED=1;
  143. fi;
  144. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.js$ ]] && [[ $FILE =~ ^core/modules/ckeditor5/js/build || $FILE =~ ^core/modules/ckeditor5/js/ckeditor5_plugins ]]; then
  145. CKEDITOR5_PLUGINS_CHANGED=1;
  146. fi;
  147. if [[ $FILE == "core/misc/cspell/dictionary.txt" || $FILE == "core/misc/cspell/drupal-dictionary.txt" || $FILE == "core/.cspell.json" ]]; then
  148. CSPELL_DICTIONARY_FILE_CHANGED=1;
  149. fi
  150. if [[ $FILE == "core/MAINTAINERS.txt" ]]; then
  151. MAINTAINERS_TXT_CHANGED=1;
  152. fi
  153. done
  154. # Exit early if there are no files.
  155. if [[ "$ABS_FILES" == "" ]]; then
  156. printf "There are no files to check. If you have staged a commit use the --cached option.\n"
  157. exit;
  158. fi;
  159. # This script assumes that composer install and yarn install have already been
  160. # run and all dependencies are updated.
  161. FINAL_STATUS=0
  162. DEPENDENCIES_NEED_INSTALLING=0
  163. # Ensure PHP development dependencies are installed.
  164. # @todo https://github.com/composer/composer/issues/4497 Improve this to
  165. # determine if dependencies in the lock file match the installed versions.
  166. # Using composer install --dry-run is not valid because it would depend on
  167. # user-facing strings in Composer.
  168. if ! [[ -f 'vendor/bin/phpcs' ]]; then
  169. printf "Drupal's PHP development dependencies are not installed. Run 'composer install' from the root directory.\n"
  170. DEPENDENCIES_NEED_INSTALLING=1;
  171. fi
  172. cd "$TOP_LEVEL/core"
  173. # Ensure JavaScript development dependencies are installed.
  174. yarn --version
  175. yarn >/dev/null
  176. # Check all files for spelling in one go for better performance.
  177. if [[ $CSPELL_DICTIONARY_FILE_CHANGED == "1" ]] ; then
  178. printf "\nRunning spellcheck on *all* files.\n"
  179. yarn run spellcheck:core --no-must-find-files --no-progress
  180. else
  181. # Check all files for spelling in one go for better performance. We pipe the
  182. # list files in so we obey the globs set on the spellcheck:core command in
  183. # core/package.json.
  184. echo "${ABS_FILES}" | tr ' ' '\n' | yarn run spellcheck:core --no-must-find-files --file-list stdin
  185. fi
  186. if [ "$?" -ne "0" ]; then
  187. # If there are failures set the status to a number other than 0.
  188. FINAL_STATUS=1
  189. printf "\nCSpell: ${red}failed${reset}\n"
  190. else
  191. printf "\nCSpell: ${green}passed${reset}\n"
  192. fi
  193. cd "$TOP_LEVEL"
  194. # Add a separator line to make the output easier to read.
  195. printf "\n"
  196. printf -- '-%.0s' {1..100}
  197. printf "\n"
  198. # Run PHPStan on all files when phpstan files are changed.
  199. # APCu is disabled to ensure that the composer classmap is not corrupted.
  200. if [[ $PHPSTAN_DIST_FILE_CHANGED == "1" ]]; then
  201. printf "\nRunning PHPStan on *all* files.\n"
  202. php -d apc.enabled=0 -d apc.enable_cli=0 vendor/bin/phpstan analyze --no-progress --configuration="$TOP_LEVEL/core/phpstan.neon.dist" $memory_limit
  203. else
  204. # Only run PHPStan on changed files locally.
  205. printf "\nRunning PHPStan on changed files.\n"
  206. php -d apc.enabled=0 -d apc.enable_cli=0 vendor/bin/phpstan analyze --no-progress --configuration="$TOP_LEVEL/core/phpstan-partial.neon" $ABS_FILES $memory_limit
  207. fi
  208. if [ "$?" -ne "0" ]; then
  209. # If there are failures set the status to a number other than 0.
  210. FINAL_STATUS=1
  211. printf "\nPHPStan: ${red}failed${reset}\n"
  212. else
  213. printf "\nPHPStan: ${green}passed${reset}\n"
  214. fi
  215. # Add a separator line to make the output easier to read.
  216. printf "\n"
  217. printf -- '-%.0s' {1..100}
  218. printf "\n"
  219. # Run PHPCS on all files when phpcs files are changed.
  220. if [[ $PHPCS_XML_DIST_FILE_CHANGED == "1" ]]; then
  221. # Test all files with phpcs rules.
  222. vendor/bin/phpcs $phpcs_memory_limit -ps --parallel="$( (nproc || sysctl -n hw.logicalcpu || echo 4) 2>/dev/null)" --standard="$TOP_LEVEL/core/phpcs.xml.dist"
  223. PHPCS=$?
  224. if [ "$PHPCS" -ne "0" ]; then
  225. # If there are failures set the status to a number other than 0.
  226. FINAL_STATUS=1
  227. printf "\nPHPCS: ${red}failed${reset}\n"
  228. else
  229. printf "\nPHPCS: ${green}passed${reset}\n"
  230. fi
  231. # Add a separator line to make the output easier to read.
  232. printf "\n"
  233. printf -- '-%.0s' {1..100}
  234. printf "\n"
  235. fi
  236. # When the eslint config has been changed, then eslint must check all files.
  237. if [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "1" ]]; then
  238. cd "$TOP_LEVEL/core"
  239. yarn run lint:core-js-passing "$TOP_LEVEL/core"
  240. CORRECTJS=$?
  241. if [ "$CORRECTJS" -ne "0" ]; then
  242. # If there are failures set the status to a number other than 0.
  243. FINAL_STATUS=1
  244. printf "\neslint: ${red}failed${reset}\n"
  245. else
  246. printf "\neslint: ${green}passed${reset}\n"
  247. fi
  248. cd $TOP_LEVEL
  249. # Add a separator line to make the output easier to read.
  250. printf "\n"
  251. printf -- '-%.0s' {1..100}
  252. printf "\n"
  253. fi
  254. # When the stylelint config has been changed, then stylelint must check all files.
  255. if [[ $STYLELINT_CONFIG_FILE_CHANGED == "1" ]]; then
  256. cd "$TOP_LEVEL/core"
  257. yarn run lint:css
  258. if [ "$?" -ne "0" ]; then
  259. # If there are failures set the status to a number other than 0.
  260. FINAL_STATUS=1
  261. printf "\nstylelint: ${red}failed${reset}\n"
  262. else
  263. printf "\nstylelint: ${green}passed${reset}\n"
  264. fi
  265. cd $TOP_LEVEL
  266. # Add a separator line to make the output easier to read.
  267. printf "\n"
  268. printf -- '-%.0s' {1..100}
  269. printf "\n"
  270. fi
  271. # When JavaScript packages change, then rerun all JavaScript style checks.
  272. if [[ "$JAVASCRIPT_PACKAGES_CHANGED" == "1" ]]; then
  273. cd "$TOP_LEVEL/core"
  274. yarn run build:css --check
  275. CORRECTCSS=$?
  276. if [ "$CORRECTCSS" -ne "0" ]; then
  277. FINAL_STATUS=1
  278. printf "\n${red}ERROR: The compiled CSS from the PCSS files"
  279. printf "\n does not match the current CSS files. Some added"
  280. printf "\n or updated JavaScript package made changes."
  281. printf "\n Recompile the CSS with: yarn run build:css${reset}\n\n"
  282. fi
  283. cd $TOP_LEVEL
  284. # Add a separator line to make the output easier to read.
  285. printf "\n"
  286. printf -- '-%.0s' {1..100}
  287. printf "\n"
  288. fi
  289. # Build file type lists for batch checks.
  290. PHP_FILES=""
  291. JS_FILES=""
  292. CSS_FILES=""
  293. for FILE in $FILES; do
  294. if [[ -f "$TOP_LEVEL/$FILE" ]]; then
  295. if [[ $FILE =~ \.(inc|install|module|php|profile|test|theme|yml)$ ]]; then
  296. PHP_FILES="$PHP_FILES $TOP_LEVEL/$FILE"
  297. fi
  298. if [[ $FILE =~ \.(yml|js)$ ]]; then
  299. JS_FILES="$JS_FILES $TOP_LEVEL/$FILE"
  300. fi
  301. if [[ $FILE =~ \.css$ ]]; then
  302. BASENAME=${FILE%.css}
  303. if [[ $FILE =~ \.pcss\.css$ ]] || [[ ! -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
  304. CSS_FILES="$CSS_FILES $TOP_LEVEL/$FILE"
  305. fi
  306. fi
  307. fi
  308. done
  309. # Run PHPCS on changed PHP and YAML files.
  310. if [[ "$PHP_FILES" != "" ]] && [[ $PHPCS_XML_DIST_FILE_CHANGED == "0" ]]; then
  311. vendor/bin/phpcs $phpcs_memory_limit --standard="$TOP_LEVEL/core/phpcs.xml.dist" $PHP_FILES
  312. if [ "$?" -ne "0" ]; then
  313. FINAL_STATUS=1
  314. printf "\nPHPCS: ${red}failed${reset}\n"
  315. else
  316. printf "\nPHPCS: ${green}passed${reset}\n"
  317. fi
  318. # Add a separator line to make the output easier to read.
  319. printf "\n"
  320. printf -- '-%.0s' {1..100}
  321. printf "\n"
  322. fi
  323. # Run ESLint on changed YAML and JavaScript files.
  324. if [[ "$JS_FILES" != "" ]] && [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "0" ]]; then
  325. cd "$TOP_LEVEL/core"
  326. node ./node_modules/eslint/bin/eslint.js --quiet --resolve-plugins-relative-to . --config=.eslintrc.passing.json $JS_FILES
  327. if [ "$?" -ne "0" ]; then
  328. FINAL_STATUS=1
  329. printf "\nESLint: ${red}failed${reset}\n"
  330. else
  331. printf "\nESLint: ${green}passed${reset}\n"
  332. fi
  333. cd "$TOP_LEVEL"
  334. # Add a separator line to make the output easier to read.
  335. printf "\n"
  336. printf -- '-%.0s' {1..100}
  337. printf "\n"
  338. fi
  339. # Run Stylelint on changed CSS files.
  340. if [[ "$CSS_FILES" != "" ]] && [[ $STYLELINT_CONFIG_FILE_CHANGED == "0" ]] && [[ -f "core/node_modules/.bin/stylelint" ]]; then
  341. cd "$TOP_LEVEL/core"
  342. node_modules/.bin/stylelint --allow-empty-input $CSS_FILES
  343. if [ "$?" -ne "0" ]; then
  344. FINAL_STATUS=1
  345. printf "\nStylelint: ${red}failed${reset}\n"
  346. else
  347. printf "\nStylelint: ${green}passed${reset}\n"
  348. fi
  349. cd "$TOP_LEVEL"
  350. # Add a separator line to make the output easier to read.
  351. printf "\n"
  352. printf -- '-%.0s' {1..100}
  353. printf "\n"
  354. fi
  355. for FILE in $FILES; do
  356. # Ensure the file still exists (i.e. is not being deleted).
  357. if [ -a $FILE ]; then
  358. if [ ${FILE: -3} != ".sh" ]; then
  359. if [ -x $FILE ]; then
  360. printf "${red}check failed:${reset} file $FILE should not be executable\n"
  361. FINAL_STATUS=1
  362. fi
  363. fi
  364. fi
  365. # Don't commit changes to vendor.
  366. if [[ "$FILE" =~ ^vendor/ ]]; then
  367. printf "${red}check failed:${reset} file in vendor directory being committed ($FILE)\n"
  368. FINAL_STATUS=1
  369. fi
  370. # Don't commit changes to core/node_modules.
  371. if [[ "$FILE" =~ ^core/node_modules/ ]]; then
  372. printf "${red}check failed:${reset} file in core/node_modules directory being committed ($FILE)\n"
  373. FINAL_STATUS=1
  374. fi
  375. ############################################################################
  376. ### CSS FILES
  377. ############################################################################
  378. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.css$ ]]; then
  379. # Work out the root name of the CSS so we can ensure that the PostCSS
  380. # version has been compiled correctly.
  381. if [[ $FILE =~ \.pcss\.css$ ]]; then
  382. BASENAME=${FILE%.pcss.css}
  383. COMPILE_CHECK=1
  384. else
  385. BASENAME=${FILE%.css}
  386. # We only need to compile check if the .pcss.css file is not also
  387. # changing. This is because the compile check will occur for the
  388. # .pcss.css file. This might occur if the compiled stylesheets have
  389. # changed.
  390. contains_element "$BASENAME.pcss.css" "${FILES[@]}"
  391. HASPOSTCSS=$?
  392. if [ "$HASPOSTCSS" -ne "0" ]; then
  393. COMPILE_CHECK=1
  394. else
  395. COMPILE_CHECK=0
  396. fi
  397. fi
  398. # PostCSS
  399. if [[ "$COMPILE_CHECK" == "1" ]] && [[ -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
  400. cd "$TOP_LEVEL/core"
  401. yarn run build:css --check --file "$TOP_LEVEL/$BASENAME.pcss.css"
  402. CORRECTCSS=$?
  403. if [ "$CORRECTCSS" -ne "0" ]; then
  404. # If the CSS does not match the PCSS, set the status to a number other
  405. # than 0.
  406. FINAL_STATUS=1
  407. printf "\n${red}ERROR: The compiled CSS from"
  408. printf "\n ${BASENAME}.pcss.css"
  409. printf "\n does not match its CSS file. Recompile the CSS with:"
  410. printf "\n yarn run build:css${reset}\n\n"
  411. fi
  412. cd $TOP_LEVEL
  413. fi
  414. fi
  415. done
  416. if [[ "$MAINTAINERS_TXT_CHANGED" == "1" ]]; then
  417. printf "\n${blue}INFO: MAINTAINERS.TXT changed"
  418. printf "\n Make sure follow up changes are made to documentation, Slack channel, email group etc."
  419. printf "\n See https://www.drupal.org/about/core/policies/maintainers/add-or-remove-a-subsystem-or-topic-maintainer.${reset}\n\n"
  420. # Add a separator line to make the output easier to read.
  421. printf "\n"
  422. printf -- '-%.0s' {1..100}
  423. printf "\n"
  424. fi
  425. exit $FINAL_STATUS

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.