[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ant-dev
Subject:    Re: Antifying bash?
From:       Mike Williams <mikew () cortexebusiness ! com ! au>
Date:       2001-10-31 4:10:02
[Download RAW message or body]

>>> On Sat, 27 Oct 2001 16:47:18 -0400,
  >>> "Bill" == "Bill Burton" <billb@progress.com> wrote:

  Bill> I thought I'd try and kick start the process of adding Ant completion
  Bill> support for Z-Shell. 

Cool.

  Bill> But for those who can't or won't switch to Z-Shell, Bash 2.0
  Bill> supports Programmable Completion although it's very primitive ...
  Bill> it should be possible for someone to write some basic completion
  Bill> support for Ant options, build.xml selection after an -f or
  Bill> -buildfile option and targets.

I'll bite.  See attached Perl script, which provides
options/buildFile/target completion for bash.

  Bill> I believe I've found a way to reliably cache the results from
  Bill> -projecthelp so that it needs to be run only once.

Yup, my script has a similar optimisation, caching the output of 
"ant -projecthelp".

-- 
cheers, Mike

"Don't pet the sweaty things."

["complete-ant-cmd" (application/x-perl)]

#!/usr/bin/perl
#
# A script to allow Bash to complete an Ant command-line.  
# Install it with
# 
#     $ complete -C complete-ant-cmd ant
#
# @author Mike Williams <mikew@cortexebusiness.com.au>

my $cmdLine = $ENV{'COMP_LINE'};
my $antCmd = $ARGV[0];
my $word = $ARGV[1];

my @completions;
if ($word =~ /^-/) {
    list( restrict( $word, getArguments() ));
} elsif ($cmdLine =~ /-(f|buildfile)\s+\S*$/) {
    list( getBuildFiles($word) );
} else {
    list( restrict( $word, getTargets() ));
}

exit(0);

sub list {
    for (@_) {
        print "$_\n";
    }
}

sub restrict {
    my ($word, @completions) = @_;
    grep( /^\Q$word\E/, @completions );
}

sub getArguments {
    qw(-buildfile -debug -emacs -f -find -help -listener -logfile 
       -logger -projecthelp -quiet -verbose -version); 
}


sub getBuildFiles {
    my ($word) = @_;
    grep( /\.xml$/, glob( "$word*" ));
}

sub getTargets {

    # Look for build-file
    my $buildFile = 'build.xml';
    if ($cmdLine =~ /-(f|buildfile)\s+(\S+)/) {
        $buildFile = $2;
    }
    return () unless (-f $buildFile);

    # Run "ant -projecthelp" to list targets.  Keep a cache of results in a
    # cache-file.
    my $cacheFile = $buildFile;
    $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
    if ((!-e $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
        open( CACHE, '>'.$cacheFile ) || die "can\'t write $cacheFile: $!\n";
        open( HELP, "$antCmd -projecthelp -f '$buildFile'|" ) || return(); 
        my %targets;
        while( <HELP> ) {
            if (/^\s+(\w+)/) {
                $targets{$1}++;
            }
        }
        my @targets = sort keys %targets;
        for (@targets) { print CACHE "$_\n"; }
        return @targets;
    }
    
    # Read the target-cache
    open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
    my @targets;
    while (<CACHE>) {
        chop;
        push( @targets, $_ );
    }
    close( CACHE );
    @targets;

}



--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic