]> git.nbdom.net Git - nb.git/commitdiff
bin/shell-replace
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 10 Dec 2022 21:03:59 +0000 (22:03 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 10 Dec 2022 21:03:59 +0000 (22:03 +0100)
bin/shell-replace

index 3c78cfb2de4084d7ecb556857744db0f55f2c83b..ea89b09dc334729b158cf77dc60522067a02768e 100755 (executable)
@@ -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}<SHELL_REPLACE/;
 
-               verbose(1,"Found: $file\n");
+               verbose(1,"Found: $file");
                push @WANTED_FILES, $file;
                last;
        }
@@ -141,14 +164,15 @@ sub wanted {
 }
 
 sub shell_replace {
+#
 # Search tags and execute command, return new lines
-my $ident;
-my $i;
-my $change;
-my $cmd;
-my @lines = ();
+#
+       my $blank;
+       my $i;
+       my $cmd;
+       my @lines = ();
 
-my $hfile = shift @_;
+       my $hfile = shift @_;
        while (my $line = <$hfile>) {
 
                if (
@@ -156,26 +180,41 @@ my $hfile = shift @_;
                        ..
                        $line =~ /^\s*.{1,3}<SHELL_REPLACE.*/
                ) {
+                       #
+                       # Start / End tags
+                       #
+
+                       $blank = $1 ? $1 : "";
+                       $i++;
+
+                       if ($i==1) {
+                               $cmd = "bash -lc \"$2\"";
+                               push @lines, $line;
+                               push @lines, map{s/^/$blank/;$_} `$cmd`;
+
+                       } elsif ($line =~ /^\s*.{1,3}<SHELL_REPLACE/) {
+                               $i = 0;
+                               push @lines, $line;
+
+                       } else {
+                               # In between is ignored
+                       }
 
-                $change = 1;
-                $ident = $1 ? $1 : "";
-                #die $line unless $2;
-                #warn ">>".$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}<SHELL_REPLACE/) { push @lines, $line; }
-                }
 
                } elsif ($line =~ /^(\s*).{1,3}=SHELL_REPLACE (.*)$/) {
-                       $ident = $1;
-                       <>;
+                       #
+                       # 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)