|
| 1 | +#! /usr/bin/env bash |
| 2 | + |
| 3 | +# Readonly vars |
| 4 | +readonly CACHE_DIR="${HOME}/.git-references" |
| 5 | + |
| 6 | +# Global vars |
| 7 | +declare -a cloned_git_repos |
| 8 | + |
| 9 | +#============================================================================== |
| 10 | +# |
| 11 | +# What: A library of standard Bash functions |
| 12 | +# Why: Simplify and improve the quality of bash scripts being produced. |
| 13 | +# |
| 14 | +#============================================================================== |
| 15 | + |
| 16 | +# cache_maven_deps function |
| 17 | +# ------------------------------------- |
| 18 | +# Run a `mvnw compile` in order to populate the local |
| 19 | +# m2 repository. |
| 20 | +# |
| 21 | +# Expects a single param which is a project directory to change into |
| 22 | +# ------------------------------------- |
| 23 | +cache_maven_deps() { |
| 24 | + local project=$1 |
| 25 | + |
| 26 | + # Check that 'project' dir exists |
| 27 | + if [[ ! -d $project ]]; then |
| 28 | + printf 'Project dir %s doesn''t exist. Exiting...\n' $project >&2 |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + |
| 32 | + # Switch directory, and run mvnw compile |
| 33 | + pushd $project |
| 34 | + ./mvnw compile |
| 35 | + popd |
| 36 | +} |
| 37 | + |
| 38 | +# clean_git_repo_clones function |
| 39 | +# ------------------------------------- |
| 40 | +# Clean up any Git repo clones performed by the 'clone_git_repo' function. |
| 41 | +# Should be called by the 'EXIT' trap. |
| 42 | +# ------------------------------------- |
| 43 | +clean_git_repo_clones() { |
| 44 | + printf 'Cleaning cloned git repos...\n' |
| 45 | + |
| 46 | + # Loop over cloned repos and remove them... |
| 47 | + for cloned_repo in "${cloned_git_repos[@]}" |
| 48 | + do |
| 49 | + printf 'Removing repo %s\n' "$cloned_repo" |
| 50 | + rm -rf $cloned_repo |
| 51 | + done |
| 52 | +} |
| 53 | + |
| 54 | +# clone_git_repo function |
| 55 | +# ------------------------------------- |
| 56 | +# Clone a Git repo, using either a Git SSH source, or a repo short-name. |
| 57 | +# If a matching local reference repo is found, then that will be used as the `--reference` for the Git clone, |
| 58 | +# otherwise a full clone is undertaken. |
| 59 | +# |
| 60 | +# Expects a single param which is the repo name or repo SSH source |
| 61 | +# ------------------------------------- |
| 62 | +clone_git_repo() { |
| 63 | + local repo=$1 |
| 64 | + local repo_name |
| 65 | + local repo_url |
| 66 | + |
| 67 | + # Calculate the "humanish" part of the source repo |
| 68 | + repo_name=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') |
| 69 | + local mirror_dir="${CACHE_DIR}/${repo_name}.git" |
| 70 | + |
| 71 | + # If $repo is a SSH repo source, use it as the repo_url. |
| 72 | + # Otherwise, attempt to get the origin URL from the matching reference repo, failing if the reference repo is not present. |
| 73 | + if [[ $repo == git* ]]; then |
| 74 | + repo_url=$repo |
| 75 | + else |
| 76 | + if [[ -d $mirror_dir ]]; then |
| 77 | + repo_url=$(cd "$mirror_dir" && git remote get-url origin) |
| 78 | + else |
| 79 | + printf 'Attempting to clone %s using the short-name, however no matching reference repo found. Exiting...\n' "$repo" >&2 |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + fi |
| 83 | + |
| 84 | + # Check if we have a reference repo clone for this repo, |
| 85 | + # and use it for the clone if we do. |
| 86 | + # Otherwise clone the repo directly. |
| 87 | + if [[ -d $mirror_dir ]]; then |
| 88 | + printf 'Cloning repo %s using reference repo...\n' "$repo" |
| 89 | + git clone -q --reference "$mirror_dir" "$repo_url" || exit 1 |
| 90 | + else |
| 91 | + printf 'Cloning repo %s directly...' "$repo" |
| 92 | + git clone -q "$repo_url" || exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + # Add repo to cloned repo array |
| 96 | + cloned_git_repos+=("$repo") |
| 97 | + |
| 98 | + # Clean up clone on exit |
| 99 | + trap clean_git_repo_clones EXIT |
| 100 | +} |
| 101 | + |
| 102 | +# retry function |
| 103 | +# ------------------------------------- |
| 104 | +# Retry a command for a specified number of times until the command exits successfully. |
| 105 | +# Retry wait period backs off exponentially after each retry. |
| 106 | +# |
| 107 | +# The first argument should be the number of retries. |
| 108 | +# Remainder is treated as the command to execute. |
| 109 | +# ------------------------------------- |
| 110 | +retry() { |
| 111 | + local retries=$1 |
| 112 | + shift |
| 113 | + |
| 114 | + local count=0 |
| 115 | + until "$@"; do |
| 116 | + exit=$? |
| 117 | + wait=$((2 ** $count)) |
| 118 | + count=$(($count + 1)) |
| 119 | + if [ $count -lt $retries ]; then |
| 120 | + printf "Retry %s/%s exited %s, retrying in %s seconds...\n" "$count" "$retries" "$exit" "$wait" >&2 |
| 121 | + sleep $wait |
| 122 | + else |
| 123 | + printf "Retry %s/%s exited %s, no more retries left.\n" "$count" "$retries" "$exit" >&2 |
| 124 | + return $exit |
| 125 | + fi |
| 126 | + done |
| 127 | + return 0 |
| 128 | +} |
0 commit comments