deploy-dotorg.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # Skip any classic themes (assuming that none of our classic themes use a theme.json file)
  33. if ! test -f "./${THEME_SLUG}/theme.json"; then
  34. # echo "Ignoring classic theme ${THEME_SLUG}. Moving on."
  35. continue;
  36. fi
  37. # Skip any themes that are in the ignore list
  38. if [[ " ${THEMES_TO_IGNORE[*]} " == *" ${THEME_SLUG//\//} "* ]]; then
  39. # echo "Ignoring theme ${THEME_SLUG} from list. Moving on."
  40. continue;
  41. fi
  42. if test -f "./${THEME_SLUG}/style.css"; then
  43. THEME_VERSION=$(cat ./${THEME_SLUG}/style.css \
  44. | grep Version \
  45. | head -1 \
  46. | awk -F: '{ print $2 }' \
  47. | sed 's/[",]//g' \
  48. | sed 's/-wpcom//g' \
  49. | tr -d '[[:space:]]')
  50. SVN_URL="https://themes.svn.wordpress.org/${THEME_SLUG}/"
  51. SVN_DIR="$PWD/deploy/${THEME_SLUG}"
  52. response=$(curl -s -o /dev/null -w "%{http_code}" "$SVN_URL")
  53. if [ "$response" != "200" ]; then
  54. # echo "No theme with slug ${THEME_SLUG} to be updated. Moving on."
  55. continue;
  56. fi
  57. response=$(curl -s -o /dev/null -w "%{http_code}" "$SVN_URL/$THEME_VERSION/")
  58. if [ "$response" == "200" ]; then
  59. # echo "${THEME_SLUG} version '$THEME_VERSION' already deployed. Moving on."
  60. continue;
  61. fi
  62. # printf "\n\nAttempting to deploy theme ${THEME_SLUG} version ${THEME_VERSION}\n"
  63. svn checkout --depth immediates "$SVN_URL" "$SVN_DIR" --no-auth-cache --non-interactive > /dev/null
  64. if [ ! -d "$SVN_DIR" ]; then
  65. continue;
  66. fi
  67. if [ -d "$SVN_DIR/$THEME_VERSION" ]; then
  68. rm -rf $SVN_DIR
  69. echo "${THEME_SLUG} version '$THEME_VERSION' already deployed. Moving on."
  70. continue;
  71. fi
  72. directories=($SVN_DIR/*)
  73. last_directory=${directories[${#directories[@]}-1]}
  74. last_version="${last_directory##*/}"
  75. echo "➤ Upgrading ${THEME_SLUG} from ${last_version} to $THEME_VERSION"
  76. if [[ $1 == "preview" ]]; then
  77. continue;
  78. fi
  79. echo "➤ Copying previous version of theme ('${last_directory}') '${THEME_SLUG}' to svn repository... "
  80. svn update --set-depth infinity ${last_directory} --non-interactive
  81. svn cp ${last_directory} $SVN_DIR/$THEME_VERSION
  82. echo "➤ Copying theme '${THEME_SLUG}' version '${THEME_VERSION}' to svn repository... "
  83. rsync -rc --delete --include=theme.json --exclude-from $IGNORE_FILE ./$THEME_SLUG/ $SVN_DIR/$THEME_VERSION
  84. # Remove the wpcom-specific tags used in some themes
  85. find $SVN_DIR/$THEME_VERSION/style.css -type f -exec sed -i '' 's/, auto-loading-homepage//g' {} \;
  86. # Remove files from the previous version
  87. svn status $SVN_DIR/$THEME_VERSION | grep "^\!" | sed 's/^\! *//g' | xargs svn rm;
  88. # Add the version to SVN
  89. svn add $SVN_DIR/$THEME_VERSION --force --depth infinity -q > /dev/null
  90. echo "➤ Committing files..."
  91. 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'
  92. if [[ $? -eq 0 ]]; then
  93. echo 'Commit failed.'
  94. exit 1
  95. fi
  96. fi
  97. done