/[cvs]/nfo/perl/libs/Data/Rap/Engine.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Rap/Engine.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sat Feb 22 16:51:21 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.3: +21 -3 lines
+ enhanced run_executable
modified logging output

1 ## ----------------------------------------------------------------------
2 ## $Id: Engine.pm,v 1.3 2003/02/21 01:46:17 joko Exp $
3 ## ----------------------------------------------------------------------
4 ## $Log: Engine.pm,v $
5 ## Revision 1.3 2003/02/21 01:46:17 joko
6 ## renamed core function
7 ##
8 ## Revision 1.2 2003/02/20 19:46:33 joko
9 ## renamed and revamped some of modules
10 ## renamed methods
11 ## + sub run_executable
12 ##
13 ## Revision 1.1 2003/02/18 15:35:25 joko
14 ## + initial commit
15 ##
16 ## ----------------------------------------------------------------------
17
18
19 package Data::Rap::Engine;
20
21 use strict;
22 use warnings;
23
24
25 use Data::Dumper;
26 use Hash::Merge qw( merge );
27 use Iterate;
28
29 use shortcuts qw( run_cmd );
30 use Data::Code::Ref qw( ref_slot );
31 use Data::Transform::Deep qw( expand );
32
33
34 sub performTarget {
35 my $self = shift;
36 my $targetname = shift;
37 $self->perform_target($targetname);
38 }
39
40 sub perform_target {
41 my $self = shift;
42 my $targetname = shift;
43
44 my $header = ("- " x 12) . " " . $targetname . " " . ("- " x 6);
45
46 #$self->log("- " x 35, 'notice');
47 #$self->log("Performing Target '$targetname'.", 'notice');
48
49 $self->log($header, 'notice');
50
51 #exit;
52
53 my $target = $self->getTargetDetails($targetname);
54
55 # trace
56 #print Dumper($target);
57 #exit;
58
59 $self->perform_dependencies($target);
60 $self->perform_details($target);
61
62 }
63
64 sub perform_dependencies {
65 my $self = shift;
66 my $target = shift;
67 # resolve dependencies (just run prior)
68 if (my $targetname_dep = $target->{attrib}->{depends}) {
69 my @targets = split(/,\s|,/, $targetname_dep);
70 #print Dumper(@targets);
71 #$self->perform($targetname_dep);
72 #delete $target->{attrib}->{depends};
73 foreach (@targets) {
74 if (!$self->{__rap}->{dependencies}->{resolved}->{$_}++) {
75 $self->perform_target($_);
76 }
77 }
78 delete $target->{attrib}->{depends};
79 }
80 }
81
82 sub perform_details {
83 my $self = shift;
84 my $target = shift;
85
86 #print Dumper($target);
87 #exit;
88
89 foreach my $entry (@{$target->{content}}) {
90 my $command = $entry->{name};
91 my $args = $entry->{attrib};
92 $self->perform_command($command, $args);
93 # check recursiveness
94 if ($entry->{content} && ref $entry->{content}) {
95 $self->perform_details($entry);
96 }
97 }
98 }
99
100 sub rc {
101 my $self = shift;
102 return $self->perform_command(@_);
103 }
104
105 sub perform_command {
106 my $self = shift;
107 my $command = shift;
108
109 if (!$command) {
110 $self->log("Command was empty!", 'debug');
111 return;
112 }
113
114 # FIXME: make '__PACKAGE__' go one level deeper properly!
115 $self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug');
116
117 my $args_list = shift;
118 my $args = {};
119 #print Dumper($args_list);
120 if ($args_list) {
121 if (ref $args_list eq 'ARRAY') {
122 foreach (@$args_list) {
123 $args = merge($args, $_);
124 }
125 } else {
126 $args = $args_list;
127 }
128 }
129 $command = '_' . $command;
130
131
132 # determine container
133 my $container; # = $self->getInstance();
134 $container ||= $self;
135
136 if ($container->can($command)) {
137 $container->$command($args);
138 } else {
139 $self->log("Command '$command' not implemented.", "warning");
140 }
141
142 }
143
144
145 sub merge_properties {
146 my $self = shift;
147 my $name = shift;
148 my $data = shift;
149
150 $self->log("merge-name: $name");
151 #print "name: $name", "\n";
152 #print Dumper($data);
153 #exit;
154
155 # check if slot (or childs of it) is/are already occupied
156 #if (my $data_old = ref_slot($self, $name, undef, '.')) {
157 if (my $data_old = $self->get_property($name)) {
158 #print "old:", "\n";
159 #print Dumper($data_old);
160
161 # FIXME: review - any options for 'merge' here?
162 my $data_new = merge($data_old, $data);
163 #print "DATA NEE! - MERGE!", "\n";
164 #print Dumper($data_new);
165 #exit;
166 #merge_to($self, $data_new);
167 $self->set_property( { name => $name, value => $data_new } );
168
169 } else {
170
171 =pod
172 # make up a dummy hash matching the structure of the destination one
173 my $dummy = {};
174 ref_slot($dummy, $name, $data, '.');
175 print Dumper($dummy);
176
177 # mix into destination
178 mixin($self, $dummy, { init => 1 });
179 =cut
180
181 ref_slot($self, $name, $data, '.');
182
183 }
184
185
186 #print Dumper($self);
187 #exit;
188 #$self->
189
190 }
191
192 sub set_property {
193 my $self = shift;
194 my $args = shift;
195
196 $self->interpolate($args) ;
197 my $name = $args->{name};
198 my $value = $args->{value};
199 $name = '__rap.properties.' . $name;
200
201 $self->log("set-name: $name");
202
203 # fill property slot with given value
204 if ($value) {
205 ref_slot($self, $name, $value, '.');
206 }
207
208 #print Dumper($self);
209
210 # FIXME!!!
211 #return if ! ref $args;
212
213 # fill property slot with (probably bigger) data structure from property-/configuration-style - file
214 if (my $file = $args->{file}) {
215 my $type = $args->{type};
216 if (!$type) {
217 die("default file (no type specified) is not implemented yet!");
218
219 } elsif ($type eq 'perl-eval') {
220 #die($type);
221 $self->loadFromPerlFile($file, $name, $args->{'varnames'});
222
223 } elsif ($type eq 'App::Config') {
224 die("not implemented: $type");
225
226 }
227
228 } elsif (my $command = $args->{command}) {
229 $self->perform_command($command, $args);
230
231 }
232
233 }
234
235 sub get_property {
236 my $self = shift;
237 #my $args = shift;
238 my $name = shift;
239
240 #$self->interpolate($args);
241
242 #my $name = $args->{name};
243 my $result;
244
245 if (!$name) {
246 $self->log( __PACKAGE__ . ": no name!", 'critical');
247
248 } elsif ($name eq '/') {
249 $result = expand($self);
250
251 } elsif ($name eq '.') {
252 if (my $instance = $self->getInstance()) {
253 $result = expand($instance);
254 } else {
255 $result = ref_slot($self, '__rap.properties', undef, '.');
256
257 }
258
259 } else {
260
261 $name = '__rap.properties.' . $name;
262
263 $self->log("get-name: $name");
264
265 # get property slot and return value
266 $result = ref_slot($self, $name, undef, '.');
267
268 # FIXME: Is this okay? It's provided for now in order not
269 # to deliver an 'undef' to the regex below inside 'interpolate'.
270 # revamp this, maybe!
271 #$result ||= ''; # NO!!!
272 }
273
274 return $result;
275 }
276
277 sub interpolate {
278 my $self = shift;
279 my $ref = shift;
280 IterHash %$ref, sub {
281 #print $_[1], "\n";
282 $_[1] =~ s/\${(.+)}/$self->get_property($1)/e;
283 }
284 }
285
286 sub run_executable {
287 my $self = shift;
288 my $opts = shift;
289
290 if ($opts->{executable}) {
291
292 my $program = $opts->{executable};
293 delete $opts->{executable};
294
295 my @buf;
296 foreach (keys %$opts) {
297 my $value = $opts->{$_};
298 if (m/^_/) {
299 if ($_ eq '_switches') {
300 my @switches = split(/,\s|,/, $value);
301 foreach my $switch (@switches) {
302 push @buf, '--' . $switch;
303 }
304 }
305 next;
306 }
307
308 if ($value =~ /\s/) {
309 $value = "\"$value\"";
310 }
311 push @buf, "--$_=$value";
312 }
313
314 my $cmd = $program . ' ' . join(' ', @buf);
315
316 # trace
317 #print "command: $cmd", "\n";
318
319 # start process
320 # V1: via shortcut
321 #$ENV{PERL5LIB} = join(' ', @INC);
322
323 # FIXME!!! what about the other slots of @INC?
324 $ENV{PERL5LIB} = $INC[0];
325
326 #print Dumper(%ENV);
327 run_cmd($cmd);
328 # V2: via IPC::Run
329 # .... (TODO)
330
331
332 }
333
334 }
335
336
337 1;
338 __END__

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed