1 |
## ---------------------------------------------------------------------- |
2 |
## $Id: Command.pm,v 1.5 2003/03/27 15:31:04 joko Exp $ |
3 |
## ---------------------------------------------------------------------- |
4 |
## $Log: Command.pm,v $ |
5 |
## Revision 1.5 2003/03/27 15:31:04 joko |
6 |
## fixes to modules regarding new namespace(s) below Data::Mungle::* |
7 |
## |
8 |
## Revision 1.4 2003/02/22 16:48:58 joko |
9 |
## modified rapcall behaviour |
10 |
## |
11 |
## Revision 1.3 2003/02/21 07:39:13 joko |
12 |
## modified 'rapcall' processing |
13 |
## modified merging of options/arguments in there |
14 |
## |
15 |
## Revision 1.2 2003/02/20 19:37:09 joko |
16 |
## renamed modules |
17 |
## - removed command 'exec' |
18 |
## + enhanced command 'rapcall' - now handles various different modes and behaviours |
19 |
## renamed methods |
20 |
## + command 'exit' |
21 |
## + command 'use' - switch to foreign namespaces using contexts |
22 |
## |
23 |
## Revision 1.1 2003/02/18 15:35:06 joko |
24 |
## + initial commit |
25 |
## |
26 |
## ---------------------------------------------------------------------- |
27 |
|
28 |
|
29 |
package Data::Rap::Command; |
30 |
|
31 |
use strict; |
32 |
use warnings; |
33 |
|
34 |
|
35 |
use Data::Dumper; |
36 |
use Hash::Merge qw( merge ); |
37 |
|
38 |
use DesignPattern::Object; |
39 |
use Data::Mungle::Transform::Deep qw( merge_to ); |
40 |
use shortcuts qw( run_cmd ); |
41 |
|
42 |
|
43 |
sub _echo { |
44 |
my $self = shift; |
45 |
my $args = shift; |
46 |
#print Dumper($args); |
47 |
my $message; |
48 |
my $level; |
49 |
|
50 |
if (ref $args eq 'HASH') { |
51 |
$message = $args->{message}; |
52 |
$level = $args->{level}; |
53 |
} else { |
54 |
my @buf = (); |
55 |
push @buf, $args; |
56 |
push @buf, @_; |
57 |
$message = join(' ', @buf); |
58 |
} |
59 |
|
60 |
$message ||= 'n/a'; |
61 |
$level ||= 'info'; |
62 |
|
63 |
$self->log($message, $level); |
64 |
} |
65 |
|
66 |
sub _context { |
67 |
my $self = shift; |
68 |
my $args = shift; |
69 |
print Dumper($args); |
70 |
foreach my $task_command (keys %$args) { |
71 |
my $bunch = $args->{$task_command}; |
72 |
foreach my $task_args (@$bunch) { |
73 |
#print Dumper($task_args); |
74 |
$self->perform_command($task_command, $task_args); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
# FIXME |
80 |
sub _id {} |
81 |
|
82 |
# FIXME |
83 |
sub _description {} |
84 |
|
85 |
sub _container { |
86 |
my $self = shift; |
87 |
my $args = shift; |
88 |
$self->setContainer($args); |
89 |
} |
90 |
|
91 |
sub _sync { |
92 |
my $self = shift; |
93 |
my $args = shift; |
94 |
|
95 |
#print Dumper($args); |
96 |
|
97 |
# V1 |
98 |
#$bizProcess->runSync($task); |
99 |
|
100 |
#V2 |
101 |
my $container = $self->getInstance(); |
102 |
$container ||= $self; |
103 |
$container->runSync($args); |
104 |
|
105 |
} |
106 |
|
107 |
sub _depends_old { |
108 |
my $self = shift; |
109 |
my $args = shift; |
110 |
#print Dumper($args); |
111 |
$self->performTarget($args); |
112 |
} |
113 |
|
114 |
sub _dump { |
115 |
my $self = shift; |
116 |
my $args = shift; |
117 |
my $name = $args->{name}; |
118 |
my $message = Dumper($self->get_property($name)); |
119 |
$self->rc('echo', "\n" . $message, $args->{level}); |
120 |
} |
121 |
|
122 |
|
123 |
sub _plugin { |
124 |
my $self = shift; |
125 |
my $args = shift; |
126 |
|
127 |
if (my $instance = $args->{instance}) { |
128 |
#$self->{$instance} = DesignPattern::Object->fromPackage($args->{module}); |
129 |
my $object = DesignPattern::Object->fromPackage($args->{module}); |
130 |
#$self->{$instance} |
131 |
#print Dumper($object); |
132 |
#$object->constructor() if $object->can('constructor'); |
133 |
|
134 |
} elsif (my $name = $args->{name}) { |
135 |
|
136 |
# 1. namespaces - load module into current scope or foreign namespace? |
137 |
my $container; |
138 |
if (my $namespace = $args->{namespace}) { |
139 |
|
140 |
$self->log("Loading plugin '$name' into foreign namespace '$namespace'.", 'info'); |
141 |
|
142 |
eval qq{ |
143 |
|
144 |
# 0. pre-flight check|trace |
145 |
#print "YAI"; |
146 |
#$self->log('YAIYAI', 'info'); # FIXME! |
147 |
|
148 |
# 1. work in foreign namespace |
149 |
package $namespace; |
150 |
use strict; |
151 |
use warnings; |
152 |
# 2. mungle with/the |
153 |
# - package namespace (llp) |
154 |
# - object (lloo) |
155 |
# - inheritance (lloo) |
156 |
# - plugin (hlf) |
157 |
# symbols for scopes: |
158 |
# - llp = low-level perl |
159 |
# - lloo = low-level oo |
160 |
# - hlf = high-level framework |
161 |
# bless larry for perl having this implemented in an orthogonal way |
162 |
# 2.1: behaviour=coerce |
163 |
#use base qw( $name ); |
164 |
# 2.2: behaviour=plugin |
165 |
use base qw( DesignPattern::Bridge ); |
166 |
1; |
167 |
}; |
168 |
$self->checkExceptions(); |
169 |
$container = $namespace->new(); |
170 |
|
171 |
} elsif (my $instance = $self->getInstance()) { |
172 |
$self->log("Loading plugin '$name' into foreign namespace of instance '" . (ref $instance) . "'.", 'info'); |
173 |
$container = $instance; |
174 |
|
175 |
} else { |
176 |
#$self->log("Loading plugin '$name' into local scope's namespace (ref \$self='" . (ref $self) . "').", 'info'); |
177 |
$self->log("Loading plugin '$name' into local namespace '" . (ref $self) . "'.", 'info'); |
178 |
$container = $self; |
179 |
} |
180 |
|
181 |
# 1.b. intermediate checks |
182 |
if (!$container) { |
183 |
$self->log("No container to load '$name' into!", 'warning'); |
184 |
return; |
185 |
} |
186 |
|
187 |
# 1.c. inheritance of (e.g.) properties |
188 |
if (my $inherit = $args->{inherit}) { |
189 |
my $properties = $self->get_property($inherit); |
190 |
merge_to($container, { $inherit => $properties }, { init => 1 } ); |
191 |
} |
192 |
|
193 |
# 2. plugin - load module into container |
194 |
$container->load($name); |
195 |
|
196 |
# 3. methods - run object methods |
197 |
if (my $methods = $args->{methods}) { |
198 |
my @methods = split(/,|,\s/, $methods); |
199 |
foreach (@methods) { |
200 |
$self->log("Running method '$_'.", 'info'); |
201 |
$container->$_() if $container->can($_); |
202 |
} |
203 |
} |
204 |
|
205 |
# 4. instances - create references in local scope |
206 |
if (my $instances = $args->{instances}) { |
207 |
my @instances = split(/,|,\s/, $instances); |
208 |
foreach (@instances) { |
209 |
$self->log("Creating instance '$_'.", 'info'); |
210 |
$self->{$_} = $container; |
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
} |
216 |
} |
217 |
|
218 |
sub _rapcall { |
219 |
my $self = shift; |
220 |
my $args = shift; |
221 |
|
222 |
if (my $container = $self->getContainer()) { |
223 |
#my $opts = merge($container, $args); |
224 |
#print Dumper($container); |
225 |
merge_to($args, $container, { init => 1 }); |
226 |
} |
227 |
|
228 |
# trace |
229 |
#print Dumper($args); |
230 |
|
231 |
if ($args->{executable}) { |
232 |
$self->run_executable($args); |
233 |
return; |
234 |
} |
235 |
|
236 |
if (my $command = $args->{command}) { |
237 |
$self->perform_command($command, $args); |
238 |
return; |
239 |
} |
240 |
|
241 |
if (my $target = $args->{target}) { |
242 |
$self->performTarget($target, $args); |
243 |
return; |
244 |
} |
245 |
|
246 |
if (my $method = $args->{method}) { |
247 |
#$self->performTarget($target, $args); |
248 |
if (my $refkey = $args->{base}) { |
249 |
#eval($namespace . '::' . $method . '();'); |
250 |
#die($@) if $@; |
251 |
#print Dumper($self); |
252 |
$self->{$refkey}->$method(); |
253 |
|
254 |
} elsif (my $ref = $self->getInstance()) { |
255 |
$ref->$method(); |
256 |
} |
257 |
|
258 |
return; |
259 |
|
260 |
} |
261 |
|
262 |
} |
263 |
|
264 |
sub _exit { |
265 |
exit; |
266 |
} |
267 |
|
268 |
sub _use { |
269 |
my $self = shift; |
270 |
my $args = shift; |
271 |
my $name = $args->{name}; |
272 |
$self->log("Switching to '$name'.", 'info'); |
273 |
$self->setInstance($self->{$name}) if $args->{type} eq 'instance'; |
274 |
} |
275 |
|
276 |
sub _script { |
277 |
my $self = shift; |
278 |
my $args = shift; |
279 |
my $content = shift; |
280 |
|
281 |
# trace |
282 |
#print Dumper($args); |
283 |
#print Dumper($content); |
284 |
|
285 |
my $code = $content->[0]->{content}; |
286 |
$code ||= ''; |
287 |
|
288 |
$self->run_script($args, $code); |
289 |
|
290 |
} |
291 |
|
292 |
1; |
293 |
__END__ |