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 2.4 kB

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