| 1 | package UR::Namespace::Command::Show::Subclasses; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | use YAML; | ||
| 7 | |||
| 8 | my $spacing = ''; | ||
| 9 | |||
| 10 | class UR::Namespace::Command::Show::Subclasses { | ||
| 11 | is => 'Command::V2', | ||
| 12 | has=> [ | ||
| 13 | superclass => { | ||
| 14 | is => 'Text', | ||
| 15 | shell_args_position => 1, | ||
| 16 | doc => 'Only show subclasses of this class.', | ||
| 17 | }, | ||
| 18 | color => { | ||
| 19 | is => 'Boolean', | ||
| 20 | is_optional => 1, | ||
| 21 | default_value => 1, | ||
| 22 | doc => 'Display in color.', | ||
| 23 | }, | ||
| 24 | maximum_depth => { | ||
| 25 | is => 'Int', | ||
| 26 | is_optional => 1, | ||
| 27 | default_value => -1, | ||
| 28 | doc => 'Maximum subclass depth. Negative means infinite.', | ||
| 29 | }, | ||
| 30 | recalculate => { | ||
| 31 | is => 'Boolean', | ||
| 32 | is_optional => 1, | ||
| 33 | default_value => 0, | ||
| 34 | doc => 'Recreate the cache instead of using the results of a previous run.', | ||
| 35 | }, | ||
| 36 | flat => { | ||
| 37 | is => 'Boolean', | ||
| 38 | is_optional => 1, | ||
| 39 | doc => 'Simply prints the subclass names with no other formatting or coloring.', | ||
| 40 | } | ||
| 41 | ], | ||
| 42 | doc => 'Display subclasses of a given class.', | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub help_synopsis { | ||
| 46 | my $self = shift; | ||
| 47 | my $result .= <<EOP; | ||
| 48 | Displays a tree of subclasses of a given class. | ||
| 49 | EOP | ||
| 50 | return $result; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub help_detail { | ||
| 54 | my $self = shift; | ||
| 55 | my $result .= <<EOP; | ||
| 56 | Displays a tree containing the names (and optionally other info) of the subclasses | ||
| 57 | of a given class. | ||
| 58 | |||
| 59 | ur show subclasses <class_name> | ||
| 60 | ur show subclasses <class_filename> | ||
| 61 | |||
| 62 | EOP | ||
| 63 | return $result; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _mine_tree_for_class { | ||
| 67 | my ($tree, $name, $result) = @_; | ||
| 68 | |||
| 69 | if(ref($tree) eq 'HASH') { | ||
| 70 | for my $key (keys %{$tree}) { | ||
| 71 | if($key eq $name) { | ||
| 72 | push(@{$result}, 1); | ||
| 73 | } else { | ||
| 74 | _mine_tree_for_class($tree->{$key}, $name, $result); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } elsif(ref($tree) eq 'ARRAY') { | ||
| 78 | for my $item (@{$tree}) { | ||
| 79 | if($item eq $name) { | ||
| 80 | push(@{$result}, 1); | ||
| 81 | } | ||
| 82 | _mine_tree_for_class($item, $name, $result); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub execute { | ||
| 89 | my ($self) = @_; | ||
| 90 | my $indexfile = '/tmp/.ur_class_index'; | ||
| 91 | |||
| 92 | my $subclass_index_ref; | ||
| 93 | if($self->recalculate or (not -e $indexfile)) { | ||
| 94 | my $test_use_cmd = UR::Namespace::Command::Test::Use->create(); | ||
| 95 | $test_use_cmd->execute(); | ||
| 96 | |||
| 97 | $subclass_index_ref = {}; | ||
| 98 | create_subclass_index('UR::Object', $subclass_index_ref); | ||
| 99 | |||
| 100 | my %subclass_index = %{$subclass_index_ref}; | ||
| 101 | open(my $output_fh, '>', $indexfile); | ||
| 102 | for my $key (keys %subclass_index) { | ||
| 103 | print $output_fh sprintf("%s %s\n", $key, | ||
| 104 | join("\t", @{$subclass_index{$key}})); | ||
| 105 | } | ||
| 106 | close($output_fh); | ||
| 107 | } else { | ||
| 108 | $subclass_index_ref = parse_subclass_index_file($indexfile); | ||
| 109 | } | ||
| 110 | |||
| 111 | # check to see if superclass is even in the subclass_index | ||
| 112 | my @result; | ||
| 113 | _mine_tree_for_class($subclass_index_ref, $self->superclass, \@result); | ||
| 114 | unless(@result) { | ||
| 115 | my $class_name = $self->color ? | ||
| 116 | Term::ANSIColor::colored($self->superclass, 'red') : | ||
| 117 | $self->superclass; | ||
| 118 | printf "%s is not a valid class, check your spelling or " . | ||
| 119 | "see --help (recalculate).\n", $class_name; | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | if($self->flat) { | ||
| 124 | $self->display_subclasses_flat($subclass_index_ref, | ||
| 125 | $self->superclass, 0) | ||
| 126 | } else { | ||
| 127 | $self->display_subclasses($subclass_index_ref, | ||
| 128 | $self->superclass, '', ' ', 0); | ||
| 129 | } | ||
| 130 | |||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub create_subclass_index { | ||
| 135 | my ($seed, $index_ref) = @_; | ||
| 136 | |||
| 137 | my @children = $seed->__meta__->subclasses_loaded; | ||
| 138 | for my $child (@children) { | ||
| 139 | my @parents = @{$child->__meta__->{is}}; | ||
| 140 | for my $parent (@parents) { | ||
| 141 | if($index_ref->{$parent}) { | ||
| 142 | push(@{$index_ref->{$parent}}, $child); | ||
| 143 | } else { | ||
| 144 | $index_ref->{$parent} = [$child]; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub parse_subclass_index_file { | ||
| 151 | my ($indexfile) = @_; | ||
| 152 | |||
| 153 | open(IN, '<', $indexfile); | ||
| 154 | my %index; | ||
| 155 | while(my $line = <IN>) { | ||
| 156 | chomp($line); | ||
| 157 | if($line) { | ||
| 158 | my ($parent, $rest) = split(/ /, $line); | ||
| 159 | if($rest) { | ||
| 160 | my @children = split('\t', $rest); | ||
| 161 | $index{$parent} = \@children; | ||
| 162 | } else { | ||
| 163 | $index{$parent} = []; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | return \%index | ||
| 168 | } | ||
| 169 | |||
| 170 | sub display_subclasses_flat { | ||
| 171 | my ($self, $index_ref, $name, $depth) = @_; | ||
| 172 | my $maximum_depth = $self->maximum_depth; | ||
| 173 | if($depth == $maximum_depth + 1 and $maximum_depth != -1) { | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | print "$name\n"; | ||
| 177 | |||
| 178 | # get the children | ||
| 179 | my $children_ref = $index_ref->{$name}; | ||
| 180 | my @children; | ||
| 181 | if($children_ref) { | ||
| 182 | @children = @{$index_ref->{$name}}; | ||
| 183 | } else { # if it isn't in index it has no children. | ||
| 184 | @children = (); | ||
| 185 | } | ||
| 186 | |||
| 187 | # loop over children | ||
| 188 | for my $child (@children) { | ||
| 189 | $self->display_subclasses_flat($index_ref, $child, $depth+1); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | sub display_subclasses { | ||
| 194 | my ($self, $index_ref, $name, $global_prefix, $personal_prefix, $depth) = @_; | ||
| 195 | my $maximum_depth = $self->maximum_depth; | ||
| 196 | |||
| 197 | my ($dgp, $dpp, $dn) = ($global_prefix, $personal_prefix, $name); | ||
| 198 | if($self->color) { | ||
| 199 | ($dgp, $dpp, $dn) = colorize_output($global_prefix, $personal_prefix, | ||
| 200 | $name, $self->superclass); | ||
| 201 | } | ||
| 202 | print join('', $dgp, $dpp, $spacing, $dn); | ||
| 203 | |||
| 204 | my $o = ($personal_prefix =~ /^\|/ ) ? '|' : ' '; | ||
| 205 | my $child_global_prefix = sprintf("%s%s %s", $global_prefix, $o, $spacing); | ||
| 206 | |||
| 207 | # get the children | ||
| 208 | my $children_ref = $index_ref->{$name}; | ||
| 209 | my @children; | ||
| 210 | if($children_ref) { | ||
| 211 | @children = @{$index_ref->{$name}}; | ||
| 212 | } else { # if it isn't in index it has no children. | ||
| 213 | @children = (); | ||
| 214 | } | ||
| 215 | |||
| 216 | # loop over children | ||
| 217 | my $len_children = scalar(@children); | ||
| 218 | if($len_children and $depth == $maximum_depth and $maximum_depth != -1) { | ||
| 219 | print " ...\n"; | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | print "\n"; | ||
| 223 | |||
| 224 | my $i = 1; | ||
| 225 | for my $child (@children) { | ||
| 226 | my $child_personal_prefix = ($len_children == $i) ? '`-' : '|-'; | ||
| 227 | |||
| 228 | $self->display_subclasses($index_ref, $child, $child_global_prefix, | ||
| 229 | $child_personal_prefix, $depth+1); | ||
| 230 | $i += 1; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | sub colorize_output { | ||
| 235 | my ($global_prefix, $personal_prefix, $name, $superclass) = @_; | ||
| 236 | |||
| 237 | my $dgp = Term::ANSIColor::colored($global_prefix, 'white'); | ||
| 238 | my $dpp = Term::ANSIColor::colored($personal_prefix, 'white'); | ||
| 239 | my $name_prefix = $name; | ||
| 240 | if($name_prefix =~ /^($superclass)/) { | ||
| 241 | $name_prefix = $superclass; | ||
| 242 | } else { | ||
| 243 | $name_prefix = ''; | ||
| 244 | } | ||
| 245 | my $name_suffix = $name; | ||
| 246 | $name_suffix =~ s/^($superclass)//; | ||
| 247 | my $dn = sprintf("%s%s", Term::ANSIColor::colored($name_prefix, 'white'), $name_suffix ); | ||
| 248 | |||
| 249 | return ($dgp, $dpp, $dn); | ||
| 250 | } | ||
| 251 | |||
| 252 | 1; |