theme-batch-utils.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. command=$1
  91. echo
  92. # Do these things for a command, but before running the command on each theme
  93. case $command in
  94. "build-org-zip-all")
  95. ;&
  96. "build-org-zip-if-changed")
  97. # Get the /build folder ready to recieve zips
  98. if [ ! -d "./build" ]; then
  99. mkdir build
  100. fi
  101. rm -rf ./build/*
  102. ;;
  103. esac
  104. # Do things for all of the themes
  105. for theme in */ ; do
  106. if test -f "./${theme}/package.json"; then
  107. cd './'${theme}
  108. case $command in
  109. "install-dependencies")
  110. echo 'Installing Dependencies for '${theme}
  111. npm install
  112. echo
  113. ;;
  114. "audit-dependencies")
  115. echo 'Auditing and fixing dependencies for '${theme}
  116. npm audit fix
  117. echo
  118. ;;
  119. "update-dependencies")
  120. echo 'Updating Dependencies for '${theme}
  121. npx npm-check -y --skip-unused
  122. echo
  123. ;;
  124. "build")
  125. build-if-changed ${theme}
  126. ;;
  127. "build-all")
  128. echo 'Building '${theme}
  129. npm run build
  130. echo
  131. ;;
  132. "build-org-zip-if-changed")
  133. build-org-zip-if-changed ${theme}
  134. ;;
  135. "build-org-zip-all")
  136. build-org-zip ${theme}
  137. ;;
  138. "version-bump")
  139. version-bump ${theme}
  140. ;;
  141. "apply-version")
  142. echo 'Applying version from package.json throughout '${theme}
  143. apply-version ${theme}
  144. echo
  145. ;;
  146. esac
  147. cd ..
  148. else
  149. # echo 'Skipping '${theme}
  150. fi
  151. done
  152. # Do things for the root folder
  153. case $command in
  154. "audit-dependencies")
  155. echo 'Auditing and fixing dependencies for root project'
  156. npm audit fix
  157. ;;
  158. "version-bump")
  159. version-bump "ROOT"
  160. ;;
  161. esac