1 |
package Frey::PPI; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey::ClassLoader'; |
5 |
|
6 |
use PPI; |
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
has 'class' => ( |
10 |
is => 'rw', |
11 |
isa => 'Str', |
12 |
required => 1, |
13 |
documentation => 'Name of class to parse', |
14 |
); |
15 |
|
16 |
has 'doc' => ( |
17 |
is => 'ro', |
18 |
isa => 'PPI::Document', |
19 |
lazy => 1, |
20 |
default => sub { |
21 |
my ( $self ) = @_; |
22 |
my $path = $self->class; |
23 |
$path = $self->class_path( $path ) unless $path =~ m{/}; |
24 |
warn "# doc from ", $self->class, " at ", $path if $self->debug; |
25 |
my $doc = PPI::Document->new( $path ); |
26 |
$doc->prune('PPI::Token::Whitespace'); |
27 |
return $doc; |
28 |
}, |
29 |
); |
30 |
|
31 |
sub find { |
32 |
my ( $self, $coderef ) = @_; |
33 |
|
34 |
my $doc = $self->doc; |
35 |
$doc->find(sub { |
36 |
my ( $doc,$el ) = @_; |
37 |
eval { $coderef->( $doc, $el ) }; |
38 |
warn "ERROR: $@" if $@; |
39 |
}); |
40 |
} |
41 |
|
42 |
sub attribute_order { |
43 |
my ( $self ) = @_; |
44 |
|
45 |
my @attribute_order; |
46 |
$self->find(sub { |
47 |
my ($doc,$el) = @_; |
48 |
return unless |
49 |
$el->isa('PPI::Statement') && |
50 |
$el->{children}->[0]->isa('PPI::Token::Word') && |
51 |
$el->{children}->[0]->literal eq 'has'; |
52 |
|
53 |
warn "## has ",$el->{children}->[1]->literal if $self->debug; |
54 |
push @attribute_order, $el->{children}->[1]->literal; |
55 |
}); |
56 |
|
57 |
warn "# ", $self->class, " attribute_order ", dump( @attribute_order ) if $self->debug; |
58 |
|
59 |
return @attribute_order if wantarray; |
60 |
return \@attribute_order; |
61 |
} |
62 |
|
63 |
sub includes { |
64 |
my $self = shift; |
65 |
|
66 |
my $include; |
67 |
$self->find(sub { |
68 |
my ($doc,$el) = @_; |
69 |
return unless $el->isa('PPI::Statement::Include'); |
70 |
|
71 |
warn "## include ",dump( $el->module, $el->type, $el->pragma ) if $self->debug; |
72 |
push @{ $include->{ $el->type } }, $el->module |
73 |
}); |
74 |
warn "# ", $self->class, " include ", dump( $include ) if $self->debug; |
75 |
return $include; |
76 |
} |
77 |
|
78 |
our $class_has_tests; |
79 |
|
80 |
sub parse_tests { |
81 |
my ( $self ) = @_; |
82 |
|
83 |
map { |
84 |
warn "## ppi test $_" if $self->debug; |
85 |
|
86 |
my $doc = Frey::PPI->new( class => $_ ); |
87 |
my @tests; |
88 |
|
89 |
# warn "## ",dump( $doc ); |
90 |
|
91 |
$doc->find(sub { |
92 |
my ($doc,$el) = @_; |
93 |
return unless |
94 |
$el->isa('PPI::Statement') && |
95 |
$el->{children}->[0]->isa('PPI::Token::Word') && |
96 |
$el->{children}->[0]->literal eq 'use_ok'; |
97 |
|
98 |
# warn "## ",dump( $el ); |
99 |
my $class = $el->child(1)->child(0)->child(0)->literal; |
100 |
$class_has_tests->{$class}->{$_}++; |
101 |
}); |
102 |
} glob 't/*.t'; |
103 |
warn "# collected class tests ",dump( $class_has_tests ) if $self->debug; |
104 |
|
105 |
return $class_has_tests; |
106 |
} |
107 |
|
108 |
=head2 has_tests |
109 |
|
110 |
my @tests = $self->has_tests; |
111 |
|
112 |
=cut |
113 |
|
114 |
sub has_tests { |
115 |
my ($self) = shift; |
116 |
|
117 |
$self->parse_tests unless $class_has_tests; |
118 |
|
119 |
my $class = $self->class; |
120 |
|
121 |
if ( my $tests = $class_has_tests->{ $class } ) { |
122 |
my @tests = keys %$tests; |
123 |
warn "# has_tests $class ",dump( @tests ); |
124 |
return @tests if wantarray; |
125 |
return \@tests; |
126 |
} |
127 |
} |
128 |
|
129 |
=head2 as_data |
130 |
|
131 |
Debugging output |
132 |
|
133 |
=cut |
134 |
|
135 |
sub as_data { |
136 |
my $self = shift; |
137 |
return { |
138 |
includes => $self->includes, |
139 |
attribute_order => [ $self->attribute_order ], |
140 |
doc => $self->doc, |
141 |
}; |
142 |
} |
143 |
|
144 |
1; |