summaryrefslogtreecommitdiffhomepage
path: root/android/scripts/tidy.sh
blob: 85d5630f6443f54f41ab1a6ed98d29c1c5ff35c3 (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
#!/usr/bin/env bash

# CI/Developer script to format
# Relies on Tidy - https://github.com/htacg/tidy-html5

set -eu

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

function main {
    case ${1:-""} in
        format) format;;
        formatAndCheckDiff) format && checkDiff;;
        "")
            echo "Available subcommands: format, formatAndCheckDiff"
            ;;
        *)
            echo "Unknown parameter: $1"
            exit 1
            ;;
    esac
}

# Autoformats Android XML files
function format {
    non_text_xml_paths=("$(find .. -wholename "*/src/*.xml" ! -name "strings*.xml" ! -name plurals.xml)")
    for xml_path in "${non_text_xml_paths[@]}"; do
        tidy -xml \
            -m  \
            -i  \
            -w 100 \
            -utf8 \
            --quiet yes \
            --indent-attributes yes \
            --indent-spaces 4 \
            --literal-attributes yes \
            "$xml_path"
    done

    # We only format non-translated files since we don't want
    # to introduce a mismatch between the xml files and source
    # (.po) files.
    non_translated_text_xml_paths=("$(find .. -wholename "*/values/strings*.xml" -o -wholename "*/values/plurals.xml")")
    for xml_path in "${non_translated_text_xml_paths[@]}"; do
        tidy -xml \
            -m  \
            -i  \
            -w 0 \
            -utf8 \
            --quiet yes \
            --indent-spaces 4 \
            --literal-attributes yes \
            --indent-cdata yes \
            "$xml_path"
    done

    # FIXME - when tidy learns to not leave whitespace around, remove the line below - https://github.com/htacg/tidy-html5/issues/864
    find .. -name '*.xml' -exec sed -i -e 's/[ \t]*$//' '{}' ';'
}

function checkDiff {
    if git diff --exit-code -- ../**/*.xml; then
        echo "Android XML files are correctly formatted"
        return 0
    else
        echo "Android XML files are NOT correctly formatted"
        return 1
    fi
}

main "$@"