| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # brik::search Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Brik::Search; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | commands => { | ||
| 17 | all => [ ], | ||
| 18 | string => [ qw(string) ], | ||
| 19 | tag => [ qw(Tag) ], | ||
| 20 | not_tag => [ qw(Tag) ], | ||
| 21 | used => [ ], | ||
| 22 | not_used => [ ], | ||
| 23 | show_require_modules => [ ], | ||
| 24 | command => [ qw(Command) ], | ||
| 25 | category => [ qw(Category) ], | ||
| 26 | list_categories => [ ], | ||
| 27 | }, | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub all { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | if (! defined($self->context)) { | ||
| 35 | return $self->log->error("all: no core::context Brik"); | ||
| 36 | } | ||
| 37 | |||
| 38 | my $context = $self->context; | ||
| 39 | my $status = $context->status; | ||
| 40 | |||
| 41 | my $total = 0; | ||
| 42 | my $count = 0; | ||
| 43 | $self->log->info("Used:"); | ||
| 44 | for my $brik (@{$status->{used}}) { | ||
| 45 | next unless $context->used->{$brik}->can('brik_tags'); | ||
| 46 | my $tags = $context->used->{$brik}->brik_tags; | ||
| 47 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 48 | $count++; | ||
| 49 | $total++; | ||
| 50 | } | ||
| 51 | $self->log->info("Count: $count"); | ||
| 52 | |||
| 53 | $count = 0; | ||
| 54 | $self->log->info("Not used:"); | ||
| 55 | for my $brik (@{$status->{not_used}}) { | ||
| 56 | next unless $context->not_used->{$brik}->can('brik_tags'); | ||
| 57 | my $tags = $context->not_used->{$brik}->brik_tags; | ||
| 58 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 59 | $count++; | ||
| 60 | $total++; | ||
| 61 | } | ||
| 62 | $self->log->info("Count: $count"); | ||
| 63 | |||
| 64 | return $total; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub string { | ||
| 68 | my $self = shift; | ||
| 69 | my ($string) = @_; | ||
| 70 | |||
| 71 | if (! defined($self->context)) { | ||
| 72 | return $self->log->error("string: no core::context Brik"); | ||
| 73 | } | ||
| 74 | |||
| 75 | $self->brik_help_run_undef_arg('string', $string) or return; | ||
| 76 | |||
| 77 | my $context = $self->context; | ||
| 78 | my $status = $context->status; | ||
| 79 | |||
| 80 | my $total = 0; | ||
| 81 | $self->log->info("Used:"); | ||
| 82 | for my $brik (@{$status->{used}}) { | ||
| 83 | next unless $brik =~ /$string/; | ||
| 84 | next unless $context->used->{$brik}->can('brik_tags'); | ||
| 85 | my $tags = $context->used->{$brik}->brik_tags; | ||
| 86 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 87 | $total++; | ||
| 88 | } | ||
| 89 | |||
| 90 | $self->log->info("Not used:"); | ||
| 91 | for my $brik (@{$status->{not_used}}) { | ||
| 92 | next unless $brik =~ /$string/; | ||
| 93 | next unless $context->not_used->{$brik}->can('brik_tags'); | ||
| 94 | my $tags = $context->not_used->{$brik}->brik_tags; | ||
| 95 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 96 | $total++; | ||
| 97 | } | ||
| 98 | |||
| 99 | return $total; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub tag { | ||
| 103 | my $self = shift; | ||
| 104 | my ($tag) = @_; | ||
| 105 | |||
| 106 | if (! defined($self->context)) { | ||
| 107 | return $self->log->error("tag: no core::context Brik"); | ||
| 108 | } | ||
| 109 | |||
| 110 | $self->brik_help_run_undef_arg('tag', $tag) or return; | ||
| 111 | |||
| 112 | my $context = $self->context; | ||
| 113 | my $status = $context->status; | ||
| 114 | |||
| 115 | my $total = 0; | ||
| 116 | $self->log->info("Used:"); | ||
| 117 | for my $brik (@{$status->{used}}) { | ||
| 118 | next unless $context->used->{$brik}->can('brik_tags'); | ||
| 119 | my $tags = $context->used->{$brik}->brik_tags; | ||
| 120 | push @$tags, 'used'; | ||
| 121 | for my $this (@$tags) { | ||
| 122 | next unless $this eq $tag; | ||
| 123 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 124 | $total++; | ||
| 125 | last; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | $self->log->info("Not used:"); | ||
| 130 | for my $brik (@{$status->{not_used}}) { | ||
| 131 | next unless $context->not_used->{$brik}->can('brik_tags'); | ||
| 132 | my $tags = $context->not_used->{$brik}->brik_tags; | ||
| 133 | push @$tags, 'not_used'; | ||
| 134 | for my $this (@$tags) { | ||
| 135 | next unless $this eq $tag; | ||
| 136 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 137 | $total++; | ||
| 138 | last; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | return $total; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub not_tag { | ||
| 147 | my $self = shift; | ||
| 148 | my ($tag) = @_; | ||
| 149 | |||
| 150 | if (! defined($self->context)) { | ||
| 151 | return $self->log->error("not_tag: no core::context Brik"); | ||
| 152 | } | ||
| 153 | |||
| 154 | $self->brik_help_run_undef_arg('not_tag', $tag) or return; | ||
| 155 | |||
| 156 | my $context = $self->context; | ||
| 157 | my $status = $context->status; | ||
| 158 | |||
| 159 | my $total = 0; | ||
| 160 | $self->log->info("Used:"); | ||
| 161 | for my $brik (@{$status->{used}}) { | ||
| 162 | next unless $context->used->{$brik}->can('brik_tags'); | ||
| 163 | my $tags = $context->used->{$brik}->brik_tags; | ||
| 164 | push @$tags, 'used'; | ||
| 165 | for my $this (@$tags) { | ||
| 166 | next if $this eq $tag; | ||
| 167 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 168 | $total++; | ||
| 169 | last; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | $self->log->info("Not used:"); | ||
| 174 | for my $brik (@{$status->{not_used}}) { | ||
| 175 | next unless $context->not_used->{$brik}->can('brik_tags'); | ||
| 176 | my $tags = $context->not_used->{$brik}->brik_tags; | ||
| 177 | push @$tags, 'not_used'; | ||
| 178 | for my $this (@$tags) { | ||
| 179 | next if $this eq $tag; | ||
| 180 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 181 | $total++; | ||
| 182 | last; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | return $total; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub used { | ||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | return $self->tag('used'); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub not_used { | ||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | return $self->not_tag('used'); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub show_require_modules { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | if (! defined($self->context)) { | ||
| 205 | return $self->log->error("show_require_modules: no core::context Brik"); | ||
| 206 | } | ||
| 207 | |||
| 208 | my $context = $self->context; | ||
| 209 | my $available = $context->available; | ||
| 210 | |||
| 211 | # Don't show require for Core modules | ||
| 212 | my $core = { | ||
| 213 | 'core::context', | ||
| 214 | 'core::log', | ||
| 215 | 'core::shell', | ||
| 216 | 'core::global', | ||
| 217 | }; | ||
| 218 | |||
| 219 | my %require_modules = (); | ||
| 220 | for my $brik (keys %$available) { | ||
| 221 | next if (exists($core->{$brik})); | ||
| 222 | if ($available->{$brik}->can('brik_properties')) { | ||
| 223 | my $modules = $available->{$brik}->brik_properties->{require_modules}; | ||
| 224 | for my $module (keys %$modules) { | ||
| 225 | next if $module =~ /^Metabrik/; | ||
| 226 | $require_modules{$module} = $brik; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | return [ sort { $a cmp $b } keys %require_modules ]; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub command { | ||
| 235 | my $self = shift; | ||
| 236 | my ($command) = @_; | ||
| 237 | |||
| 238 | if (! defined($self->context)) { | ||
| 239 | return $self->log->error("command: no core::context Brik"); | ||
| 240 | } | ||
| 241 | |||
| 242 | $self->brik_help_run_undef_arg('command', $command) or return; | ||
| 243 | |||
| 244 | my $context = $self->context; | ||
| 245 | my $status = $context->status; | ||
| 246 | |||
| 247 | my $total = 0; | ||
| 248 | $self->log->info("Used:"); | ||
| 249 | for my $brik (@{$status->{used}}) { | ||
| 250 | if (exists($context->used->{$brik}->brik_properties->{commands})) { | ||
| 251 | next unless $context->used->{$brik}->can('brik_tags'); | ||
| 252 | my $tags = $context->used->{$brik}->brik_tags; | ||
| 253 | push @$tags, 'used'; | ||
| 254 | for my $key (keys %{$context->used->{$brik}->brik_properties->{commands}}) { | ||
| 255 | if ($key =~ /$command/i) { | ||
| 256 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 257 | $total++; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | $self->log->info("Not used:"); | ||
| 264 | for my $brik (@{$status->{not_used}}) { | ||
| 265 | if (exists($context->not_used->{$brik}->brik_properties->{commands})) { | ||
| 266 | next unless $context->not_used->{$brik}->can('brik_tags'); | ||
| 267 | my $tags = $context->not_used->{$brik}->brik_tags; | ||
| 268 | push @$tags, 'not_used'; | ||
| 269 | for my $key (keys %{$context->not_used->{$brik}->brik_properties->{commands}}) { | ||
| 270 | if ($key =~ /$command/i) { | ||
| 271 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 272 | $total++; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | return $total; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub category { | ||
| 282 | my $self = shift; | ||
| 283 | my ($category) = @_; | ||
| 284 | |||
| 285 | if (! defined($self->context)) { | ||
| 286 | return $self->log->error("category: no core::context Brik"); | ||
| 287 | } | ||
| 288 | |||
| 289 | $self->brik_help_run_undef_arg('category', $category) or return; | ||
| 290 | |||
| 291 | my $context = $self->context; | ||
| 292 | my $status = $context->status; | ||
| 293 | |||
| 294 | my $total = 0; | ||
| 295 | $self->log->info("Used:"); | ||
| 296 | for my $brik (@{$status->{used}}) { | ||
| 297 | next unless defined($context->used->{$brik}); | ||
| 298 | next unless $context->used->{$brik}->can('brik_category'); | ||
| 299 | my $brik_category = $context->used->{$brik}->brik_category; | ||
| 300 | next unless $brik_category eq $category; | ||
| 301 | next unless $context->used->{$brik}->can('brik_tags'); | ||
| 302 | my $tags = $context->used->{$brik}->brik_tags; | ||
| 303 | push @$tags, 'used'; | ||
| 304 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 305 | $total++; | ||
| 306 | } | ||
| 307 | |||
| 308 | $self->log->info("Not used:"); | ||
| 309 | for my $brik (@{$status->{not_used}}) { | ||
| 310 | next unless defined($context->not_used->{$brik}); | ||
| 311 | next unless $context->not_used->{$brik}->can('brik_category'); | ||
| 312 | my $brik_category = $context->not_used->{$brik}->brik_category; | ||
| 313 | next unless $brik_category eq $category; | ||
| 314 | next unless $context->not_used->{$brik}->can('brik_tags'); | ||
| 315 | my $tags = $context->not_used->{$brik}->brik_tags; | ||
| 316 | push @$tags, 'not_used'; | ||
| 317 | $self->log->info(sprintf(" %-20s [%s]", $brik, join(', ', @$tags))); | ||
| 318 | $total++; | ||
| 319 | } | ||
| 320 | |||
| 321 | return $total; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub list_categories { | ||
| 325 | my $self = shift; | ||
| 326 | |||
| 327 | if (! defined($self->context)) { | ||
| 328 | return $self->log->error("list_categories: no core::context Brik"); | ||
| 329 | } | ||
| 330 | |||
| 331 | my $con = $self->context; | ||
| 332 | my $available = $con->find_available; | ||
| 333 | |||
| 334 | my @categories = (); | ||
| 335 | for my $k (keys %$available) { | ||
| 336 | my @t = split(/::/, $k); | ||
| 337 | if (@t == 2) { # Category and Name | ||
| 338 | push @categories, $t[0]; | ||
| 339 | } | ||
| 340 | elsif (@t == 3) { # Repository, Category and Name | ||
| 341 | push @categories, $t[1]; | ||
| 342 | } | ||
| 343 | else { # Error | ||
| 344 | $self->log->warning("list_categories: Brik [$k] has no Category?"); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | my %uniq = map { $_ => 1 } @categories; | ||
| 349 | return [ sort { $a cmp $b } keys %uniq ]; | ||
| 350 | } | ||
| 351 | |||
| 352 | 1; | ||
| 353 | |||
| 354 | __END__ | ||