From 0fde4cd137f16728885a82452b92c05eb34e9016 Mon Sep 17 00:00:00 2001 From: Tanner Doshier Date: Wed, 29 Jan 2025 13:48:17 -0500 Subject: [PATCH] Add scripts for generating migration checkpoints in `template-infra` --- bin/templatize-legacy-infra | 99 +++++++++++++++++++++++++++++ bin/templatize-legacy-infra-version | 34 ++++++++++ 2 files changed, 133 insertions(+) create mode 100755 bin/templatize-legacy-infra create mode 100755 bin/templatize-legacy-infra-version diff --git a/bin/templatize-legacy-infra b/bin/templatize-legacy-infra new file mode 100755 index 0000000..6d5a1d2 --- /dev/null +++ b/bin/templatize-legacy-infra @@ -0,0 +1,99 @@ +#!/usr/bin/env bash +# Convert a template-infra worktree into one that minimally supports the +# Platform CLI tooling. +# +# Run from the root of a template-infra worktree. + +set -eu + +rename_file() { + local original_file=$1 + local renamed_file=$2 + + mkdir -p "${renamed_file%/*}" + git mv "$original_file" "${renamed_file}" +} + +rename_app_file() { + local original_file=$1 + local renamed_file="${original_file/app/{{app_name\}\}}" + + rename_file "${original_file}" "${renamed_file}" +} + +if [[ -d .template-infra/ ]]; then + echo "Already templatized" + exit +fi + +# get rid of example app junk +find app -not -name Makefile -delete &> /dev/null || : +git add -u + +# rename "app" files +for f in $(git ls-files '*app*' | grep -v '^docs/' | grep -v 'template-only' | grep '\bapp\b'); do + rename_app_file "${f}" +done + +# rather than copy over the upstream copier file: +# +# curl -O https://raw.githubusercontent.com/navapbc/template-infra/refs/heads/main/copier.yml +# +# we'll use a simplified version, since the only variable we are using is +# `app_name` +cat < copier.yml +template: + type: str + choices: + - base + - app + +# +# App vars +# +app_name: + type: str + help: The name of the app + validator: >- + {% if not (app_name | regex_search('^[a-z0-9\-_]+$')) %} + The app name can not be empty and should only contain lower case letters, digits, dashes, and underscores. + {% endif %} + when: &app + "{{ template == 'app' }}" + +_envops: + trim_blocks: true + lstrip_blocks: true + +_skip_if_exists: + - "/{{ app_name }}/" + - "/{{ app_name }}/Makefile" + +_exclude: + - /.git + - /copier.yml + - /CODEOWNERS + - /CONTRIBUTING.md + - /LICENSE.md + - /README.md +EOF + +# create answers file +mkdir .template-infra +echo -e "# Changes here will be overwritten by Copier\n{{ _copier_answers|to_nice_yaml -}}" > '.template-infra/{{_copier_conf.answers_file}}.jinja' + +# fixup CI stuff, for file/path triggers +for f in $(rg -l -e 'app/' -e '-app-' .github/ | grep -v 'template-only' | grep -v 'README.md'); do + renamed_file=$f.jinja + git mv "${f}" "${renamed_file}" + sed -ri 's|app/|{{ app_name }}/|g' "${renamed_file}" + # sed -ri 's|-app-|-{{ app_name }}-|g' "${renamed_file}" + sed -ri "s/-app(\b.*yml)/-{{ app_name}}\1/g" "${renamed_file}" + sed -ri "s/app_name: app/app_name: {{ app_name}}/g" "${renamed_file}" + sed -ri 's/app_name: "app"/app_name: {{ app_name}}/g' "${renamed_file}" + # sed -ri 's/\$\{\{(.*)\}\}/\$\{\{"\{\{"\}\}\1\{\{"\}\}"\}\}/g' "${renamed_file}" + perl -pi -e 's|\$\{\{(.*?)\}\}|\$\{\{"\{\{"\}\}\1\{\{"\}\}"\}\}|g' "${renamed_file}" + sed -ri "s/\bApp\b/{{ app_name}}/g" "${renamed_file}" +done + +git add copier.yml .template-infra/ diff --git a/bin/templatize-legacy-infra-version b/bin/templatize-legacy-infra-version new file mode 100755 index 0000000..a3d5732 --- /dev/null +++ b/bin/templatize-legacy-infra-version @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# Run from the root of a template-infra repo + +set -eux + +VERSION=$1 + +# add any executables next to this script to the PATH for easy calling, like +# templatize-legacy-infra +SCRIPT_PATH=$(dirname "$(realpath -s "$0")") +PATH=${SCRIPT_PATH}:${PATH} +export PATH + +worktree_path=$(realpath "../template-infra-${VERSION}") + +git worktree add -d "${worktree_path}" "${VERSION}" + +pushd "${worktree_path}" + +templatize-legacy-infra + +msg="${VERSION} Platform CLI Migration Checkpoint" +git commit --all -m "${msg}" +git tag -a "platform-cli-migration/${VERSION}" -m "${msg}" + +# could push up the tag: +# +# git push origin "platform-cli-migration/${VERSION}" +# +# but will do that manually + +popd + +git worktree remove "${worktree_path}"