From 014ee56a82e5acae0e31f955b95692bb98d41279 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Wed, 8 Mar 2023 10:54:28 +0100 Subject: [PATCH] FEATURE: add tool functions for logging to `bump.sh` (#67) * add tool logging functions * replace all the `echo`s by the new tool functions The following `nushell` little script have been used to replace them automatically ```bash let file = "scripts/bump.sh" [ [token tool]; ["\\+" info] [~ warning] [! error] [i ok] [- normal] ["\\?" hint] ] | each {|it| # replace "echo '[.] " or 'echo "[.] ' by the right log function sd $"echo \('|\"\)\\[($it.token)\\] " $"log_($it.tool) $1" $file } ``` One can see that all the `echo`s have been replaced by looking at ```bash > rg "echo .\\[.\\]" scripts/bump.sh ``` before and after the replacement above. * change the colors to make more sense * add ";" to all `log_*` functions This commits helps mitigate the following error ```bash > ./scripts/bump.sh ./scripts/bump.sh: line 159: syntax error: unexpected end of file ``` The `bash` interpreter needs a ";" at the end of the lines inside a oneline `{ ... }` block, just as in the `log_*` functions. Otherwise, the parser does not know where the line ends, throwing the error above. * wrap `$1` inside quotes in the `log_*` functions This commit helps mitigate the following error ```bash > DRY_RUN="yes" ./scripts/bump.sh [i] Current [-] Exactly [?] Example: [?] see ``` i.e. the `log_*` functions only print the first word they are being given... This is because of the `log_any ... $1`, as `log_any` takes the `$3` only and not everything after the fourth argument. Writing `log_any ... "$1"` makes sure the `$3` argument is made of all the message and not the first word only!! With this commit, the output of the same command as above is now ```bash > DRY_RUN="yes" ./scripts/bump.sh [i] Current directory is repo's root OK [-] Exactly one argument is needed, got 0. [?] Example: `./bump.sh 0.2.6` [?] see https://github.com/iScsc/iscsc.fr/wiki/Version-bump-procedure#automatic-version-bump for full documentation ``` * swap "info" and "ok" * remove "normal" and use "error" only, with "-" * swap the colors of "info" and "ok" This change comes from the comment of @ctmbl in #67. --- scripts/bump.sh | 72 ++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/scripts/bump.sh b/scripts/bump.sh index 5e18fb1..6df2d4a 100755 --- a/scripts/bump.sh +++ b/scripts/bump.sh @@ -9,50 +9,62 @@ DEPENDENCIES=( # Variables NB_ARGS="$#" +# ------------------------------- Tool functions ------------------------------- + +log_any () { + echo -e "[\e[$1m$2\e[0m] $3" +} + +log_error () { log_any "31" "-" "$1"; } +log_ok () { log_any "32" "+" "$1"; } +log_warning () { log_any "33" "~" "$1"; } +log_hint () { log_any "34" "?" "$1"; } +log_info () { log_any "35" "i" "$1"; } + # -------------------------------- Basic Checks -------------------------------- # check that current directory is repo root check_pwd () { root="$(dirname $(realpath $(dirname "$0")))" [ "$root" != "$(pwd)" ] && { - echo "[!] the script should be run from the root of the repo" - echo "[-] expected '$root'" - echo "[-] found '$(pwd)'" + log_error "the script should be run from the root of the repo" + log_error "expected '$root'" + log_error "found '$(pwd)'" exit 1 } - [ -n "$DRY_RUN" ] && echo "[i] Current directory is repo's root OK" + [ -n "$DRY_RUN" ] && log_ok "Current directory is repo's root OK" } # Check that 1 arg has been supplied check_arg () { local nb_args="$1" if [ "${nb_args}" -ne "1" ]; then - echo "[-] Exactly one argument is needed, got ${nb_args}." - echo '[?] Example: `./bump.sh 0.2.6`' - echo "[?] see https://github.com/iScsc/iscsc.fr/wiki/Version-bump-procedure#automatic-version-bump for full documentation" + log_error "Exactly one argument is needed, got ${nb_args}." + log_hint 'Example: `./bump.sh 0.2.6`' + log_hint "see https://github.com/iScsc/iscsc.fr/wiki/Version-bump-procedure#automatic-version-bump for full documentation" exit 1 fi - [ -n "$DRY_RUN" ] && echo "[i] Only argument has been given OK" + [ -n "$DRY_RUN" ] && log_ok "Only argument has been given OK" } # Check that required binaries are installed check_dependencies () { for package in ${DEPENDENCIES[@]}; do if [ ! $(which $package) ]; then - echo "[-] All of '${DEPENDENCIES[@]}' are needed to bump version" + log_error "All of '${DEPENDENCIES[@]}' are needed to bump version" exit 1 fi done - [ -n "$DRY_RUN" ] && echo "[i] Dependencies: '${DEPENDENCIES[@]}' are installed on the system OK" + [ -n "$DRY_RUN" ] && log_ok "Dependencies: '${DEPENDENCIES[@]}' are installed on the system OK" } # Check that git working directory is clean... check_clean_git_working_dir () { if [ -n "$(git status --short --untracked-files=no)" ]; then - echo "[-] git working directory isn't clean" + log_error "git working directory isn't clean" exit 1 fi - [ -n "$DRY_RUN" ] && echo "[i] git working directory is clean OK" + [ -n "$DRY_RUN" ] && log_ok "git working directory is clean OK" } # Run all basic checks @@ -75,30 +87,30 @@ ISCSC_REMOTE=$(git remote -v | grep 'git@github.com:iScsc/iscsc.fr.git' | awk '{ check_version_semantics () { local version="$1" if [ "${version}" != "$(semver ${version})" ]; then - echo "[-] ${version} is not a valid version number according to semver" + log_error "${version} is not a valid version number according to semver" exit 1 fi - [ -n "$DRY_RUN" ] && echo "[i] Provided version '${version}' is semantically correct OK" + [ -n "$DRY_RUN" ] && log_ok "Provided version '${version}' is semantically correct OK" } # Check that targeted version is > than current check_version_greater () { - echo "[+] Current version is '${CURRENT_VERSION}'" + log_info "Current version is '${CURRENT_VERSION}'" if [ ! $(semver "${NEW_VERSION}" -r ">${CURRENT_VERSION}") ]; then - echo "[-] '${NEW_VERSION}'<='${CURRENT_VERSION}', '${NEW_VERSION}' isn't accepted as new version." + log_error "'${NEW_VERSION}'<='${CURRENT_VERSION}', '${NEW_VERSION}' isn't accepted as new version." exit 1 fi - [ -n "$DRY_RUN" ] && echo "[i] '${NEW_VERSION}' is greater than older version OK" + [ -n "$DRY_RUN" ] && log_ok "'${NEW_VERSION}' is greater than older version OK" } # Check that iScsc/iscsc.fr is in git remotes list check_iscsc_remote () { local remote="$1" if [ -z "$remote" ]; then - echo "[-] 'iScsc/iscsc.fr' remote is not in remote list" + log_error "'iScsc/iscsc.fr' remote is not in remote list" exit 1 fi - echo "[i] 'iScsc/iscsc.fr' is in remote list as '$remote' OK" + log_ok "'iScsc/iscsc.fr' is in remote list as '$remote' OK" } # Run all advanced checks @@ -106,40 +118,40 @@ check_version_semantics "${NEW_VERSION}" check_version_greater check_iscsc_remote "${ISCSC_REMOTE}" -echo "[+] '${NEW_VERSION}'>'${CURRENT_VERSION}', '${NEW_VERSION}' is accepted as new version." +log_info "'${NEW_VERSION}'>'${CURRENT_VERSION}', '${NEW_VERSION}' is accepted as new version." # --------------------------------- Git setup ---------------------------------- # ...and checkout on main to create a version bump branch -echo "[+] Checkout on ${ISCSC_REMOTE}/main" +log_info "Checkout on ${ISCSC_REMOTE}/main" [ -z "$DRY_RUN" ] && git checkout ${ISCSC_REMOTE}/main || exit 1 -echo "[+] switching to ${BUMP_BRANCH}" +log_info "switching to ${BUMP_BRANCH}" [ -z "$DRY_RUN" ] && git switch -c ${BUMP_BRANCH} # -------------------------------- Version Bump -------------------------------- # ------------------------- Bump frontend and backend -------------------------- -echo '[+] Bumping `frontend`' +log_info 'Bumping `frontend`' ( cd frontend [ -z "$DRY_RUN" ] && npm version "${NEW_VERSION}" --no-git-tag-version ) -echo '[+] Bumping `backend`' +log_info 'Bumping `backend`' ( cd backend [ -z "$DRY_RUN" ] && npm version "${NEW_VERSION}" --no-git-tag-version ) -echo '[+] Commiting `frontend` and `backend` bump' +log_info 'Commiting `frontend` and `backend` bump' [ -z "$DRY_RUN" ] && { git commit -m "Bump frontend and backend versions to ${NEW_VERSION}" || exit 1; } # ----------------------------- Bump root and push ----------------------------- -echo '[+] Bumping `root`' +log_info 'Bumping `root`' [ -z "$DRY_RUN" ] && { npm version "${NEW_VERSION}" -m "Bump to version %s" || exit 1; } -echo '[+] Pushing branch and new version tag' -echo "[~] pushing to \`${ISCSC_REMOTE}\` please type your passphrase/password if required:" -[ -z "$DRY_RUN" ] && { git push ${ISCSC_REMOTE} ${BUMP_BRANCH} v${CURRENT_VERSION} || echo "[-] push failed, you can push with \`git push ${ISCSC_REMOTE} ${BUMP_BRANCH} v${CURRENT_VERSION}\`"; } +log_info 'Pushing branch and new version tag' +log_warning "pushing to \`${ISCSC_REMOTE}\` please type your passphrase/password if required:" +[ -z "$DRY_RUN" ] && { git push ${ISCSC_REMOTE} ${BUMP_BRANCH} v${CURRENT_VERSION} || log_error "push failed, you can push with \`git push ${ISCSC_REMOTE} ${BUMP_BRANCH} v${CURRENT_VERSION}\`"; } -echo '[!] `npm install` has been run during the bump, you MUST review the changes during PR review to ensure package.json and package-lock.json where compatible!!!' +log_error '`npm install` has been run during the bump, you MUST review the changes during PR review to ensure package.json and package-lock.json where compatible!!!'