From b88f1e854404e0f90a2a5483f9ca3d1f429d3dba Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Sat, 10 Dec 2022 22:03:59 +0100 Subject: [PATCH] bin/shell-replace --- bin/shell-replace | 95 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 27 deletions(-) diff --git a/bin/shell-replace b/bin/shell-replace index 3c78cfb2..ea89b09d 100755 --- a/bin/shell-replace +++ b/bin/shell-replace @@ -60,36 +60,59 @@ if ($Opt{find}) { no_chdir => 1 }, @ARGV); #exit; - #die Dumper(\@WANTED_FILES); @ARGV = @WANTED_FILES; } # -# Process +# Process each files # my $hfile; my $file; for $file (@ARGV) { + my $is_stdin = 0; + if ($file eq "-") { #$hfile = \*ARGV; $hfile = \*STDIN; + $is_stdin = 1; + } else { next if myself($file); open($hfile,"<",$file); } + my @original = <$hfile>; seek($hfile,0,SEEK_SET); - verbose(1,"Processing: $file\n"); + verbose(1,"Processing file $file"); my @lines = &shell_replace($hfile); - print map { $_ } @lines; close $hfile; - verbose(1,"Changed: $file\n") if join('',@original) ne join('',@lines); + + if ( join('',@original) ne join('',@lines) ) { + verbose(1," Change detected from $file"); + verbose(2,' lines '.@lines.'/'.@original); + #&shell_replace_write($file,@lines) if !$is_stdin and !$DEBUG; + + # Write changes + if (!$is_stdin and !$DEBUG) { + if ($Opt{extension}) { + verbose(1," Copy $file to ".$file.'.'.$Opt{extension}); + File::Copy::cp($file,$file.'.'.$Opt{extension}) or die $!; + } + verbose(1,' Write '.$file); + open $hfile,">",$file or die $!; + print $hfile @lines; + close $hfile; + } + + } + print map { $_ } @lines if $is_stdin; } +use File::Copy; ################################################################################# # @@ -113,7 +136,7 @@ sub verbose { sub myself { my $file = shift @_; if ($file eq $0 or $file =~ /(^|\/)$NAME$/) { - verbose(1,"ignore file $file\n"); + verbose(1,"ignore file $file"); return 1 } return 0; @@ -133,7 +156,7 @@ sub wanted { next unless /^.{1,3}) { if ( @@ -156,26 +180,41 @@ my $hfile = shift @_; .. $line =~ /^\s*.{1,3}>".$line; next; - $i++; - if ($2) { - $cmd = "bash -lc \"$2\""; - if ($i==1) { push @lines, $line; push @lines, map{s/^/$ident/;$_} `$cmd`; } - elsif (/^\s*.{1,3}; + # + # Single line tags + # + $blank = $1; + $cmd = "bash -lc \"$2\""; + #$line = <$hfile>; push @lines, $line; - push @lines, map{ s/^/$ident/; $_ } `$cmd`; - $change = 1; + push @lines, map{ s/^/$blank/; $_ } `$cmd`; } else { + # + # No matched tags line + # $i=0; push @lines, $line; @@ -290,6 +329,8 @@ Description! =head1 OPTIONS -option[find|f!] Search first files containing tags from args + -option[extension|e=s] Copy original to a new file with this value as an extension + -option[verbose|v+] Verbose mode: increase the verbosity level. -option[debug+] Debug mode: increase the verbosity level. -option[version|V] Print version (default: $VERSION) -- 2.47.3