-
Notifications
You must be signed in to change notification settings - Fork 0
/
package
executable file
·92 lines (76 loc) · 2.42 KB
/
package
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
#!/bin/bash
# Usage info
show_help() {
cat << EOF
Usage: [-hf] [-v <git tag>]
Parameter details:
-h display help and exit
-v git tag, checkout release tag specified
EOF
}
# Initialise variables
OPTIND=1 # Reset for previous uses of getops in shell
OPTSTRING="hv:"
gitTag=false
cd "$( dirname "${BASH_SOURCE[0]}" )" # cd into directory the script is inside
confFile="package.conf"
while getopts "$OPTSTRING" opt; do
case "$opt" in
h)
show_help
exit 0
;;
v)
gitTag=$OPTARG
;;
*)
show_help >&2
exit 1
;;
esac
done
# Arg1 is the text, arg2 is the file path
function prepend_text_to_file {
echo -e "$1$(cat $2)" > $2
}
if [[ -f "$confFile" ]]; then
# takes values from conf file and converts them into bash variables
source $confFile
else
echo "$confFile not found"
exit 1
fi
# Validation
if [[ ! -f "$changelog_file_path" ]]; then
echo "$changelog_file_path is not a file"
fi
if [[ ! -d "$unreleased_changes_directory_path" ]]; then
echo "$unreleased_changes_directory_path is not a directory"
fi
if [[ "$(ls -1 "$unreleased_changes_directory_path" | wc -l)" == 0 ]]; then
echo "changes directory is empty"
exit 1
fi
# Append tag and date to changelog
echo "Adding to $changelog_file_path"
newChange="\\\t<release>\n\t\t<meta>\n\t\t\t<tag>$gitTag</tag>\n\t\t\t<date>$(date +"%y-%m-%d")</date>\n\t\t</meta>\n"
# Iterate over files by globbing
for filepath in "$unreleased_changes_directory_path"*; do
# avoid glob not matching
[ -e "$filepath" ] || continue
# read file line by line
# -r prevents backslash escapes from being interpreted.
# || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero
# exit code when it encounters EOF).
while read -r line || [[ -n "$line" ]]; do
# strips directory path prefix off of path to file to leave only the filepath
trueFilename=${filepath#"$unreleased_changes_directory_path"}
# add to newChange
newChange="$newChange\t\t<change>$trueFilename - $line</change>\n"
done < "$filepath"
done
newChange="$newChange\t</release>"
sed -i "/<changelog>/a $newChange" "$changelog_file_path"
# Remove files in $unreleased_changes_directory_path as they're now in the changelog
echo "Removing files matching $unreleased_changes_directory_path*"
rm -rf "$unreleased_changes_directory_path"*