complete-ant-cmd.pl 3.4 KB

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