forked from Automattic/themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-batch-utils.sh
executable file
·179 lines (156 loc) · 4.95 KB
/
theme-batch-utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/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 build 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
}
build-org-zip-if-changed() {
# Only build 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
build-org-zip $1
}
build-org-zip() {
THEME=${1//\/}
echo "Building .zip file for $THEME"
current_version=$(node -p "require('./package.json').version")
# Copy the theme into a subfolder (excluding the excludables) to be packaged up in a zip
mkdir $THEME;
rsync -avzq --include='theme.json' --exclude $THEME --exclude-from '../dotorg-exclude.txt' ./ $THEME
# Make sure the -wpcom version naming and tags aren't shipped
#NOTE: (can we be rid of that -wpcom 'versioning')
find ./$THEME/style.css -type f -exec sed -i '' 's/-wpcom//g' {} \;
find ./$THEME/style.css -type f -exec sed -i '' 's/, auto-loading-homepage//g' {} \;
find ./$THEME/style.css -type f -exec sed -i '' 's/, jetpack-global-styles//g' {} \;
mkdir ../build/"$THEME"_"$current_version"/
zip -q -r -X ../build/"$THEME"_"$current_version"/$THEME.zip $THEME
rm -rf $THEME
echo
}
command=$1
echo
# Do these things for a command, but before running the command on each theme
case $command in
"build-org-zip-all")
;&
"build-org-zip-if-changed")
# Get the /build folder ready to recieve zips
if [ ! -d "./build" ]; then
mkdir build
fi
rm -rf ./build/*
;;
esac
# 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
;;
"build-org-zip-if-changed")
build-org-zip-if-changed ${theme}
;;
"build-org-zip-all")
build-org-zip ${theme}
;;
"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