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