deploy-dotorg.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/bash
  2. # Make sure SVN credentials and project slug are set
  3. if [[ -z "$SVN_USERNAME" ]] && [[ $1 != "preview" ]]; then
  4. echo "Set the SVN_USERNAME secret"
  5. exit 1
  6. fi
  7. if [[ -z "$SVN_PASSWORD" ]] && [[ $1 != "preview" ]]; then
  8. echo "Set the SVN_PASSWORD secret"
  9. exit 1
  10. fi
  11. # These themes either:
  12. # are not (and not expected to be) deployed to wporg
  13. # have been deployed to wporg, however they are not expected to be updated
  14. # Note that all classic themes (those without a theme.json file) are ALSO ignored
  15. # Get the directory of the script
  16. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  17. IGNORE_FILE="$SCRIPT_DIR/.dotorg-ignore"
  18. # Initialize an empty array for themes to ignore
  19. THEMES_TO_IGNORE=()
  20. # Read themes to ignore from .dotorg-ignore file, ignoring comments
  21. if [[ -f "$IGNORE_FILE" ]]; then
  22. while IFS= read -r line || [[ -n "$line" ]]; do
  23. [[ "$line" =~ ^#.*$ ]] && continue
  24. THEMES_TO_IGNORE+=("$line")
  25. done < "$IGNORE_FILE"
  26. else
  27. echo ".dotorg-ignore file not found, proceeding without ignoring any themes."
  28. fi
  29. rm -rf ./deploy
  30. # Do things for all of the themes
  31. for THEME_SLUG in */ ; do
  32. # Remove slashes from the theme slug
  33. THEME_SLUG=$(echo $THEME_SLUG | tr -d '/')
  34. # Skip any classic themes (assuming that none of our classic themes use a theme.json file)
  35. if ! test -f "./${THEME_SLUG}/theme.json"; then
  36. # echo "Ignoring classic theme ${THEME_SLUG}. Moving on."
  37. continue;
  38. fi
  39. # Skip any themes that are in the ignore list
  40. if [[ " ${THEMES_TO_IGNORE[*]} " == *" ${THEME_SLUG//\//} "* ]]; then
  41. # echo "Ignoring theme ${THEME_SLUG} from list. Moving on."
  42. continue;
  43. fi
  44. if test -f "./${THEME_SLUG}/style.css"; then
  45. THEME_VERSION=$(cat ./${THEME_SLUG}/style.css \
  46. | grep Version \
  47. | head -1 \
  48. | awk -F: '{ print $2 }' \
  49. | sed 's/[",]//g' \
  50. | sed 's/-wpcom//g' \
  51. | tr -d '[[:space:]]')
  52. DIRECTORY_URL="https://wordpress.org/themes/${THEME_SLUG}/"
  53. SVN_URL="https://themes.svn.wordpress.org/${THEME_SLUG}/"
  54. SVN_DIR="$PWD/deploy/${THEME_SLUG}"
  55. response=$(curl -s -o /dev/null -w "%{http_code}" "$DIRECTORY_URL")
  56. if [ "$response" != "200" ]; then
  57. # echo "Theme with slug ${THEME_SLUG} has not been approved to the themes directory. Moving on."
  58. continue;
  59. fi
  60. response=$(curl -s -o /dev/null -w "%{http_code}" "$SVN_URL")
  61. if [ "$response" != "200" ]; then
  62. # echo "No theme with slug ${THEME_SLUG} to be updated. Moving on."
  63. continue;
  64. fi
  65. response=$(curl -s -o /dev/null -w "%{http_code}" "$SVN_URL/$THEME_VERSION/")
  66. if [ "$response" == "200" ]; then
  67. # echo "${THEME_SLUG} version '$THEME_VERSION' already deployed. Moving on."
  68. continue;
  69. fi
  70. # printf "\n\nAttempting to deploy theme ${THEME_SLUG} version ${THEME_VERSION}\n"
  71. svn checkout --depth immediates "$SVN_URL" "$SVN_DIR" --no-auth-cache --non-interactive > /dev/null
  72. if [ ! -d "$SVN_DIR" ]; then
  73. continue;
  74. fi
  75. if [ -d "$SVN_DIR/$THEME_VERSION" ]; then
  76. rm -rf $SVN_DIR
  77. echo "${THEME_SLUG} version '$THEME_VERSION' already deployed. Moving on."
  78. continue;
  79. fi
  80. directories=($SVN_DIR/*)
  81. last_directory=${directories[${#directories[@]}-1]}
  82. last_version="${last_directory##*/}"
  83. # Check if last_version is a later version than THEME_VERSION
  84. theme_version_without_decimal=$(echo $THEME_VERSION | tr -d '.')
  85. last_version_without_decimal=$(echo $last_version | tr -d '.')
  86. if (( last_version_without_decimal > theme_version_without_decimal )); then
  87. echo "${THEME_SLUG} version '${last_version}' is newer than '${THEME_VERSION}', so it must not need updating. Moving on."
  88. continue;
  89. fi
  90. echo "➤ Upgrading ${THEME_SLUG} from ${last_version} to $THEME_VERSION"
  91. if [[ $1 == "preview" ]]; then
  92. continue;
  93. fi
  94. echo "➤ Copying previous version of theme ('${last_directory}') '${THEME_SLUG}' to svn repository... "
  95. svn update --set-depth infinity ${last_directory} --non-interactive
  96. svn cp ${last_directory} $SVN_DIR/$THEME_VERSION
  97. echo "➤ Copying theme '${THEME_SLUG}' version '${THEME_VERSION}' to svn repository... "
  98. rsync -rc --delete --include=theme.json --exclude-from $IGNORE_FILE ./$THEME_SLUG/ $SVN_DIR/$THEME_VERSION
  99. # Remove the wpcom-specific tags used in some themes
  100. find $SVN_DIR/$THEME_VERSION/style.css -type f -exec sed -i '' 's/, auto-loading-homepage//g' {} \;
  101. # Remove files from the previous version
  102. svn status $SVN_DIR/$THEME_VERSION | grep "^\!" | sed 's/^\! *//g' | xargs svn rm;
  103. # Add the version to SVN
  104. svn add $SVN_DIR/$THEME_VERSION --force --depth infinity -q > /dev/null
  105. echo "➤ Committing files..."
  106. svn commit $SVN_DIR -m "Update to version ${THEME_VERSION} from GitHub" --no-auth-cache --non-interactive --username ${SVN_USERNAME} --password ${SVN_PASSWORD} 2>&1 | grep 'svn: E'
  107. if [[ $? -eq 0 ]]; then
  108. echo 'Commit failed.'
  109. exit 1
  110. fi
  111. fi
  112. done