Skip to content

Commit 1640bb1

Browse files
authored
Update template-only-bin scripts (#29)
1 parent 8cafc7f commit 1640bb1

File tree

6 files changed

+182
-78
lines changed

6 files changed

+182
-78
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ See [`navapbc/platform`](https://github.com/navapbc/platform) for other template
3434

3535
To get started using the template application on your project:
3636

37-
1. Run the [download and install script](./template-only-bin/download-and-install-template.sh) in your project's root directory.
37+
1. Run the [download and install script](./template-only-bin/download-and-install-template) in your project's root directory.
3838

3939
```bash
40-
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template.sh | bash -s
40+
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template | bash -s
4141
```
4242

4343
This script will:
4444

4545
1. Clone the template repository
4646
2. Copy the template files into your project directory
47-
3. Remove any files specific to the template repository, like this README.
47+
3. Ignore any files specific to the template repository, like this README.
4848

4949
You can optionally pass in a branch, commit hash, or release that you want to install. For example:
5050

5151
```bash
52-
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template.sh | bash -s -- <commit_hash>
52+
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template | bash -s -- <commit_hash>
5353
```
5454
2. [Follow the steps in `app-rails/README.md`](./app-rails/README.md) to set up the application locally.
5555
3. Optional, if using the Platform infrastructure template: [Follow the steps in the `template-infra` README](https://github.com/navapbc/template-infra#installation) to set up the various pieces of your infrastructure.
@@ -58,16 +58,16 @@ To get started using the template application on your project:
5858

5959
If you have previously installed this template and would like to update your project to use a newer version of this application:
6060

61-
1. Run the [download and install script](./template-only-bin/download-and-install-template.sh) in your project's root directory and pass in the branch, commit hash, or release that you want to update to, followed by the name of your application directory (e.g. `app-rails`).
61+
1. Run the [update script](./template-only-bin/update-template) in your project's root directory and pass in the branch, commit hash, or release that you want to update to, followed by the name of your application directory (e.g. `app-rails`).
6262
6363
```bash
64-
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template.sh | bash -s -- <commit_hash> <app_name>
64+
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template | bash -s -- <commit_hash> <app_name>
6565
```
6666
6767
This script will:
6868
6969
1. Clone the template repository
7070
2. Copy the template files into your project directory
71-
3. Remove any files specific to the template repository, like this README.
71+
3. Ignore any files specific to the template repository, like this README.
7272
7373
⚠️ Warning! This will modify existing files. Review all changes carefully after executing the script by running `git diff`.

template-only-bin/download-and-install-template.sh renamed to template-only-bin/download-and-install-template

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ template_short_name="app-${template_name##*-}"
2121
target_version=${1:-"main"}
2222
app_name=${2:-"${template_short_name}"}
2323

24-
echo "template_short_name: ${template_short_name}"
25-
echo "app_name: ${app_name}"
26-
echo "target_version: ${target_version}"
27-
2824
git clone "https://github.com/navapbc/${template_name}.git"
2925
cd "${template_name}"
3026

@@ -33,7 +29,7 @@ git checkout "$target_version"
3329
cd - &> /dev/null
3430

3531
echo "Installing ${template_name}..."
36-
"./${template_name}/template-only-bin/install-template.sh" "${template_name}" "${app_name}"
32+
curl "https://raw.githubusercontent.com/navapbc/${template_name}/rocket/update-template-only-bin-scripts/template-only-bin/install-template" | bash -s -- "${template_name}" "${app_name}"
3733

3834
echo "Storing template version in a file..."
3935
cd "${template_name}"
Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env bash
22
# -----------------------------------------------------------------------------
33
# This script installs an application template to your project.
4-
# Run this script using ./download-and-install-template.sh
4+
# Run this script using ./download-and-install-template. Expected to be run
5+
# from the project's root directory.
56
#
67
# Positional parameters:
78
# template_name (required) – the name of the template to install
@@ -16,34 +17,27 @@ template_name=$1
1617
template_short_name="app-${template_name##*-}"
1718
app_name=$2
1819

19-
echo "template_short_name: ${template_short_name}"
20-
echo "app_name: ${app_name}"
21-
2220
curr_dir=$(pwd)
23-
script_dir=$(dirname $0)
24-
template_dir="${script_dir}/.."
2521

26-
cd $template_dir
22+
cd "${template_name}"
2723

28-
if [ "$template_short_name" != "$app_name" ]; then
24+
if [ "${template_short_name}" != "${app_name}" ]; then
2925
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}"
26+
curl "https://raw.githubusercontent.com/navapbc/${template_name}/rocket/update-template-only-bin-scripts/template-only-bin/rename-template-app" | bash -s -- "${template_short_name}" "${app_name}"
3127
fi
3228

33-
echo "Copying files from $template_name..."
34-
# Note: Keep this list of paths in sync with INCLUDE_PATHS in update-template.sh
29+
# Note: Keep this list in sync with the files listed in update-template
30+
# Copy only relevant files that should be included in the project repo.
31+
echo "Copying files from ${template_name}..."
32+
# Copy top level paths.
3533
cp -r \
36-
.github \
37-
.gitignore \
3834
.grype.yml \
3935
"${app_name}" \
4036
docker-compose.yml \
4137
docker-compose.mock-production.yml \
42-
docs \
43-
$curr_dir
44-
cd - >& /dev/null
38+
${curr_dir}
39+
# Copy nested paths. Make any missing directories.
40+
mkdir -p "${curr_dir}/.github/workflows" && cp ".github/workflows/ci-${app_name}.yml" "${curr_dir}/.github/workflows"
41+
mkdir -p "${curr_dir}/docs" && cp -r "docs/${app_name}" "${curr_dir}/docs"
4542

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
43+
cd - >& /dev/null

template-only-bin/rename-template-app

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
# -----------------------------------------------------------------------------
3+
# This script renames the template application in a project.
4+
# Run this script in a project's root directory.
5+
#
6+
# The project name is the name of the folder in your project's root directory. Use
7+
# lowercase letters and hyphens. Do not use spaces. Underscores may have unexpected side
8+
# effects. Choose a unique string that will avoid collisions with commonly used words.
9+
# By default, the application name is `app-rails`.
10+
#
11+
# Positional parameters:
12+
# current_name (required) – the current name for the application
13+
# new_name (required) - the new name for the application
14+
# -----------------------------------------------------------------------------
15+
set -euo pipefail
16+
17+
# Helper to get the correct sed -i behavior for both GNU sed and BSD sed (installed by default on macOS)
18+
# Hat tip: https://stackoverflow.com/a/38595160
19+
sedi () {
20+
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
21+
}
22+
# Export the function so it can be used in the `find -exec` calls later on
23+
export -f sedi
24+
25+
current_name=$1
26+
new_name=$2
27+
default_name="app-rails"
28+
29+
# Debug:
30+
echo "---------------------------------------------------------------------"
31+
echo "current_name: ${current_name}"
32+
echo "new_name: ${new_name}"
33+
echo
34+
35+
if [[ "${current_name}" == "${new_name}" ]]; then
36+
# Debug:
37+
echo "No rename required: ${current_name} == ${new_name}"
38+
exit 0
39+
fi
40+
41+
# Note: Keep this list in sync with the files copied in install-template and update-template
42+
declare -a include_paths
43+
include_paths=(.github/workflows/ci-app-rails.yml)
44+
include_paths+=(.grype.yml)
45+
include_paths+=(app-rails)
46+
include_paths+=(docker-compose.yml)
47+
include_paths+=(docker-compose.mock-production.yml)
48+
include_paths+=(docs/app-rails)
49+
50+
# Loop through the paths to be included in this template.
51+
for include_path in "${include_paths[@]}"; do
52+
# If the application does not use the default name (i.e. it has already been renamed),
53+
# change the include path to use the correct current_name.
54+
if [[ "${current_name}" != "${default_name}" ]]; then
55+
include_path=$(echo "${include_path}" | sed "s/${default_name}/${current_name}/g")
56+
fi
57+
58+
echo "Checking '${include_path}' to rename '${current_name}' to '${new_name}'..."
59+
60+
# Skip if the path does not exist.
61+
if [[ ! -d "${include_path}" ]] && [[ ! -f "${include_path}" ]]; then
62+
echo "Skipping ahead. ${include_path} does not exist in this repo"
63+
continue
64+
fi
65+
66+
# Construct the correct string substitution that respects word boundaries.
67+
# Hat tip: https://unix.stackexchange.com/a/393968
68+
if sed --version >/dev/null 2>&1; then
69+
word_boundary_replacement="s/\<${current_name}\>/${new_name}/g"
70+
else
71+
word_boundary_replacement="s/[[:<:]]${current_name}[[:>:]]/${new_name}/g"
72+
fi
73+
74+
# Replace occurrances of the current_name with the new_name in the path.
75+
# If the path is a file, replace in the file.
76+
# If the path is a directory, recursively replace in all files in the directory.
77+
LC_ALL=C find "${include_path}" -type f -exec bash -c "sedi \"${word_boundary_replacement}\" \"{}\"" \;
78+
79+
# Rename included paths that contain the current_name.
80+
if [[ "${include_path}" =~ "${current_name}" ]]; then
81+
new_include_path=$(echo "${include_path}" | sed "s/${current_name}/${new_name}/g")
82+
echo "Renaming path from '${include_path}' to '${new_include_path}'..."
83+
mv "${include_path}" "${new_include_path}"
84+
fi
85+
done

template-only-bin/rename-template-app.sh

Lines changed: 0 additions & 46 deletions
This file was deleted.

template-only-bin/update-template

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
# -----------------------------------------------------------------------------
3+
# This script updates an application template in your project.
4+
# This script from 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 name of the application, in either snake- or kebab-case
11+
# Defaults to app-rails.
12+
# -----------------------------------------------------------------------------
13+
set -euo pipefail
14+
15+
# Helper to get the correct sed -i behavior for both GNU sed and BSD sed (installed by default on macOS)
16+
# Hat tip: https://stackoverflow.com/a/38595160
17+
sedi () {
18+
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
19+
}
20+
# Export the function so it can be used in the `find -exec` calls later on
21+
export -f sedi
22+
23+
template_name="template-application-rails"
24+
# Use shell parameter expansion to get the last word, where the delimiter between
25+
# words is `-`.
26+
# See https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
27+
template_short_name="app-${template_name##*-}"
28+
29+
target_version=${1:-"main"}
30+
app_name=${2:-"${template_short_name}"}
31+
current_version=$(cat ".${template_name}-version")
32+
33+
git clone "https://github.com/navapbc/${template_name}.git"
34+
35+
echo "Checking out $target_version..."
36+
cd "${template_name}"
37+
git checkout "$target_version"
38+
cd - &> /dev/null
39+
40+
# Note: Keep this list in sync with the files copied in install-template
41+
cd "${template_name}"
42+
include_paths=" \
43+
.github/workflows/ci-${template_short_name}.yml
44+
.grype.yml \
45+
${template_short_name} \
46+
docker-compose.yml \
47+
docker-compose.mock-production.yml \
48+
docs/${template_short_name}"
49+
git diff $current_version $target_version -- $include_paths > update.patch
50+
cd - &> /dev/null
51+
52+
if [ "$template_short_name" != "$app_name" ]; then
53+
echo "Modifying patch to use ${app_name} instead of ${template_short_name}..."
54+
# Construct the correct string substitution that respects word boundaries.
55+
# Hat tip: https://unix.stackexchange.com/a/393968
56+
if sed --version >/dev/null 2>&1; then
57+
word_boundary_replacement="s/\<${template_short_name}\>/${app_name}/g"
58+
else
59+
word_boundary_replacement="s/[[:<:]]${template_short_name}[[:>:]]/${app_name}/g"
60+
fi
61+
sedi "${word_boundary_replacement}" "${template_name}/update.patch"
62+
fi
63+
64+
echo "Applying patch..."
65+
git apply --allow-empty "${template_name}/update.patch"
66+
67+
echo "Storing template version in a file..."
68+
cd "${template_name}"
69+
git rev-parse HEAD >../".${template_name}-version"
70+
cd - &> /dev/null
71+
72+
echo "Cleaning up ${template_name} folder..."
73+
rm -fr "${template_name}"
74+
75+
echo "...Done."

0 commit comments

Comments
 (0)