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 the twig-cs-fixer config file is
# changed:
#  - core/.twig-cs-fixer.php
TWIGCSFIXER_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/.twig-cs-fixer.php" ]]; then
    TWIGCSFIXER_CONFIG_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

# Run Twig CS Fixer on all files when twig cs fixer files are changed.
if [[ $TWIGCSFIXER_CONFIG_FILE_CHANGED == "1" ]]; then
  # Test all files with twig-cs-fixer rules.
  vendor/bin/twig-cs-fixer lint -c "$TOP_LEVEL/core/.twig-cs-fixer.php"
  TWIGCS=$?
  if [ "$TWIGCS" -ne "0" ]; then
    # If there are failures set the status to a number other than 0.
    FINAL_STATUS=1
    printf "\TWIGCS: ${red}failed${reset}\n"
  else
    printf "\TWIGCS: ${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=""
TWIG_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
    if [[ $FILE =~ \.twig$ ]]; then
      TWIG_FILES="$TWIG_FILES $TOP_LEVEL/$FILE"
    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 Twig CS Fixer on changed Twig files.
if [[ "$TWIG_FILES" != "" ]] && [[ $TWIGCSFIXER_CONFIG_FILE_CHANGED == "0" ]]; then
  vendor/bin/twig-cs-fixer lint $TWIG_FILES -c "$TOP_LEVEL/core/.twig-cs-fixer.php"
  if [ "$?" -ne "0" ]; then
    FINAL_STATUS=1
    printf "\TWIGCS: ${red}failed${reset}\n"
  else
    printf "\TWIGCS: ${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" ] && [ "$FILE" != "core/scripts/dr" ]; 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 the twig-cs-fixer config file is
  110. # changed:
  111. # - core/.twig-cs-fixer.php
  112. TWIGCSFIXER_CONFIG_FILE_CHANGED=0
  113. # This variable will be set to one when JavaScript packages files are changed.
  114. # changed:
  115. # - core/package.json
  116. # - core/yarn.lock
  117. JAVASCRIPT_PACKAGES_CHANGED=0
  118. # This variable will be set when a Drupal-specific CKEditor 5 plugin has changed
  119. # it is used to make sure the compiled JS is valid.
  120. CKEDITOR5_PLUGINS_CHANGED=0
  121. # This variable will be set to one when either of the core dictionaries or the
  122. # .cspell.json config has changed.
  123. CSPELL_DICTIONARY_FILE_CHANGED=0
  124. # Build up a list of absolute file names.
  125. ABS_FILES=
  126. for FILE in $FILES; do
  127. if [ -f "$TOP_LEVEL/$FILE" ]; then
  128. ABS_FILES="$ABS_FILES $TOP_LEVEL/$FILE"
  129. fi
  130. if [[ $FILE == "core/phpcs.xml.dist" ]]; then
  131. PHPCS_XML_DIST_FILE_CHANGED=1;
  132. fi;
  133. if [[ $FILE == "core/.twig-cs-fixer.php" ]]; then
  134. TWIGCSFIXER_CONFIG_FILE_CHANGED=1;
  135. fi;
  136. if [[ $FILE == "core/.phpstan-baseline.php" || $FILE == "core/phpstan.neon.dist" ]]; then
  137. PHPSTAN_DIST_FILE_CHANGED=1;
  138. fi;
  139. if [[ $FILE == "core/.eslintrc.json" || $FILE == "core/.eslintrc.passing.json" || $FILE == "core/.eslintrc.jquery.json" ]]; then
  140. ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  141. fi;
  142. if [[ $FILE == "core/.stylelintignore" || $FILE == "core/.stylelintrc.json" ]]; then
  143. STYLELINT_CONFIG_FILE_CHANGED=1;
  144. fi;
  145. # If JavaScript packages change, then rerun all JavaScript style checks.
  146. if [[ $FILE == "core/package.json" || $FILE == "core/yarn.lock" ]]; then
  147. ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  148. STYLELINT_CONFIG_FILE_CHANGED=1;
  149. JAVASCRIPT_PACKAGES_CHANGED=1;
  150. fi;
  151. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.js$ ]] && [[ $FILE =~ ^core/modules/ckeditor5/js/build || $FILE =~ ^core/modules/ckeditor5/js/ckeditor5_plugins ]]; then
  152. CKEDITOR5_PLUGINS_CHANGED=1;
  153. fi;
  154. if [[ $FILE == "core/misc/cspell/dictionary.txt" || $FILE == "core/misc/cspell/drupal-dictionary.txt" || $FILE == "core/.cspell.json" ]]; then
  155. CSPELL_DICTIONARY_FILE_CHANGED=1;
  156. fi
  157. if [[ $FILE == "core/MAINTAINERS.txt" ]]; then
  158. MAINTAINERS_TXT_CHANGED=1;
  159. fi
  160. done
  161. # Exit early if there are no files.
  162. if [[ "$ABS_FILES" == "" ]]; then
  163. printf "There are no files to check. If you have staged a commit use the --cached option.\n"
  164. exit;
  165. fi;
  166. # This script assumes that composer install and yarn install have already been
  167. # run and all dependencies are updated.
  168. FINAL_STATUS=0
  169. DEPENDENCIES_NEED_INSTALLING=0
  170. # Ensure PHP development dependencies are installed.
  171. # @todo https://github.com/composer/composer/issues/4497 Improve this to
  172. # determine if dependencies in the lock file match the installed versions.
  173. # Using composer install --dry-run is not valid because it would depend on
  174. # user-facing strings in Composer.
  175. if ! [[ -f 'vendor/bin/phpcs' ]]; then
  176. printf "Drupal's PHP development dependencies are not installed. Run 'composer install' from the root directory.\n"
  177. DEPENDENCIES_NEED_INSTALLING=1;
  178. fi
  179. cd "$TOP_LEVEL/core"
  180. # Ensure JavaScript development dependencies are installed.
  181. yarn --version
  182. yarn >/dev/null
  183. # Check all files for spelling in one go for better performance.
  184. if [[ $CSPELL_DICTIONARY_FILE_CHANGED == "1" ]] ; then
  185. printf "\nRunning spellcheck on *all* files.\n"
  186. yarn run spellcheck:core --no-must-find-files --no-progress
  187. else
  188. # Check all files for spelling in one go for better performance. We pipe the
  189. # list files in so we obey the globs set on the spellcheck:core command in
  190. # core/package.json.
  191. echo "${ABS_FILES}" | tr ' ' '\n' | yarn run spellcheck:core --no-must-find-files --file-list stdin
  192. fi
  193. if [ "$?" -ne "0" ]; then
  194. # If there are failures set the status to a number other than 0.
  195. FINAL_STATUS=1
  196. printf "\nCSpell: ${red}failed${reset}\n"
  197. else
  198. printf "\nCSpell: ${green}passed${reset}\n"
  199. fi
  200. cd "$TOP_LEVEL"
  201. # Add a separator line to make the output easier to read.
  202. printf "\n"
  203. printf -- '-%.0s' {1..100}
  204. printf "\n"
  205. # Run PHPStan on all files when phpstan files are changed.
  206. # APCu is disabled to ensure that the composer classmap is not corrupted.
  207. if [[ $PHPSTAN_DIST_FILE_CHANGED == "1" ]]; then
  208. printf "\nRunning PHPStan on *all* files.\n"
  209. 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
  210. else
  211. # Only run PHPStan on changed files locally.
  212. printf "\nRunning PHPStan on changed files.\n"
  213. 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
  214. fi
  215. if [ "$?" -ne "0" ]; then
  216. # If there are failures set the status to a number other than 0.
  217. FINAL_STATUS=1
  218. printf "\nPHPStan: ${red}failed${reset}\n"
  219. else
  220. printf "\nPHPStan: ${green}passed${reset}\n"
  221. fi
  222. # Add a separator line to make the output easier to read.
  223. printf "\n"
  224. printf -- '-%.0s' {1..100}
  225. printf "\n"
  226. # Run PHPCS on all files when phpcs files are changed.
  227. if [[ $PHPCS_XML_DIST_FILE_CHANGED == "1" ]]; then
  228. # Test all files with phpcs rules.
  229. 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"
  230. PHPCS=$?
  231. if [ "$PHPCS" -ne "0" ]; then
  232. # If there are failures set the status to a number other than 0.
  233. FINAL_STATUS=1
  234. printf "\nPHPCS: ${red}failed${reset}\n"
  235. else
  236. printf "\nPHPCS: ${green}passed${reset}\n"
  237. fi
  238. # Add a separator line to make the output easier to read.
  239. printf "\n"
  240. printf -- '-%.0s' {1..100}
  241. printf "\n"
  242. fi
  243. # Run Twig CS Fixer on all files when twig cs fixer files are changed.
  244. if [[ $TWIGCSFIXER_CONFIG_FILE_CHANGED == "1" ]]; then
  245. # Test all files with twig-cs-fixer rules.
  246. vendor/bin/twig-cs-fixer lint -c "$TOP_LEVEL/core/.twig-cs-fixer.php"
  247. TWIGCS=$?
  248. if [ "$TWIGCS" -ne "0" ]; then
  249. # If there are failures set the status to a number other than 0.
  250. FINAL_STATUS=1
  251. printf "\TWIGCS: ${red}failed${reset}\n"
  252. else
  253. printf "\TWIGCS: ${green}passed${reset}\n"
  254. fi
  255. # Add a separator line to make the output easier to read.
  256. printf "\n"
  257. printf -- '-%.0s' {1..100}
  258. printf "\n"
  259. fi
  260. # When the eslint config has been changed, then eslint must check all files.
  261. if [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "1" ]]; then
  262. cd "$TOP_LEVEL/core"
  263. yarn run lint:core-js-passing "$TOP_LEVEL/core"
  264. CORRECTJS=$?
  265. if [ "$CORRECTJS" -ne "0" ]; then
  266. # If there are failures set the status to a number other than 0.
  267. FINAL_STATUS=1
  268. printf "\neslint: ${red}failed${reset}\n"
  269. else
  270. printf "\neslint: ${green}passed${reset}\n"
  271. fi
  272. cd $TOP_LEVEL
  273. # Add a separator line to make the output easier to read.
  274. printf "\n"
  275. printf -- '-%.0s' {1..100}
  276. printf "\n"
  277. fi
  278. # When the stylelint config has been changed, then stylelint must check all files.
  279. if [[ $STYLELINT_CONFIG_FILE_CHANGED == "1" ]]; then
  280. cd "$TOP_LEVEL/core"
  281. yarn run lint:css
  282. if [ "$?" -ne "0" ]; then
  283. # If there are failures set the status to a number other than 0.
  284. FINAL_STATUS=1
  285. printf "\nstylelint: ${red}failed${reset}\n"
  286. else
  287. printf "\nstylelint: ${green}passed${reset}\n"
  288. fi
  289. cd $TOP_LEVEL
  290. # Add a separator line to make the output easier to read.
  291. printf "\n"
  292. printf -- '-%.0s' {1..100}
  293. printf "\n"
  294. fi
  295. # When JavaScript packages change, then rerun all JavaScript style checks.
  296. if [[ "$JAVASCRIPT_PACKAGES_CHANGED" == "1" ]]; then
  297. cd "$TOP_LEVEL/core"
  298. yarn run build:css --check
  299. CORRECTCSS=$?
  300. if [ "$CORRECTCSS" -ne "0" ]; then
  301. FINAL_STATUS=1
  302. printf "\n${red}ERROR: The compiled CSS from the PCSS files"
  303. printf "\n does not match the current CSS files. Some added"
  304. printf "\n or updated JavaScript package made changes."
  305. printf "\n Recompile the CSS with: yarn run build:css${reset}\n\n"
  306. fi
  307. cd $TOP_LEVEL
  308. # Add a separator line to make the output easier to read.
  309. printf "\n"
  310. printf -- '-%.0s' {1..100}
  311. printf "\n"
  312. fi
  313. # Build file type lists for batch checks.
  314. PHP_FILES=""
  315. JS_FILES=""
  316. CSS_FILES=""
  317. TWIG_FILES=""
  318. for FILE in $FILES; do
  319. if [[ -f "$TOP_LEVEL/$FILE" ]]; then
  320. if [[ $FILE =~ \.(inc|install|module|php|profile|test|theme|yml)$ ]]; then
  321. PHP_FILES="$PHP_FILES $TOP_LEVEL/$FILE"
  322. fi
  323. if [[ $FILE =~ \.(yml|js)$ ]]; then
  324. JS_FILES="$JS_FILES $TOP_LEVEL/$FILE"
  325. fi
  326. if [[ $FILE =~ \.css$ ]]; then
  327. BASENAME=${FILE%.css}
  328. if [[ $FILE =~ \.pcss\.css$ ]] || [[ ! -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
  329. CSS_FILES="$CSS_FILES $TOP_LEVEL/$FILE"
  330. fi
  331. fi
  332. if [[ $FILE =~ \.twig$ ]]; then
  333. TWIG_FILES="$TWIG_FILES $TOP_LEVEL/$FILE"
  334. fi
  335. fi
  336. done
  337. # Run PHPCS on changed PHP and YAML files.
  338. if [[ "$PHP_FILES" != "" ]] && [[ $PHPCS_XML_DIST_FILE_CHANGED == "0" ]]; then
  339. vendor/bin/phpcs $phpcs_memory_limit --standard="$TOP_LEVEL/core/phpcs.xml.dist" $PHP_FILES
  340. if [ "$?" -ne "0" ]; then
  341. FINAL_STATUS=1
  342. printf "\nPHPCS: ${red}failed${reset}\n"
  343. else
  344. printf "\nPHPCS: ${green}passed${reset}\n"
  345. fi
  346. # Add a separator line to make the output easier to read.
  347. printf "\n"
  348. printf -- '-%.0s' {1..100}
  349. printf "\n"
  350. fi
  351. # Run Twig CS Fixer on changed Twig files.
  352. if [[ "$TWIG_FILES" != "" ]] && [[ $TWIGCSFIXER_CONFIG_FILE_CHANGED == "0" ]]; then
  353. vendor/bin/twig-cs-fixer lint $TWIG_FILES -c "$TOP_LEVEL/core/.twig-cs-fixer.php"
  354. if [ "$?" -ne "0" ]; then
  355. FINAL_STATUS=1
  356. printf "\TWIGCS: ${red}failed${reset}\n"
  357. else
  358. printf "\TWIGCS: ${green}passed${reset}\n"
  359. fi
  360. # Add a separator line to make the output easier to read.
  361. printf "\n"
  362. printf -- '-%.0s' {1..100}
  363. printf "\n"
  364. fi
  365. # Run ESLint on changed YAML and JavaScript files.
  366. if [[ "$JS_FILES" != "" ]] && [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "0" ]]; then
  367. cd "$TOP_LEVEL/core"
  368. node ./node_modules/eslint/bin/eslint.js --quiet --resolve-plugins-relative-to . --config=.eslintrc.passing.json $JS_FILES
  369. if [ "$?" -ne "0" ]; then
  370. FINAL_STATUS=1
  371. printf "\nESLint: ${red}failed${reset}\n"
  372. else
  373. printf "\nESLint: ${green}passed${reset}\n"
  374. fi
  375. cd "$TOP_LEVEL"
  376. # Add a separator line to make the output easier to read.
  377. printf "\n"
  378. printf -- '-%.0s' {1..100}
  379. printf "\n"
  380. fi
  381. # Run Stylelint on changed CSS files.
  382. if [[ "$CSS_FILES" != "" ]] && [[ $STYLELINT_CONFIG_FILE_CHANGED == "0" ]] && [[ -f "core/node_modules/.bin/stylelint" ]]; then
  383. cd "$TOP_LEVEL/core"
  384. node_modules/.bin/stylelint --allow-empty-input $CSS_FILES
  385. if [ "$?" -ne "0" ]; then
  386. FINAL_STATUS=1
  387. printf "\nStylelint: ${red}failed${reset}\n"
  388. else
  389. printf "\nStylelint: ${green}passed${reset}\n"
  390. fi
  391. cd "$TOP_LEVEL"
  392. # Add a separator line to make the output easier to read.
  393. printf "\n"
  394. printf -- '-%.0s' {1..100}
  395. printf "\n"
  396. fi
  397. for FILE in $FILES; do
  398. # Ensure the file still exists (i.e. is not being deleted).
  399. if [ -a $FILE ]; then
  400. if [ ${FILE: -3} != ".sh" ] && [ "$FILE" != "core/scripts/dr" ]; then
  401. if [ -x $FILE ]; then
  402. printf "${red}check failed:${reset} file $FILE should not be executable\n"
  403. FINAL_STATUS=1
  404. fi
  405. fi
  406. fi
  407. # Don't commit changes to vendor.
  408. if [[ "$FILE" =~ ^vendor/ ]]; then
  409. printf "${red}check failed:${reset} file in vendor directory being committed ($FILE)\n"
  410. FINAL_STATUS=1
  411. fi
  412. # Don't commit changes to core/node_modules.
  413. if [[ "$FILE" =~ ^core/node_modules/ ]]; then
  414. printf "${red}check failed:${reset} file in core/node_modules directory being committed ($FILE)\n"
  415. FINAL_STATUS=1
  416. fi
  417. ############################################################################
  418. ### CSS FILES
  419. ############################################################################
  420. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.css$ ]]; then
  421. # Work out the root name of the CSS so we can ensure that the PostCSS
  422. # version has been compiled correctly.
  423. if [[ $FILE =~ \.pcss\.css$ ]]; then
  424. BASENAME=${FILE%.pcss.css}
  425. COMPILE_CHECK=1
  426. else
  427. BASENAME=${FILE%.css}
  428. # We only need to compile check if the .pcss.css file is not also
  429. # changing. This is because the compile check will occur for the
  430. # .pcss.css file. This might occur if the compiled stylesheets have
  431. # changed.
  432. contains_element "$BASENAME.pcss.css" "${FILES[@]}"
  433. HASPOSTCSS=$?
  434. if [ "$HASPOSTCSS" -ne "0" ]; then
  435. COMPILE_CHECK=1
  436. else
  437. COMPILE_CHECK=0
  438. fi
  439. fi
  440. # PostCSS
  441. if [[ "$COMPILE_CHECK" == "1" ]] && [[ -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
  442. cd "$TOP_LEVEL/core"
  443. yarn run build:css --check --file "$TOP_LEVEL/$BASENAME.pcss.css"
  444. CORRECTCSS=$?
  445. if [ "$CORRECTCSS" -ne "0" ]; then
  446. # If the CSS does not match the PCSS, set the status to a number other
  447. # than 0.
  448. FINAL_STATUS=1
  449. printf "\n${red}ERROR: The compiled CSS from"
  450. printf "\n ${BASENAME}.pcss.css"
  451. printf "\n does not match its CSS file. Recompile the CSS with:"
  452. printf "\n yarn run build:css${reset}\n\n"
  453. fi
  454. cd $TOP_LEVEL
  455. fi
  456. fi
  457. done
  458. if [[ "$MAINTAINERS_TXT_CHANGED" == "1" ]]; then
  459. printf "\n${blue}INFO: MAINTAINERS.TXT changed"
  460. printf "\n Make sure follow up changes are made to documentation, Slack channel, email group etc."
  461. printf "\n See https://www.drupal.org/about/core/policies/maintainers/add-or-remove-a-subsystem-or-topic-maintainer.${reset}\n\n"
  462. # Add a separator line to make the output easier to read.
  463. printf "\n"
  464. printf -- '-%.0s' {1..100}
  465. printf "\n"
  466. fi
  467. exit $FINAL_STATUS

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