summaryrefslogtreecommitdiffstats
path: root/tools/findunused/removeunusedresources
blob: 0c384949954b112cbbaa04d155b31e915e224438 (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
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
#!/bin/bash

if ! which xmlstarlet > /dev/null
then
    echo "You need to have the 'xmlstarlet' command in your path"
    exit
fi

apps=$1
CWD=$(pwd)/
if [ "$apps" = "" ]
then
    echo "Please specify the path to an application, or '--all' to process all applications"
    exit
elif [ "$apps" = "--all" ]
then
    apps=$ANDROID_BUILD_TOP/packages/apps/*
fi

BASE=$(pwd)/$(dirname $0)

for app in $apps
do
    pushd $app
    $BASE/findunusedresources -p . | {
        read LINE NUM
        while [ "$LINE" != "" ]
        do
            if [ "Z$LINE" = "Z-----------------------------------------------------------" ]
            then
                # skip
                true
            elif [ "$LINE" = "$app" ]
            then
                # skip
                true
            else
                # try to find the missing resource
                find res | grep -w $LINE  | {
                    read RESLINE
                    while [ "$RESLINE" != "" ]
                    do
                        if [ -f $RESLINE ]
                        then
                            echo REMOVING FILE: $RESLINE
                            git rm $RESLINE > /dev/null
                        else
                            echo WARNING unexpected result for $LINE
                        fi
                        read RESLINE
                    done
                }
                grep -Rwl $LINE res | {
                    read RESLINE
                    while [ "$RESLINE" != "" ]
                    do
                        ISSTRING=$(echo "$RESLINE" | grep -w "strings\.xml")
                        if [ -n "$ISSTRING" ]
                        then
                            echo REMOVING STRING $LINE from $RESLINE
                            xmlstarlet ed -P -S -d "/resources/string[@name='$LINE']" $RESLINE > tf$$
                            mv tf$$ $RESLINE
                            git add $RESLINE
                        else
                            echo REMOVING $LINE from $RESLINE
                            xmlstarlet ed -P -S -d "/resources/*[@name='$LINE']" $RESLINE > tf$$
                            mv tf$$ $RESLINE
                            git add $RESLINE
                        fi
                        read RESLINE
                    done
                }
            fi
            read LINE NUM
        done
    }
    popd
done
echo
echo "Done."
echo "Please rebuild the updated applications to make sure that everything still builds."
echo "After rebuilding, rerun 'findunusedresources' or 'removeunusedresources' to see if any more resources are now unused."
echo "When you're done, you can 'git commit' the change."
echo