From 081cae48851934f737a6de89cec46b7d58154b34 Mon Sep 17 00:00:00 2001 From: Niall Byrne <9848926+niall-byrne@users.noreply.github.com> Date: Mon, 27 Feb 2023 14:24:30 -0500 Subject: [PATCH] build(COOKIECUTTER): improvements to update script --- hooks/post_gen_project.sh | 19 +++++++++------- scripts/update.sh | 48 ++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/hooks/post_gen_project.sh b/hooks/post_gen_project.sh index 615ce1ac..bf7f9cb0 100644 --- a/hooks/post_gen_project.sh +++ b/hooks/post_gen_project.sh @@ -5,6 +5,7 @@ # ANSIBLE_WORKBENCH_BRANCH_NAME_BASE: Optional alternate base branch name. # ANSIBLE_WORKBENCH_BRANCH_NAME_DEVELOPMENT: Optional alternate development branch name. +# ANSIBLE_WORKBENCH_SKIP_GIT_INIT: Optionally set to 1 to skip creating branches and initial commit. # ANSIBLE_WORKBENCH_SKIP_POETRY: Optionally set to 1 to skip installing dependencies. # ANSIBLE_WORKBENCH_SKIP_PRECOMMIT: Optionally set to 1 to skip installing pre-commit hooks. @@ -18,14 +19,16 @@ ANSIBLE_WORKBENCH_TEMPLATE_URL="https://github.com/niall-byrne/ansible-workbench initialize_git() { - git init - git checkout -b "${ANSIBLE_WORKBENCH_BRANCH_NAME_BASE}" - git stage . - git commit -m "build(COOKIECUTTER): initial generation" - git symbolic-ref HEAD "refs/heads/${ANSIBLE_WORKBENCH_BRANCH_NAME_BASE}" - git tag 0.0.0 - git checkout -b "${ANSIBLE_WORKBENCH_BRANCH_NAME_DEVELOPMENT}" - mkdir -p files templates + if [[ "${ANSIBLE_WORKBENCH_SKIP_GIT_INIT}" != "1" ]]; then + git init + git checkout -b "${ANSIBLE_WORKBENCH_BRANCH_NAME_BASE}" + git stage . + git commit -m "build(COOKIECUTTER): initial generation" + git symbolic-ref HEAD "refs/heads/${ANSIBLE_WORKBENCH_BRANCH_NAME_BASE}" + git tag 0.0.0 + git checkout -b "${ANSIBLE_WORKBENCH_BRANCH_NAME_DEVELOPMENT}" + mkdir -p files templates + fi } diff --git a/scripts/update.sh b/scripts/update.sh index 226e03a6..685018cf 100755 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -1,22 +1,31 @@ #!/bin/bash # This script helps automate the process of updating a role that has already been created. -# A branch "update-template" is created, with the changes required to update the workflow. +# A branch "update-template" is created, with the changes required to update the role. # Use git cherry-pick (or create a patch from this change set) to update your ansible role. +# Please be advised this is an partially automated solution, and still requires careful manual effort. + +# Before getting started carefully look at your role's .cookiecutter/cookiecutter.json file. +# Compare it to the newest version of the template, and update any GitHub workflow action versions and add any +# new values that have since been included. + # Requires: https://pypi.org/project/cookiecutter-project-upgrader/ -# 1: The path to the role folder you'll be upgrading. -# 2: The tag or branch of this repository you'll be using to perform the upgrade. +# 1: The local filepath to the role you'll be upgrading. +# 2: The tag or branch of this repository you'll be using to perform the upgrade. +# ANSIBLE_WORKBENCH_TEMPLATE_SOURCE Optionally repoint to forked remote repository, or a local zip bundle for the template. +# ANSIBLE_WORKBENCH_UPDATE_BRANCH Optionally set a a branch name that will be created to house the proposed changes. -# Development only script. +# Experimental end-user script. set -eo pipefail -ANSIBLE_WORKBENCH_TEMPLATE_URL="https://github.com/niall-byrne/ansible-workbench.git" +ANSIBLE_WORKBENCH_TEMPLATE_SOURCE="${ANSIBLE_WORKBENCH_TEMPLATE_SOURCE-"https://github.com/niall-byrne/ansible-workbench.git"}" +ANSIBLE_WORKBENCH_UPDATE_BRANCH="${ANSIBLE_WORKBENCH_UPDATE_BRANCH-"update-template"}" error() { - echo "USAGE: ./update.sh [ROLE FOLDER] [TEMPLATE TAG or BRANCH]" + echo "USAGE: ./update.sh [ROLE FOLDER] [TEMPLATE TAG or BRANCH] [--force]" exit 127 } @@ -26,11 +35,26 @@ error() { main() { pushd "$1" || error + if [[ "$3" == "--force" ]]; then + if git branch | grep "${ANSIBLE_WORKBENCH_UPDATE_BRANCH}"; then + set +eo pipefail + git checkout master + git reset origin/master + git clean -fd . + git branch -D "${ANSIBLE_WORKBENCH_UPDATE_BRANCH}" + rm -rf .git/cookiecutter + set -eo pipefail + fi + fi + + ANSIBLE_WORKBENCH_SKIP_GIT_INIT=1 \ + ANSIBLE_WORKBENCH_SKIP_POETRY=1 \ + ANSIBLE_WORKBENCH_SKIP_PRECOMMIT=1 \ cookiecutter_project_upgrader \ -c .cookiecutter/cookiecutter.json \ - -b "update-template" \ + -b "${ANSIBLE_WORKBENCH_UPDATE_BRANCH}" \ -u "$2" \ - -f "${ANSIBLE_WORKBENCH_TEMPLATE_URL}" \ + -f "${ANSIBLE_WORKBENCH_TEMPLATE_SOURCE}" \ -e "defaults" \ -e "handlers" \ -e "meta" \ @@ -45,6 +69,14 @@ main() { -e "README.md" git checkout update-template + + echo -e "\n===========" + echo -e "\nThe following files differ from the template's newest version:" + git diff HEAD~1 --summary + echo -e "\nPlease review these changes carefully, and exit from this shell when finished. Nothing has been pushed or merged yet." + + bash + popd || true }