diff --git a/scripts/format_xml.sh b/scripts/format_xml.sh index f6433491f3..d41e81291b 100755 --- a/scripts/format_xml.sh +++ b/scripts/format_xml.sh @@ -6,8 +6,10 @@ OPTIND=1 # Reset in case getopts has been used previously in the shell. # Initialize variables mode="format" +xml_dir="." +keep_old=0 -while getopts "h?c" opt; do +while getopts "h?cd:o" opt; do case "$opt" in h|\?) show_help @@ -15,31 +17,50 @@ while getopts "h?c" opt; do ;; c) mode="check" ;; + d) xml_dir=${OPTARG} + ;; + o) keep_old=1 + ;; esac done -xml_files=$(find . -name "*.xml") +shift $(($OPTIND - 1)) + +xml_file="$1" + +if [ "$xml_file" == "" ] +then + xml_files=$(find $xml_dir -name "*.xml") +else + xml_files="$xml_dir/$xml_file" +fi +echo "processing file(s) $xml_files" + ret=0 -for f in $xml_files +for f in $xml_files do - xmllint -format "${f}" > "${f}".new - case "$mode" in - format) - if ! cmp "${f}" "${f}".new >/dev/null 2>&1 - then - echo "formatting $f" - cp "${f}".new "${f}" - fi - ;; - check) - if ! cmp "${f}" "${f}".new >/dev/null 2>&1 - then - echo "$f needs formatting - run ./scripts/format_xml.sh $f" - ret=1 - fi - ;; - esac - rm "${f}".new + xmllint -format "${f}" > "${f}".new + case "$mode" in + format) + if ! cmp "${f}" "${f}".new >/dev/null 2>&1 + then + echo "formatting $f" + if [ $keep_old -eq 1 ] + then + cp "${f}" "${f}".old + fi + cp "${f}".new "${f}" + fi + ;; + check) + if ! cmp "${f}" "${f}".new >/dev/null 2>&1 + then + echo "file $f needs formatting - run ./scripts/format_xml.sh $f" + ret=1 + fi + ;; + esac + rm "${f}".new done exit $ret