Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6803 buildadm 1
#!/usr/bin/perl
2
#
3
#  Licensed to the Apache Software Foundation (ASF) under one or more
4
#  contributor license agreements.  See the NOTICE file distributed with
5
#  this work for additional information regarding copyright ownership.
6
#  The ASF licenses this file to You under the Apache License, Version 2.0
7
#  (the "License"); you may not use this file except in compliance with
8
#  the License.  You may obtain a copy of the License at
9
#
10
#      http://www.apache.org/licenses/LICENSE-2.0
11
#
12
#  Unless required by applicable law or agreed to in writing, software
13
#  distributed under the License is distributed on an "AS IS" BASIS,
14
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
#  See the License for the specific language governing permissions and
16
#  limitations under the License.
17
#
18
# A script to allow Bash or Z-Shell to complete an Ant command-line.
19
#
20
# To install for Bash 2.0 or better, add the following to ~/.bashrc:
21
#
22
#     complete -C complete-ant-cmd.pl ant build.sh
23
#
24
# To install for Z-Shell 2.5 or better, add the following to ~/.zshrc:
25
#
26
#     function ant_complete () {
27
#         local args_line args
28
#         read -l args_line
29
#         set -A args $args_line
30
#         set -A reply $(COMP_LINE=$args_line complete-ant-cmd.pl ${args[1]} $1)
31
#     }
32
#     compctl -K ant_complete ant build.sh
33
 
34
my $cmdLine = "$ENV{'ANT_ARGS'} $ENV{'COMP_LINE'}";
35
my $antCmd = $ARGV[0];
36
my $word = $ARGV[1];
37
 
38
my @completions;
39
if ($word =~ /^-/) {
40
    list(restrict($word, getArguments()));
41
} elsif ($cmdLine =~ /-(f|file|buildfile)\s+\S*$/) {
42
    list(getBuildFiles($word));
43
} else {
44
    list(restrict($word, getTargets()));
45
}
46
 
47
exit(0);
48
 
49
sub list {
50
    for (@_) {
51
        print "$_\n";
52
    }
53
}
54
 
55
sub restrict {
56
    my ($word, @completions) = @_;
57
    grep(/^\Q$word\E/, @completions);
58
}
59
 
60
sub getArguments {
61
    qw(-buildfile -debug -emacs -f -file -find -help -listener -logfile
62
       -logger -projecthelp -quiet -verbose -version);
63
}
64
 
65
 
66
sub getBuildFiles {
67
    my ($word) = @_;
68
    grep(/\.xml$/, glob("$word*"));
69
}
70
 
71
sub getTargets {
72
 
73
    # Look for build-file
74
    my $buildFile = 'build.xml';
75
    if ($cmdLine =~ /-(f|file|buildfile)\s+(\S+)(?!.*\s-(f|file|buildfile)\s)/) {
76
        $buildFile = $2;
77
    }
78
    return () unless (-f $buildFile);
79
 
80
    # Run "ant -projecthelp -debug" to list targets (-debug is required to get
81
    # "Other targets", i.e. targets without a description).  Keep a cache of
82
    # results in a cache-file.
83
    my $cacheFile = $buildFile;
84
    $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
85
    if ((!-e $cacheFile) || (-z $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
86
        open(CACHE, '>' . $cacheFile) || die "can\'t write $cacheFile: $!\n";
87
        open(HELP, "$antCmd -projecthelp -debug -buildfile '$buildFile'|") || return();
88
        my %targets;
89
        while (<HELP>) {
90
            # Exclude target names starting with dash, because they cannot be
91
            # specified on the command line.
92
            if (/^\s+\+Target:\s+(?!-)(\S+)/) {
93
                $targets{$1}++;
94
            }
95
        }
96
        my @targets = sort keys %targets;
97
        for (@targets) {
98
            print CACHE "$_\n";
99
        }
100
        return @targets;
101
    }
102
 
103
    # Read the target-cache
104
    open(CACHE, $cacheFile) || die "can\'t read $cacheFile: $!\n";
105
    my @targets;
106
    while (<CACHE>) {
107
        chop;
108
        s/\r$//;  # for Cygwin
109
        push(@targets, $_);
110
    }
111
    close(CACHE);
112
    @targets;
113
 
114
}