From 1a855ad41230b3ef09da4d5cc7541e47bbe915c8 Mon Sep 17 00:00:00 2001 From: Leon Brocard Date: Thu, 9 Jul 2009 16:05:04 +0100 Subject: [PATCH] munge namespace --- bin/build.pl | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/bin/build.pl b/bin/build.pl index 56b118b..55da2c3 100755 --- a/bin/build.pl +++ b/bin/build.pl @@ -1,10 +1,13 @@ #!/home/acme/perl-5.10.0/bin//perl use strict; use warnings; +use Cwd; +use File::Find::Rule; use LWP::Simple; use Perl6::Say; use Path::Class; -use Cwd; +use PPI; +use PPI::Find; my $thrift_trunk_dir = dir( cwd, 'thrift-trunk' ); my $thrift_trunk_configure = file( $thrift_trunk_dir, 'configure' ); @@ -17,6 +20,7 @@ = dir( cwd, 'cassandra-trunk', 'build', 'classes', 'org', 'apache', 'cassandra', 'service', 'CassandraDaemon.class' ); my $gen_perl_dir = dir( cwd, 'gen-perl' ); +my $perl_dir = dir( cwd, 'lib', 'Net', 'Cassandra', 'Backend' ); unless ( -d $thrift_trunk_dir ) { say 'Fetching Thrift'; @@ -74,3 +78,74 @@ "$thrift_installed_dir/bin/thrift --gen perl $cassandra_trunk_dir/interface/cassandra.thrift"; die 'Failed to generate' unless -d $gen_perl_dir; } + +unless ( -f $cassandra_class ) { + say 'Building Cassandra'; + chdir $cassandra_trunk_dir; + system "ant"; + die 'Failed to build' unless -f $cassandra_class; +} + +# my $gen_perl_dir = dir( cwd, 'gen-perl' ); +# my $perl_dir = dir(cwd, 'lib', 'Net', 'Cassandra', 'Backend'); + +unless ( -f "$perl_dir/Cassandra.pm" ) { + say 'Munging Cassandra Perl modules'; + + # first let's find the package names + my %packages; + foreach my $source ( File::Find::Rule->new->file->in($gen_perl_dir) ) { + say "$source"; + my $document = PPI::Document->new($source); + my $find + = PPI::Find->new( sub { $_[0]->isa('PPI::Statement::Package') } ); + $find->start($document) or die "Failed to execute search"; + while ( my $package = $find->match ) { + + # say $package; + $packages{ $package->namespace } = 1; + } + } + $packages{Types} = 1; # fake + + # now fix up the new package names + foreach my $source ( File::Find::Rule->new->file->in($gen_perl_dir) ) { + my $destination = file( $perl_dir, file($source)->basename ); + say "$source -> $destination"; + my $document = PPI::Document->new($source); + + # first change the words + my $find = PPI::Find->new( sub { $_[0]->isa('PPI::Token::Word') } ); + $find->start($document) or die "Failed to execute search"; + while ( my $word = $find->match ) { + my $namespace = $word->content; + if ( $packages{$namespace} ) { + $namespace = 'Net::Cassandra::Backend::' . $namespace; + $word->set_content($namespace); + + #say "* $word"; + } else { + + #say $word; + } + } + + # now try quotes + $find = PPI::Find->new( sub { $_[0]->isa('PPI::Token::Quote') } ); + $find->start($document) or die "Failed to execute search"; + while ( my $word = $find->match ) { + my $namespace = $word->content; + $namespace =~ s/^'//; + $namespace =~ s/'$//; + if ( $packages{$namespace} ) { + $namespace = 'Net::Cassandra::Backend::' . $namespace; + $word->set_content( "'" . $namespace . "'" ); + say "** $word"; + } else { + say "?? $word"; + } + } + $document->save($destination); + } + die 'Failed to build' unless -f "$perl_dir/Cassandra.pm"; +}