--- trunk/lib/Frey/Introspect.pm 2008/07/03 19:51:18 51 +++ trunk/lib/Frey/Introspect.pm 2009/02/03 21:24:02 1034 @@ -2,84 +2,67 @@ use Moose; use Carp; -use Class::MOP; -use Moose::Meta::Role; -use Moose::Meta::Class; -use Scalar::Util qw/blessed/; +#use Moose::Meta::Role; +#use Moose::Meta::Class; use Data::Dump qw/dump/; use File::Slurp; use List::Util; -extends 'Frey'; +use PPI; +use PPI::HTML; -has 'package' => ( - is => 'rw', - isa => 'Str', - required => 1, -); +use lib 'lib'; +use Frey::Pod; -has 'renderHTML' => ( - is => 'rw', - isa => 'Str', -); +extends 'Frey::PPI'; +with 'Frey::Web'; +with 'Frey::Storage'; + +=head1 DESCRIPTION -has 'path' => ( +Provide introspection on any perl class installed on system + +=cut + +has 'class' => ( is => 'rw', + isa => 'Str', + required => 1, ); -=head2 examine +=head2 joose - my $js = $o->examine( 'Some::Package' ); + my $js = $o->joose; =cut -sub examine { +sub joose { my ($self) = @_; - my $package = $self->package; + my ( $meta, $is_role ) = $self->class_meta; - #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 $@; - - #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; + if ( ! $is_role ) { my @superclasses = map{ $_->meta->name } grep { $_ ne 'Moose::Object' } $meta->superclasses; - warn "superclasses ",dump( @superclasses ); + warn "superclasses ",dump( @superclasses ) if $self->debug; } my $out; - my ( $m, $c ) = split(/::/, $class->name, 2); + my ( $m, $c ) = split(/::/, $self->class, 2); my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js'; + $c ||= ''; + $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n"; - foreach ( $class->get_attribute_list ) { + foreach ( $meta->get_attribute_list ) { $out .= "\t\t\t$_: {\n"; - my $attr = $class->get_attribute($_); - my $is = $attr->_is_metadata; + my $attr = $meta->get_attribute($_); + my $is = eval { $attr->_is_metadata; }; + return if $@; + $out .= "\t\t\t\tis: \"$is\",\n" if defined $is; $out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy; $out .= "\t\t\t\trequired: true,\n" if $attr->is_required; @@ -103,7 +86,7 @@ $out .= "\t\t},\n\t\tmeta: Frey.HTML, classMethods: { renderHTML: function () { - return new Joose.SimpleRequest().getText(\"json?class=$c\") + return new Joose.SimpleRequest().getText(\"/" . $self->class . "\") },\n"; $out .= "\t\t},\n"; @@ -113,27 +96,297 @@ $out =~ s/,\n$/\n/; $out .= "});\n"; - $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 ); - - warn "method_list = ",dump( $class->get_method_list ); - warn dump( map{ $class->get_method($_)->name } sort $class->get_method_list ); + warn "method_list = ",dump( $meta->get_method_list ) if $self->debug; # print $out; my $path = "static/blib/$filename"; write_file( $path, $out ); warn "# created $path\n"; - $self->path( $path ); return $out; } +sub as_markup { + my ( $self ) = @_; + + $self->add_head( 'static/introspect.css' ); + + my ( $meta, $is_role ) = $self->class_meta; + + my $class = $self->class; + $self->title( $class ); + + my $introspect_path = "var/introspect/$class.yaml"; + $self->mkbasepath( $introspect_path ); + my $introspect; # FIXME update with = $self->load( $introspect_path ); + + my ( $superclasses, $roles ) = ( 'Role', '' ); + if ( ! $is_role ) { + if ( $meta->superclasses ) { + $superclasses = 'Superclasses: ' . + join(', ', + map { + my $name = $_->meta->name; + $introspect->{superclass}->{$name} = {}; + + qq|$name| . + $self->dropdown( qq|?|, $_->meta ) + } + #grep { $_ ne 'Moose::Object' } + $meta->superclasses + ); + } + } + + my $method_from_role; + my $attribute_from_role; + + if ( $meta->can('roles') ) { + my $role_nr = 1; + $roles = join(' ', + grep { ! m/\Q$class\E/ } # skip me + map { + my $name = $_->name; + $introspect->{roles}->{$name} = {}; + $method_from_role->{ $_ }->{$name} = $role_nr foreach $_->get_method_list; + $attribute_from_role->{ $_ }->{$name} = $role_nr foreach $_->get_attribute_list; + + qq|$name| +# . qq|| . $self->dropdown( $role_nr++, $name->meta ) . qq|| + . $self->dropdown( + qq|| . $role_nr++ . qq||, + $name->meta + ) + ; + } + $meta->calculate_all_roles + ); + $roles = qq|with roles: $roles| if $roles; + } + warn "# method_from_role ",dump( $method_from_role ); + + my @methods; + @methods = map { + my $name = $_; + if ( $method_from_role->{$name} ) { + my ( $role_name, $nr ) = each %{ $method_from_role->{$name} }; + $introspect->{methods}->{$name}->{role} = $role_name; + $name .= qq|$nr|; + } else { + $introspect->{methods}->{$name} = {}; + } + qq|$name| + } sort { + lc($a) cmp lc($b) + } $self->class_methods( $class ); + + my @attributes; + if ( $meta->get_attribute_list ) { + @attributes = map { + my $name = $_; + $introspect->{attribute}->{$name} = {}; + my $html_name = $name; + my $attr = $meta->get_attribute($name); + confess "$class attribute $name isn't blessed ",dump( $attr ) unless blessed $attr; + warn "## attr $name ref ",ref( $attr ) if $self->debug; + + my ( $title, $properties ) = ( '', '' ); + if ( $attr->can('is_required') && $attr->is_required ) { + ( $html_name, $title ) = ( "$name", ' title="required"' ); + $introspect->{attribute}->{$name}->{required} = 1; + } + + 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/ ) { + my $getter; + + $getter = $check; + $getter =~ s/^has_//; + + if ( $attr->can($check) && $attr->$check ) { + $properties .= $check; + # we need our dump here instead of $attr->$getter->dump because default can return scalar + my $v = $attr->$getter; + $properties .= ref($v) + ? $self->dropdown( qq|?|, $attr->$getter ) + : qq| $v| + ; + } + $properties .= ' '; + } + my $type = $attr->can('has_type_constraint') && $attr->has_type_constraint ? $attr->type_constraint->name : ''; + + if ( $attribute_from_role->{$name} ) { + my ( $role_name, $nr ) = each %{ $attribute_from_role->{$name} }; + $name .= qq|$nr|; + } + + if ( my $doc = eval { $attr->documentation } ) { + $properties = qq| + $properties + $doc + |; + $self->add_css(qq| + span.documentation { + background: #eee; + padding: 0.25em; + float: left; + clear: left; + } + |); + $introspect->{action}->{$name}->{documentation} = $doc; + } + + qq|$html_name$type$properties| + } sort $meta->get_attribute_list + } + + my $table = qq||; + while ( @methods || @attributes ) { + my ($m,$a) = ( shift @methods, shift @attributes ); + $m ||= ''; + $a ||= ''; + $table .= qq|$m$a|; + } + $table .= qq|
MethodsAttributesTypeProperties
|; + + my $path = $self->class_path( $class ); + + my ( $pod_toc, $pod ) = Frey::Pod->new( class => $class, request_url => $self->request_url )->as_markup; + return $pod_toc . $pod if $path =~ m{\.pod}; + + warn "# ", $pod_toc ? 'toc' : '', ' ', $pod ? 'pod' : ''; + + my $Document = PPI::Document->new( $path ); + + # Create a reusable syntax highlighter + my $Highlight = PPI::HTML->new( + line_numbers => 1, +# page => 1, +# colors => { +# line_number => '#CCCCCC', +# number => '#990000', +# }, + ); + + # Spit out the HTML + my $source = $Highlight->html( $Document ); + + $source =~ s{(\s*)(\d+)(:\s*)}{$1$2$3}g; + + # strip page html +# $source =~ s{^.*]+>}{}s; +# $source =~ s{
", + map { + $introspect->{runnable}->{$_} = {}; + my $short = $_; + $short =~ s{_as_(?:markup|data|sponge)$}{}; + qq|$short| + } $self->class_runnable( $class ) + ); + $runnable = "
runnable
$runnable
" if $runnable; + + my $has_tests = ''; + my @tests = sort { lc($a) cmp lc($b) } grep { defined $_ } $self->has_tests; + if ( @tests ) { + $has_tests + = qq|
test| + . ( $#tests > 0 ? 's' : '' ) + . qq|
| + . join("
", map { + qq|$_| + } @tests ) + . qq|
| + ; + $introspect->{tests} = [ @tests ], + } + + my $includes = ''; + if ( my $inc = $self->includes ) { + $introspect->{includes} = $inc; + foreach my $type ( keys %$inc ) { + $includes + .= ucfirst($type) + . qq|: | + . join("\n", + map { + qq|$_| + } @{ + $inc->{$type} + } + ) + ; + } + } + + $self->store( $introspect_path, $introspect ); + + $self->add_css(qq| + .frey-introspect-right { + position: fixed; + top: 1em; + right: 1em; + z-index: 10; + background: #ffc; + padding: 0.5em; + width: 20%; + font-size: 80%; + } + .frey-introspect-right dl dd { + margin-left: 1em; + } + + /* fix pod */ + .frey-introspect-right dd ul { + padding-left: 0; + } + .frey-introspect-right dl ul > li { + list-style: none; + } + |); + + my $has_pod = qq| +
pod
+
$pod_toc
+ | if $pod_toc; + + my $has_source = qq| +
source
+ | if $source; + + my $right = qq| +
+ $runnable + $has_tests + $has_pod + $has_source +
+ |; + + + return + qq| +

$class

+
+ $superclasses $roles +
$includes +
+
+ $right +
+ $table + $pod + + +

Source

+
$source
+ | + ; +} + =head1 SEE ALSO L on which this code is based