forked from tegonal/github-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle-sections.sh
More file actions
executable file
·91 lines (85 loc) · 3.94 KB
/
toggle-sections.sh
File metadata and controls
executable file
·91 lines (85 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
#
# __ __
# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts
# / __/ -_) _ `/ _ \/ _ \/ _ `/ / Copyright 2022 Tegonal Genossenschaft <info@tegonal.com>
# \__/\__/\_, /\___/_//_/\_,_/_/ It is licensed under Apache License 2.0
# /___/ Please report bugs and contribute back your improvements
#
# Version: v4.11.0
####### Description #############
#
# Searches for <!-- for main --> ... <!-- for main end --> as well as for
# <!-- for a specific release --> ... <!-- for a specific release end -->
# and kind of toggles section in the sense of if the passed command is 'main', then
# the content of <!-- for a specific release --> sections is commented and the content in <!-- for main --> is
# uncommented. Same same but different if someone passes the command 'release'
#
####### Usage ###################
#
# #!/usr/bin/env bash
# set -euo pipefail
# shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
# # Assumes tegonal's scripts were fetched with gt - adjust location accordingly
# dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
# source "$dir_of_tegonal_scripts/setup_tegonal_scripts.sh" "$dir_of_tegonal_scripts"
#
# "$dir_of_tegonal_scripts/releasing/toggle-sections.sh" -c main
#
# # if you use it in combination with other files, then you might want to source it instead
# sourceOnce "$dir_of_tegonal_scripts/releasing/toggle-sections.sh"
#
# # and then call the function
# toggleSections -c release
#
###################################
set -euo pipefail
shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
unset CDPATH
export TEGONAL_SCRIPTS_VERSION='v4.11.0'
if ! [[ -v dir_of_tegonal_scripts ]]; then
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/.."
source "$dir_of_tegonal_scripts/setup_tegonal_scripts.sh" "$dir_of_tegonal_scripts"
fi
sourceOnce "$dir_of_tegonal_scripts/utility/parse-args.sh"
function toggleSections() {
local command file
# shellcheck disable=SC2034 # is passed by name to parseArguments
local -ra params=(
command '-c|--command' "either 'main' or 'release'"
file '-f|--file' '(optional) the file where search & replace shall be done -- default: ./README.md'
)
local -r examples=$(
# shellcheck disable=SC2312 # cat shouldn't fail for a constant string hence fine to ignore exit code
cat <<-EOM
# comment the release sections in ./README.md and uncomment the main sections
toggle-sections.sh -c main
# comment the main sections in ./docs/index.md and uncomment the release sections
toggle-sections.sh -c release -f ./docs/index.md
EOM
)
parseArguments params "$examples" "$TEGONAL_SCRIPTS_VERSION" "$@" || return $?
if ! [[ -v file ]]; then file="./README.md"; fi
exitIfNotAllArgumentsSet params "$examples" "$TEGONAL_SCRIPTS_VERSION"
function toggleSection() {
local file=$1
local comment=$2
local uncomment=$3
perl -0777 -i \
-pe "s/(<!-- for $comment -->\n)\n([\S\s]*?)(\n<!-- for $comment end -->\n)/\${1}<!--\n\${2}-->\${3}/g;" \
-pe "s/(<!-- for $uncomment -->\n)<!--\n([\S\s]*?)-->(\n<!-- for $uncomment end -->)/\${1}\n\${2}\${3}/g" \
"$file"
}
if [[ $command == main ]]; then
echo "comment release sections and uncomment main sections"
toggleSection "$file" "release" "main"
elif [[ $command == release ]]; then
echo "comment main sections and uncomment release sections"
toggleSection "$file" "main" "release"
else
echo >&2 "only 'main' and 'release' are supported as command. Following the output of calling --help"
parse_args_printHelp params "$examples" "$TEGONAL_SCRIPTS_VERSION" --help
fi
}
${__SOURCED__:+return}
toggleSections "$@"