Created
November 2, 2016 10:18
-
-
Save bikong0411/be006502e4fd81dedc96333345daa213 to your computer and use it in GitHub Desktop.
delete function for php file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use feature 'say'; | |
use Carp (carp,croak); | |
use IO::File; | |
sub del_lines($$); | |
croak "Usage: file" unless @ARGV ==1; | |
my $file = shift; | |
my $fh = IO::File->new($file,'r'); | |
my $fail = IO::File->new("$file.del_fail",'w'); | |
my $total=0; | |
while(!$fh->eof) { | |
chomp(my $line = $fh->getline()); | |
my($file,$func) = split /\s+/, $line; | |
my $code = del_lines($file, $func); | |
if($code == 1) { | |
say "del function $file $func finished"; | |
}else{ | |
$fail->print("del function $file $func fail\n"); | |
} | |
} | |
$fh->close; | |
$fail->close; | |
sub del_lines($$) { | |
my($file, $func) = @_; | |
my $f = IO::File->new($file,'r'); | |
my $bak = IO::File->new("$file.bak",'w'); | |
$bak->autoflush; | |
my $flag = 0; | |
my $line_no = 0; | |
my $branch_num = 0; | |
my @comment; | |
my $comment_flag = 0; | |
while(!$f->eof) { | |
chomp(my $line = $f->getline()); | |
if($flag == 0) { | |
if ($line =~ /^\s*\/\// ) { | |
push @comment, $line; | |
next; | |
} | |
if($line =~ /^\s*\/\*/ && $line =~ /\*\/\s*$/) { | |
push @comment, $line; | |
next; | |
} | |
if ($line =~ /^\s*\/\*/) { | |
$comment_flag = 1; | |
push @comment, $line; | |
next; | |
} | |
if($comment_flag == 1 && $line !~ /\*\//) { | |
push @comment, $line; | |
next; | |
} | |
if ($line =~ /\*\//) { | |
$comment_flag = 0; | |
push @comment, $line; | |
next; | |
} | |
} | |
if ( $line =~ /\s+$func\s*\(/ && $line =~ /function/) { #当前行 | |
$flag = 1; | |
@comment = (); | |
} | |
if ($line !~ /^\s*$/ && $flag == 0) { | |
if(@comment) { | |
foreach(@comment) { | |
$bak->write("$_\n"); | |
} | |
} | |
$bak->write("$line\n"); | |
@comment = (); | |
next; | |
} elsif ( $flag == 0 ) { | |
push @comment, $line; | |
next; | |
} | |
if($line =~ /^\s*\/\//) { | |
next; | |
} | |
#say "flag=$flag line_no=$line_no branch_num=$branch_num line=$line"; | |
$line_no += 1; | |
if(my $n=()=$line =~ /\{/g) { | |
$branch_num+=$n; | |
} | |
if(my $n = () = $line =~ /\}/g) { | |
$branch_num-=$n; | |
if ( $flag ==1 && $branch_num == 0) { | |
$flag=0; | |
next; | |
} | |
} | |
} | |
$f->close; | |
$bak->flush && $bak->close; | |
my $cmd = qq/php -l $file.bak/; | |
$cmd=qx[$cmd]; | |
if($cmd =~ /No\ssyntax\serrors/) { | |
rename("$file.bak",$file); | |
return 1; | |
} | |
return 0; | |
} | |
__COMMENT__ | |
file format | |
/path/to/models/tools/trafficData.php _getAllCities |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment