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
DRUPALCI=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 "--drupalci                a special mode for DrupalCI"
      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
      ;;
    --drupalci)
      DRUPALCI=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. Color output is disabled on
# DrupalCI because it is breaks reporting.
# @todo https://www.drupal.org/project/drupalci_testbot/issues/3181869
if [[ "$DRUPALCI" == "1" ]]; then
  red=""
  green=""
  reset=""
  DRUPAL_VERSION=$(php -r "include 'vendor/autoload.php'; print preg_replace('#\.[0-9]+-dev#', '.x', \Drupal::VERSION);")
  GIT="sudo -u www-data git"
else
  red=$(tput setaf 1 && tput bold)
  green=$(tput setaf 2)
  reset=$(tput sgr0)
  GIT="git"
fi

# Gets list of files to check.
if [[ "$BRANCH" != "" ]]; then
  FILES=$($GIT diff --name-only $BRANCH HEAD);
elif [[ "$CACHED" == "0" ]]; then
  # For DrupalCI patch testing or when running without --cached or --branch,
  # 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

if [[ "$FILES" == "" ]] && [[ "$DRUPALCI" == "1" ]]; then
  # If the FILES is empty we might be testing a merge request on DrupalCI. We
  # need to diff against the Drupal branch or tag related to the Drupal version.
  printf "Creating list of files to check by comparing branch to %s\n" "$DRUPAL_VERSION"
  # On DrupalCI there's a merge commit so we can compare to HEAD~1.
  FILES=$($GIT diff --name-only HEAD~1 HEAD);
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
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 on DrupalCI or when phpstan files are changed.
# APCu is disabled to ensure that the composer classmap is not corrupted.
if [[ $PHPSTAN_DIST_FILE_CHANGED == "1" ]] || [[ "$DRUPALCI" == "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 on DrupalCI or when phpcs files are changed.
if [[ $PHPCS_XML_DIST_FILE_CHANGED == "1" ]] || [[ "$DRUPALCI" == "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 a Drupal-specific CKEditor 5 plugin changed ensure that it is compiled
# properly. Only check on DrupalCI, since we're concerned about the build being
# run with the expected package versions and making sure the result of the build
# is in sync and conform to expectations.
if [[ "$DRUPALCI" == "1" ]] && [[ $CKEDITOR5_PLUGINS_CHANGED == "1" ]]; then
  cd "$TOP_LEVEL/core"
  yarn run check:ckeditor5
  if [ "$?" -ne "0" ]; then
    # If there are failures set the status to a number other than 0.
    FINAL_STATUS=1
    printf "\nDrupal-specific CKEditor 5 plugins: ${red}failed${reset}\n"
  else
    printf "\nDrupal-specific CKEditor 5 plugins: ${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

for FILE in $FILES; do
  STATUS=0;
  # Print a line to separate spellcheck output from per file output.
  printf "Checking %s\n" "$FILE"
  printf "\n"

  # 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"
        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"
    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"
    STATUS=1
  fi

  ############################################################################
  ### PHP AND YAML FILES
  ############################################################################
  if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.(inc|install|module|php|profile|test|theme|yml)$ ]] && [[ $PHPCS_XML_DIST_FILE_CHANGED == "0" ]] && [[ "$DRUPALCI" == "0" ]]; then
    # Test files with phpcs rules.
    vendor/bin/phpcs $phpcs_memory_limit "$TOP_LEVEL/$FILE" --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.
      STATUS=1
    else
      printf "PHPCS: $FILE ${green}passed${reset}\n"
    fi
  fi

  ############################################################################
  ### YAML FILES
  ############################################################################
  if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.yml$ ]]; then
    # Test files with ESLint.
    cd "$TOP_LEVEL/core"
    node ./node_modules/eslint/bin/eslint.js --quiet --resolve-plugins-relative-to . "$TOP_LEVEL/$FILE"
    YAMLLINT=$?
    if [ "$YAMLLINT" -ne "0" ]; then
      # If there are failures set the status to a number other than 0.
      STATUS=1
    else
      printf "ESLint: $FILE ${green}passed${reset}\n"
    fi
    cd $TOP_LEVEL
  fi

  ############################################################################
  ### JAVASCRIPT FILES
  ############################################################################
  if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.js$ ]]; then
    cd "$TOP_LEVEL/core"
    # Check the coding standards.
    node ./node_modules/eslint/bin/eslint.js --quiet --config=.eslintrc.passing.json "$TOP_LEVEL/$FILE"
    JSLINT=$?
    if [ "$JSLINT" -ne "0" ]; then
      # No need to write any output the node command will do this for us.
      STATUS=1
    else
      printf "ESLint: $FILE ${green}passed${reset}\n"
    fi
    cd $TOP_LEVEL
  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.
        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
  if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.css$ ]] && [[ -f "core/node_modules/.bin/stylelint" ]]; then
    BASENAME=${FILE%.css}
    # We only need to use stylelint on the .pcss.css file. So if this CSS file
    # has a corresponding .pcss don't do stylelint.
    if [[ $FILE =~ \.pcss\.css$ ]] || [[ ! -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
      cd "$TOP_LEVEL/core"
      node_modules/.bin/stylelint --allow-empty-input "$TOP_LEVEL/$FILE"
      if [ "$?" -ne "0" ]; then
        STATUS=1
      else
        printf "STYLELINT: $FILE ${green}passed${reset}\n"
      fi
      cd $TOP_LEVEL
    fi
  fi

  if [[ "$STATUS" == "1" ]]; then
    FINAL_STATUS=1
    # There is no need to print a failure message. The fail will be described
    # already.
  else
    printf "%s ${green}passed${reset}\n" "$FILE"
  fi

  # Print a line to separate each file's checks.
  printf "\n"
  printf -- '-%.0s' {1..100}
  printf "\n"
done

if [[ "$FINAL_STATUS" == "1" ]] && [[ "$DRUPALCI" == "1" ]]; then
  printf "${red}Drupal code quality checks failed.${reset}\n"
  printf "To reproduce this output locally:\n"
  printf "* Apply the change as a patch\n"
  printf "* Run this command locally: sh ./core/scripts/dev/commit-code-check.sh\n"
  printf "OR:\n"
  printf "* From the merge request branch\n"
  printf "* Run this command locally: sh ./core/scripts/dev/commit-code-check.sh --branch %s\n" "$DRUPAL_VERSION"
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. DRUPALCI=0
  28. BRANCH=""
  29. while test $# -gt 0; do
  30. case "$1" in
  31. -h|--help)
  32. echo "Drupal code quality checks"
  33. echo " "
  34. echo "options:"
  35. echo "-h, --help show brief help"
  36. echo "--branch BRANCH creates list of files to check by comparing against a branch"
  37. echo "--cached checks staged files"
  38. echo "--drupalci a special mode for DrupalCI"
  39. echo "--memory-unlimited bypass PHP memory limit for PHPStan and PHPCS"
  40. echo " "
  41. echo "Example usage: sh ./core/scripts/dev/commit-code-check.sh --branch 9.2.x"
  42. exit 0
  43. ;;
  44. --branch)
  45. BRANCH="$2"
  46. if [[ "$BRANCH" == "" ]]; then
  47. printf "The --branch option requires a value. For example: --branch 9.2.x\n"
  48. exit;
  49. fi
  50. shift 2
  51. ;;
  52. --cached)
  53. CACHED=1
  54. shift
  55. ;;
  56. --drupalci)
  57. DRUPALCI=1
  58. shift
  59. ;;
  60. --memory-unlimited)
  61. MEMORY_UNLIMITED=1
  62. shift
  63. ;;
  64. *)
  65. break
  66. ;;
  67. esac
  68. done
  69. memory_limit=""
  70. phpcs_memory_limit=""
  71. if [[ "$MEMORY_UNLIMITED" == "1" ]]; then
  72. memory_limit="--memory-limit=-1"
  73. phpcs_memory_limit="-d memory_limit=-1"
  74. fi
  75. # Set up variables to make colored output simple. Color output is disabled on
  76. # DrupalCI because it is breaks reporting.
  77. # @todo https://www.drupal.org/project/drupalci_testbot/issues/3181869
  78. if [[ "$DRUPALCI" == "1" ]]; then
  79. red=""
  80. green=""
  81. reset=""
  82. DRUPAL_VERSION=$(php -r "include 'vendor/autoload.php'; print preg_replace('#\.[0-9]+-dev#', '.x', \Drupal::VERSION);")
  83. GIT="sudo -u www-data git"
  84. else
  85. red=$(tput setaf 1 && tput bold)
  86. green=$(tput setaf 2)
  87. reset=$(tput sgr0)
  88. GIT="git"
  89. fi
  90. # Gets list of files to check.
  91. if [[ "$BRANCH" != "" ]]; then
  92. FILES=$($GIT diff --name-only $BRANCH HEAD);
  93. elif [[ "$CACHED" == "0" ]]; then
  94. # For DrupalCI patch testing or when running without --cached or --branch,
  95. # list of all changes in the working directory.
  96. FILES=$($GIT ls-files --other --modified --exclude-standard --exclude=vendor)
  97. else
  98. # Check staged files only.
  99. if $GIT rev-parse --verify HEAD >/dev/null 2>&1
  100. then
  101. AGAINST=HEAD
  102. else
  103. # Initial commit: diff against an empty tree object
  104. AGAINST=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  105. fi
  106. FILES=$($GIT diff --cached --name-only $AGAINST);
  107. fi
  108. if [[ "$FILES" == "" ]] && [[ "$DRUPALCI" == "1" ]]; then
  109. # If the FILES is empty we might be testing a merge request on DrupalCI. We
  110. # need to diff against the Drupal branch or tag related to the Drupal version.
  111. printf "Creating list of files to check by comparing branch to %s\n" "$DRUPAL_VERSION"
  112. # On DrupalCI there's a merge commit so we can compare to HEAD~1.
  113. FILES=$($GIT diff --name-only HEAD~1 HEAD);
  114. fi
  115. TOP_LEVEL=$($GIT rev-parse --show-toplevel)
  116. # This variable will be set to one when the file core/phpcs.xml.dist is changed.
  117. PHPCS_XML_DIST_FILE_CHANGED=0
  118. # This variable will be set to one when the files core/.phpstan-baseline.php or
  119. # core/phpstan.neon.dist are changed.
  120. PHPSTAN_DIST_FILE_CHANGED=0
  121. # This variable will be set to one when one of the eslint config file is
  122. # changed:
  123. # - core/.eslintrc.passing.json
  124. # - core/.eslintrc.json
  125. # - core/.eslintrc.jquery.json
  126. ESLINT_CONFIG_PASSING_FILE_CHANGED=0
  127. # This variable will be set to one when the stylelint config file is changed.
  128. # changed:
  129. # - core/.stylelintignore
  130. # - core/.stylelintrc.json
  131. STYLELINT_CONFIG_FILE_CHANGED=0
  132. # This variable will be set to one when JavaScript packages files are changed.
  133. # changed:
  134. # - core/package.json
  135. # - core/yarn.lock
  136. JAVASCRIPT_PACKAGES_CHANGED=0
  137. # This variable will be set when a Drupal-specific CKEditor 5 plugin has changed
  138. # it is used to make sure the compiled JS is valid.
  139. CKEDITOR5_PLUGINS_CHANGED=0
  140. # This variable will be set to one when either of the core dictionaries or the
  141. # .cspell.json config has changed.
  142. CSPELL_DICTIONARY_FILE_CHANGED=0
  143. # Build up a list of absolute file names.
  144. ABS_FILES=
  145. for FILE in $FILES; do
  146. if [ -f "$TOP_LEVEL/$FILE" ]; then
  147. ABS_FILES="$ABS_FILES $TOP_LEVEL/$FILE"
  148. fi
  149. if [[ $FILE == "core/phpcs.xml.dist" ]]; then
  150. PHPCS_XML_DIST_FILE_CHANGED=1;
  151. fi;
  152. if [[ $FILE == "core/.phpstan-baseline.php" || $FILE == "core/phpstan.neon.dist" ]]; then
  153. PHPSTAN_DIST_FILE_CHANGED=1;
  154. fi;
  155. if [[ $FILE == "core/.eslintrc.json" || $FILE == "core/.eslintrc.passing.json" || $FILE == "core/.eslintrc.jquery.json" ]]; then
  156. ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  157. fi;
  158. if [[ $FILE == "core/.stylelintignore" || $FILE == "core/.stylelintrc.json" ]]; then
  159. STYLELINT_CONFIG_FILE_CHANGED=1;
  160. fi;
  161. # If JavaScript packages change, then rerun all JavaScript style checks.
  162. if [[ $FILE == "core/package.json" || $FILE == "core/yarn.lock" ]]; then
  163. ESLINT_CONFIG_PASSING_FILE_CHANGED=1;
  164. STYLELINT_CONFIG_FILE_CHANGED=1;
  165. JAVASCRIPT_PACKAGES_CHANGED=1;
  166. fi;
  167. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.js$ ]] && [[ $FILE =~ ^core/modules/ckeditor5/js/build || $FILE =~ ^core/modules/ckeditor5/js/ckeditor5_plugins ]]; then
  168. CKEDITOR5_PLUGINS_CHANGED=1;
  169. fi;
  170. if [[ $FILE == "core/misc/cspell/dictionary.txt" || $FILE == "core/misc/cspell/drupal-dictionary.txt" || $FILE == "core/.cspell.json" ]]; then
  171. CSPELL_DICTIONARY_FILE_CHANGED=1;
  172. fi
  173. done
  174. # Exit early if there are no files.
  175. if [[ "$ABS_FILES" == "" ]]; then
  176. printf "There are no files to check. If you have staged a commit use the --cached option.\n"
  177. exit;
  178. fi;
  179. # This script assumes that composer install and yarn install have already been
  180. # run and all dependencies are updated.
  181. FINAL_STATUS=0
  182. DEPENDENCIES_NEED_INSTALLING=0
  183. # Ensure PHP development dependencies are installed.
  184. # @todo https://github.com/composer/composer/issues/4497 Improve this to
  185. # determine if dependencies in the lock file match the installed versions.
  186. # Using composer install --dry-run is not valid because it would depend on
  187. # user-facing strings in Composer.
  188. if ! [[ -f 'vendor/bin/phpcs' ]]; then
  189. printf "Drupal's PHP development dependencies are not installed. Run 'composer install' from the root directory.\n"
  190. DEPENDENCIES_NEED_INSTALLING=1;
  191. fi
  192. cd "$TOP_LEVEL/core"
  193. # Ensure JavaScript development dependencies are installed.
  194. yarn --version
  195. yarn >/dev/null
  196. # Check all files for spelling in one go for better performance.
  197. if [[ $CSPELL_DICTIONARY_FILE_CHANGED == "1" ]] ; then
  198. printf "\nRunning spellcheck on *all* files.\n"
  199. yarn run spellcheck:core --no-must-find-files --no-progress
  200. else
  201. # Check all files for spelling in one go for better performance. We pipe the
  202. # list files in so we obey the globs set on the spellcheck:core command in
  203. # core/package.json.
  204. echo "${ABS_FILES}" | tr ' ' '\n' | yarn run spellcheck:core --no-must-find-files --file-list stdin
  205. fi
  206. if [ "$?" -ne "0" ]; then
  207. # If there are failures set the status to a number other than 0.
  208. FINAL_STATUS=1
  209. printf "\nCSpell: ${red}failed${reset}\n"
  210. else
  211. printf "\nCSpell: ${green}passed${reset}\n"
  212. fi
  213. cd "$TOP_LEVEL"
  214. # Add a separator line to make the output easier to read.
  215. printf "\n"
  216. printf -- '-%.0s' {1..100}
  217. printf "\n"
  218. # Run PHPStan on all files on DrupalCI or when phpstan files are changed.
  219. # APCu is disabled to ensure that the composer classmap is not corrupted.
  220. if [[ $PHPSTAN_DIST_FILE_CHANGED == "1" ]] || [[ "$DRUPALCI" == "1" ]]; then
  221. printf "\nRunning PHPStan on *all* files.\n"
  222. 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
  223. else
  224. # Only run PHPStan on changed files locally.
  225. printf "\nRunning PHPStan on changed files.\n"
  226. 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
  227. fi
  228. if [ "$?" -ne "0" ]; then
  229. # If there are failures set the status to a number other than 0.
  230. FINAL_STATUS=1
  231. printf "\nPHPStan: ${red}failed${reset}\n"
  232. else
  233. printf "\nPHPStan: ${green}passed${reset}\n"
  234. fi
  235. # Add a separator line to make the output easier to read.
  236. printf "\n"
  237. printf -- '-%.0s' {1..100}
  238. printf "\n"
  239. # Run PHPCS on all files on DrupalCI or when phpcs files are changed.
  240. if [[ $PHPCS_XML_DIST_FILE_CHANGED == "1" ]] || [[ "$DRUPALCI" == "1" ]]; then
  241. # Test all files with phpcs rules.
  242. 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"
  243. PHPCS=$?
  244. if [ "$PHPCS" -ne "0" ]; then
  245. # If there are failures set the status to a number other than 0.
  246. FINAL_STATUS=1
  247. printf "\nPHPCS: ${red}failed${reset}\n"
  248. else
  249. printf "\nPHPCS: ${green}passed${reset}\n"
  250. fi
  251. # Add a separator line to make the output easier to read.
  252. printf "\n"
  253. printf -- '-%.0s' {1..100}
  254. printf "\n"
  255. fi
  256. # When the eslint config has been changed, then eslint must check all files.
  257. if [[ $ESLINT_CONFIG_PASSING_FILE_CHANGED == "1" ]]; then
  258. cd "$TOP_LEVEL/core"
  259. yarn run lint:core-js-passing "$TOP_LEVEL/core"
  260. CORRECTJS=$?
  261. if [ "$CORRECTJS" -ne "0" ]; then
  262. # If there are failures set the status to a number other than 0.
  263. FINAL_STATUS=1
  264. printf "\neslint: ${red}failed${reset}\n"
  265. else
  266. printf "\neslint: ${green}passed${reset}\n"
  267. fi
  268. cd $TOP_LEVEL
  269. # Add a separator line to make the output easier to read.
  270. printf "\n"
  271. printf -- '-%.0s' {1..100}
  272. printf "\n"
  273. fi
  274. # When the stylelint config has been changed, then stylelint must check all files.
  275. if [[ $STYLELINT_CONFIG_FILE_CHANGED == "1" ]]; then
  276. cd "$TOP_LEVEL/core"
  277. yarn run lint:css
  278. if [ "$?" -ne "0" ]; then
  279. # If there are failures set the status to a number other than 0.
  280. FINAL_STATUS=1
  281. printf "\nstylelint: ${red}failed${reset}\n"
  282. else
  283. printf "\nstylelint: ${green}passed${reset}\n"
  284. fi
  285. cd $TOP_LEVEL
  286. # Add a separator line to make the output easier to read.
  287. printf "\n"
  288. printf -- '-%.0s' {1..100}
  289. printf "\n"
  290. fi
  291. # When a Drupal-specific CKEditor 5 plugin changed ensure that it is compiled
  292. # properly. Only check on DrupalCI, since we're concerned about the build being
  293. # run with the expected package versions and making sure the result of the build
  294. # is in sync and conform to expectations.
  295. if [[ "$DRUPALCI" == "1" ]] && [[ $CKEDITOR5_PLUGINS_CHANGED == "1" ]]; then
  296. cd "$TOP_LEVEL/core"
  297. yarn run check:ckeditor5
  298. if [ "$?" -ne "0" ]; then
  299. # If there are failures set the status to a number other than 0.
  300. FINAL_STATUS=1
  301. printf "\nDrupal-specific CKEditor 5 plugins: ${red}failed${reset}\n"
  302. else
  303. printf "\nDrupal-specific CKEditor 5 plugins: ${green}passed${reset}\n"
  304. fi
  305. cd $TOP_LEVEL
  306. # Add a separator line to make the output easier to read.
  307. printf "\n"
  308. printf -- '-%.0s' {1..100}
  309. printf "\n"
  310. fi
  311. # When JavaScript packages change, then rerun all JavaScript style checks.
  312. if [[ "$JAVASCRIPT_PACKAGES_CHANGED" == "1" ]]; then
  313. cd "$TOP_LEVEL/core"
  314. yarn run build:css --check
  315. CORRECTCSS=$?
  316. if [ "$CORRECTCSS" -ne "0" ]; then
  317. FINAL_STATUS=1
  318. printf "\n${red}ERROR: The compiled CSS from the PCSS files"
  319. printf "\n does not match the current CSS files. Some added"
  320. printf "\n or updated JavaScript package made changes."
  321. printf "\n Recompile the CSS with: yarn run build:css${reset}\n\n"
  322. fi
  323. cd $TOP_LEVEL
  324. # Add a separator line to make the output easier to read.
  325. printf "\n"
  326. printf -- '-%.0s' {1..100}
  327. printf "\n"
  328. fi
  329. for FILE in $FILES; do
  330. STATUS=0;
  331. # Print a line to separate spellcheck output from per file output.
  332. printf "Checking %s\n" "$FILE"
  333. printf "\n"
  334. # Ensure the file still exists (i.e. is not being deleted).
  335. if [ -a $FILE ]; then
  336. if [ ${FILE: -3} != ".sh" ]; then
  337. if [ -x $FILE ]; then
  338. printf "${red}check failed:${reset} file $FILE should not be executable\n"
  339. STATUS=1
  340. fi
  341. fi
  342. fi
  343. # Don't commit changes to vendor.
  344. if [[ "$FILE" =~ ^vendor/ ]]; then
  345. printf "${red}check failed:${reset} file in vendor directory being committed ($FILE)\n"
  346. STATUS=1
  347. fi
  348. # Don't commit changes to core/node_modules.
  349. if [[ "$FILE" =~ ^core/node_modules/ ]]; then
  350. printf "${red}check failed:${reset} file in core/node_modules directory being committed ($FILE)\n"
  351. STATUS=1
  352. fi
  353. ############################################################################
  354. ### PHP AND YAML FILES
  355. ############################################################################
  356. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.(inc|install|module|php|profile|test|theme|yml)$ ]] && [[ $PHPCS_XML_DIST_FILE_CHANGED == "0" ]] && [[ "$DRUPALCI" == "0" ]]; then
  357. # Test files with phpcs rules.
  358. vendor/bin/phpcs $phpcs_memory_limit "$TOP_LEVEL/$FILE" --standard="$TOP_LEVEL/core/phpcs.xml.dist"
  359. PHPCS=$?
  360. if [ "$PHPCS" -ne "0" ]; then
  361. # If there are failures set the status to a number other than 0.
  362. STATUS=1
  363. else
  364. printf "PHPCS: $FILE ${green}passed${reset}\n"
  365. fi
  366. fi
  367. ############################################################################
  368. ### YAML FILES
  369. ############################################################################
  370. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.yml$ ]]; then
  371. # Test files with ESLint.
  372. cd "$TOP_LEVEL/core"
  373. node ./node_modules/eslint/bin/eslint.js --quiet --resolve-plugins-relative-to . "$TOP_LEVEL/$FILE"
  374. YAMLLINT=$?
  375. if [ "$YAMLLINT" -ne "0" ]; then
  376. # If there are failures set the status to a number other than 0.
  377. STATUS=1
  378. else
  379. printf "ESLint: $FILE ${green}passed${reset}\n"
  380. fi
  381. cd $TOP_LEVEL
  382. fi
  383. ############################################################################
  384. ### JAVASCRIPT FILES
  385. ############################################################################
  386. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.js$ ]]; then
  387. cd "$TOP_LEVEL/core"
  388. # Check the coding standards.
  389. node ./node_modules/eslint/bin/eslint.js --quiet --config=.eslintrc.passing.json "$TOP_LEVEL/$FILE"
  390. JSLINT=$?
  391. if [ "$JSLINT" -ne "0" ]; then
  392. # No need to write any output the node command will do this for us.
  393. STATUS=1
  394. else
  395. printf "ESLint: $FILE ${green}passed${reset}\n"
  396. fi
  397. cd $TOP_LEVEL
  398. fi
  399. ############################################################################
  400. ### CSS FILES
  401. ############################################################################
  402. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.css$ ]]; then
  403. # Work out the root name of the CSS so we can ensure that the PostCSS
  404. # version has been compiled correctly.
  405. if [[ $FILE =~ \.pcss\.css$ ]]; then
  406. BASENAME=${FILE%.pcss.css}
  407. COMPILE_CHECK=1
  408. else
  409. BASENAME=${FILE%.css}
  410. # We only need to compile check if the .pcss.css file is not also
  411. # changing. This is because the compile check will occur for the
  412. # .pcss.css file. This might occur if the compiled stylesheets have
  413. # changed.
  414. contains_element "$BASENAME.pcss.css" "${FILES[@]}"
  415. HASPOSTCSS=$?
  416. if [ "$HASPOSTCSS" -ne "0" ]; then
  417. COMPILE_CHECK=1
  418. else
  419. COMPILE_CHECK=0
  420. fi
  421. fi
  422. # PostCSS
  423. if [[ "$COMPILE_CHECK" == "1" ]] && [[ -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
  424. cd "$TOP_LEVEL/core"
  425. yarn run build:css --check --file "$TOP_LEVEL/$BASENAME.pcss.css"
  426. CORRECTCSS=$?
  427. if [ "$CORRECTCSS" -ne "0" ]; then
  428. # If the CSS does not match the PCSS, set the status to a number other
  429. # than 0.
  430. STATUS=1
  431. printf "\n${red}ERROR: The compiled CSS from"
  432. printf "\n ${BASENAME}.pcss.css"
  433. printf "\n does not match its CSS file. Recompile the CSS with:"
  434. printf "\n yarn run build:css${reset}\n\n"
  435. fi
  436. cd $TOP_LEVEL
  437. fi
  438. fi
  439. if [[ -f "$TOP_LEVEL/$FILE" ]] && [[ $FILE =~ \.css$ ]] && [[ -f "core/node_modules/.bin/stylelint" ]]; then
  440. BASENAME=${FILE%.css}
  441. # We only need to use stylelint on the .pcss.css file. So if this CSS file
  442. # has a corresponding .pcss don't do stylelint.
  443. if [[ $FILE =~ \.pcss\.css$ ]] || [[ ! -f "$TOP_LEVEL/$BASENAME.pcss.css" ]]; then
  444. cd "$TOP_LEVEL/core"
  445. node_modules/.bin/stylelint --allow-empty-input "$TOP_LEVEL/$FILE"
  446. if [ "$?" -ne "0" ]; then
  447. STATUS=1
  448. else
  449. printf "STYLELINT: $FILE ${green}passed${reset}\n"
  450. fi
  451. cd $TOP_LEVEL
  452. fi
  453. fi
  454. if [[ "$STATUS" == "1" ]]; then
  455. FINAL_STATUS=1
  456. # There is no need to print a failure message. The fail will be described
  457. # already.
  458. else
  459. printf "%s ${green}passed${reset}\n" "$FILE"
  460. fi
  461. # Print a line to separate each file's checks.
  462. printf "\n"
  463. printf -- '-%.0s' {1..100}
  464. printf "\n"
  465. done
  466. if [[ "$FINAL_STATUS" == "1" ]] && [[ "$DRUPALCI" == "1" ]]; then
  467. printf "${red}Drupal code quality checks failed.${reset}\n"
  468. printf "To reproduce this output locally:\n"
  469. printf "* Apply the change as a patch\n"
  470. printf "* Run this command locally: sh ./core/scripts/dev/commit-code-check.sh\n"
  471. printf "OR:\n"
  472. printf "* From the merge request branch\n"
  473. printf "* Run this command locally: sh ./core/scripts/dev/commit-code-check.sh --branch %s\n" "$DRUPAL_VERSION"
  474. fi
  475. exit $FINAL_STATUS

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