Skip to content

Commit 0c04e00

Browse files
authored
Add template-only-bin scripts (#6)
1 parent 2a1933a commit 0c04e00

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

docker-compose.mock-production.yml

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
# -----------------------------------------------------------------------------
3+
# This script installs an application template to your project.
4+
# Run this script in your project's root directory.
5+
#
6+
# Positional parameters:
7+
# target_version (optional) – the version of the template application to install.
8+
# Defaults to main. Can be any target that can be checked out, including a branch,
9+
# version tag, or commit hash.
10+
# app_name (optional) – the new name for the application, in either snake- or kebab-case
11+
# Defaults to app-rails.
12+
# -----------------------------------------------------------------------------
13+
set -euo pipefail
14+
15+
template_name="template-application-rails"
16+
# Use shell parameter expansion to get the last word, where the delimiter between
17+
# words is `-`.
18+
# See https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
19+
template_short_name="app-${template_name##*-}"
20+
21+
target_version=${1:-"main"}
22+
app_name=${2:-"${template_short_name}"}
23+
24+
echo "template_short_name: ${template_short_name}"
25+
echo "app_name: ${app_name}"
26+
echo "target_version: ${target_version}"
27+
28+
git clone "https://github.com/navapbc/${template_name}.git"
29+
cd "${template_name}"
30+
31+
echo "Checking out $target_version..."
32+
git checkout "$target_version"
33+
cd - &> /dev/null
34+
35+
echo "Installing ${template_name}..."
36+
"./${template_name}/template-only-bin/install-template.sh" "${template_name}" "${app_name}"
37+
38+
echo "Storing template version in a file..."
39+
cd "${template_name}"
40+
git rev-parse HEAD >../".${template_name}-version"
41+
cd - &> /dev/null
42+
43+
echo "Cleaning up ${template_name} folder..."
44+
rm -fr "${template_name}"
45+
46+
echo "...Done."
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
# -----------------------------------------------------------------------------
3+
# This script installs an application template to your project.
4+
# Run this script using ./download-and-install-template.sh
5+
#
6+
# Positional parameters:
7+
# template_name (required) – the name of the template to install
8+
# app_name (required) – the name of the application
9+
# -----------------------------------------------------------------------------
10+
set -euo pipefail
11+
12+
template_name=$1
13+
# Use shell parameter expansion to get the last word, where the delimiter between
14+
# words is `-`.
15+
# See https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
16+
template_short_name="app-${template_name##*-}"
17+
app_name=$2
18+
19+
echo "template_short_name: ${template_short_name}"
20+
echo "app_name: ${app_name}"
21+
22+
curr_dir=$(pwd)
23+
script_dir=$(dirname $0)
24+
template_dir="${script_dir}/.."
25+
26+
cd $template_dir
27+
28+
if [ "$template_short_name" != "$app_name" ]; then
29+
echo "Modifying template to use ${app_name} instead of ${template_short_name}..."
30+
"./template-only-bin/rename-template-app.sh" "${app_name}" "${template_short_name}"
31+
fi
32+
33+
echo "Copying files from $template_name..."
34+
# Note: Keep this list of paths in sync with INCLUDE_PATHS in update-template.sh
35+
# @TODO: Add .grype.yml
36+
cp -r \
37+
.github \
38+
.gitignore \
39+
"${app_name}" \
40+
docker-compose.yml \
41+
docker-compose.mock-production.yml \
42+
docs \
43+
$curr_dir
44+
cd - >& /dev/null
45+
46+
echo "Removing files relevant only to template development..."
47+
# Note: Keep this list of paths in sync with EXCLUDE_OPT in update-template.sh
48+
rm -rf .github/workflows/template-only-*
49+
rm -rf .github/ISSUE_TEMPLATE
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
# -----------------------------------------------------------------------------
3+
# This script renames the application template in your project.
4+
# Run this script in your project's root directory.
5+
#
6+
# Positional parameters:
7+
# new_name (required) - the new name for the application, in either snake- or kebab-case
8+
# old_name (required) – the old name for the application, in either snake- or kebab-case
9+
# -----------------------------------------------------------------------------
10+
set -euo pipefail
11+
12+
# Helper to get the correct sed -i behavior for both GNU sed and BSD sed (installed by default on macOS)
13+
# Hat tip: https://stackoverflow.com/a/38595160
14+
sedi () {
15+
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
16+
}
17+
# Export the function so it can be used in the `find -exec` calls later on
18+
export -f sedi
19+
20+
new_name=$1
21+
old_name=$2
22+
23+
# Don't make assumptions about whether the arguments are snake- or kebab-case. Handle both.
24+
# Get kebab-case names
25+
old_name_kebab=$(echo $old_name | tr "_" "-")
26+
new_name_kebab=$(echo $new_name | tr "_" "-")
27+
28+
# Get snake-case names
29+
old_name_snake=$(echo $old_name | tr "-" "_")
30+
new_name_snake=$(echo $new_name | tr "-" "_")
31+
32+
# Rename the app directory
33+
if [ -d "$old_name" ]; then
34+
echo "Renaming ${old_name} to ${new_name}..."
35+
mv "${old_name}" "${new_name}"
36+
fi
37+
38+
# Rename all kebab-case instances
39+
echo "Performing a find-and-replace for all instances of (kebab-case) '$old_name_kebab' with '$new_name_kebab'..."
40+
LC_ALL=C find . -type f -not -path "./.git/*" -exec bash -c "sedi \"s/$old_name_kebab/$new_name_kebab/g\" \"{}\"" \;
41+
42+
# Rename all snake-case instances
43+
echo "Performing a find-and-replace for all instances of (snake-case) '$old_name_snake' with '$new_name_snake'..."
44+
LC_ALL=C find . -type f -not -path "./.git/*" -exec bash -c "sedi \"s/$old_name_snake/$new_name_snake/g\" \"{}\"" \;
45+
46+
echo "...Done."

0 commit comments

Comments
 (0)