/[Frey]/trunk/lib/Frey/Introspect.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/Introspect.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 51 by dpavlin, Thu Jul 3 19:51:18 2008 UTC revision 972 by dpavlin, Fri Jan 9 19:33:26 2009 UTC
# Line 2  package Frey::Introspect; Line 2  package Frey::Introspect;
2    
3  use Moose;  use Moose;
4  use Carp;  use Carp;
5  use Class::MOP;  #use Moose::Meta::Role;
6  use Moose::Meta::Role;  #use Moose::Meta::Class;
 use Moose::Meta::Class;  
 use Scalar::Util qw/blessed/;  
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use File::Slurp;  use File::Slurp;
9  use List::Util;  use List::Util;
10    
11  extends 'Frey';  use PPI;
12    use PPI::HTML;
13    
14  has 'package' => (  use lib 'lib';
15          is => 'rw',  use Frey::Pod;
         isa => 'Str',  
         required => 1,  
 );  
16    
17  has 'renderHTML' => (  extends 'Frey::PPI';
18          is => 'rw',  with 'Frey::Web';
19          isa => 'Str',  with 'Frey::Storage';
 );  
20    
21  has 'path' => (  =head1 DESCRIPTION
22    
23    Provide introspection on any perl class installed on system
24    
25    =cut
26    
27    has 'class' => (
28          is => 'rw',          is => 'rw',
29            isa => 'Str',
30            required => 1,
31  );  );
32    
33  =head2 examine  =head2 joose
34    
35    my $js = $o->examine( 'Some::Package' );    my $js = $o->joose;
36    
37  =cut  =cut
38    
39  sub examine {  sub joose {
40          my ($self) = @_;          my ($self) = @_;
41    
42          my $package = $self->package;          my ( $meta, $is_role ) = $self->class_meta;
43    
44          #intercept role application so we can accurately generate          if ( ! $is_role ) {
         #method and attribute information for the parent class.  
         #this is fragile, but there is not better way that i am aware of  
         my $rmeta = Moose::Meta::Role->meta;  
         $rmeta->make_mutable if $rmeta->is_immutable;  
         my $original_apply = $rmeta->get_method("apply")->body;  
         $rmeta->remove_method("apply");  
         my @roles_to_apply;  
         $rmeta->add_method("apply", sub{push(@roles_to_apply, [@_])});  
         #load the package with the hacked Moose::Meta::Role  
         eval { Class::MOP::load_class($package); };  
         confess "Failed to load package ${package} $@" if $@;  
   
         #get on with analyzing the      package  
         my $meta = $package->meta;  
         my $spec = {};  
         my ($class, $is_role);  
         if($package->meta->isa('Moose::Meta::Role')){  
                 $is_role = 1;  
                 # we need to apply the role to a class to be able to properly introspect it  
                 $class = Moose::Meta::Class->create_anon_class;  
                 $original_apply->($meta, $class);  
         } else {  
                 #roles don't have superclasses ...  
                 $class = $meta;  
45                  my @superclasses = map{ $_->meta->name }                  my @superclasses = map{ $_->meta->name }
46                          grep { $_ ne 'Moose::Object' } $meta->superclasses;                          grep { $_ ne 'Moose::Object' } $meta->superclasses;
47                  warn "superclasses ",dump( @superclasses );                  warn "superclasses ",dump( @superclasses ) if $self->debug;
48          }          }
49    
50          my $out;          my $out;
51    
52          my ( $m, $c ) = split(/::/, $class->name, 2);          my ( $m, $c ) = split(/::/, $self->class, 2);
53          my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js';          my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js';
54    
55            $c ||= '';
56    
57          $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";          $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";
58    
59          foreach ( $class->get_attribute_list ) {          foreach ( $meta->get_attribute_list ) {
60                  $out .= "\t\t\t$_: {\n";                  $out .= "\t\t\t$_: {\n";
61    
62                  my $attr = $class->get_attribute($_);                  my $attr = $meta->get_attribute($_);
63                  my $is = $attr->_is_metadata;                  my $is = eval { $attr->_is_metadata; };
64                    return if $@;
65    
66                  $out .= "\t\t\t\tis: \"$is\",\n" if defined $is;                  $out .= "\t\t\t\tis: \"$is\",\n" if defined $is;
67                  $out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy;                  $out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy;
68                  $out .= "\t\t\t\trequired: true,\n" if $attr->is_required;                  $out .= "\t\t\t\trequired: true,\n" if $attr->is_required;
# Line 103  sub examine { Line 86  sub examine {
86          $out .= "\t\t},\n\t\tmeta: Frey.HTML,          $out .= "\t\t},\n\t\tmeta: Frey.HTML,
87                  classMethods: {                  classMethods: {
88                          renderHTML: function () {                          renderHTML: function () {
89                                  return new Joose.SimpleRequest().getText(\"json?class=$c\")                                  return new Joose.SimpleRequest().getText(\"/" . $self->class . "\")
90                          },\n";                          },\n";
91    
92          $out .= "\t\t},\n";          $out .= "\t\t},\n";
# Line 113  sub examine { Line 96  sub examine {
96          $out =~ s/,\n$/\n/;          $out =~ s/,\n$/\n/;
97          $out .= "});\n";          $out .= "});\n";
98    
99          $out .= "\nconsole.log( 'loaded " . $class->name . " from $filename' );\n";          $out .= "\nconsole.log( 'loaded " . $self->class . " from $filename' );\n";
   
         warn $class->dump(2);  
   
         my $attr;  
         $attr->{$_}++ foreach $class->get_attribute_list;  
         my @methods = grep { ! defined($attr->{$_}) } $class->get_method_list;  
         warn "methods = ",dump( @methods );  
100    
101          warn "method_list = ",dump( $class->get_method_list );          warn "method_list = ",dump( $meta->get_method_list ) if $self->debug;
         warn dump( map{ $class->get_method($_)->name } sort $class->get_method_list );  
102    
103  #       print $out;  #       print $out;
104          my $path = "static/blib/$filename";          my $path = "static/blib/$filename";
105          write_file( $path, $out );          write_file( $path, $out );
106          warn "# created $path\n";          warn "# created $path\n";
         $self->path( $path );  
107    
108          return $out;          return $out;
109  }  }
110    
111    sub as_markup {
112            my ( $self ) = @_;
113    
114            $self->add_head( 'static/introspect.css' );
115    
116            my ( $meta, $is_role ) = $self->class_meta;
117    
118            my $class = $self->class;
119            $self->title( $class );
120    
121            my $introspect_path = "var/introspect/$class.yaml";
122            $self->mkbasepath( $introspect_path );
123            my $introspect; # FIXME update with = $self->load( $introspect_path );
124    
125            my ( $superclasses, $roles ) = ( '<b>Role</b>', '' );
126            if ( ! $is_role ) {
127                    if ( $meta->superclasses ) {
128                            $superclasses = 'Superclasses: ' .
129                                    join(', ',
130                                            map {
131                                                    my $name = $_->meta->name;
132                                                    $introspect->{superclass}->{$name} = {};
133    
134                                                    qq|<a target="$name" href="/$name" title="introspect $name">$name</a>| .
135                                                    $self->dropdown( qq|<sup>?</sup>|, $_->meta )
136                                            }
137                                            #grep { $_ ne 'Moose::Object' }
138                                            $meta->superclasses
139                                    );
140                    }
141            }
142    
143            my $method_from_role;
144            my $attribute_from_role;
145    
146            if ( $meta->can('roles') ) {
147                    my $role_nr = 1;
148                    $roles = join(' ',
149                            grep { ! m/\Q$class\E/ }        # skip me
150                            map {
151                                    my $name = $_->name;
152                                    $introspect->{roles}->{$name} = {};
153                                    $method_from_role->{    $_ }->{$name} = $role_nr foreach $_->get_method_list;
154                                    $attribute_from_role->{ $_ }->{$name} = $role_nr foreach $_->get_attribute_list;
155    
156                                      qq|<a target="$name" href="/$name" title="introspect $name">$name</a>|
157    #                               . qq|<sup>| . $self->dropdown( $role_nr++, $name->meta ) . qq|</sup>|
158                                    . $self->dropdown(
159                                            qq|<sup>| . $role_nr++ . qq|</sup>|,
160                                            $name->meta
161                                    )
162                                    ;
163                            }
164                            $meta->calculate_all_roles
165                    );
166                    $roles = qq|with roles: $roles| if $roles;
167            }
168            warn "# method_from_role ",dump( $method_from_role );
169    
170            my @methods;
171            @methods = map {
172                    my $name = $_;
173                    if ( $method_from_role->{$name} ) {
174                            my ( $role_name, $nr ) = each %{ $method_from_role->{$name} };
175                            $introspect->{methods}->{$name}->{role} = $role_name;
176                            $name .= qq|<sup title="$role_name">$nr</sup>|;
177                    } else {
178                            $introspect->{methods}->{$name} = {};
179                    }
180                    qq|<td class="m">$name</td>|
181            } sort {
182                    lc($a) cmp lc($b)      
183            } $self->class_methods( $class );
184    
185            my @attributes;
186            if ( $meta->get_attribute_list ) {
187                    @attributes = map {
188                            my $name = $_;
189                            $introspect->{attribute}->{$name} = {};
190                            my $html_name = $name;
191                            my $attr = $meta->get_attribute($name);
192                            confess "$class attribute $name isn't blessed ",dump( $attr ) unless blessed $attr;
193                            warn "## attr $name ref ",ref( $attr ) if $self->debug;
194    
195                            my ( $title, $properties ) = ( '', '' );
196                            if ( $attr->can('is_required') && $attr->is_required ) {
197                                    ( $html_name, $title ) = ( "<b>$name</b>", ' title="required"' );
198                                    $introspect->{attribute}->{$name}->{required} = 1;
199                            }
200    
201                            foreach my $check ( qw/has_type_constraint has_handles is_weak_ref is_required is_lazy should_coerce should_auto_deref has_default has_trigger has_applied_traits/ ) {
202                                    my $getter;
203                                    
204                                    $getter = $check;
205                                    $getter =~ s/^has_//;
206                                    
207                                    if ( $attr->can($check) && $attr->$check ) {
208                                            $properties .= $check;
209                                            # we need our dump here instead of $attr->$getter->dump because default can return scalar
210                                            my $v = $attr->$getter;
211                                            $properties .= ref($v)
212                                                    ? $self->dropdown( qq|<sup>?</sup>|, $attr->$getter )
213                                                    : qq| <code>$v</code>|
214                                                    ;
215                                    }
216                                    $properties .= ' ';
217                            }
218                            my $type = $attr->can('has_type_constraint') && $attr->has_type_constraint ? $attr->type_constraint->name : '';
219    
220                            if ( $attribute_from_role->{$name} ) {
221                                    my ( $role_name, $nr ) = each %{ $attribute_from_role->{$name} };
222                                    $name .= qq|<sup title="$role_name">$nr</sup>|;
223                            }
224    
225                            if ( my $doc = eval { $attr->documentation } ) {
226                                    $properties = qq|
227                                            $properties
228                                            <span class="documentation">$doc</span>
229                                    |;
230                                    $self->add_css(qq|
231                                            span.documentation {
232                                                    background: #eee;
233                                                    padding: 0.25em;
234                                                    float: left;
235                                                    clear: left;
236                                            }
237                                    |);
238                                    $introspect->{action}->{$name}->{documentation} = $doc;
239                            }
240    
241                            qq|<td class="a">$html_name</td><td class="t">$type</td><td class="p">$properties</td>|
242                    } sort $meta->get_attribute_list
243            }
244    
245            my $table = qq|<table class="frey-introspect"><tr><th class="m">Methods</th><th class="a">Attributes</th><th>Type</th><th class="p">Properties</th></tr>|;
246            while ( @methods || @attributes ) {
247                    my ($m,$a) = ( shift @methods, shift @attributes );
248                    $m ||= '<td></td>';
249                    $a ||= '<td></td>';
250                    $table .= qq|<tr>$m$a</tr>|;
251            }
252            $table .= qq|</table>|;
253    
254            my $path = $self->class_path( $class );
255    
256            my $pod = Frey::Pod->new( class => $class, request_url => $self->request_url )->as_markup;
257            return $pod if $path =~ m{\.pod};
258    
259            my $Document = PPI::Document->new( $path );
260    
261            # Create a reusable syntax highlighter
262            my $Highlight = PPI::HTML->new(
263                    line_numbers => 1,
264    #               page => 1,
265    #               colors => {
266    #                       line_number => '#CCCCCC',
267    #                       number      => '#990000',
268    #               },
269            );
270    
271            # Spit out the HTML
272            my $source = $Highlight->html( $Document );
273    
274            $source =~ s{(<span.*?line_number.*>\s*)(\d+)(:\s*</span>)}{$1<a target="editor" href="/editor+$path+$2">$2</a>$3}g;
275    
276            # strip page html
277    #       $source =~ s{^.*<body[^>]+>}{}s;
278    #       $source =~ s{</body.*$}{}s;
279    
280            my $runnable = join("\n",
281                    map {
282                            $introspect->{runnable}->{$_} = {};
283                            my $short = $_;
284                            $short =~ s{_as_(?:markup|data|sponge)$}{};
285                            qq|<a target="$class" href="/$class/$_" title="/$class/$_">$short</a>|
286                    } $self->class_runnable( $class )
287            );
288            $runnable = "Runnable: $runnable" if $runnable;
289    
290            my $has_tests = '';
291            my @tests = grep { defined $_ } $self->has_tests;
292            if ( @tests ) {
293                    $has_tests =
294                    'Test' . ( $#tests > 0 ? 's' : '' ) . ': ' .    
295                    join("\n", map {
296                            qq|<a target="$class" href="/Frey::Test::Runner/as_markup?test=$_">$_</a>|
297                    } @tests );
298                    $introspect->{tests} = [ @tests ],
299            }
300    
301            my $includes = '';
302            if ( my $inc = $self->includes ) {
303                    $introspect->{includes} = $inc;
304                    foreach my $type ( keys %$inc ) {
305                            $includes
306                                    .= ucfirst($type)
307                                    . qq|: |
308                                    . join("\n",
309                                            map {
310                                                    qq|<a target="$_" href="/$_">$_</a>|
311                                            } @{
312                                                    $inc->{$type}
313                                            }
314                                    )
315                                    ;
316                    }
317            }
318    
319            $self->store( $introspect_path, $introspect );
320    
321            return join("\n",
322                    qq|<h1>$class</h1>|,
323                    qq|<div class="frey-introspect">
324                            $superclasses $roles
325                            <br>
326                            $includes
327                            <br>
328                            $runnable
329                    |,
330                    $has_tests,
331                    $pod    ? qq|<a class="frey-skip" href="#___top" title="Skip to POD"    >pod</a>|    : '',
332                    $source ? qq|<a class="frey-skip" href="#source" title="Skip to source" >source</a>| : '',
333                    qq|$table\n$pod\n</div>\n|,
334                    qq|<h1>Source</h1><a name="source"></a><div class="frey-source">$source</div>|,
335            );
336    }
337    
338  =head1 SEE ALSO  =head1 SEE ALSO
339    
340  L<MooseX::AutoDoc> on which this code is based  L<MooseX::AutoDoc> on which this code is based

Legend:
Removed from v.51  
changed lines
  Added in v.972

  ViewVC Help
Powered by ViewVC 1.1.26