Commit 702a14d1 authored by Shawn M Moore's avatar Shawn M Moore
Browse files

Compile-check template code as soon as it is saved

parent 015855dc
......@@ -635,5 +635,46 @@ sub SetQueue {
return $self->_Set( Field => 'Queue', Value => $NewQueueObj->id );
}
=head2 CompileCheck
If the template's Type is Full, then compile check all the codeblocks to see if
they are syntactically valid. We eval them in a codeblock to avoid actually
executing the code.
Returns an (ok, message) pair.
=cut
sub CompileCheck {
my $self = shift;
return (1, "Template does not include Perl code")
unless $self->Type eq 'Full';
my $template = Text::Template->new(
TYPE => 'STRING',
SOURCE => $self->Content,
);
$template->compile;
# copied from Text::Template::fill_in and refactored to be compile checks
foreach my $fi_item (@{$template->{SOURCE}}) {
my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;
next unless $fi_type eq 'PROG';
eval "sub { $fi_text }";
next if !$@;
my $error = $@;
# provide a (hopefully) useful line number for the error, but clean up
# all the other extraneous garbage
$error =~ s/\(eval \d+\) line (\d+).*/"template line " . ($1+$fi_lineno-1)/es;
return (0, $self->loc("Couldn't compile template codeblock '[_1]': [_2]", $fi_text, $error));
}
return (1, "Template compiles");
}
1;
......@@ -116,6 +116,9 @@ if ($TemplateObj->Id()) {
path => "Admin/Global/Template.html?Queue=0&Template=".$TemplateObj->Id(),
};
push @results, @aresults;
my ($ok, $msg) = $TemplateObj->CompileCheck;
push @results, $msg if !$ok;
}
</%INIT>
......
......@@ -122,6 +122,9 @@ if ($TemplateObj->Id()) {
path => "Admin/Queues/Template.html?Queue=$Queue&Template=".$TemplateObj->Id(),
};
push @results, @aresults;
my ($ok, $msg) = $TemplateObj->CompileCheck;
push @results, $msg if !$ok;
} else {
$QueueObj = RT::Queue->new($session{'CurrentUser'});
$QueueObj->Load($Queue);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment