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
|
package UR::Namespace::Command::Show::Subclasses;
use strict;
use warnings;
use UR;
use YAML;
my $spacing = '';
class UR::Namespace::Command::Show::Subclasses {
is => 'Command::V2',
has=> [
superclass => {
is => 'Text',
shell_args_position => 1,
doc => 'Only show subclasses of this class.',
},
color => {
is => 'Boolean',
is_optional => 1,
default_value => 1,
doc => 'Display in color.',
},
maximum_depth => {
is => 'Int',
is_optional => 1,
default_value => -1,
doc => 'Maximum subclass depth. Negative means infinite.',
},
recalculate => {
is => 'Boolean',
is_optional => 1,
default_value => 0,
doc => 'Recreate the cache instead of using the results of a previous run.',
},
flat => {
is => 'Boolean',
is_optional => 1,
doc => 'Simply prints the subclass names with no other formatting or coloring.',
}
],
doc => 'Display subclasses of a given class.',
};
sub help_synopsis {
my $self = shift;
my $result .= <<EOP;
Displays a tree of subclasses of a given class.
EOP
return $result;
}
sub help_detail {
my $self = shift;
my $result .= <<EOP;
Displays a tree containing the names (and optionally other info) of the subclasses
of a given class.
ur show subclasses <class_name>
ur show subclasses <class_filename>
EOP
return $result;
}
sub _mine_tree_for_class {
my ($tree, $name, $result) = @_;
if(ref($tree) eq 'HASH') {
for my $key (keys %{$tree}) {
if($key eq $name) {
push(@{$result}, 1);
} else {
_mine_tree_for_class($tree->{$key}, $name, $result);
}
}
} elsif(ref($tree) eq 'ARRAY') {
for my $item (@{$tree}) {
if($item eq $name) {
push(@{$result}, 1);
}
_mine_tree_for_class($item, $name, $result);
}
}
return;
}
sub execute {
my ($self) = @_;
my $indexfile = '/tmp/.ur_class_index';
my $subclass_index_ref;
if($self->recalculate or (not -e $indexfile)) {
my $test_use_cmd = UR::Namespace::Command::Test::Use->create();
$test_use_cmd->execute();
$subclass_index_ref = {};
create_subclass_index('UR::Object', $subclass_index_ref);
my %subclass_index = %{$subclass_index_ref};
open(my $output_fh, '>', $indexfile);
for my $key (keys %subclass_index) {
print $output_fh sprintf("%s %s\n", $key,
join("\t", @{$subclass_index{$key}}));
}
close($output_fh);
} else {
$subclass_index_ref = parse_subclass_index_file($indexfile);
}
# check to see if superclass is even in the subclass_index
my @result;
_mine_tree_for_class($subclass_index_ref, $self->superclass, \@result);
unless(@result) {
my $class_name = $self->color ?
Term::ANSIColor::colored($self->superclass, 'red') :
$self->superclass;
printf "%s is not a valid class, check your spelling or " .
"see --help (recalculate).\n", $class_name;
return;
}
if($self->flat) {
$self->display_subclasses_flat($subclass_index_ref,
$self->superclass, 0)
} else {
$self->display_subclasses($subclass_index_ref,
$self->superclass, '', ' ', 0);
}
return 1;
}
sub create_subclass_index {
my ($seed, $index_ref) = @_;
my @children = $seed->__meta__->subclasses_loaded;
for my $child (@children) {
my @parents = @{$child->__meta__->{is}};
for my $parent (@parents) {
if($index_ref->{$parent}) {
push(@{$index_ref->{$parent}}, $child);
} else {
$index_ref->{$parent} = [$child];
}
}
}
}
sub parse_subclass_index_file {
my ($indexfile) = @_;
open(IN, '<', $indexfile);
my %index;
while(my $line = <IN>) {
chomp($line);
if($line) {
my ($parent, $rest) = split(/ /, $line);
if($rest) {
my @children = split('\t', $rest);
$index{$parent} = \@children;
} else {
$index{$parent} = [];
}
}
}
return \%index
}
sub display_subclasses_flat {
my ($self, $index_ref, $name, $depth) = @_;
my $maximum_depth = $self->maximum_depth;
if($depth == $maximum_depth + 1 and $maximum_depth != -1) {
return;
}
print "$name\n";
# get the children
my $children_ref = $index_ref->{$name};
my @children;
if($children_ref) {
@children = @{$index_ref->{$name}};
} else { # if it isn't in index it has no children.
@children = ();
}
# loop over children
for my $child (@children) {
$self->display_subclasses_flat($index_ref, $child, $depth+1);
}
}
sub display_subclasses {
my ($self, $index_ref, $name, $global_prefix, $personal_prefix, $depth) = @_;
my $maximum_depth = $self->maximum_depth;
my ($dgp, $dpp, $dn) = ($global_prefix, $personal_prefix, $name);
if($self->color) {
($dgp, $dpp, $dn) = colorize_output($global_prefix, $personal_prefix,
$name, $self->superclass);
}
print join('', $dgp, $dpp, $spacing, $dn);
my $o = ($personal_prefix =~ /^\|/ ) ? '|' : ' ';
my $child_global_prefix = sprintf("%s%s %s", $global_prefix, $o, $spacing);
# get the children
my $children_ref = $index_ref->{$name};
my @children;
if($children_ref) {
@children = @{$index_ref->{$name}};
} else { # if it isn't in index it has no children.
@children = ();
}
# loop over children
my $len_children = scalar(@children);
if($len_children and $depth == $maximum_depth and $maximum_depth != -1) {
print " ...\n";
return;
}
print "\n";
my $i = 1;
for my $child (@children) {
my $child_personal_prefix = ($len_children == $i) ? '`-' : '|-';
$self->display_subclasses($index_ref, $child, $child_global_prefix,
$child_personal_prefix, $depth+1);
$i += 1;
}
}
sub colorize_output {
my ($global_prefix, $personal_prefix, $name, $superclass) = @_;
my $dgp = Term::ANSIColor::colored($global_prefix, 'white');
my $dpp = Term::ANSIColor::colored($personal_prefix, 'white');
my $name_prefix = $name;
if($name_prefix =~ /^($superclass)/) {
$name_prefix = $superclass;
} else {
$name_prefix = '';
}
my $name_suffix = $name;
$name_suffix =~ s/^($superclass)//;
my $dn = sprintf("%s%s", Term::ANSIColor::colored($name_prefix, 'white'), $name_suffix );
return ($dgp, $dpp, $dn);
}
1;
|