root/local/greenstone3/windows-64bit/apache-ant-1.9.5/bin/complete-ant-cmd.pl @ 30443

Revision 30443, 3.4 KB (checked in by davidb, 4 years ago)

Move to newer version of ant needed, for some of the features now used in Greenstone3's build.xml + newer versions of Java

  • Property svn:executable set to *
Line 
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
34my $cmdLine = "$ENV{'ANT_ARGS'} $ENV{'COMP_LINE'}";
35my $antCmd = $ARGV[0];
36my $word = $ARGV[1];
37
38my @completions;
39if ($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
47exit(0);
48
49sub list {
50    for (@_) {
51        print "$_\n";
52    }
53}
54
55sub restrict {
56    my ($word, @completions) = @_;
57    grep( /^\Q$word\E/, @completions );
58}
59
60sub getArguments {
61    qw(-buildfile -debug -emacs -f -file -find -help -listener -logfile
62       -logger -projecthelp -quiet -verbose -version);
63}
64
65
66sub getBuildFiles {
67    my ($word) = @_;
68    grep( /\.xml$/, glob( "$word*" ));
69}
70
71sub 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) { print CACHE "$_\n"; }
98        return @targets;
99    }
100   
101    # Read the target-cache
102    open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
103    my @targets;
104    while (<CACHE>) {
105        chop;
106        s/\r$//;  # for Cygwin
107        push( @targets, $_ );
108    }
109    close( CACHE );
110    @targets;
111
112}
113
114
115
Note: See TracBrowser for help on using the browser.