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.2 kB

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