| 1 | package Command::DynamicSubCommands; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | |||
| 7 | class Command::DynamicSubCommands { | ||
| 8 | is => 'Command', | ||
| 9 | is_abstract => 1, | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub _init_subclass { | ||
| 13 | my $subclass = shift; | ||
| 14 | my $meta = $subclass->__meta__; | ||
| 15 | if (grep { $_ eq __PACKAGE__ } $meta->parent_class_names) { | ||
| 16 | my $delegating_class_name = $subclass; | ||
| 17 | eval "sub ${subclass}::_delegating_class_name { '$delegating_class_name' }"; | ||
| 18 | } | ||
| 19 | return 1; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub __extend_namespace__ { | ||
| 23 | # auto generate sub-classes at the time of first reference | ||
| 24 | my ($self,$ext) = @_; | ||
| 25 | |||
| 26 | my $meta = $self->SUPER::__extend_namespace__($ext); | ||
| 27 | return $meta if $meta; | ||
| 28 | |||
| 29 | unless ($self->can('_sub_commands_from')) { | ||
| 30 | die "Class " . $self->class . " does not implement _sub_commands_from()!\n" | ||
| 31 | . "This method should return the namespace to use a reference " | ||
| 32 | . "for defining sub-commands." | ||
| 33 | } | ||
| 34 | my $ref_class = $self->_sub_commands_from; | ||
| 35 | my $target_class_name = join('::', $ref_class, $ext); | ||
| 36 | my $target_class_meta = UR::Object::Type->get($target_class_name); | ||
| 37 | if ($target_class_meta and $target_class_name->isa($ref_class)) { | ||
| 38 | my $subclass_name = join('::', $self->class, $ext); | ||
| 39 | my $subclass = $self->_build_sub_command($subclass_name, $self->class, $target_class_name); | ||
| 40 | |||
| 41 | my $meta = $subclass->__meta__; | ||
| 42 | return $meta; | ||
| 43 | } | ||
| 44 | |||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _build_all_sub_commands { | ||
| 49 | my ($class) = @_; | ||
| 50 | |||
| 51 | unless ($class->can('_sub_commands_from')) { | ||
| 52 | die "Class $class does not implement _sub_commands_from()!\n" | ||
| 53 | . "This method should return the namespace to use a reference " | ||
| 54 | . "for defining sub-commands." | ||
| 55 | } | ||
| 56 | my $ref_class = $class->_sub_commands_from; | ||
| 57 | |||
| 58 | my $delegating_class_name = $class; | ||
| 59 | |||
| 60 | my $module = $ref_class; | ||
| 61 | $module =~ s/::/\//g; | ||
| 62 | $module .= '.pm'; | ||
| 63 | my $base_path = $INC{$module}; | ||
| 64 | unless ($base_path) { | ||
| 65 | if (UR::Object::Type->get($ref_class)) { | ||
| 66 | $base_path = $INC{$module}; | ||
| 67 | } | ||
| 68 | unless ($base_path) { | ||
| 69 | die "Failed to find the path for ref class $ref_class!"; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | $base_path =~ s/$module//; | ||
| 73 | |||
| 74 | my $ref_path = $ref_class; | ||
| 75 | $ref_path =~ s/::/\//g; | ||
| 76 | |||
| 77 | my $full_ref_path = $base_path . '/' . $ref_path; | ||
| 78 | |||
| 79 | my @target_paths = glob("$full_ref_path/*.pm"); | ||
| 80 | |||
| 81 | my @target_class_names; | ||
| 82 | for my $target_path (@target_paths) { | ||
| 83 | my $target = $target_path; | ||
| 84 | $target =~ s#$base_path\/$ref_path/##; | ||
| 85 | $target =~ s/\.pm//; | ||
| 86 | my $target_class_name = $ref_class . '::' . $target; | ||
| 87 | my $target_meta = UR::Object::Type->get($target_class_name); | ||
| 88 | next unless $target_meta; | ||
| 89 | next unless $target_class_name->isa($ref_class); | ||
| 90 | push @target_class_names, $target => $target_class_name; | ||
| 91 | } | ||
| 92 | my %target_classes = @target_class_names; | ||
| 93 | |||
| 94 | my @subclasses; | ||
| 95 | for my $target (sort keys %target_classes) { | ||
| 96 | my $target_class_name = $target_classes{$target}; | ||
| 97 | my $class_name = $delegating_class_name . '::' . $target; | ||
| 98 | |||
| 99 | # skip commands which have a module | ||
| 100 | my $module_name = $class_name; | ||
| 101 | $module_name =~ s|::|/|g; | ||
| 102 | $module_name .= '.pm'; | ||
| 103 | |||
| 104 | if (my @matches = grep { -e $_ . '/' . $module_name } @INC) { | ||
| 105 | my $c = UR::Object::Type->get($class_name); | ||
| 106 | push @subclasses, $class_name; | ||
| 107 | next; | ||
| 108 | } | ||
| 109 | |||
| 110 | my @new_class_names = $class->_build_sub_command($class_name,$delegating_class_name,$target_class_name); | ||
| 111 | for my $new_class_name (@new_class_names) { | ||
| 112 | eval "sub ${new_class_name}::_target_class_name { '$target_class_name' }"; | ||
| 113 | push @subclasses, $new_class_name; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | return @subclasses; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _build_sub_command { | ||
| 121 | my ($self,$class_name,$delegating_class_name,$reference_class_name) = @_; | ||
| 122 | class {$class_name} { | ||
| 123 | is => $delegating_class_name, | ||
| 124 | doc => '', | ||
| 125 | }; | ||
| 126 | return $class_name; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub sub_command_dirs { | ||
| 130 | my $class = ref($_[0]) || $_[0]; | ||
| 131 | return ( $class eq $class->_delegating_class_name ? 1 : 0 ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub sub_command_classes { | ||
| 135 | my $class = shift; | ||
| 136 | |||
| 137 | unless(exists $class->__meta__->{_sub_commands}) { | ||
| 138 | my @subclasses = $class->_build_all_sub_commands; | ||
| 139 | $class->__meta__->{_sub_commands} = \@subclasses; | ||
| 140 | } | ||
| 141 | |||
| 142 | return @{ $class->__meta__->{_sub_commands} }; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _target_class_name { undef } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | =pod | ||
| 150 | |||
| 151 | =head1 NAME | ||
| 152 | |||
| 153 | Command::DynamicSubCommands - auto-generate sub-commands based on other classes | ||
| 154 | |||
| 155 | =head1 SYNOPSIS | ||
| 156 | |||
| 157 | # given that these classes exist: | ||
| 158 | # Acme::Task::Foo | ||
| 159 | # Acme::Task::Bar | ||
| 160 | |||
| 161 | # in Acme/Worker/Command/DoTask.pm: | ||
| 162 | |||
| 163 | class Acme::Worker::Command::DoTask { | ||
| 164 | is => 'Command::DynamicSubCommands', | ||
| 165 | has => [ | ||
| 166 | param1 => { is => 'Text' }, | ||
| 167 | param2 => { is => 'Text' }, | ||
| 168 | ] | ||
| 169 | }; | ||
| 170 | |||
| 171 | sub _sub_commands_from { 'Acme::Task' } | ||
| 172 | |||
| 173 | sub execute { | ||
| 174 | my $self = shift; | ||
| 175 | print "this command " . ref($self) . " applies to " . $self->_target_class_name; | ||
| 176 | return 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | # the class above will discover them at compile, | ||
| 180 | # and auto-generate these subclasses of itself: | ||
| 181 | # Acme::Worker::Command::DoTask::Foo | ||
| 182 | # Acme::Worker::Command::DoTask::Bar | ||
| 183 | |||
| 184 | # in the shell... | ||
| 185 | # | ||
| 186 | # $ acme worker do-task | ||
| 187 | # foo | ||
| 188 | # bar | ||
| 189 | # | ||
| 190 | # $ acme worker do-task foo --param1 aaa --param2 bbb | ||
| 191 | # this command Acme::Worker::Command::DoTask::Foo applies to Acme::Task::Foo | ||
| 192 | # | ||
| 193 | # $ acme worker do-task bar --param1 ccc --param2 ddd | ||
| 194 | # this command Acme::Worker::Command::DoTask::Bar applies to Acme::Task::Bar | ||
| 195 | |||
| 196 | =head1 DESCRIPTION | ||
| 197 | |||
| 198 | This module helps you avoid writing boilerplate commands. | ||
| 199 | |||
| 200 | When a command has a set of sub-commands which are meant to be derived from another | ||
| 201 | group of classes, this module lets you auto-generate those sub-commands at run | ||
| 202 | time. | ||
| 203 | |||
| 204 | =head1 REQUIRED ABSTRACT METHOD | ||
| 205 | |||
| 206 | =over 4 | ||
| 207 | |||
| 208 | =item _sub_commands_from | ||
| 209 | |||
| 210 | $base_namespace = Acme::Order::Command->_sub_commands_from(); | ||
| 211 | # 'Acme::Task | ||
| 212 | |||
| 213 | Returns the namespace from which target classes will be discovered, and | ||
| 214 | sub-commands will be generated. | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =head1 PRIVATE API | ||
| 219 | |||
| 220 | =over 4 | ||
| 221 | |||
| 222 | =item _target_class_name | ||
| 223 | |||
| 224 | $c= Acme::Order::Command::Purchasing->_target_class_name; | ||
| 225 | # 'Acme::Task::Foo' | ||
| 226 | |||
| 227 | The name of some class under the _sub_commands_from() namespace. | ||
| 228 | This value is set during execute, revealing which sub-command the caller is using. | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =head1 OPTIONAL OVERRIDES | ||
| 233 | |||
| 234 | =over 4 | ||
| 235 | |||
| 236 | =item _build_sub_commmand | ||
| 237 | |||
| 238 | This can be overridden to customize the sub-command construction. | ||
| 239 | By default, each target under _sub_commands_from will result in | ||
| 240 | a call to this method. The default implementation is below: | ||
| 241 | |||
| 242 | my $self = shift; | ||
| 243 | my ($suggested_class_name,$delegator_class_name,$target_class_name) = @_; | ||
| 244 | |||
| 245 | class {$suggested_class_name} { | ||
| 246 | is => $delegator_class_name, | ||
| 247 | sub_classify_by => 'class', | ||
| 248 | has_constant => [ | ||
| 249 | _target_class_name => { value => $target_class_name }, | ||
| 250 | ] | ||
| 251 | }; | ||
| 252 | |||
| 253 | return ($suggested_class_name); | ||
| 254 | |||
| 255 | Note that the class in question may be on the filesystem, and not need | ||
| 256 | to be created. The return list can include more than one class name, | ||
| 257 | or zero class names. | ||
| 258 | |||
| 259 | =item _build_all_sub_commands | ||
| 260 | |||
| 261 | This is called once for any class which inherits from Command::DynamicSubCommands. | ||
| 262 | |||
| 263 | It generates the sub-commands as needed, and returns a list. | ||
| 264 | |||
| 265 | By default it resolves the target classes, and calls _build_sub_command | ||
| 266 | |||
| 267 | It can be overridden to customize behavior, or filter results. Be sure | ||
| 268 | to call @cmds = $self->SUPER::_build_all_sub_commands() if you want | ||
| 269 | to get the default commands in addition to overriding. | ||
| 270 | |||
| 271 | =back | ||
| 272 | |||
| 273 | The sub-commands need not be 1:1 with the target classes, though this is the default. | ||
| 274 | |||
| 275 | The sub-commands need not inherit from the Command::DynamicSubCommands base command | ||
| 276 | which generates them, though this is the default. | ||
| 277 | |||
| 278 | |||
| 279 | =cut | ||
| 280 |