-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefinitions.bash
More file actions
156 lines (134 loc) · 4.12 KB
/
definitions.bash
File metadata and controls
156 lines (134 loc) · 4.12 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
echo "This script is meant to be sourced from other scripts; it is not meant to be directly executed."
set -o errexit # Fail if any command has a non-zero exit status. Equivalent to `-e`. PEARL: it still won't fail if any of the following `set` commands fails.
set -o nounset # Fail if an undefined variable is referenced. Equivalent to `-u`.
set -o pipefail # Prevent pipelines from masking errors. (Use `command1 | command2 || true` to mask.)
set -C # do not overwrite an existing file when redirecting. use `>|` instead of `>` to override.
shopt -s expand_aliases # Do not ignore aliases. (What kind of idiot made ignoring aliases the default behavior?)
#shopt -s extglob # enable extended pattern matching.
PS4='+ $LINENO: ' # See https://stackoverflow.com/a/17805088/773113
# set -x # enable echoing commands for the purpose of troubleshooting.
alias info='log "${BASH_SOURCE[0]}" $LINENO INFO'
alias warn='log "${BASH_SOURCE[0]}" $LINENO WARN'
alias error='log "${BASH_SOURCE[0]}" $LINENO ERROR'
function log()
{
local -; set +x
declare -r self=$(realpath --relative-to="$PWD" "$1"); shift
declare -r line=$1; shift
declare -r level=$1; shift
declare -r message=$*
printf "%s(%s): %s: %s\n" "$self" "$line" "$level" "$message"
}
function increment_version()
{
declare -r version=$1
declare part_to_increment=${2-}
if [[ "$part_to_increment" == "" ]]; then
part_to_increment="patch"
fi
declare -i major
declare -i minor
declare -i patch
IFS=. read -r major minor patch <<< "$version"
case "$part_to_increment" in
"major")
major=$((major+1))
minor=0
patch=0
;;
"minor")
minor=$((minor+1))
patch=0
;;
"patch")
patch=$((patch+1))
;;
*)
error "Invalid increment: '$part_to_increment'; expected 'major', 'minor', or 'patch' (the default)"
exit 1
esac
printf "%s.%s.%s" "$major" "$minor" "$patch"
}
function assert_no_untracked_files()
{
declare -r untracked_files=$(git ls-files -o --directory --exclude-standard --no-empty-directory)
if [ "$untracked_files" != "" ]; then
error "You have untracked files!"
info $untracked_files
info "Please add, stage, and commit first."
return 1
fi
}
function assert_no_tracked_but_unstaged_changes()
{
declare -r unstaged_files=$(git diff-files --name-only)
if [ "$unstaged_files" != "" ]; then
error "You have tracked but unstanged changes!"
info "$unstaged_files"
info "Please stage and commit first."
return 1
fi
}
function assert_no_staged_but_uncommitted_changes()
{
declare -r uncommitted_files=$(git diff-index --name-only --cached HEAD)
if [ "$uncommitted_files" != "" ]; then
error "You have staged but uncommitted changes!"
info "$uncommitted_files"
info "Please unstage or commit first."
return 1
fi
}
function remove_if_exists()
{
declare -r pattern=$1
for i in $pattern; do
if [ -f "$i" ]; then
rm "$i";
fi
done
}
function get_xml_value()
{
declare -r file=$1
declare -r element=$2
declare -r default=${3-}
# Voodoo magick from Stack Overflow: "Extract XML Value in bash script" https://stackoverflow.com/a/17334043/773113
declare value=$(cat $file | sed -ne "/$element/{s/.*<$element>\(.*\)<\/$element>.*/\1/p;q;}")
if [[ -z "$value" ]]; then
value="$default"
fi
printf "$value"
}
function git_get_last_commit_message()
{
# PEARL: as the case is with virtually all git commands, the git command that prints the last commit message looks
# absolutely nothing like a command that would print the last commit message. Linus Torvalds you are not just a
# geek, you are a fucking dork.
# from https://stackoverflow.com/a/7293026/773113
# and https://stackoverflow.com/questions/7293008/display-last-git-commit-comment#comment105325732_7293026
git log -1 --pretty=format:%B
}
function run_this_script_in_its_directory()
{
declare -r directory=$(dirname $(realpath $0))
if [[ "$directory" != "$PWD" ]]; then
(cd "$directory"; bash $(basename $0) $@)
exit $?
fi
}
function join
{
declare -r delimiter=$1
shift
declare result=""
declare delimiter_to_use=""
while [ $# -gt 0 ]; do
if [[ ! -z "$1" ]]; then
printf "%s%s" "$delimiter_to_use" "$1"
delimiter_to_use="$delimiter"
fi
shift
done
}