theme-batch-utils.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/zsh
  2. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  3. IGNORE_FILE="$SCRIPT_DIR/.dotorg-ignore"
  4. git remote update > /dev/null
  5. current_branch=$(git branch --show-current)
  6. hash_at_divergence=$(git merge-base origin/trunk ${current_branch})
  7. version_bump_count=0
  8. # version bump (patch) any theme that has any *comitted* changes since it was branched from /trunk or any *uncomitted* changes
  9. version-bump() {
  10. # Only version bump things that haven't already had their version changed
  11. if [[ $1 = 'ROOT' ]]; then
  12. # Only version bump ROOT if another project has been version bumped
  13. if [[ $version_bump_count = 0 ]]; then
  14. echo "No projects have changed. Nothing version bumped."
  15. echo
  16. return
  17. fi
  18. package_string=$(git show ${hash_at_divergence}:package.json)
  19. else
  20. package_string=$(git show ${hash_at_divergence}:$1package.json 2>/dev/null)
  21. fi
  22. # If the last command exited poorly ($? = last command exit code) the package.json didn't exist at the point of divergence and we can stop here.
  23. if [[ $? -ne 0 ]]; then
  24. return
  25. fi
  26. current_version=$(node -p "require('./package.json').version")
  27. previous_version=$(node -pe "JSON.parse(process.argv[1]).version" "${package_string}")
  28. if [[ $current_version != $previous_version ]]; then
  29. return
  30. fi
  31. # Only version bump things that have changes
  32. uncomitted_changes=$(git diff-index --name-only HEAD -- .)
  33. comitted_changes=$(git diff --name-only ${hash_at_divergence} HEAD -- .)
  34. if [ -z "$comitted_changes" ] && [ -z "$uncomitted_changes" ]; then
  35. return
  36. fi
  37. ((version_bump_count+=1))
  38. echo "Version bumping $1"
  39. npm version patch --no-git-tag-version
  40. if [[ $1 != 'ROOT' ]]; then
  41. apply-version $1
  42. fi
  43. echo
  44. }
  45. # copy the version from package.json (the source of truth) to other standard locations (including style.css, style.scss and style-child-theme.scss).
  46. apply-version() {
  47. current_version=$(node -p "require('./package.json').version")
  48. files_to_update=( $(find . -name style.css -o -name style.scss -o -name style-child-theme.scss -maxdepth 2) )
  49. for file_to_update in "${files_to_update[@]}"; do
  50. if test -f "$file_to_update"; then
  51. echo "Apply version from package.json to $file_to_update"
  52. perl -pi -e 's/Version: (.*)$/"Version: '$current_version'"/ge' $file_to_update
  53. fi
  54. done
  55. }
  56. # only build the project if is has changed since it diverged from trunk
  57. build-if-changed() {
  58. # Only build things that have changes
  59. uncomitted_changes=$(git diff-index --name-only HEAD -- .)
  60. comitted_changes=$(git diff --name-only ${hash_at_divergence} HEAD -- .)
  61. if [ -z "$comitted_changes" ] && [ -z "$uncomitted_changes" ]; then
  62. return
  63. fi
  64. npm run build
  65. }
  66. build-org-zip-if-changed() {
  67. # Only build things that have changes
  68. uncomitted_changes=$(git diff-index --name-only HEAD -- .)
  69. comitted_changes=$(git diff --name-only ${hash_at_divergence} HEAD -- .)
  70. if [ -z "$comitted_changes" ] && [ -z "$uncomitted_changes" ]; then
  71. return
  72. fi
  73. build-org-zip $1
  74. }
  75. build-org-zip() {
  76. THEME=${1//\/}
  77. echo "Building .zip file for $THEME"
  78. current_version=$(node -p "require('./package.json').version")
  79. # Copy the theme into a subfolder (excluding the excludables) to be packaged up in a zip
  80. mkdir $THEME;
  81. rsync -avzq --include='theme.json' --exclude $THEME --exclude-from $IGNORE_FILE ./ $THEME
  82. # Make sure the -wpcom version naming and tags aren't shipped
  83. #NOTE: (can we be rid of that -wpcom 'versioning')
  84. find ./$THEME/style.css -type f -exec sed -i '' 's/-wpcom//g' {} \;
  85. find ./$THEME/style.css -type f -exec sed -i '' 's/, auto-loading-homepage//g' {} \;
  86. find ./$THEME/style.css -type f -exec sed -i '' 's/, jetpack-global-styles//g' {} \;
  87. mkdir ../build/"$THEME"_"$current_version"/
  88. zip -q -r -X ../build/"$THEME"_"$current_version"/$THEME.zip $THEME
  89. rm -rf $THEME
  90. echo
  91. }
  92. build-blockbase-child() {
  93. THEME=${1//\/}
  94. if grep -q "Template: blockbase" ./style.css; then
  95. echo "building" $THEME;
  96. npm run build
  97. fi
  98. }
  99. command=$1
  100. echo
  101. # Do these things for a command, but before running the command on each theme
  102. case $command in
  103. "build-org-zip-all")
  104. ;&
  105. "build-org-zip-if-changed")
  106. # Get the /build folder ready to recieve zips
  107. if [ ! -d "./build" ]; then
  108. mkdir build
  109. fi
  110. rm -rf ./build/*
  111. ;;
  112. esac
  113. # Do things for all of the themes
  114. for theme in */ ; do
  115. if test -f "./${theme}/package.json"; then
  116. cd './'${theme}
  117. case $command in
  118. "install-dependencies")
  119. echo 'Installing Dependencies for '${theme}
  120. npm install
  121. echo
  122. ;;
  123. "audit-dependencies")
  124. echo 'Auditing and fixing dependencies for '${theme}
  125. npm audit fix
  126. echo
  127. ;;
  128. "update-dependencies")
  129. echo 'Updating Dependencies for '${theme}
  130. npx npm-check -y --skip-unused
  131. echo
  132. ;;
  133. "build")
  134. build-if-changed ${theme}
  135. ;;
  136. "build-all")
  137. echo 'Building '${theme}
  138. npm run build
  139. echo
  140. ;;
  141. "build-org-zip-if-changed")
  142. build-org-zip-if-changed ${theme}
  143. ;;
  144. "build-org-zip-all")
  145. build-org-zip ${theme}
  146. ;;
  147. "build-blockbase-children")
  148. build-blockbase-child ${theme}
  149. ;;
  150. "version-bump")
  151. version-bump ${theme}
  152. ;;
  153. "apply-version")
  154. echo 'Applying version from package.json throughout '${theme}
  155. apply-version ${theme}
  156. echo
  157. ;;
  158. esac
  159. cd ..
  160. else
  161. # echo 'Skipping '${theme}
  162. fi
  163. done
  164. # Do things for the root folder
  165. case $command in
  166. "audit-dependencies")
  167. echo 'Auditing and fixing dependencies for root project'
  168. npm audit fix
  169. ;;
  170. "version-bump")
  171. version-bump "ROOT"
  172. ;;
  173. esac