| 1 | package Command::SubCommandFactory; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | |||
| 7 | class Command::SubCommandFactory { | ||
| 8 | is => 'Command::Tree', | ||
| 9 | is_abstract => 1, | ||
| 10 | doc => 'Base class for commands that delegate to sub-commands that may need to be dynamically created', | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub _init_subclass { | ||
| 14 | my $subclass = shift; | ||
| 15 | my $meta = $subclass->__meta__; | ||
| 16 | if (grep { $_ eq __PACKAGE__ } $meta->parent_class_names) { | ||
| 17 | my $delegating_class_name = $subclass; | ||
| 18 | eval "sub ${subclass}::_delegating_class_name { '$delegating_class_name' }"; | ||
| 19 | } | ||
| 20 | |||
| 21 | return 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _build_sub_command_mapping { | ||
| 25 | my ($class) = @_; | ||
| 26 | |||
| 27 | unless ($class->can('_sub_commands_from')) { | ||
| 28 | die "Class $class does not implement _sub_commands_from()!\n" | ||
| 29 | . "This method should return the namespace to use a reference " | ||
| 30 | . "for defining sub-commands." | ||
| 31 | } | ||
| 32 | my $ref_class = $class->_sub_commands_from; | ||
| 33 | |||
| 34 | my @inheritance; | ||
| 35 | if ($class->can('_sub_commands_inherit_from') and defined $class->_sub_commands_inherit_from) { | ||
| 36 | @inheritance = $class->_sub_commands_inherit_from(); | ||
| 37 | } | ||
| 38 | else { | ||
| 39 | @inheritance = $class; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $module = $ref_class; | ||
| 43 | $module =~ s/::/\//g; | ||
| 44 | $module .= '.pm'; | ||
| 45 | my $base_path = $INC{$module}; | ||
| 46 | unless ($base_path) { | ||
| 47 | if (UR::Object::Type->get($ref_class)) { | ||
| 48 | $base_path = $INC{$module}; | ||
| 49 | } | ||
| 50 | unless ($base_path) { | ||
| 51 | die "Failed to find the path for ref class $ref_class!"; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | $base_path =~ s/$module//; | ||
| 55 | |||
| 56 | my $ref_path = $ref_class; | ||
| 57 | $ref_path =~ s/::/\//g; | ||
| 58 | my $full_ref_path = $base_path . '/' . $ref_path; | ||
| 59 | |||
| 60 | my @target_paths = glob("$full_ref_path/*.pm"); | ||
| 61 | my @target_class_names; | ||
| 62 | for my $target_path (@target_paths) { | ||
| 63 | my $target = $target_path; | ||
| 64 | $target =~ s#$base_path\/$ref_path/##; | ||
| 65 | $target =~ s/\.pm//; | ||
| 66 | |||
| 67 | my $target_base_class = $class->_target_base_class; | ||
| 68 | my $target_class_name = $target_base_class . '::' . $target; | ||
| 69 | |||
| 70 | my $target_meta = UR::Object::Type->get($target_class_name); | ||
| 71 | next unless $target_meta; | ||
| 72 | next unless $target_class_name->isa($target_base_class); | ||
| 73 | |||
| 74 | push @target_class_names, $target => $target_class_name; | ||
| 75 | } | ||
| 76 | my %target_classes = @target_class_names; | ||
| 77 | |||
| 78 | # Create a mapping of command names to command classes, and either find or | ||
| 79 | # create those command classes | ||
| 80 | my $mapping; | ||
| 81 | for my $target (sort keys %target_classes) { | ||
| 82 | my $target_class_name = $target_classes{$target}; | ||
| 83 | |||
| 84 | my $command_class_name = $class . '::' . $target; | ||
| 85 | my $command_module_name = $command_class_name; | ||
| 86 | $command_module_name =~ s|::|/|g; | ||
| 87 | $command_module_name .= '.pm'; | ||
| 88 | |||
| 89 | # If the command class already exists, load it. Otherwise, create one. | ||
| 90 | if (grep { -e $_ . '/' . $command_module_name } @INC) { | ||
| 91 | UR::Object::Type->get($command_class_name); | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | next if not $class->_build_sub_command($command_class_name, @inheritance); | ||
| 95 | } | ||
| 96 | |||
| 97 | # Created commands need to know where their parameters came from | ||
| 98 | no warnings 'redefine'; | ||
| 99 | eval "sub ${command_class_name}::_target_class_name { '$target_class_name' }"; | ||
| 100 | use warnings; | ||
| 101 | |||
| 102 | my $command_name = $class->_command_name_for_class_word($target); | ||
| 103 | $mapping->{$command_name} = $command_class_name; | ||
| 104 | } | ||
| 105 | |||
| 106 | return $mapping; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _build_sub_command { | ||
| 110 | my ($self, $class_name, @inheritance) = @_; | ||
| 111 | class {$class_name} { | ||
| 112 | is => \@inheritance, | ||
| 113 | doc => '', | ||
| 114 | }; | ||
| 115 | return $class_name; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _target_base_class { return $_[0]->_sub_commands_from; } | ||
| 119 | sub _target_class_name { undef } | ||
| 120 | sub _sub_commands_inherit_from { undef } | ||
| 121 | |||
| 122 | 1; | ||
| 123 |