summaryrefslogtreecommitdiffstats
path: root/misc/replace_includehints
blob: edccc89c672208751c58b8a425f62999b6186753 (plain)
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
#!/bin/bash
#
# This script replaces <includehints> in TQt designer UI files
# with global <include>'s, removing duplicate includes.
#
# The reasons for this is that includehints are not well supported
# and can cause FTBFS.
#
# Copyright (C) 2022 Mavridis Philippe <mavridisf@gmail.com>
#                    for the Trinity Desktop Project
#
# Licensed under GNU GPLv2 or later.
#

# Find files needing update
TO_REPLACE=$(find * -name \*.ui -exec grep -l includehint '{}' \;)

if [[ ${#TO_REPLACE} == 0 ]]
then
	echo "No files need to be modified."
	exit 0
fi

echo "Files that will be modified:"
for f in $TO_REPLACE
do
	echo " - $f"
done

echo
echo "Press any key to continue to ^C to cancel."
read

# Start replacing
declare -a headers
for f in $TO_REPLACE
do
	echo "Updating file '$f'..."

	# Replace containing tag
	sed -Ei 's!<(\/?)includehints>!<\1includes>!g' $f

	# Replace includes themselves while avoiding duplicates (a simple
	#   sed -Ei 's/<includehint>([[:alnum:]\/.]*)</includehint>/<include location="global" impldecl="in implementation">\1</include>/g' $f
	# would leave duplicates behind).
	headers=()
	for h in $(grep -o "<includehint>[[:alnum:]\/.]*</includehint>" $f | sed -E 's/<\/?includehint>//g')
	do
		if [[ ! "${headers[*]}" =~ $h ]] # if this is a unique header
		then
			echo " - $h"
			sed -i "0,/<includehint>$h<\/includehint>/{s//<include location=\"global\" impldecl=\"in implementation\">$h<\/include>/}" $f
			headers+=($h)
		fi
	done

	# Remove leftover duplicate includehints
	sed -Ei '/<includehint>([[:alnum:]\/.]*)<\/includehint>/d' $f
done