|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# __ __ |
| 4 | +# / /____ ___ ____ ___ ___ _/ / This file is provided to you by https://github.com/tegonal/github-commons |
| 5 | +# / __/ -_) _ `/ _ \/ _ \/ _ `/ / Copyright 2022 Tegonal Genossenschaft <info@tegonal.com> |
| 6 | +# \__/\__/\_, /\___/_//_/\_,_/_/ It is licensed under Creative Commons Zero v1.0 Universal |
| 7 | +# /___/ Please report bugs and contribute back your improvements |
| 8 | +# |
| 9 | +# Version: v2.8.2 |
| 10 | +####### Description ############# |
| 11 | +# |
| 12 | +# functions which can be used to update the placeholders in the templates in a gt pull-hook.sh |
| 13 | +# |
| 14 | +####### Usage ################### |
| 15 | +# |
| 16 | +# #!/usr/bin/env bash |
| 17 | +# set -euo pipefail |
| 18 | +# shopt -s inherit_errexit |
| 19 | +# MY_PROJECT_LATEST_VERSION="v1.0.0" |
| 20 | +# |
| 21 | +# # Assumes tegonal's github-commons was fetched with gt and put into repoRoot/lib/tegonal-gh-commons/src |
| 22 | +# dir_of_github_commons="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../../../lib/tegonal-gh-commons/src" |
| 23 | +# |
| 24 | +# if ! [[ -v dir_of_tegonal_scripts ]]; then |
| 25 | +# dir_of_tegonal_scripts="$dir_of_github_commons/../../tegonal-scripts/src" |
| 26 | +# source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts" |
| 27 | +# fi |
| 28 | +# |
| 29 | +# source "$dir_of_github_commons/gt/pull-hook-functions.sh" |
| 30 | +# |
| 31 | +# declare _tag=$1 source=$2 _target=$3 |
| 32 | +# shift 3 || die "could not shift by 3" |
| 33 | +# |
| 34 | +# # replaces placeholders in all files github-commons provides with placeholders |
| 35 | +# replaceTegonalGhCommonsPlaceholders "$source" "my-project-name" "$MY_PROJECT_LATEST_VERSION" \ |
| 36 | +# "MyCompanyName, Country" "code-of-conduct@my-company.com" "my-companies-github-name" "my-project-github-name" |
| 37 | +# |
| 38 | +################################### |
| 39 | +set -euo pipefail |
| 40 | +shopt -s inherit_errexit |
| 41 | +unset CDPATH |
| 42 | + |
| 43 | +if ! [[ -v dir_of_github_commons ]]; then |
| 44 | + dir_of_github_commons="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/.." |
| 45 | + readonly dir_of_github_commons |
| 46 | +fi |
| 47 | + |
| 48 | +if ! [[ -v dir_of_tegonal_scripts ]]; then |
| 49 | + dir_of_tegonal_scripts="$dir_of_github_commons/../tegonal-scripts/src" |
| 50 | + source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts" |
| 51 | +fi |
| 52 | + |
| 53 | +sourceOnce "$dir_of_tegonal_scripts/utility/parse-fn-args.sh" |
| 54 | + |
| 55 | +function replaceTegonalGhCommonsPlaceholders() { |
| 56 | + local source projectName version owner ownerEmail ownerGithub projectNameGithub |
| 57 | + # shellcheck disable=SC2034 # is passed to parseFnArgs by name |
| 58 | + local -ra params=(source projectName version owner ownerEmail ownerGithub projectNameGithub) |
| 59 | + parseFnArgs params "$@" |
| 60 | + |
| 61 | + if [[ $source =~ .*/\.github/CODE_OF_CONDUCT.md ]]; then |
| 62 | + replacePlaceholdersCodeOfConduct "$source" "$ownerEmail" |
| 63 | + elif [[ $source =~ .*/\.github/Contributor[[:space:]]Agreement\.txt ]]; then |
| 64 | + replacePlaceholdersContributorsAgreement "$source" "$projectName" "$projectNameGithub" "$owner" "$ownerGithub" |
| 65 | + elif [[ $source =~ .*/\.github/PULL_REQUEST_TEMPLATE.md ]]; then |
| 66 | + local -r githubUrl="https://github.com/$ownerGithub/$projectNameGithub" |
| 67 | + replacePlaceholdersPullRequestTemplate "$source" "$githubUrl" "$version" |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +function replaceTegonalGhCommonsPlaceholders_Tegonal() { |
| 72 | + local source projectName version projectNameGithub |
| 73 | + # shellcheck disable=SC2034 # is passed to parseFnArgs by name |
| 74 | + local -ra params=(source projectName version projectNameGithub) |
| 75 | + parseFnArgs params "$@" |
| 76 | + |
| 77 | + local tegonalFullName tegonalEmail tegonalGithubName |
| 78 | + source "$dir_of_github_commons/gt/tegonal.data.source.sh" || die "could not source tegonal.data.source.sh" |
| 79 | + |
| 80 | + replaceTegonalGhCommonsPlaceholders "$source" "$projectName" "$version" "$tegonalFullName" "$tegonalEmail" "$tegonalGithubName" "$projectNameGithub" |
| 81 | +} |
| 82 | + |
| 83 | +function replacePlaceholdersContributorsAgreement() { |
| 84 | + if ! (($# == 5)); then |
| 85 | + logError "you need to pass 5 arguments to replacePlaceholdersContributorsAgreement" |
| 86 | + echo "1: file represents the 'Contributor Agreement.txt'" |
| 87 | + echo "2: projectName the name of the project" |
| 88 | + echo "3: projectNameGithub the name of the project on GitHub" |
| 89 | + echo "4: owner the owner of the project" |
| 90 | + echo "5: ownerGithub the name of the organisation/owner on GitHub" |
| 91 | + printStackTrace |
| 92 | + exit 9 |
| 93 | + fi |
| 94 | + local -r file=$1 |
| 95 | + local -r projectName=$2 |
| 96 | + local -r projectNameGithub=$3 |
| 97 | + local -r owner=$4 |
| 98 | + local -r ownerGithub=$5 |
| 99 | + shift 5 || die "could not shift by 5" |
| 100 | + PROJECT_NAME="$projectName" \ |
| 101 | + PROJECT_NAME_GITHUB="$projectNameGithub" \ |
| 102 | + OWNER="$owner" \ |
| 103 | + OWNER_GITHUB="$ownerGithub" \ |
| 104 | + perl -0777 -i \ |
| 105 | + -pe 's/<PROJECT_NAME>/$ENV{PROJECT_NAME}/g;' \ |
| 106 | + -pe 's/<PROJECT_NAME_GITHUB>/$ENV{PROJECT_NAME_GITHUB}/g;' \ |
| 107 | + -pe 's/<OWNER>/$ENV{OWNER}/g;' \ |
| 108 | + -pe 's/<OWNER_GITHUB>/$ENV{OWNER_GITHUB}/g;' \ |
| 109 | + "$file" |
| 110 | +} |
| 111 | + |
| 112 | +function replacePlaceholdersPullRequestTemplate() { |
| 113 | + if ! (($# == 3)); then |
| 114 | + logError "you need to pass three arguments to replacePlaceholdersPullRequestTemplate" |
| 115 | + echo "1: file represents the 'PULL_REQUEST_TEMPLATE.md'" |
| 116 | + echo "2: url the github url" |
| 117 | + echo "3: latestTag latest tag" |
| 118 | + printStackTrace |
| 119 | + exit 9 |
| 120 | + fi |
| 121 | + local -r file=$1 |
| 122 | + local -r url=$2 |
| 123 | + local -r tag=$3 |
| 124 | + shift 3 || die "could not shift by 3" |
| 125 | + TAG="$tag" GITHUB_URL="$url" perl -0777 -i \ |
| 126 | + -pe 's#<GITHUB_URL>#$ENV{GITHUB_URL}#g;' \ |
| 127 | + -pe 's#<TAG>#$ENV{TAG}#g;' \ |
| 128 | + "$file" |
| 129 | +} |
| 130 | + |
| 131 | +function replacePlaceholdersCodeOfConduct() { |
| 132 | + if ! (($# == 2)); then |
| 133 | + logError "you need to pass two arguments to replacePlaceholdersCodeOfConductTemplate" |
| 134 | + echo "1: file represents the 'CODE_OF_CONDUCT.md'" |
| 135 | + echo "2: owner_email email address which should be contacted in case of a violation" |
| 136 | + printStackTrace |
| 137 | + exit 9 |
| 138 | + fi |
| 139 | + local -r file=$1 |
| 140 | + local -r ownerEmail=$2 |
| 141 | + shift 2 || die "could not shift by 2" |
| 142 | + EMAIL="$ownerEmail" perl -0777 -i \ |
| 143 | + -pe 's/<OWNER_EMAIL>/$ENV{EMAIL}/g;' \ |
| 144 | + "$file" |
| 145 | +} |
| 146 | + |
| 147 | +function replaceTagInPullRequestTemplate() { |
| 148 | + if ! (($# == 3)); then |
| 149 | + logError "you need to pass three arguments to replaceTagInPullRequestTemplate" |
| 150 | + echo "1: file represents the 'PULL_REQUEST_TEMPLATE.md'" |
| 151 | + echo "2: url the github url" |
| 152 | + echo "3: tag tag to set in url" |
| 153 | + printStackTrace |
| 154 | + exit 9 |
| 155 | + fi |
| 156 | + local -r file=$1 |
| 157 | + local -r url=$2 |
| 158 | + local -r tag=$3 |
| 159 | + shift 3 || die "could not shift by 3" |
| 160 | + |
| 161 | + perl -0777 -i \ |
| 162 | + -pe "s#($url/blob/)[^/]+/#\${1}$tag/#;" \ |
| 163 | + "$file" |
| 164 | +} |
0 commit comments