This file is indexed.

/usr/share/perl5/Command/DynamicSubCommands.pm is in libur-perl 0.440-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package Command::DynamicSubCommands;

use strict;
use warnings;
use UR;

class Command::DynamicSubCommands {
    is => 'Command',
    is_abstract => 1,
};

sub _init_subclass {
    my $subclass = shift;
    my $meta = $subclass->__meta__;
    if (grep { $_ eq __PACKAGE__ } $meta->parent_class_names) {
        my $delegating_class_name = $subclass;
        eval "sub ${subclass}::_delegating_class_name { '$delegating_class_name' }";
    }
    return 1;
}

sub __extend_namespace__ {
    # auto generate sub-classes at the time of first reference
    my ($self,$ext) = @_;

    my $meta = $self->SUPER::__extend_namespace__($ext);
    return $meta if $meta;

    unless ($self->can('_sub_commands_from')) {
        die "Class " . $self->class . " does not implement _sub_commands_from()!\n"
            . "This method should return the namespace to use a reference "
            . "for defining sub-commands."
    }
    my $ref_class = $self->_sub_commands_from;
    my $target_class_name = join('::', $ref_class, $ext);
    my $target_class_meta = UR::Object::Type->get($target_class_name);
    if ($target_class_meta and $target_class_name->isa($ref_class)) {
        my $subclass_name = join('::', $self->class, $ext);
        my $subclass = $self->_build_sub_command($subclass_name, $self->class, $target_class_name);

        my $meta = $subclass->__meta__;
        return $meta;
    }

    return;
}

sub _build_all_sub_commands {
    my ($class) = @_;

    unless ($class->can('_sub_commands_from')) {
        die "Class $class does not implement _sub_commands_from()!\n"
            . "This method should return the namespace to use a reference "
            . "for defining sub-commands."
    }
    my $ref_class = $class->_sub_commands_from;

    my $delegating_class_name = $class;

    my $module = $ref_class;
    $module =~ s/::/\//g;
    $module .= '.pm';
    my $base_path = $INC{$module};
    unless ($base_path) {
        if (UR::Object::Type->get($ref_class)) {
            $base_path = $INC{$module};
        }
        unless ($base_path) {
           die "Failed to find the path for ref class $ref_class!"; 
        }
    }
    $base_path =~ s/$module//;

    my $ref_path = $ref_class;
    $ref_path =~ s/::/\//g;

    my $full_ref_path = $base_path . '/' . $ref_path;

    my @target_paths = glob("$full_ref_path/*.pm");

    my @target_class_names;
    for my $target_path (@target_paths) {
        my $target = $target_path;
        $target =~ s#$base_path\/$ref_path/##;
        $target =~ s/\.pm//;
        my $target_class_name = $ref_class . '::' . $target;
        my $target_meta = UR::Object::Type->get($target_class_name);
        next unless $target_meta;
        next unless $target_class_name->isa($ref_class);
        push @target_class_names, $target => $target_class_name;
    }
    my %target_classes = @target_class_names;

    my @subclasses;
    for my $target (sort keys %target_classes) {
        my $target_class_name = $target_classes{$target};
        my $class_name = $delegating_class_name . '::' . $target;

        # skip commands which have a module
        my $module_name = $class_name;
        $module_name =~ s|::|/|g;
        $module_name .= '.pm';

        if (my @matches = grep { -e $_ . '/' . $module_name } @INC) {
            my $c = UR::Object::Type->get($class_name);
            push @subclasses, $class_name;
            next;
        }

        my @new_class_names = $class->_build_sub_command($class_name,$delegating_class_name,$target_class_name);
        for my $new_class_name (@new_class_names) {
            eval "sub ${new_class_name}::_target_class_name { '$target_class_name' }";
            push @subclasses, $new_class_name;
        }
    }

    return @subclasses;
}

sub _build_sub_command {
    my ($self,$class_name,$delegating_class_name,$reference_class_name) = @_;
    class {$class_name} { 
        is => $delegating_class_name, 
        doc => '',
    };
    return $class_name;
}

sub sub_command_dirs {
    my $class = ref($_[0]) || $_[0];
    return ( $class eq $class->_delegating_class_name ? 1 : 0 );
}

sub sub_command_classes {
    my $class = shift;

    unless(exists $class->__meta__->{_sub_commands}) {
        my @subclasses = $class->_build_all_sub_commands;
        $class->__meta__->{_sub_commands} = \@subclasses;
    }

    return @{ $class->__meta__->{_sub_commands} };
}

sub _target_class_name { undef }

1;

=pod

=head1 NAME

Command::DynamicSubCommands - auto-generate sub-commands based on other classes

=head1 SYNOPSIS

 # given that these classes exist:
 #   Acme::Task::Foo
 #   Acme::Task::Bar
 
 # in Acme/Worker/Command/DoTask.pm:

    class Acme::Worker::Command::DoTask {
        is => 'Command::DynamicSubCommands',
        has => [
            param1 => { is => 'Text' },
            param2 => { is => 'Text' },
        ]
    };

    sub _sub_commands_from { 'Acme::Task' }

    sub execute {
        my $self = shift;
        print "this command " . ref($self) . " applies to " . $self->_target_class_name;
        return 1;
    }

 # the class above will discover them at compile, 
 # and auto-generate these subclasses of itself:
 #   Acme::Worker::Command::DoTask::Foo
 #   Acme::Worker::Command::DoTask::Bar
 
 # in the shell...
 #
 #   $ acme worker do-task
 #   foo
 #   bar
 #
 #   $ acme worker do-task foo --param1 aaa --param2 bbb 
 #   this command Acme::Worker::Command::DoTask::Foo applies to Acme::Task::Foo
 #
 #   $ acme worker do-task bar --param1 ccc --param2 ddd
 #   this command Acme::Worker::Command::DoTask::Bar applies to Acme::Task::Bar

=head1 DESCRIPTION

This module helps you avoid writing boilerplate commands.

When a command has a set of sub-commands which are meant to be derived from another 
group of classes, this module lets you auto-generate those sub-commands at run 
time.

=head1 REQUIRED ABSTRACT METHOD

=over 4

=item _sub_commands_from 

    $base_namespace = Acme::Order::Command->_sub_commands_from();
    # 'Acme::Task

    Returns the namespace from which target classes will be discovered, and
    sub-commands will be generated.

=back

=head1 PRIVATE API

=over 4

=item _target_class_name

    $c= Acme::Order::Command::Purchasing->_target_class_name;
    # 'Acme::Task::Foo'

    The name of some class under the _sub_commands_from() namespace.
    This value is set during execute, revealing which sub-command the caller is using. 

=back

=head1 OPTIONAL OVERRIDES

=over 4

=item _build_sub_commmand

    This can be overridden to customize the sub-command construction.
    By default, each target under _sub_commands_from will result in 
    a call to this method.  The default implementation is below:

    my $self = shift;
    my ($suggested_class_name,$delegator_class_name,$target_class_name) = @_;
    
    class {$suggested_class_name} { 
        is => $delegator_class_name, 
        sub_classify_by => 'class',
        has_constant => [
            _target_class_name => { value => $target_class_name },
        ]
    };

    return ($suggested_class_name);
    
    Note that the class in question may be on the filesystem, and not need
    to be created.  The return list can include more than one class name,
    or zero class names.

=item _build_all_sub_commands 

    This is called once for any class which inherits from Command::DynamicSubCommands.

    It generates the sub-commands as needed, and returns a list.

    By default it resolves the target classes, and calls  _build_sub_command

    It can be overridden to customize behavior, or filter results.  Be sure
    to call @cmds = $self->SUPER::_build_all_sub_commands() if you want 
    to get the default commands in addition to overriding.

=back 

The sub-commands need not be 1:1 with the target classes, though this is the default.

The sub-commands need not inherit from the Command::DynamicSubCommands base command
which generates them, though this is the default.


=cut