-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertSolutions.pl
More file actions
executable file
·165 lines (121 loc) · 4.88 KB
/
insertSolutions.pl
File metadata and controls
executable file
·165 lines (121 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env perl
# This is based on work that is in the public domain
# and modified by Marc Boudreau
#TODO:mpb Figure out how to do the multiple regex with strict
#use strict;
use warnings;
use File::Path;
use Getopt::Long ();
use XML::LibXML;
use Pod::Usage;
my $solutionsFile;
my $variableFile;
my $sigperlFile;
my $outputFile;
my $help;
my $quiet;
my $solutionParser;
my $solutionXmlDoc;
my $solutionXmlContext;
my $variableParser;
my $variableXmlDoc;
my $variableXmlContext;
sub performSubstitution() {
my $line = $_;
my $substitution;
if (($line =~ m/%script_lib_obj:(.*)%/) ) {
# If the line starts with a script library token
# then replace it with the corresponding script library
# By spec. this is only one per line
return ($solutionXmlContext->find("//CODE_SNIPPET[\@NAME=\"$1\"]")."\n") if (defined $solutionXmlContext);
}
elsif ($line =~ m/%(\D.*?)%/) {
# In case there are multiple variables on the line
foreach my $expr (1..$#-) {
next if (${$expr} =~ m/dds_property/); # Skip warnings about outputs and other not so useful things
my $variableToken = ${$expr};
if (defined $variableXmlContext) {
$substitution = $variableXmlContext->findvalue("//variable[\@name=\"\%$variableToken\%\"]");
}
# TODO:mpb Fix this
# Looking for problems with strftime tokens
if (defined $substitution) {
$line =~ s/%$variableToken%/$substitution/g;
}
elsif (defined $variableFile) {
print STDERR "WARNING: No substitution found for: [\%${$expr}\%]\n" unless (defined $quiet);
}
}
}
return $line;
}
pod2usage(-verbose => 2, -noperldoc => 1) if (
! Getopt::Long::GetOptions(
"s|solution|script:s" => \$solutionsFile, # The xml containing the Signiant solutions scripts
"v|variables|var:s" => \$variableFile, # The xml containing the variable substitutions. Not substituted if not provided
"i|input|in:s" => \$sigperlFile, # The Signiant perl source (default: STDIN)
"o|output|out:s" => \$outputFile, # where to put the perl output (default: STDOUT)
"q|quiet" => \$quiet, # Don't output STDERR messages (except for Usage)
"h|help|?" => \$help ) or
defined $help);
$outputFile = "-" if (!defined $outputFile); # default to STDOUT
$sigperlFile = "-" if (!defined $sigperlFile); # default to STDIN
if (defined $solutionsFile) {
# Open the Solutions File and get ready to parse it...
$solutionParser = XML::LibXML->new();
$solutionXmlDoc = $solutionParser->parse_file( $solutionsFile );
$solutionXmlContext = XML::LibXML::XPathContext->new( $solutionXmlDoc );
}
else {
print STDERR "WARNING: No Solution Script file provided.\n" unless (defined $quiet);
}
if (defined $variableFile) {
# Open the Variable File and get ready to parse it...
$variableParser = XML::LibXML->new();
$variableXmlDoc = $variableParser->parse_file( $variableFile );
$variableXmlContext = XML::LibXML::XPathContext->new( $variableXmlDoc );
}
else {
print STDERR "WARNING: No Variable file provided.\n" unless (defined $quiet);
}
open CODEFILE, "<$sigperlFile" or die "ERROR: Unable to open $sigperlFile\n";
my @lines = <CODEFILE>;
close CODEFILE;
@lines = map { &performSubstitution } @lines;
open OUTFILE, ">$outputFile" or die "ERROR: Unable to open $outputFile\n";
print OUTFILE @lines;
close OUTFILE;
__END__
=head1 NAME
insertSolutions.pl - Run a Signiant perl file
=head1 SYNOPSIS
insertSolutions.pl [options]
Options:
-f -file Perl input file
-d -debug run in debugger
-dds use dds_perl
-ns -noscript skip insertion of Solution Scripts
-nv -novariables skip insertion of the variables
-show output code to STDOUT
-help brief help message
=head1 OPTIONS
=over 8
=item B<-file> (required)
The file containing the Perl script to run.
=item B<-debug>
Perform all the specified substitutions and start the program in the perl debugger.
=item B<-dds>
Run/debug the script with dds_perl (as opposed to the default Perl on the system).
=item B<-noscript>
Perform all other specified substitutions except the insertion of the code in SolutionsScripts.xml.
=item B<-novariable>
Perform all other specified substitutions except the insertion of the variables specified in the corresponding variables file.
=item B<-show>
Perform the specified substitutions and output the resulting script to STDOUT instead of running it.
=item B<-help>
Prints this page and exits.
=back
=head1 DESCRIPTION
B<This program> will read the given specified perl script and merge the required elements from the SolutionsScript.xml and the corresponding variable file.
The resultant file will be run using the system default Perl.
=cut