| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # shell::script Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Shell::Script; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(scripting) ], | ||
| 16 | attributes => { | ||
| 17 | input => [ qw(file) ], | ||
| 18 | }, | ||
| 19 | commands => { | ||
| 20 | load => [ qw(input|OPTIONAL) ], | ||
| 21 | execute => [ qw($line_list) ], | ||
| 22 | load_and_execute => [ qw(input|OPTIONAL) ], | ||
| 23 | }, | ||
| 24 | }; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub load { | ||
| 28 | my $self = shift; | ||
| 29 | my ($input) = @_; | ||
| 30 | |||
| 31 | $input ||= $self->input; | ||
| 32 | $self->brik_help_run_file_not_found('load', $input) or return; | ||
| 33 | |||
| 34 | my @lines = (); | ||
| 35 | open(my $in, '<', $input) | ||
| 36 | or return $self->log->error("load: can't open file [$input]: $!"); | ||
| 37 | while (defined(my $line = <$in>)) { | ||
| 38 | chomp($line); | ||
| 39 | next if $line =~ /^\s*$/; # Skip blank lines | ||
| 40 | next if $line =~ /^\s*#/; # Skip comments | ||
| 41 | $line =~ s/^(.*)#.*$/$1/; # Strip comments at end of line | ||
| 42 | push @lines, "$line "; # Add a trailing slash in case of a multiline | ||
| 43 | # So when joining them, there is no unwanted concatenation | ||
| 44 | } | ||
| 45 | close($in); | ||
| 46 | |||
| 47 | return \@lines; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub execute { | ||
| 51 | my $self = shift; | ||
| 52 | my ($lines) = @_; | ||
| 53 | |||
| 54 | if (! defined($self->shell)) { | ||
| 55 | return $self->log->error("execute: no core::shell Brik"); | ||
| 56 | } | ||
| 57 | |||
| 58 | $self->brik_help_run_undef_arg('execute', $lines) or return; | ||
| 59 | $self->brik_help_run_invalid_arg('execute', $lines, 'ARRAY') or return; | ||
| 60 | |||
| 61 | my $shell = $self->shell; | ||
| 62 | |||
| 63 | $shell->cmdloop($lines); | ||
| 64 | |||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub load_and_execute { | ||
| 69 | my $self = shift; | ||
| 70 | my ($input) = @_; | ||
| 71 | |||
| 72 | $input ||= $self->input; | ||
| 73 | |||
| 74 | my $lines = $self->load($input) or return; | ||
| 75 | return $self->execute($lines); | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; | ||
| 79 | |||
| 80 | __END__ | ||