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

complete-ant-cmd.pl 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (c) 2001 The Apache Software Foundation. All rights
  4. # reserved.
  5. #
  6. # A script to allow Bash or Z-Shell to complete an Ant command-line.
  7. #
  8. # To install for Bash 2.0 or better, add the following to ~/.bashrc:
  9. #
  10. # $ complete -C complete-ant-cmd ant build.sh
  11. #
  12. # To install for Z-Shell 2.5 or better, add the following to ~/.zshrc:
  13. #
  14. # function ant_complete () {
  15. # local args_line args
  16. # read -l args_line
  17. # set -A args $args_line
  18. # set -A reply $(COMP_LINE=$args_line complete-ant-cmd ${args[1]} $1)
  19. # }
  20. # compctl -K ant_complete ant build.sh
  21. #
  22. # @author Mike Williams <mikew@cortexebusiness.com.au>
  23. my $cmdLine = $ENV{'COMP_LINE'};
  24. my $antCmd = $ARGV[0];
  25. my $word = $ARGV[1];
  26. my @completions;
  27. if ($word =~ /^-/) {
  28. list( restrict( $word, getArguments() ));
  29. } elsif ($cmdLine =~ /-(f|buildfile)\s+\S*$/) {
  30. list( getBuildFiles($word) );
  31. } else {
  32. list( restrict( $word, getTargets() ));
  33. }
  34. exit(0);
  35. sub list {
  36. for (@_) {
  37. print "$_\n";
  38. }
  39. }
  40. sub restrict {
  41. my ($word, @completions) = @_;
  42. grep( /^\Q$word\E/, @completions );
  43. }
  44. sub getArguments {
  45. qw(-buildfile -debug -emacs -f -find -help -listener -logfile
  46. -logger -projecthelp -quiet -verbose -version);
  47. }
  48. sub getBuildFiles {
  49. my ($word) = @_;
  50. grep( /\.xml$/, glob( "$word*" ));
  51. }
  52. sub getTargets {
  53. # Look for build-file
  54. my $buildFile = 'build.xml';
  55. if ($cmdLine =~ /-(f|buildfile)\s+(\S+)/) {
  56. $buildFile = $2;
  57. }
  58. return () unless (-f $buildFile);
  59. # Run "ant -projecthelp" to list targets. Keep a cache of results in a
  60. # cache-file.
  61. my $cacheFile = $buildFile;
  62. $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
  63. if ((!-e $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
  64. open( CACHE, '>'.$cacheFile ) || die "can\'t write $cacheFile: $!\n";
  65. open( HELP, "$antCmd -projecthelp -f '$buildFile'|" ) || return();
  66. my %targets;
  67. while( <HELP> ) {
  68. if (/^\s+(\S+)/) {
  69. $targets{$1}++;
  70. }
  71. }
  72. my @targets = sort keys %targets;
  73. for (@targets) { print CACHE "$_\n"; }
  74. return @targets;
  75. }
  76. # Read the target-cache
  77. open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
  78. my @targets;
  79. while (<CACHE>) {
  80. chop;
  81. s/\r$//; # for Cygwin
  82. push( @targets, $_ );
  83. }
  84. close( CACHE );
  85. @targets;
  86. }