Same filename and directory in other branches
  1. 8.9.x core/scripts/dev/commit-code-check.sh
  2. 9 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
}

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 " "
      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
      ;;
    *)
      break
      ;;
  esac
done

# 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 when the dictionary 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" ]]; 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 check -s 2>/dev/null
if [ "$?" -ne "0" ]; then
  printf "Drupal's JavaScript development dependencies are not installed or cannot be resolved. Run 'yarn install' inside the core directory, or 'yarn check -s' to list other errors.\n"
  DEPENDENCIES_NEED_INSTALLING=1;
fi

if [ $DEPENDENCIES_NEED_INSTALLING -ne 0 ]; then
  exit 1;
fi

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