forked from internetisaiah/braze-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdocs
More file actions
executable file
·299 lines (270 loc) · 9.46 KB
/
Copy pathbdocs
File metadata and controls
executable file
·299 lines (270 loc) · 9.46 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
# This is a wrapper script for interacting with the files in './scripts/'.
#
# Usage: ./bdocs [option]
set -e
# The project's root directory, primary branch, and redirect file.
export PROJECT_ROOT="$(dirname "$(realpath "$0")")"
export PRIMARY_BRANCH="main"
export REDIRECT_FILE="$PROJECT_ROOT/assets/js/broken_redirect_list.js"
export REDIRECT_MATCHES="$PROJECT_ROOT/scripts/temp/redirect_matches.json"
# DEPRECATED: both repositories now use 'main' as the base branch.
# Set the primary branch depending which Braze Docs repository.
#if [[ "$PROJECT_ROOT" == *"braze-docs-hidden"* ]]; then
# export PRIMARY_BRANCH="main"
#else
# export PRIMARY_BRANCH="master"
#fi
# All scripts exported so they can source bdocs and call each other if needed.
export DEPLOY="$PROJECT_ROOT/scripts/create_deploy_text.sh"
export GEN_RELEASE_DEPLOY="$PROJECT_ROOT/scripts/generate_releases_deploy.py"
export TLINKS="$PROJECT_ROOT/scripts/transform_reference_links.py"
export RLINKS="$PROJECT_ROOT/scripts/remove_unused_reference_links.rb"
export ULINKS="$PROJECT_ROOT/scripts/update_old_links.py"
export MREDIRECTS="$PROJECT_ROOT/scripts/make_redirects.py"
export LREDIRECTS="$PROJECT_ROOT/scripts/list_new_redirect_urls.sh"
export CLANGS="$PROJECT_ROOT/scripts/clean_orphaned_translations.py"
export RAKE="$PROJECT_ROOT/scripts/print_rake_params.sh"
export SYNTAX="$PROJECT_ROOT/scripts/print_unique_syntax.sh"
# Utility scripts that are not directly used in bdocs:
export MRD="$PROJECT_ROOT/scripts/utils/merge_redirect_descendants.py"
export CHECK_REDIRECTS="$PROJECT_ROOT/scripts/validate_doc_redirects.rb"
export TEMP_DIR="$PROJECT_ROOT/scripts/temp"
# Displays usage for bdocs
display_help() {
cat << EOF
bdocs is a CLI tool for executing Braze Docs scripts.
USAGE:
./bdocs [option]
OPTIONS:
deploy Create the deploy body text for weekly deployments
release Generate deploy-PR list from last v.* tag through today (gh + git); optional args
tlinks Transform reference links to inline links on 1 or more pages
rlinks Remove unused reference links on 1 or more pages
ulinks Update old links using newest redirect on 1 or more pages
mredirects Make redirects for all renamed files in this branch
check_redirects Validate redirects vs Jekyll URLs (develop...HEAD) for doc moves
lredirects Test new redirects by listing old URLs in this branch
fblinks Finds broken links throughout the docs site
clangs Clean language directories by deleting orphaned files
rake Print all supported parameters for 'rake'
syntax Print all unique Markdown syntax supported by Braze Docs
help Display this help message and exit
EOF
}
# If no './scripts/temp' directory, create one.
if [ ! -d "$TEMP_DIR" ]; then
mkdir "$TEMP_DIR"
fi
# If a file or directory is required, pass or fail.
require_path_or_file() {
if [[ -z "$1" ]]; then
echo "Error: A file or directory path is required."
exit 1
fi
}
# If new merges into 'develop' are required, pass or fail.
require_new_merges() {
LATEST_COMMIT_HASH=$(git log --max-count=1 --format="%H" origin/$PRIMARY_BRANCH ^origin/develop)
COMMIT_LOGS=$(git log --first-parent "$LATEST_COMMIT_HASH"..origin/develop)
if [ -z "$COMMIT_LOGS" ]; then
echo "Error: No new merges into 'develop' since the last deployment."
exit 1
fi
}
# If the GitHub CLI is required, pass or fail.
require_gh() {
if ! command -v gh &> /dev/null; then
echo "Error: 'gh' (GitHub CLI) is required to run this command. On macOS, run:"
echo " brew install gh"
echo " gh auth login"
exit 1
fi
}
# If yarn-related project dependencies are required, pass or fail.
require_yarn() {
if [ ! -d "$PROJECT_ROOT/node_modules" ]; then
echo "Error: 'node_modules' are required to run this command. On MacOS, run:"
echo " brew install yarn"
echo " yarn install"
exit 1
fi
}
# Stop commands that are unsupported in the hidden repository.
# Add more commands to this list as needed.
readonly UNSUPPORTED_HIDDEN_COMMANDS="release,ulinks,mredirects,fblinks,lredirects"
not_supported_for_hidden() {
local cmd="$1"
local repo_url
repo_url="$(git config --get remote.origin.url)"
if [[ "$repo_url" == *"braze-inc/braze-docs-hidden"* ]]; then
IFS=',' read -ra blocked_cmds <<< "$UNSUPPORTED_HIDDEN_COMMANDS"
for blocked in "${blocked_cmds[@]}"; do
if [[ "$cmd" == "$blocked" ]]; then
echo "error: '$cmd' is not supported in the braze-docs-hidden repository."
exit 1
fi
done
fi
}
# Fetch the latest changes from the remote quietly.
require_git_fetch() {
git fetch origin develop --quiet
}
# Check if no arguments were provided
if [[ $# -eq 0 ]]; then
display_help
exit 1
fi
# Indicates that the current process is still running and not hung.
logger() {
# Create a log file to capture command I/O.
local output_file="$TEMP_DIR/logger_output.log"
"$@" > "$output_file" 2>&1 &
local pid=$!
# A "loading spinner" to visually indicate process is running.
local spin='-\|/'
local i=0
while kill -0 "$pid" 2>/dev/null; do
i=$(( (i+1) % 4 ))
printf "\r[%c] Running..." "${spin:$i:1}"
sleep 0.1
done
wait $pid
local exit_code=$?
if [ $exit_code -eq 0 ]; then
printf "\r[✓] Complete. \n"
else
printf "\r[✗] Failed. \n"
fi
# Return any command-level logs if applicable
if [ -s "$output_file" ]; then
printf "\n"
cat "$output_file"
fi
return $exit_code
}
# Always check if the command is unsupported in hidden repo
not_supported_for_hidden "$1"
# Argument parsing
case $1 in
deploy)
require_git_fetch
require_new_merges
if [[ $# -eq 3 ]]; then
"$DEPLOY" "$2" "$3"
else
"$DEPLOY"
fi
;;
release)
require_gh
# Latest v.* tag (same anchor as auto date window in generate_releases_deploy.py)
git fetch origin main --tags --quiet 2>/dev/null || git fetch origin --tags --quiet 2>/dev/null || true
case $# in
1)
python3 "$GEN_RELEASE_DEPLOY" \
--git-repo-root "$PROJECT_ROOT" \
--auto-from-last-release-tag
;;
2)
OUT="$2"
if [[ "$OUT" != /* ]]; then
OUT="$PROJECT_ROOT/$OUT"
fi
python3 "$GEN_RELEASE_DEPLOY" \
--git-repo-root "$PROJECT_ROOT" \
--auto-from-last-release-tag \
--output "$OUT"
;;
4|5)
START_DATE="$2"
END_DATE="$3"
MONTH_TITLE="$4"
if [[ $# -eq 5 ]]; then
OUTPUT="$5"
if [[ "$OUTPUT" != /* ]]; then
OUTPUT="$PROJECT_ROOT/$OUTPUT"
fi
else
OUTPUT="$PROJECT_ROOT/scripts/temp/releases_deploy_${START_DATE}_to_${END_DATE}.md"
fi
python3 "$GEN_RELEASE_DEPLOY" \
--git-repo-root "$PROJECT_ROOT" \
--merged-search "${START_DATE}..${END_DATE}" \
--window-start "${START_DATE}T00:00:00Z" \
--window-end "${END_DATE}T23:59:59Z" \
--month-title "$MONTH_TITLE" \
--output "$OUTPUT"
;;
*)
echo "Usage:"
echo " ./bdocs release"
echo " Lists merged 'deploy' PRs from the day after the latest v.* tag through today (UTC)."
echo " Default output: scripts/temp/releases_deploy_<start>_to_<end>.md"
echo ""
echo " ./bdocs release <output.md>"
echo " Same window, write to the given path (relative paths are under the repo root)."
echo ""
echo " ./bdocs release <start-date> <end-date> <month-title> [output.md]"
echo " Explicit YYYY-MM-DD range (optional output path)."
echo ""
echo "Filtering and format: scripts/generate_releases_deploy.py"
exit 1
;;
esac
;;
tlinks)
require_path_or_file "$2"
"$TLINKS" "$2"
"$RLINKS" "$2" # Run rlinks next, to clean up unused reference links.
;;
rlinks)
require_path_or_file "$2"
"$RLINKS" "$2"
;;
ulinks)
require_git_fetch
require_path_or_file "$2"
touch "$REDIRECT_MATCHES"
"$MRD"
logger "$ULINKS" "$2"
# rm "$REDIRECT_MATCHES"
;;
mredirects)
"$MREDIRECTS"
;;
check_redirects)
shift
bundle exec ruby "$CHECK_REDIRECTS" "$@"
;;
lredirects)
require_git_fetch
if [[ $# -eq 2 ]]; then
"$LREDIRECTS" "$2"
else
"$LREDIRECTS"
fi
;;
fblinks)
# require_git_fetch
require_yarn
"$PROJECT_ROOT/node_modules/.bin/ts-node" ./scripts/find_broken_links.ts
;;
clangs)
"$CLANGS"
;;
rake)
"$RAKE"
;;
syntax)
"$SYNTAX"
;;
help)
display_help
;;
*)
echo "Error: Invalid choice: '$1'. To see all options, run: ./bdocs help"
exit 1
;;
esac