Skip to content

Commit

Permalink
adding credirects v1
Browse files Browse the repository at this point in the history
  • Loading branch information
internetisaiah committed Nov 1, 2024
1 parent 66e3eee commit b18c690
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 25 deletions.
55 changes: 38 additions & 17 deletions bdocs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

set -e

# The project's root directory.
# The project's root directory and redirect file.
export PROJECT_ROOT="$(dirname "$(realpath "$0")")"
export REDIRECT_FILE="./assets/js/broken_redirect_list.js"

# All scripts exported so they can source bdocs and call each other if needed.
export DEPLOY="$PROJECT_ROOT/scripts/create_deploy_text.sh"
export RELEASE="$PROJECT_ROOT/scripts/create_release_text.sh"
export TLINKS="$PROJECT_ROOT/scripts/transform_reference_links.py"
export RLINKS="$PROJECT_ROOT/scripts/remove_unused_reference_links.rb"
export REDIRECTS="$PROJECT_ROOT/scripts/list_redirect_urls.sh"
export CREDIRECTS="$PROJECT_ROOT/scripts/create_redirects.sh"
export LREDIRECTS="$PROJECT_ROOT/scripts/list_redirect_urls.sh"

# Displays usage for bdocs
display_help() {
Expand All @@ -25,56 +27,75 @@ USAGE:
./bdocs [option]
OPTIONS:
deploy Create the deploy body text for weekly deployments
release Create the release body text for monthly releases
tlinks Transform reference links to inline links on 1 or more pages
rlinks Remove reference links that are not being used on 1 or more pages
redirects List the old URLs for all new redirects in this branch
help Display this help message and exit
deploy Create the deploy body text for weekly deployments
release Create the release body text for monthly releases
tlinks Transform reference links to inline links on 1 or more pages
rlinks Remove unused reference links on 1 or more pages
credirects Create redirects for all changed files in _docs
lredirects List the old URLs for all new redirects in this branch
help Display this help message and exit
EOF
}

# If a file or directory is required, check if it was passed and block if not.
# 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/master ^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
}

# Check if no arguments were provided
if [[ $# -eq 0 ]]; then
display_help
exit 1
fi

# Fetch the latest changes from the remote quietly.
git fetch origin develop --quiet

# Argument parsing
case $1 in
deploy)
require_new_merges
if [[ $# -eq 3 ]]; then
"$DEPLOY" "$2" "$3"
else
"$DEPLOY"
fi
;;
release)
require_new_merges
"$RELEASE"
;;
tlinks)
require_path_or_file "$2" && "$TLINKS" "$2"

# Run rlinks next to clean up unused reference links.
"$RLINKS" "$2"
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"
require_path_or_file "$2"
"$RLINKS" "$2"
;;
credirects)
"$CREDIRECTS"
;;
redirects)
lredirects)
if [[ $# -eq 2 ]]; then
"$REDIRECTS" "$2"
"$LREDIRECTS" "$2"
else
"$REDIRECTS"
"$LREDIRECTS"
fi
;;
help)
Expand Down
47 changes: 47 additions & 0 deletions scripts/create_redirects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
#
# DESCRIPTION
#
# Usage: ./bdocs redirects -c

# Get the list of renamed files in `_docs` directory from `develop` branch
CHANGED_FILES=$(git diff --name-status origin/develop -- "$PROJECT_ROOT/_docs" | awk '$1 == "R" {print $2 " " $3}')

# Check if there are any renamed files
require_changed_files() {
if [ -z "$CHANGED_FILES" ]; then
echo "Error: No files or directories changed in the '_docs' directory."
exit 1
fi
}

# Function to format paths to remove underscores and `.md` extension
format_path() {
echo "$1" | sed -E 's|/_|/|g' | sed -E 's|\.md$||g'
}

main() {
# Create redirects
redirects=""
while IFS= read -r line; do
old_path=$(echo "$line" | awk '{print $1}')
new_path=$(echo "$line" | awk '{print $2}')

# Format the paths
formatted_old_path=$(format_path "$old_path")
formatted_new_path=$(format_path "$new_path")

# Create the redirect entry
redirect="validurls['$formatted_old_path'] = '$formatted_new_path';"
redirects+="$redirect"$'\n'
done <<< "$CHANGED_FILES"

# Write redirects to the redirect file
echo "Appending redirects to $REDIRECT_FILE..."
echo "$redirects" >> "$REDIRECT_FILE"

echo "Redirects added successfully!"
}

require_changed_files
main
2 changes: 0 additions & 2 deletions scripts/create_release_text.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#
# Usage: ./bdocs release

source "$PROJECT_ROOT/bdocs"

main() {
# Run script from the root of the git repository.
cd "$PROJECT_ROOT"
Expand Down
5 changes: 1 addition & 4 deletions scripts/list_redirect_urls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
# base URL to list all old URLs so the user can open old links directly from
# the terminal to test redirects.
#
# Usage: ./bdocs redirects

# Fetch the latest changes from the remote.
git fetch origin develop --quiet
# Usage: ./bdocs redirects -l

# Check new redirects by comparing the current branch to develop.
NEW_REDIRECTS=$(git diff develop -- $PROJECT_ROOT/assets/js/broken_redirect_list.js)
Expand Down
2 changes: 1 addition & 1 deletion scripts/remove_unused_reference_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Removes unused reference-style links from the bottom of a file, such as:
# [1]: {{site.baseurl}}/contributing/your_first_contribution/
#
# Usage: ./bdocs rlinks [FILE|DIRECTORY]
# Usage: ./bdocs links -r [FILE|DIRECTORY]
#
# Options:
# FILE Delete unused reference links in a single file.
Expand Down
2 changes: 1 addition & 1 deletion scripts/transform_reference_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# For more information, see:
# https://www.braze.com/docs/contributing/content_management/cross_referencing/
#
# Usage: ./bdocs tlinks [FILE|DIRECTORY]
# Usage: ./bdocs links -t [FILE|DIRECTORY]
#
# Options:
# FILE Transform reference links in a single file.
Expand Down

0 comments on commit b18c690

Please sign in to comment.