Skip to content

Commit 7cdbb15

Browse files
authored
scripts: add release/dump-version-info.sh (#309)
I used this script to help me orient myself. I think it might be possible to use it to simplify the other CI and/or release scripts that are related.
2 parents a70f0f3 + f454cc9 commit 7cdbb15

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

scripts/release/dump-version-info.sh

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
# Emit a JSON object in which each property is a package name and its value is
6+
# an object whose properties are the version (as a string) from its cabal file,
7+
# the latest version (as a string) from its `CHANGELOG.md' file, and the names
8+
# of the files other than `scriv.ini' in its `changelog.d' directory.
9+
#
10+
# There are two assumptions made. First, every `scriv.ini' file is at depth 3
11+
# in the git repository. Second, each `${GITROOT}/X/changelog.d/scriv.ini' file
12+
# has matching `${GITROOT}/X/X.cabal' and `${GITROOT}/X/CHANGELOG.md' files,
13+
# such that the cabal package name is also `X'.
14+
15+
# See https://stackoverflow.com/a/3352015
16+
function trim {
17+
local var="$*"
18+
# remove leading whitespace characters
19+
var="${var#"${var%%[![:space:]]*}"}"
20+
# remove trailing whitespace characters
21+
var="${var%"${var##*[![:space:]]}"}"
22+
printf '%s' "$var"
23+
}
24+
25+
function project_cabal_file_version {
26+
trim "$(grep -E -e '^version:' | head -n1 | cut -d':' -f2-)"
27+
}
28+
29+
function project_latest_changelog_version {
30+
trim "$(grep -E -e '^## ' | head -n1 | awk '{print $2}')"
31+
}
32+
33+
function normalize_changelog.d_filenames {
34+
grep -vE -e '^scriv.ini$' | sort
35+
}
36+
37+
function WORKINGTREE_package {
38+
pkg="$(basename "$1")"
39+
echo "\"$pkg\": {"
40+
41+
echo -n '"version": "'
42+
cat "$1/$pkg.cabal" | project_cabal_file_version
43+
echo '",'
44+
45+
echo -n '"changelog-version": "'
46+
cat "$1/CHANGELOG.md" | project_latest_changelog_version
47+
echo '",'
48+
49+
echo -n '"changelog.d-files": '
50+
echo -n '['
51+
local comma=0
52+
(cd "$1/changelog.d/"; ls -A1) | normalize_changelog.d_filenames | while read -r line; do
53+
[[ $((comma++)) -gt 0 ]] && echo -n ','
54+
echo -n "\"$line\""
55+
done
56+
echo ']'
57+
58+
echo '}'
59+
}
60+
61+
function GITREV_package {
62+
echo "\"$2\": {"
63+
64+
echo -n '"version": "'
65+
git show "$1:$2/$2.cabal" | project_cabal_file_version
66+
echo '",'
67+
68+
echo -n '"changelog-version": "'
69+
git show "$1:$2/CHANGELOG.md" | project_latest_changelog_version
70+
echo '",'
71+
72+
echo -n '"changelog.d-files": '
73+
echo -n '['
74+
local comma=0
75+
basename -a $(git ls-tree --name-only "$1" --full-tree -- "$2/changelog.d/") | normalize_changelog.d_filenames | while read -r line; do
76+
[[ $((comma++)) -gt 0 ]] && echo -n ','
77+
echo -n "\"$line\""
78+
done
79+
echo ']'
80+
81+
echo '}'
82+
}
83+
84+
function WORKINGTREE_case {
85+
local comma=0
86+
find "$(git rev-parse --show-toplevel)" -maxdepth 3 -type f -name 'scriv.ini' | sort | while read -r path; do
87+
[[ $((comma++)) -gt 0 ]] && echo -n ','
88+
WORKINGTREE_package "$(realpath "$(dirname "$path")/..")"
89+
done
90+
}
91+
92+
function GITREV_case {
93+
local comma=0
94+
# `sort' behaves differently if the trailing slash is present or not, and
95+
# it will be present in the `WORKINGTREE_case', so synthetically add it
96+
# here.
97+
git ls-tree --name-only "$1" --full-tree | sed -r 's|$|/|' | sort | while read -r dir; do
98+
dir="$(basename "$dir")" # remove the trailing slash
99+
for i in $(git ls-tree --name-only "$1" --full-tree -- "$dir/changelog.d"); do
100+
[[ $((comma++)) -gt 0 ]] && echo -n ','
101+
GITREV_package "$1" "$dir"
102+
done
103+
done
104+
}
105+
106+
function main {
107+
echo '{'
108+
if [ $# -eq 0 ]; then
109+
WORKINGTREE_case
110+
else
111+
GITREV_case "$1"
112+
fi
113+
echo '}'
114+
}
115+
116+
main "$@"

0 commit comments

Comments
 (0)