Skip to content

Add template-only-bin scripts #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
46 changes: 46 additions & 0 deletions template-only-bin/download-and-install-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# This script installs an application template to your project.
# Run this script in your project's root directory.
#
# Positional parameters:
# target_version (optional) – the version of the template application to install.
# Defaults to main. Can be any target that can be checked out, including a branch,
# version tag, or commit hash.
# app_name (optional) – the new name for the application, in either snake- or kebab-case
# Defaults to app-rails.
# -----------------------------------------------------------------------------
set -euo pipefail

template_name="template-application-rails"
# Use shell parameter expansion to get the last word, where the delimiter between
# words is `-`.
# See https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
template_short_name="app-${template_name##*-}"

target_version=${1:-"main"}
app_name=${2:-"${template_short_name}"}

echo "template_short_name: ${template_short_name}"
echo "app_name: ${app_name}"
echo "target_version: ${target_version}"

git clone "https://github.com/navapbc/${template_name}.git"
cd "${template_name}"

echo "Checking out $target_version..."
git checkout "$target_version"
cd - &> /dev/null

echo "Installing ${template_name}..."
"./${template_name}/template-only-bin/install-template.sh" "${template_name}" "${app_name}"

echo "Storing template version in a file..."
cd "${template_name}"
git rev-parse HEAD >../".${template_name}-version"
cd - &> /dev/null

echo "Cleaning up ${template_name} folder..."
rm -fr "${template_name}"

echo "...Done."
49 changes: 49 additions & 0 deletions template-only-bin/install-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# This script installs an application template to your project.
# Run this script using ./download-and-install-template.sh
#
# Positional parameters:
# template_name (required) – the name of the template to install
# app_name (required) – the name of the application
# -----------------------------------------------------------------------------
set -euo pipefail

template_name=$1
# Use shell parameter expansion to get the last word, where the delimiter between
# words is `-`.
# See https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
template_short_name="app-${template_name##*-}"
app_name=$2

echo "template_short_name: ${template_short_name}"
echo "app_name: ${app_name}"

curr_dir=$(pwd)
script_dir=$(dirname $0)
template_dir="${script_dir}/.."

cd $template_dir

if [ "$template_short_name" != "$app_name" ]; then
echo "Modifying template to use ${app_name} instead of ${template_short_name}..."
"./template-only-bin/rename-template-app.sh" "${app_name}" "${template_short_name}"
fi

echo "Copying files from $template_name..."
# Note: Keep this list of paths in sync with INCLUDE_PATHS in update-template.sh
# @TODO: Add .grype.yml
cp -r \
.github \
.gitignore \
"${app_name}" \
docker-compose.yml \
docker-compose.mock-production.yml \
docs \
$curr_dir
cd - >& /dev/null

echo "Removing files relevant only to template development..."
# Note: Keep this list of paths in sync with EXCLUDE_OPT in update-template.sh
rm -rf .github/workflows/template-only-*
rm -rf .github/ISSUE_TEMPLATE
46 changes: 46 additions & 0 deletions template-only-bin/rename-template-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# This script renames the application template in your project.
# Run this script in your project's root directory.
#
# Positional parameters:
# new_name (required) - the new name for the application, in either snake- or kebab-case
# old_name (required) – the old name for the application, in either snake- or kebab-case
# -----------------------------------------------------------------------------
set -euo pipefail

# Helper to get the correct sed -i behavior for both GNU sed and BSD sed (installed by default on macOS)
# Hat tip: https://stackoverflow.com/a/38595160
sedi () {
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
}
# Export the function so it can be used in the `find -exec` calls later on
export -f sedi

new_name=$1
old_name=$2

# Don't make assumptions about whether the arguments are snake- or kebab-case. Handle both.
# Get kebab-case names
old_name_kebab=$(echo $old_name | tr "_" "-")
new_name_kebab=$(echo $new_name | tr "_" "-")

# Get snake-case names
old_name_snake=$(echo $old_name | tr "-" "_")
new_name_snake=$(echo $new_name | tr "-" "_")

# Rename the app directory
if [ -d "$old_name" ]; then
echo "Renaming ${old_name} to ${new_name}..."
mv "${old_name}" "${new_name}"
fi

# Rename all kebab-case instances
echo "Performing a find-and-replace for all instances of (kebab-case) '$old_name_kebab' with '$new_name_kebab'..."
LC_ALL=C find . -type f -not -path "./.git/*" -exec bash -c "sedi \"s/$old_name_kebab/$new_name_kebab/g\" \"{}\"" \;

# Rename all snake-case instances
echo "Performing a find-and-replace for all instances of (snake-case) '$old_name_snake' with '$new_name_snake'..."
LC_ALL=C find . -type f -not -path "./.git/*" -exec bash -c "sedi \"s/$old_name_snake/$new_name_snake/g\" \"{}\"" \;

echo "...Done."
Loading