1402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#!/usr/bin/perl
2402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
3402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  Licensed to the Apache Software Foundation (ASF) under one or more
4402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  contributor license agreements.  See the NOTICE file distributed with
5402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  this work for additional information regarding copyright ownership.
6402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  The ASF licenses this file to You under the Apache License, Version 2.0
7402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  (the "License"); you may not use this file except in compliance with
8402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  the License.  You may obtain a copy of the License at
9402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
10402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#      http://www.apache.org/licenses/LICENSE-2.0
11402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
12402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  Unless required by applicable law or agreed to in writing, software
13402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  distributed under the License is distributed on an "AS IS" BASIS,
14402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  See the License for the specific language governing permissions and
16402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#  limitations under the License.
17402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
18402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll# A script to allow Bash or Z-Shell to complete an Ant command-line.
19402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
20402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll# To install for Bash 2.0 or better, add the following to ~/.bashrc:
21402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
22402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#     $ complete -C complete-ant-cmd ant build.sh
23402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
24402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll# To install for Z-Shell 2.5 or better, add the following to ~/.zshrc:
25402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
26402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#     function ant_complete () {
27402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#         local args_line args
28402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#         read -l args_line
29402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#         set -A args $args_line
30402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#         set -A reply $(COMP_LINE=$args_line complete-ant-cmd ${args[1]} $1)
31402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#     }
32402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#     compctl -K ant_complete ant build.sh
33402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll#
34402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll# @author Mike Williams <mikew@cortexebusiness.com.au>
35402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
36402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollmy $cmdLine = $ENV{'COMP_LINE'};
37402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollmy $antCmd = $ARGV[0];
38402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollmy $word = $ARGV[1];
39402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
40402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollmy @completions;
41402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollif ($word =~ /^-/) {
42402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    list( restrict( $word, getArguments() ));
43402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll} elsif ($cmdLine =~ /-(f|buildfile)\s+\S*$/) {
44402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    list( getBuildFiles($word) );
45402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll} else {
46402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    list( restrict( $word, getTargets() ));
47402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
48402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
49402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollexit(0);
50402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
51402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollsub list {
52402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    for (@_) {
53402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        print "$_\n";
54402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
55402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
56402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
57402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollsub restrict {
58402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    my ($word, @completions) = @_;
59402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    grep( /^\Q$word\E/, @completions );
60402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
61402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
62402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollsub getArguments {
63402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    qw(-buildfile -debug -emacs -f -find -help -listener -logfile
64402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll       -logger -projecthelp -quiet -verbose -version);
65402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
66402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
67402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
68402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollsub getBuildFiles {
69402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    my ($word) = @_;
70402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    grep( /\.xml$/, glob( "$word*" ));
71402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
72402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
73402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollsub getTargets {
74402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
75402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    # Look for build-file
76402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    my $buildFile = 'build.xml';
77402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    if ($cmdLine =~ /-(f|buildfile)\s+(\S+)/) {
78402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        $buildFile = $2;
79402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
80402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    return () unless (-f $buildFile);
81402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
82402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    # Run "ant -projecthelp" to list targets.  Keep a cache of results in a
83402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    # cache-file.
84402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    my $cacheFile = $buildFile;
85402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
86402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    if ((!-e $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
87402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        open( CACHE, '>'.$cacheFile ) || die "can\'t write $cacheFile: $!\n";
88402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        open( HELP, "$antCmd -projecthelp -f '$buildFile'|" ) || return();
89402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        my %targets;
90402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        while( <HELP> ) {
91402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll            if (/^\s+(\S+)/) {
92402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll                $targets{$1}++;
93402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll            }
94402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        }
95402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        my @targets = sort keys %targets;
96402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        for (@targets) { print CACHE "$_\n"; }
97402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        return @targets;
98402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
99402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
100402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    # Read the target-cache
101402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
102402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    my @targets;
103402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    while (<CACHE>) {
104402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        chop;
105402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        s/\r$//;  # for Cygwin
106402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        push( @targets, $_ );
107402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
108402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    close( CACHE );
109402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    @targets;
110402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
111402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
112402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
113402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
114402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
115