You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

complete-ant-cmd.pl 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. my $cmdLine = $ENV{'COMP_LINE'};
  35. my $antCmd = $ARGV[0];
  36. my $word = $ARGV[1];
  37. my @completions;
  38. if ($word =~ /^-/) {
  39. list( restrict( $word, getArguments() ));
  40. } elsif ($cmdLine =~ /-(f|buildfile)\s+\S*$/) {
  41. list( getBuildFiles($word) );
  42. } else {
  43. list( restrict( $word, getTargets() ));
  44. }
  45. exit(0);
  46. sub list {
  47. for (@_) {
  48. print "$_\n";
  49. }
  50. }
  51. sub restrict {
  52. my ($word, @completions) = @_;
  53. grep( /^\Q$word\E/, @completions );
  54. }
  55. sub getArguments {
  56. qw(-buildfile -debug -emacs -f -find -help -listener -logfile
  57. -logger -projecthelp -quiet -verbose -version);
  58. }
  59. sub getBuildFiles {
  60. my ($word) = @_;
  61. grep( /\.xml$/, glob( "$word*" ));
  62. }
  63. sub getTargets {
  64. # Look for build-file
  65. my $buildFile = 'build.xml';
  66. if ($cmdLine =~ /-(f|buildfile)\s+(\S+)/) {
  67. $buildFile = $2;
  68. }
  69. return () unless (-f $buildFile);
  70. # Run "ant -projecthelp" to list targets. Keep a cache of results in a
  71. # cache-file.
  72. my $cacheFile = $buildFile;
  73. $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
  74. if ((!-e $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
  75. open( CACHE, '>'.$cacheFile ) || die "can\'t write $cacheFile: $!\n";
  76. open( HELP, "$antCmd -projecthelp -f '$buildFile'|" ) || return();
  77. my %targets;
  78. while( <HELP> ) {
  79. if (/^\s+(\S+)/) {
  80. $targets{$1}++;
  81. }
  82. }
  83. my @targets = sort keys %targets;
  84. for (@targets) { print CACHE "$_\n"; }
  85. return @targets;
  86. }
  87. # Read the target-cache
  88. open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
  89. my @targets;
  90. while (<CACHE>) {
  91. chop;
  92. s/\r$//; # for Cygwin
  93. push( @targets, $_ );
  94. }
  95. close( CACHE );
  96. @targets;
  97. }