theme-batch-utils.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 version bump 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. command=$1
  65. echo
  66. # Do things for all of the themes
  67. for theme in */ ; do
  68. if test -f "./${theme}/package.json"; then
  69. cd './'${theme}
  70. case $command in
  71. "install-dependencies")
  72. echo 'Installing Dependencies for '${theme}
  73. npm install
  74. echo
  75. ;;
  76. "audit-dependencies")
  77. echo 'Auditing and fixing dependencies for '${theme}
  78. npm audit fix
  79. echo
  80. ;;
  81. "build")
  82. build-if-changed ${theme}
  83. ;;
  84. "build-all")
  85. echo 'Building '${theme}
  86. npm run build
  87. echo
  88. ;;
  89. "version-bump")
  90. version-bump ${theme}
  91. ;;
  92. "apply-version")
  93. echo 'Applying version from package.json throughout '${theme}
  94. apply-version ${theme}
  95. echo
  96. ;;
  97. esac
  98. cd ..
  99. else
  100. # echo 'Skipping '${theme}
  101. fi
  102. done
  103. # Do things for the root folder
  104. case $command in
  105. "audit-dependencies")
  106. echo 'Auditing and fixing dependencies for root project'
  107. npm audit fix
  108. ;;
  109. "version-bump")
  110. version-bump "ROOT"
  111. ;;
  112. esac