theme-batch-utils.sh 5.3 KB

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