فهرست منبع

Try/add batch utils (#4478)

Added batch utilities to operate on all theme projects (that have a package.json file)
Jason Crist 3 سال پیش
والد
کامیت
1d70a2d7bc
2فایلهای تغییر یافته به همراه134 افزوده شده و 1 حذف شده
  1. 7 1
      package.json
  2. 127 0
      theme-batch-utils.sh

+ 7 - 1
package.json

@@ -11,7 +11,13 @@
 		"sandbox:push": "./sandbox.sh push",
 		"sandbox:push:ignore": "./sandbox.sh push --ignore",
 		"sandbox:push:force": "./sandbox.sh push --force",
-		"sandbox:watch": "chokidar '**/*' -i '*/node_modules' -i '.git' -c './sandbox.sh push --ignore' --initial"
+		"sandbox:watch": "chokidar '**/*' -i '*/node_modules' -i '.git' -c './sandbox.sh push --ignore' --initial",
+		"local:clean": "git reset --hard HEAD; git clean -fd",
+		"batch:build:changed": "./theme-batch-utils.sh build",
+		"batch:build:all": "./theme-batch-utils.sh build-all",
+		"batch:install": "./theme-batch-utils.sh install-dependencies",
+		"batch:audit:fix": "./theme-batch-utils.sh audit-dependencies",
+		"batch:version-bump": "./theme-batch-utils.sh version-bump"
 	},
 	"devDependencies": {
 		"@wordpress/prettier-config": "^1.0.1",

+ 127 - 0
theme-batch-utils.sh

@@ -0,0 +1,127 @@
+#!/bin/zsh
+
+git remote update > /dev/null
+current_branch=$(git branch --show-current)
+hash_at_divergence=$(git merge-base origin/trunk ${current_branch})
+version_bump_count=0
+
+# version bump (patch) any theme that has any *comitted* changes since it was branched from /trunk or any *uncomitted* changes
+version-bump() {
+	# Only version bump things that haven't already had their version changed
+	if [[ $1 = 'ROOT' ]]; then
+		# Only version bump ROOT if another project has been version bumped
+		if [[ $version_bump_count = 0 ]]; then
+			echo "No projects have changed.  Nothing version bumped."
+			echo
+			return
+		fi
+		package_string=$(git show ${hash_at_divergence}:package.json)
+	else
+		package_string=$(git show ${hash_at_divergence}:$1package.json 2>/dev/null)
+	fi
+
+	# 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.
+	if [[ $? -ne 0 ]]; then
+		return
+	fi
+
+ 	current_version=$(node -p "require('./package.json').version")
+	previous_version=$(node -pe "JSON.parse(process.argv[1]).version" "${package_string}")
+	if [[ $current_version != $previous_version ]]; then
+		return
+	fi
+
+	# Only version bump things that have changes
+	uncomitted_changes=$(git diff-index --name-only HEAD -- .)
+	comitted_changes=$(git diff --name-only ${hash_at_divergence} HEAD -- .)
+	if [ -z "$comitted_changes" ] && [ -z "$uncomitted_changes" ]; then
+		return
+	fi
+
+	((version_bump_count+=1))
+
+	echo "Version bumping $1"
+	npm version patch --no-git-tag-version
+	if [[ $1 != 'ROOT' ]]; then
+		apply-version $1
+	fi
+	echo
+}
+
+# copy the version from package.json (the source of truth) to other standard locations (including style.css, style.scss and style-child-theme.scss).
+apply-version() {
+
+ 	current_version=$(node -p "require('./package.json').version")
+	files_to_update=( $(find . -name style.css -o -name style.scss -o -name style-child-theme.scss -maxdepth 2) )
+
+	for file_to_update in "${files_to_update[@]}"; do
+		if test -f "$file_to_update"; then
+			echo "Apply version from package.json to $file_to_update"
+	 		perl -pi -e 's/Version: (.*)$/"Version: '$current_version'"/ge' $file_to_update 
+		fi
+	done
+
+}
+
+# only build the project if is has changed since it diverged from trunk
+build-if-changed() {
+	# Only version bump things that have changes
+	uncomitted_changes=$(git diff-index --name-only HEAD -- .)
+	comitted_changes=$(git diff --name-only ${hash_at_divergence} HEAD -- .)
+	if [ -z "$comitted_changes" ] && [ -z "$uncomitted_changes" ]; then
+		return
+	fi
+	npm run build
+}
+
+command=$1
+echo
+
+# Do things for all of the themes
+for theme in */ ; do
+	if test -f "./${theme}/package.json"; then
+		cd './'${theme}
+		case $command in
+			"install-dependencies")
+				echo 'Installing Dependencies for '${theme}
+				npm install
+				echo
+				;;
+			"audit-dependencies")
+				echo 'Auditing and fixing dependencies for '${theme}
+				npm audit fix
+				echo
+				;;
+			"build")
+				build-if-changed ${theme}
+				;;
+			"build-all")
+				echo 'Building '${theme}
+				npm run build
+				echo
+				;;
+			"version-bump")
+				version-bump ${theme}
+				;;
+			"apply-version")
+				echo 'Applying version from package.json throughout '${theme}
+				apply-version ${theme}
+				echo
+				;;
+		esac	
+		cd ..
+	else
+		# echo 'Skipping '${theme}
+	fi
+done
+
+# Do things for the root folder
+case $command in
+	"audit-dependencies")
+		echo 'Auditing and fixing dependencies for root project'
+		npm audit fix
+		;;
+	"version-bump")
+		version-bump "ROOT"
+		;;
+esac