1# bash completion for ant and phing                        -*- shell-script -*-
2
3_ant_parse_targets()
4{
5    local line basedir
6
7    [[ $1 == */* ]] && basedir=${1%/*} || basedir=.
8
9    # parse buildfile for targets
10    while read -rd '>' line; do
11        if [[ $line =~ \<(target|extension-point)[[:space:]].*name=[\"\']([^\"\']+) ]]; then
12            targets+=" ${BASH_REMATCH[2]}"
13        fi
14    done <$1
15
16    # parse imports
17    while read -rd '>' line; do
18        if [[ $line =~ \<import[[:space:]].*file=[\"\']([^\"\']+) ]]; then
19            local imported_buildfile
20            imported_buildfile="${basedir}/${BASH_REMATCH[1]}"
21            if [[ -f $imported_buildfile ]]; then
22                _ant_parse_targets $imported_buildfile
23            fi
24        fi
25    done <$1
26}
27
28_ant()
29{
30    local cur prev words cword
31    _init_completion || return
32
33    case $prev in
34        -h | -help | --h | --help | -projecthelp | -p | -version | -diagnostics)
35            return
36            ;;
37        -buildfile | -file | -f)
38            _filedir 'xml'
39            return
40            ;;
41        -logfile | -l)
42            [[ $1 != *phing || $prev != -l ]] && _filedir
43            return
44            ;;
45        -propertyfile)
46            _filedir properties
47            return
48            ;;
49        -nice)
50            COMPREPLY=($(compgen -W '{1..10}' -- "$cur"))
51            return
52            ;;
53        -lib)
54            _filedir -d
55            return
56            ;;
57        -logger | -listener | -inputhandler | -main | -find | -s)
58            return
59            ;;
60    esac
61
62    if [[ $cur == -D* ]]; then
63        return
64    elif [[ $cur == -* ]]; then
65        # The </dev/null prevents "phing -" weirdness/getting just a literal
66        # tab displayed on complete on CentOS 6 with phing 2.6.1.
67        COMPREPLY=(
68            $(compgen -W '$(_parse_help "$1" -h </dev/null)' -- "$cur"))
69    else
70        # available targets completion
71        # find which buildfile to use
72        local buildfile=build.xml i
73        for ((i = 1; i < cword; i++)); do
74            if [[ ${words[i]} == -@(?(build)file|f) ]]; then
75                buildfile=${words[i + 1]}
76                break
77            fi
78        done
79        if ((i == cword)); then
80            for i in ${ANT_ARGS-}; do
81                if [[ $prev == -@(?(build)file|f) ]]; then
82                    buildfile=$i
83                    break
84                fi
85                prev=$i
86            done
87        fi
88        [[ ! -f $buildfile ]] && return
89
90        local targets
91
92        # fill targets
93        _ant_parse_targets $buildfile
94
95        COMPREPLY=($(compgen -W '$targets' -- "$cur"))
96    fi
97} &&
98    complete -F _ant ant phing
99type complete-ant-cmd.pl &>/dev/null &&
100    complete -C complete-ant-cmd.pl -F _ant ant || :
101
102# ex: filetype=sh
103