-
Notifications
You must be signed in to change notification settings - Fork 662
/
make-metainfo-changelog.sh
executable file
·144 lines (114 loc) · 4.33 KB
/
make-metainfo-changelog.sh
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
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later OR ISC OR MIT
# Copyright Stefan "Newbyte" Hansson
# Script to automatically update the Appstream metainfo release section based on CHANGELOG.md.
# Dependencies: Bash, GNU Awk 4.1.0 or later, and seq.
set -refu
CHANGELOG_ENTRY_PATTERN='^-\ *'
CHANGELOG_TYPE_PATTERN='^###\ *'
CHANGELOG_FILENAME='CHANGELOG.md'
METAINFO_FILENAME='resources/com.jpexs.decompiler.flash.metainfo.xml'
RELEASE_DATE_PATTERN='[[:digit:]]+-[[:digit:]]+-[[:digit:]]+'
RELEASE_VERSION_PATTERN='[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+'
RELEASE_PATTERN_CHANGELOG="^##\ \[$RELEASE_VERSION_PATTERN\]"
RELEASE_PATTERN_METAINFO="<release version=\"$RELEASE_VERSION_PATTERN\" date=\"$RELEASE_DATE_PATTERN\">"
find_metainfo_entry_line() {
local line
while read -r line; do
echo "$line"
done < "$METAINFO_FILENAME"
}
get_date_from_line() {
echo "$1" | grep -Eo "$RELEASE_DATE_PATTERN"
}
get_release_from_line() {
echo "$1" | grep -Eo "$RELEASE_VERSION_PATTERN"
}
get_latest_metainfo_release() {
local line_number=0
local line
while read -r line; do
let 'line_number++'
if [[ $line =~ $RELEASE_PATTERN_METAINFO ]]; then
echo $(get_release_from_line "$line") "$line_number"
break
fi
done < "$METAINFO_FILENAME"
}
process_line() {
# Remove [] from the line to convert [#1234] to #1234
echo "$1" | sed 's/[][]//g'
}
get_changelog_until_version() {
print_with_indent() {
local indents="$1"
local message="$2"
printf '\t%.0s' $(seq "$indents")
echo "$message"
}
local version_limit="$1"
local previous_type='undefined'
local start_parsing='false'
local changelog_text
local current_type
local line
local release_date
local release_version
while read -r line; do
if [[ $line =~ $RELEASE_PATTERN_CHANGELOG ]]; then
release_version=$(get_release_from_line "$line")
release_date=$(get_date_from_line "$line")
[ "$release_version" == "$version_limit" ] && break
start_parsing='true'
fi
# We don't want to parse the changelog's preamble
if [ "$start_parsing" == 'true' ]; then
if [[ $line =~ $RELEASE_PATTERN_CHANGELOG ]]; then
current_type='release'
elif [[ $line =~ $CHANGELOG_TYPE_PATTERN ]]; then
current_type='type'
elif [[ $line =~ $CHANGELOG_ENTRY_PATTERN ]]; then
current_type='entry'
else
current_type='none'
fi
line=$(process_line "$line")
if [ "$current_type" == 'entry' ] && [ "$previous_type" != 'entry' ]; then
print_with_indent 4 '<ul>'
fi
if [ "$current_type" != 'entry' ] && [ "$previous_type" == 'entry' ]; then
print_with_indent 4 '</ul>'
fi
if [ "$current_type" == 'release' ] && [ "$previous_type" == 'none' ]; then
print_with_indent 3 '</description>'
print_with_indent 2 '</release>'
fi
case "$current_type" in
release)
print_with_indent 2 "<release version=\"$release_version\" date=\"$release_date\">"
print_with_indent 3 '<description>'
;;
type)
print_with_indent 4 "<p>${line:4}</p>"
;;
entry)
print_with_indent 5 "<li>${line:2}</li>"
;;
esac
previous_type="$current_type"
fi
done < "$CHANGELOG_FILENAME"
if [ "$start_parsing" == 'true' ]; then
if [ "$previous_type" == 'entry' ]; then
print_with_indent 4 '</ul>'
fi
print_with_indent 3 '</description>'
print_with_indent 2 '</release>'
fi
}
read -ra args <<< $(get_latest_metainfo_release)
latest_release="${args[0]}"
insert_at_line="${args[1]}"
changelog=$(get_changelog_until_version "$latest_release")
[ -z "$changelog" ] && exit
awk -i inplace -v changelog="$changelog" -v insert_at_line="$insert_at_line" 'NR==insert_at_line {print changelog} 1' resources/com.jpexs.decompiler.flash.metainfo.xml