0N/A#!/usr/bin/perl
0N/A#
809N/A# Licensed to the Apache Software Foundation (ASF) under one or more
809N/A# contributor license agreements. See the NOTICE file distributed with
809N/A# this work for additional information regarding copyright ownership.
809N/A# The ASF licenses this file to You under the Apache License, Version 2.0
809N/A# (the "License"); you may not use this file except in compliance with
809N/A# the License. You may obtain a copy of the License at
0N/A#
0N/A# http://www.apache.org/licenses/LICENSE-2.0
0N/A#
0N/A# Unless required by applicable law or agreed to in writing, software
0N/A# distributed under the License is distributed on an "AS IS" BASIS,
0N/A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A# See the License for the specific language governing permissions and
0N/A# limitations under the License.
0N/A#
0N/A# A script to allow Bash or Z-Shell to complete an Ant command-line.
0N/A#
0N/A# To install for Bash 2.0 or better, add the following to ~/.bashrc:
0N/A#
0N/A# $ complete -C complete-ant-cmd ant build.sh
0N/A#
0N/A# To install for Z-Shell 2.5 or better, add the following to ~/.zshrc:
0N/A#
0N/A# function ant_complete () {
0N/A# local args_line args
0N/A# read -l args_line
0N/A# set -A args $args_line
0N/A# set -A reply $(COMP_LINE=$args_line complete-ant-cmd ${args[1]} $1)
0N/A# }
0N/A# compctl -K ant_complete ant build.sh
0N/A#
0N/A# @author Mike Williams <mikew@cortexebusiness.com.au>
0N/A
0N/Amy $cmdLine = $ENV{'COMP_LINE'};
0N/Amy $antCmd = $ARGV[0];
0N/Amy $word = $ARGV[1];
0N/A
0N/Amy @completions;
0N/Aif ($word =~ /^-/) {
0N/A list( restrict( $word, getArguments() ));
0N/A} elsif ($cmdLine =~ /-(f|buildfile)\s+\S*$/) {
0N/A list( getBuildFiles($word) );
0N/A} else {
0N/A list( restrict( $word, getTargets() ));
0N/A}
0N/A
0N/Aexit(0);
0N/A
0N/Asub list {
0N/A for (@_) {
0N/A print "$_\n";
0N/A }
0N/A}
0N/A
0N/Asub restrict {
0N/A my ($word, @completions) = @_;
0N/A grep( /^\Q$word\E/, @completions );
0N/A}
0N/A
0N/Asub getArguments {
0N/A qw(-buildfile -debug -emacs -f -find -help -listener -logfile
0N/A -logger -projecthelp -quiet -verbose -version);
0N/A}
0N/A
0N/A
0N/Asub getBuildFiles {
0N/A my ($word) = @_;
0N/A grep( /\.xml$/, glob( "$word*" ));
0N/A}
0N/A
0N/Asub getTargets {
0N/A
0N/A # Look for build-file
0N/A my $buildFile = 'build.xml';
0N/A if ($cmdLine =~ /-(f|buildfile)\s+(\S+)/) {
0N/A $buildFile = $2;
0N/A }
0N/A return () unless (-f $buildFile);
0N/A
0N/A # Run "ant -projecthelp" to list targets. Keep a cache of results in a
0N/A # cache-file.
0N/A my $cacheFile = $buildFile;
0N/A $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
5713N/A if ((!-e $cacheFile) || (-z $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
0N/A open( CACHE, '>'.$cacheFile ) || die "can\'t write $cacheFile: $!\n";
0N/A open( HELP, "$antCmd -projecthelp -f '$buildFile'|" ) || return();
0N/A my %targets;
0N/A while( <HELP> ) {
0N/A if (/^\s+(\S+)/) {
0N/A $targets{$1}++;
0N/A }
0N/A }
0N/A my @targets = sort keys %targets;
0N/A for (@targets) { print CACHE "$_\n"; }
0N/A return @targets;
0N/A }
0N/A
0N/A # Read the target-cache
0N/A open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
0N/A my @targets;
0N/A while (<CACHE>) {
0N/A chop;
0N/A s/\r$//; # for Cygwin
0N/A push( @targets, $_ );
0N/A }
0N/A close( CACHE );
0N/A @targets;
0N/A
0N/A}
0N/A
0N/A
0N/A