deploy-dotorg.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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=${THEME_SLUG%/}
  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. SVN_URL="https://themes.svn.wordpress.org/${THEME_SLUG}/"
  53. SVN_DIR="$PWD/deploy/${THEME_SLUG}"
  54. response=$(curl -s -o /dev/null -w "%{http_code}" "$SVN_URL")
  55. if [ "$response" != "200" ]; then
  56. # echo "No theme with slug ${THEME_SLUG} to be updated. Moving on."
  57. continue;
  58. fi
  59. response=$(curl -s -o /dev/null -w "%{http_code}" "$SVN_URL/$THEME_VERSION/")
  60. if [ "$response" == "200" ]; then
  61. # echo "${THEME_SLUG} version '$THEME_VERSION' already deployed. Moving on."
  62. continue;
  63. fi
  64. # printf "\n\nAttempting to deploy theme ${THEME_SLUG} version ${THEME_VERSION}\n"
  65. svn checkout --depth immediates "$SVN_URL" "$SVN_DIR" --no-auth-cache --non-interactive > /dev/null
  66. if [ ! -d "$SVN_DIR" ]; then
  67. continue;
  68. fi
  69. if [ -d "$SVN_DIR/$THEME_VERSION" ]; then
  70. rm -rf $SVN_DIR
  71. echo "${THEME_SLUG} version '$THEME_VERSION' already deployed. Moving on."
  72. continue;
  73. fi
  74. directories=($SVN_DIR/*)
  75. last_directory=${directories[${#directories[@]}-1]}
  76. last_version="${last_directory##*/}"
  77. echo "➤ Upgrading ${THEME_SLUG} from ${last_version} to $THEME_VERSION"
  78. if [[ $1 == "preview" ]]; then
  79. continue;
  80. fi
  81. echo "➤ Copying previous version of theme ('${last_directory}') '${THEME_SLUG}' to svn repository... "
  82. svn update --set-depth infinity ${last_directory} --non-interactive
  83. svn cp ${last_directory} $SVN_DIR/$THEME_VERSION
  84. echo "➤ Copying theme '${THEME_SLUG}' version '${THEME_VERSION}' to svn repository... "
  85. rsync -rc --delete --include=theme.json --exclude-from $IGNORE_FILE ./$THEME_SLUG/ $SVN_DIR/$THEME_VERSION
  86. # Remove the wpcom-specific tags used in some themes
  87. find $SVN_DIR/$THEME_VERSION/style.css -type f -exec sed -i '' 's/, auto-loading-homepage//g' {} \;
  88. # Remove files from the previous version
  89. svn status $SVN_DIR/$THEME_VERSION | grep "^\!" | sed 's/^\! *//g' | xargs svn rm;
  90. # Add the version to SVN
  91. svn add $SVN_DIR/$THEME_VERSION --force --depth infinity -q > /dev/null
  92. echo "➤ Committing files..."
  93. 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'
  94. if [[ $? -eq 0 ]]; then
  95. echo 'Commit failed.'
  96. exit 1
  97. fi
  98. fi
  99. done