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
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)
  blue=$(tput setaf 4 && 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

  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 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 [[ "$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

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

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