-
Notifications
You must be signed in to change notification settings - Fork 2
Update template-only-bin scripts #29
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
Changes from 21 commits
35d861e
30d702d
ec2fbb2
4b5140f
c0a3918
ad0263f
0c62add
a23e287
4ad779d
370734c
a630c27
a290bb3
301e0f9
b5c052d
10ca65a
acda975
e3c25d7
86d8d87
4176b63
4df01b8
c1ed001
a93f984
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this parameters of this script makes it seem like it supports renaming from any current name to any new name, but i imagine the find/replace logic won't work for some edge cases, like if the current name is something too common. i know we have a goal of switching to copier soon, where effectively "current name" would become something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It currently supports renaming from anything to anything. The regex is restricted to word boundaries, so if you try to rename
🤩
I have a separate script I'm finishing up that effectively "installs an app" for the infra template, allowing the user to provide the app name as an argument. That's in a forthcoming PR.
Per the interest and arguments in navapbc/platform#21, I was sticking with Is the idea behind option b that all the application templates will use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the idea behind option b is that all application templates will use template-only-app and force the project team to decide on an app name (which if they really don't want to think about it they can rename it back to something generic like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to postpone digging into this question until after I've had a chance to make a proof-of-concept of how things might work with copier. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env bash | ||
# ----------------------------------------------------------------------------- | ||
# This script renames the template application in a project. | ||
# Run this script in a project's root directory. | ||
# | ||
# The project name is the name of the folder in your project's root directory. Use | ||
# lowercase letters and hyphens. Do not use spaces. Underscores may have unexpected side | ||
# effects. Choose a unique string that will avoid collisions with commonly used words. | ||
# By default, the application name is `app-rails`. | ||
# | ||
# Positional parameters: | ||
# current_name (required) – the current name for the application | ||
# new_name (required) - the new name for the application | ||
# ----------------------------------------------------------------------------- | ||
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 | ||
|
||
current_name=$1 | ||
new_name=$2 | ||
default_name="app-rails" | ||
|
||
# Debug: | ||
echo "---------------------------------------------------------------------" | ||
echo "current_name: ${current_name}" | ||
echo "new_name: ${new_name}" | ||
echo | ||
|
||
if [[ "${current_name}" == "${new_name}" ]]; then | ||
# Debug: | ||
echo "No rename required: ${current_name} == ${new_name}" | ||
exit 0 | ||
fi | ||
|
||
# Note: Keep this list in sync with the files copied in install-template and update-template | ||
declare -a include_paths | ||
include_paths=(.github/workflows/ci-app-rails.yml) | ||
include_paths+=(.grype.yml) | ||
include_paths+=(app-rails) | ||
include_paths+=(docker-compose.yml) | ||
include_paths+=(docker-compose.mock-production.yml) | ||
include_paths+=(docs/app-rails) | ||
|
||
# Loop through the paths to be included in this template. | ||
for include_path in "${include_paths[@]}"; do | ||
# If the application does not use the default name (i.e. it has already been renamed), | ||
# change the include path to use the correct current_name. | ||
if [[ "${current_name}" != "${default_name}" ]]; then | ||
include_path=$(echo "${include_path}" | sed "s/${default_name}/${current_name}/g") | ||
fi | ||
|
||
echo "Checking '${include_path}' to rename '${current_name}' to '${new_name}'..." | ||
|
||
# Skip if the path does not exist. | ||
if [[ ! -d "${include_path}" ]] && [[ ! -f "${include_path}" ]]; then | ||
echo "Skipping ahead. ${include_path} does not exist in this repo" | ||
continue | ||
fi | ||
lorenyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Construct the correct string substitution that respects word boundaries. | ||
# Hat tip: https://unix.stackexchange.com/a/393968 | ||
if sed --version >/dev/null 2>&1; then | ||
word_boundary_replacement="s/\<${current_name}\>/${new_name}/g" | ||
else | ||
word_boundary_replacement="s/[[:<:]]${current_name}[[:>:]]/${new_name}/g" | ||
fi | ||
|
||
# Replace occurrances of the current_name with the new_name in the path. | ||
# If the path is a file, replace in the file. | ||
# If the path is a directory, recursively replace in all files in the directory. | ||
LC_ALL=C find "${include_path}" -type f -exec bash -c "sedi \"${word_boundary_replacement}\" \"{}\"" \; | ||
|
||
# Rename included paths that contain the current_name. | ||
if [[ "${include_path}" =~ "${current_name}" ]]; then | ||
new_include_path=$(echo "${include_path}" | sed "s/${current_name}/${new_name}/g") | ||
echo "Renaming path from '${include_path}' to '${new_include_path}'..." | ||
mv "${include_path}" "${new_include_path}" | ||
fi | ||
done |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env bash | ||
# ----------------------------------------------------------------------------- | ||
# This script updates an application template in your project. | ||
# This script from 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 name of the application, in either snake- or kebab-case | ||
# Defaults to app-rails. | ||
# ----------------------------------------------------------------------------- | ||
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 | ||
|
||
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}"} | ||
current_version=$(cat ".${template_name}-version") | ||
|
||
git clone "https://github.com/navapbc/${template_name}.git" | ||
|
||
echo "Checking out $target_version..." | ||
cd "${template_name}" | ||
git checkout "$target_version" | ||
cd - &> /dev/null | ||
|
||
# Note: Keep this list in sync with the files copied in install-template | ||
cd "${template_name}" | ||
include_paths=" \ | ||
.github/workflows/ci-${template_short_name}.yml | ||
.grype.yml \ | ||
${template_short_name} \ | ||
docker-compose.yml \ | ||
docker-compose.mock-production.yml \ | ||
docs/${template_short_name}" | ||
git diff $current_version $target_version -- $include_paths > update.patch | ||
cd - &> /dev/null | ||
|
||
if [ "$template_short_name" != "$app_name" ]; then | ||
echo "Modifying patch to use ${app_name} instead of ${template_short_name}..." | ||
# Construct the correct string substitution that respects word boundaries. | ||
# Hat tip: https://unix.stackexchange.com/a/393968 | ||
if sed --version >/dev/null 2>&1; then | ||
word_boundary_replacement="s/\<${template_short_name}\>/${app_name}/g" | ||
else | ||
word_boundary_replacement="s/[[:<:]]${template_short_name}[[:>:]]/${app_name}/g" | ||
fi | ||
sedi "${word_boundary_replacement}" "${template_name}/update.patch" | ||
fi | ||
|
||
echo "Applying patch..." | ||
git apply --allow-empty "${template_name}/update.patch" | ||
|
||
echo "Storing template version in a file..." | ||
cd "${template_name}" | ||
git rev-parse HEAD >../".${template_name}-version" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clarification question: is the template version file being stored in the application folder? or the project repo root? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, in the project root dir. Not the application dir. Same as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying. I think that poses a problem if a repo has two application using the same application template. Should we consider moving the template file into the app folder? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. I agree that we should move the |
||
cd - &> /dev/null | ||
|
||
echo "Cleaning up ${template_name} folder..." | ||
rm -fr "${template_name}" | ||
|
||
echo "...Done." |
Uh oh!
There was an error while loading. Please reload this page.