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