/[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 66 by dpavlin, Wed Jul 9 12:08:07 2008 UTC revision 471 by dpavlin, Thu Nov 20 10:55:56 2008 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  use Continuity::Widget::DomNode;  use PPI;
12    use PPI::HTML;
13    
14  use lib 'lib';  use lib 'lib';
15    
16  extends 'Frey';  extends 'Frey::ClassLoader';
17    with 'Frey::Web';
18    
19  has 'package' => (  has 'class' => (
20          is => 'rw',          is => 'rw',
21          isa => 'Str',          isa => 'Str',
22          required => 1,          required => 1,
23  );  );
24    
 has 'path' => (  
         is => 'rw',  
 );  
   
 =head2 load_package  
   
   my ( $class, $meta, $is_role ) = $o->load_package( 'Some::Package' );  
   
 =cut  
   
 sub load_package {  
         my ( $self ) = @_;  
   
         my $package = $self->package;  
   
         #intercept role application so we can accurately generate  
         #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 $@;  
         Class::MOP::load_class($package);  
   
         my $meta = $package->meta;  
   
         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;  
         }  
         return ( $class, $meta, $is_role );  
 }  
   
25  =head2 joose  =head2 joose
26    
27    my $js = $o->joose( 'Some::Package' );    my $js = $o->joose;
28    
29  =cut  =cut
30    
31  sub joose {  sub joose {
32          my ($self) = @_;          my ($self) = @_;
33    
34          my ( $class, $meta, $is_role ) = $self->load_package;          my ( $meta, $is_role ) = $self->class_meta;
35    
36          if ( ! $is_role ) {          if ( ! $is_role ) {
37                  my @superclasses = map{ $_->meta->name }                  my @superclasses = map{ $_->meta->name }
38                          grep { $_ ne 'Moose::Object' } $meta->superclasses;                          grep { $_ ne 'Moose::Object' } $meta->superclasses;
39                  warn "superclasses ",dump( @superclasses );                  warn "superclasses ",dump( @superclasses ) if $self->debug;
40          }          }
41    
42          my $out;          my $out;
43    
44          my ( $m, $c ) = split(/::/, $class->name, 2);          my ( $m, $c ) = split(/::/, $self->class, 2);
45          my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js';          my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js';
46    
47            $c ||= '';
48    
49          $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";          $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";
50    
51          foreach ( $class->get_attribute_list ) {          foreach ( $meta->get_attribute_list ) {
52                  $out .= "\t\t\t$_: {\n";                  $out .= "\t\t\t$_: {\n";
53    
54                  my $attr = $class->get_attribute($_);                  my $attr = $meta->get_attribute($_);
55                  my $is = $attr->_is_metadata;                  my $is = $attr->_is_metadata;
56                  $out .= "\t\t\t\tis: \"$is\",\n" if defined $is;                  $out .= "\t\t\t\tis: \"$is\",\n" if defined $is;
57                  $out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy;                  $out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy;
# Line 118  sub joose { Line 76  sub joose {
76          $out .= "\t\t},\n\t\tmeta: Frey.HTML,          $out .= "\t\t},\n\t\tmeta: Frey.HTML,
77                  classMethods: {                  classMethods: {
78                          renderHTML: function () {                          renderHTML: function () {
79                                  return new Joose.SimpleRequest().getText(\"/~/" . $self->package . "\")                                  return new Joose.SimpleRequest().getText(\"/" . $self->class . "\")
80                          },\n";                          },\n";
81    
82          $out .= "\t\t},\n";          $out .= "\t\t},\n";
# Line 128  sub joose { Line 86  sub joose {
86          $out =~ s/,\n$/\n/;          $out =~ s/,\n$/\n/;
87          $out .= "});\n";          $out .= "});\n";
88    
89          $out .= "\nconsole.log( 'loaded " . $class->name . " from $filename' );\n";          $out .= "\nconsole.log( 'loaded " . $self->class . " from $filename' );\n";
90    
91          warn $class->dump(2);          warn "method_list = ",dump( $meta->get_method_list ) if $self->debug;
   
         warn "method_list = ",dump( $class->get_method_list );  
         warn dump( map{ $class->get_method($_)->name } sort $class->get_method_list );  
92    
93  #       print $out;  #       print $out;
94          my $path = "static/blib/$filename";          my $path = "static/blib/$filename";
95          write_file( $path, $out );          write_file( $path, $out );
96          warn "# created $path\n";          warn "# created $path\n";
         $self->path( $path );  
97    
98          return $out;          return $out;
99  }  }
100    
101  =head2 methods  sub as_markup {
102            my ( $self ) = @_;
   my @methods = $o->methods;  
   
 =cut  
   
 sub methods {  
         my $self = shift;  
   
         my ( $class, $meta, $is_role ) = $self->load_package;  
   
         my $attr;  
         $attr->{$_}++ foreach $class->get_attribute_list;  
         my @methods = grep { ! defined($attr->{$_}) } $class->get_method_list;  
         warn "# methods = ",dump( @methods );  
   
         return @methods;  
 }  
   
 =head1 OUTPUT GENERATION  
   
 =head2 html  
103    
104    $o->html( $request );          $self->add_head( 'static/introspect.css' );
105    
106  =cut          my ( $meta, $is_role ) = $self->class_meta;
107    
108  our @javascript = ( qw'          my $class = $self->class;
 ../lib/Joose.js  
 ');  
109    
110  sub html {          my ( $superclasses, $roles ) = ( '<b>Role</b>', '' );
111          my ( $self, $request ) = @_;          if ( ! $is_role ) {
112                    if ( $meta->superclasses ) {
113                            $superclasses = 'Superclasses: ' .
114                                    join(', ',
115                                            map {
116                                                    my $name = $_->meta->name;
117                                                    qq|<a class="frey-popdown" href="/$name">$name<code>| . $_->meta->dump(2) . qq|</code></a>|;
118                                            }
119                                            #grep { $_ ne 'Moose::Object' }
120                                            $meta->superclasses
121                                    );
122                    }
123            }
124    
125          while (1) {          my $role_method;
126            my $role_attribute;
127    
128                  my $js = Continuity::Widget::DomNode->create(          if ( $meta->can('roles') ) {
129                    my $role_nr = 1;
130                    $roles = join(' ',
131                            grep { ! m/\Q$class\E/ }        # skip me
132                          map {                          map {
133                                  ( script => { type => 'text/javascript', src => $_ } )                                  my $name = $_->name;
134                          } @javascript                                  $role_method->{    $_ }->{$name} = $role_nr foreach $_->get_method_list;
135                  )->to_string;                                  $role_attribute->{ $_ }->{$name} = $role_nr foreach $_->get_attribute_list;
136                                    qq|<a class="frey-popdown" href="/$name">$name<code>| . $name->meta->dump(2) . qq|</code></a><sup>| . $role_nr++ . qq|</sup>|;
137                  $js .= << '__END_OF_JS__';                          }
138  <script type="text/javascript">                          $meta->calculate_all_roles
139  joose.loadComponents("../lib")                  );
140                    $roles = qq| with roles: $roles| if $roles;
141            }
142            warn "# role_method ",dump( $role_method );
143    
144  function $(id) {          my @methods;
145          return document.getElementById(id)          @methods = map {
146  }                  my $name = $_;
147                    if ( $role_method->{$name} ) {
148                            my ( $role_name, $nr ) = each %{ $role_method->{$name} };
149                            $name .= qq|<sup title="$role_name">$nr</sup>|;
150                    }
151                    qq|<td class="m">$name</td>|
152            } $self->class_methods( $class );
153    
154  </script>          my @attributes;
155  __END_OF_JS__          if ( $meta->get_attribute_list ) {
156                    @attributes = map {
157                            my $name = $_;
158                            my $attr = $meta->get_attribute($name);
159                            confess "$class attribute $name isn't blessed ",dump( $attr ) unless blessed $attr;
160                            warn "## attr $name ref ",ref( $attr ) if $self->debug;
161                            my ( $before, $title, $after ) = ( '', '', '' );
162                            ( $before, $title, $after ) = ( '<b>', ' title="required"', '</b>' ) if $attr->can('is_required') && $attr->is_required;
163                            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_documentation has_applied_traits/ ) {
164                                    my $getter;
165                                    
166                                    $getter = $check;
167                                    $getter =~ s/^has_//;
168                                    
169                                    if ( $attr->can($check) && $attr->$check ) {
170                                            if ( $getter eq $check ) {
171                                                    $after .= "$check";
172                                            } else {
173                                                    $after .= qq{<span class="frey-popdown">$check};
174                                                    # we need dump here instead of $attr->$getter->dump because default can return scalar
175                                                    $after .= '<code>' . dump( $attr->$getter ) . '</code>' if $getter ne $check;
176                                                    $after .= '</span>';
177                                            }
178                                    }
179                                    $after .= ' ';
180                            }
181                            my $type = $attr->can('has_type_constraint') && $attr->has_type_constraint ? $attr->type_constraint->name : '';
182    
183                  warn "# >>> js\n$js\n" if $self->debug;                          if ( $role_attribute->{$name} ) {
184                                    my ( $role_name, $nr ) = each %{ $role_attribute->{$name} };
185                                    $name .= qq|<sup title="$role_name">$nr</sup>|;
186                            }
187    
188                  my ( $class, $meta, $is_role ) = $self->load_package();                          qq|<td class="a">$before $name</td><td class="t">$type</td><td>$after</td>|
189                    } $meta->get_attribute_list
190            }
191    
192                  my $methods;          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>|;
193                  if ( $class->can('meta') ) {          while ( @methods || @attributes ) {
194                          $methods = Continuity::Widget::DomNode->create(                  my ($m,$a) = ( shift @methods, shift @attributes );
195                                  ul => [                  $m ||= '<td></td>';
196                                          map { (                  $a ||= '<td></td>';
197                                                  li => [ a => { href => '/~/' . $self->package . '/' . $_ } => [ $_ ] ]                  $table .= qq|<tr>$m$a</tr>|;
198                                          ) } $self->methods          }
199                                  ]          $table .= qq|</table>|;
                         )->to_string;  
                 } else {  
                         $methods = '<b>not introspectable</b>';  
                 }  
200    
201                  my $attributes = Continuity::Widget::DomNode->create(          my $pod = Frey::Pod->new( class => $class )->as_markup;
202                          ul => [          $pod = $pod->{body} if ref($pod);
                                 map {  
                                         my $attr = $class->get_attribute($_);  
                                         warn "## $_ ", $attr->is_required ? 'required' : 'optional';  
                                         ( li => [ a => { href => '/~/' . $self->package . '/' . $_ } => [ $_ ], ( $attr->is_required ? ' <b>required</b>' : '' ) ] )  
                                 } $class->get_attribute_list  
                         ],  
                 )->to_string;  
   
                 my $doc = Continuity::Widget::DomNode->create(  
                         html => [  
                                 head => [  
                                         link => { rel=>"stylesheet", href=>"/static/app.css", type=>"text/css", media=>"screen" },  
                                         $js,  
                                         title => [ 'Introspect ', $self->package ],  
                                 ],  
                                 body => [  
                                         h1 => [ $self->package ],  
                                         h2 => [ 'Methods' ],  
                                         $methods,  
                                         h2 => [ 'Atrributes' ],  
                                         $attributes,  
                                 ],  
                         ]  
                 );  
203    
204                  $request->print($doc->to_string);          my $path = $self->class_path( $class );
205                  warn "# >>> html\n", $doc->to_string, "\n";          my $Document = PPI::Document->new( $path );
206                  $request->next;  
207          }          # Create a reusable syntax highlighter
208          warn "# exit html";          my $Highlight = PPI::HTML->new(
209                    line_numbers => 1,
210    #               page => 1,
211    #               colors => {
212    #                       line_number => '#CCCCCC',
213    #                       number      => '#990000',
214    #               },
215            );
216    
217            # Spit out the HTML
218            my $source = $Highlight->html( $Document );
219    
220            $source =~ s{(<span.*?line_number.*>\s*)(\d+)(:\s*</span>)}{$1<a target="editor" href="/editor+$path+$2">$2</a>$3}g;
221    
222            # strip page html
223    #       $source =~ s{^.*<body[^>]+>}{}s;
224    #       $source =~ s{</body.*$}{}s;
225    
226    
227            my $runnable = join("\n",
228                    map {
229                            qq|<a href="/$class/$_">$_</a>|
230                    } $self->class_runnable( $class )
231            );
232            $runnable = " runnable: $runnable" if $runnable;
233    
234            $self->title( $class );
235    
236            return join("\n",
237                    qq|<h1>$class</h1>|,
238                    qq|<div class="frey-introspect">$superclasses\n$roles\n$runnable\n|,
239                    $pod    ? qq|<a class="frey-skip" href="#___top" title="Skip to POD"    >pod</a>|    : '',
240                    $source ? qq|<a class="frey-skip" href="#source" title="Skip to source" >source</a>| : '',
241                    qq|$table\n$pod\n</div>\n|,
242                    qq|<h1>Source</h1><a name="source"></a><div class="frey-source">$source</div>|,
243            );
244  }  }
245    
246  =head1 SEE ALSO  =head1 SEE ALSO

Legend:
Removed from v.66  
changed lines
  Added in v.471

  ViewVC Help
Powered by ViewVC 1.1.26