lib/Stepford/Grapher.pm

filter: Statementstmt 96.5% 85 / 88 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 18 / 18 Podpod 50.0% 1 / 2 totaltotal 87.1% 115 / 132 CC 12 SCAR 10.3
CC
12
Cov
89%
_build_step_deps
6.3
_build_step_classes
4.2
_deps_for
3.0
CRAP 2.8
↑ Summary script/graph-stepford.pl »
1 package Stepford::Grapher;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Stepford::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '1.01';
13  
14 2 ▶use Stepford::Grapher::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 ArrayRef ArrayOfSteps ArrayOfClassPrefixes HashRef Int Step Str
16 2 );
17  
18 2 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 has step => (
21 is => 'ro',
22 isa => Str,
23 required => 1,
24 );
25  
26 2 has _step_classes => (
27 is => 'ro',
28 isa => ArrayOfSteps,
29 init_arg => 'step_classes',
30 lazy => 1,
31 builder => '_build_step_classes',
32 );
33  
34 2 has step_namespace => (
35 traits => ['Array'],
36 is => 'ro',
37 isa => ArrayOfClassPrefixes,
38 required => 1,
39 handles => {
40 all_step_namespaces => 'elements',
41 },
42 );
43  
44 2 has _renderer => (
45 is => 'ro',
46 does => 'Stepford::Grapher::Role::Renderer',
47 init_arg => 'renderer',
48 required => 1,
49 );
50  
51 2 has depth => (
52 is => 'ro',
53 isa => Int,
54 default => 0,
55 );
56  
57 # We want to preload all the step classes so that the final_steps passed to
58 # run are recognized as valid classes.
59 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
60 1 my $self = shift;
61  
62 1 $self->_step_classes;
63  
64 1 return;
65 }
66  
67 ########################################################################
68 # cargo culted from Stepford::Runner
69 # ugh, but whatchagonnado?
70  
71 1 ▶sub _build_step_classes {
Subroutine _build_step_classes: called CC=4 SCAR=14.2
72 1 my $self = shift;
73  
74 # Module::Pluggable does not document whether it returns class names in
75 # any specific order.
76 1 my $sorter = $self->_step_class_sorter;
77  
78 1 my @classes;
79  
80 1 for my $class (
81 19 sort { $sorter->() } Module::Pluggable::Object->new(
82 search_path => [ $self->all_step_namespaces ],
83 require => 1,
84 )->plugins
85 ) {
86  
87 # We need to skip roles
88 10 ▶ next unless $class->isa('Moose::Object');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Moose::Object") 0 10 10
89  
90 10 ▶ unless ( $class->does('Stepford::Role::Step') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->does("Stepford::Role::Step")) 0 10 10
91 0 Stepford::Error->throw( message =>
92 qq{Found a class which doesn't do the Stepford::Role::Step role: $class}
93 );
94 }
95  
96 # $self->logger->debug("Found step class $class");
97 10 push @classes, $class;
98 }
99  
100 1 return \@classes;
101 }
102  
103 1 ▶sub _step_class_sorter {
Subroutine _step_class_sorter: called CC=1 SCAR=0.0
104 1 my $self = shift;
105  
106 1 my $x = 0;
107 1 my @namespaces = $self->all_step_namespaces;
108 1 my %order = map { $_ => $x++ } @namespaces;
109  
110 19 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
111 19 ▶ my $a_prefix = first { $a =~ /^\Q$_/ } @namespaces;
Subroutine __ANON__: called CC=1 SCAR=0.0
112 19 ▶ my $b_prefix = first { $b =~ /^\Q$_/ } @namespaces;
Subroutine __ANON__: called CC=1 SCAR=0.0
113  
114 19 ▶ return ( $order{$a_prefix} <=> $order{$b_prefix} or $a cmp $b );
Condition33%1/3
$order{$a_prefix} <=> $order{$b_prefix} || $a cmp $b
l!l&&r!l&&!r
0190
MC/DC0%0/2
$order{$a_prefix} <=> $order{$b_prefix} || $a cmp $b
$order{$a_prefix} <=> $order{$b_prefix} $a cmp $b
Truth table33%1/3
A || B
A= $order{$a_prefix} <=> $order{$b_prefix}B= $a cmp $b
ABresult
1X1
011
000
115 1 };
116 }
117  
118 ########################################################################
119  
120 # has _renderer => (
121 # is => 'ro',
122 # init_arg => 'renderer',
123 # does => 'Stepford::Grapher::Role::Renderer',
124 # required => 1,
125 # );
126  
127 2 has _step_deps => (
128 traits => ['Hash'],
129 is => 'ro',
130 isa => HashRef [ HashRef [Step] ],
131 lazy => 1,
132 builder => '_build_step_deps',
133 );
134  
135 1 ▶sub _build_step_deps {
Subroutine _build_step_deps: called CC=6 SCAR=18.3
136 1 my $self = shift;
137  
138 1 ▶ unless ( is_Step( $self->step ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_Step($self->step)) 0 1 1
139 0 Stepford::Error->throw( message => $self->step
140 . q{ is not a valid Step (maybe your step_namespace didn't load it?)}
141 );
142 }
143  
144 1 my $depth = 0;
145  
146 1 my %steps;
147 1 my @todo_steps = ( $self->step );
148 1 ▶ while (@todo_steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo_steps) 10 1 11
149 10 ▶ last if $self->depth && $depth > $self->depth;
Branch: 1 branch
BranchTrueFalseTotal
if $self->depth and $depth > $self->depth 0 10 10
Condition33%1/3
$self->depth and $depth > $self->depth
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->depth and $depth > $self->depth
$self->depth $depth > $self->depth
Truth table33%1/3
A and B
A= $self->depthB= $depth > $self->depth
ABresult
0X0
100
111
150  
151 10 my $step = shift @todo_steps;
152 10 ▶ next if $steps{$step};
Branch: 1 branch
BranchTrueFalseTotal
if $steps{$step} 1 9 10
153  
154 9 $steps{$step} = $self->_deps_for($step);
155 9 push @todo_steps, values %{ $steps{$step} };
156  
157 9 $depth++;
158 }
159  
160 1 return \%steps;
161 }
162  
163 9 ▶sub _deps_for {
Subroutine _deps_for: called CC=3 SCAR=11.1
164 9 my $self = shift;
165 9 my $step = shift;
166  
167 9 my %return_values;
168  
169 9 for my $dep ( $step->dependencies ) {
170 9 my $name = $dep->name;
171  
172 # inefficent; Should probably make this quicker
173 52 ▶ my $producing_class = first {
Subroutine __ANON__: called CC=1 SCAR=0.0
174 52 $_->has_production($name)
175 }
176 9 @{ $self->_step_classes };
177  
178 9 ▶ unless ($producing_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($producing_class) 0 9 9
179 0 Stepford::Error->throw(
180 message => "Cannot resolve step dependancy '$name'" );
181 }
182 9 $return_values{$name} = $producing_class;
183 }
184  
185 9 return \%return_values;
186 }
187  
188 ########################################################################
189  
190 2 with 'MooseX::Getopt::Dashes';
191  
192 1 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
193 1 my $self = shift;
194 1 $self->_renderer->render( $self->_step_deps );
195 1 return 0;
196 }
197  
198 2 __PACKAGE__->meta->make_immutable;
199 2 1;
200  
201 =pod
202  
203 =encoding UTF-8
204  
205 =head1 NAME
206  
207 Stepford::Grapher - produce graphs of Stepford Dependencies
208  
209 =head1 VERSION
210  
211 version 1.01
212  
213 =head1 SYNOPSIS
214  
215 From the shell:
216  
217 foo@bar:~/steps$ graph-stepford.pl --step-namespace=My::Step --step=My::Step::ExampleStep --output='diagram.png'
218  
219 Or from code:
220  
221 my $grapher = Stepford::Grapher->new(
222 step => 'My::Step::ExampleStep',
223 step_namespaces => ['My::Steps'],
224 renderer => Stepford::Grapher::Renderer::Graphviz->new(
225 output => 'diagram.png',
226 ),
227 );
228 $grapher->run;
229  
230 =head1 DESCRIPTION
231  
232 STOP: The most common usage for this module is to use the command line C<graph-
233 stepford.pl> program. You should read the documentation for C<graph-stepford.pl>
234 to see how that works.
235  
236 =head1 ATTRIBUTES
237  
238 =head2 step
239  
240 A string containing the class name of the step you wish to create a diagram for.
241  
242 Required.
243  
244 =head2 step_namespace
245  
246 An array of strings containing the prefixes of step class names that should
247 be loaded. This must contain the step class passed in the C<step> parameter.
248  
249 For example, if you have the steps C<My::Step::Foo>, C<My::Step::Bar>,
250 C<ThirdyParty::Step::Baz> you would need to pass
251 C<['My::Step','ThirdParty::Step']>
252  
253 All classes under the prefix will be loaded and it is an error if any of those
254 classes are do not consume the L<Stepford::Role::Step> role (this is the same
255 behavior as Stepford itself.)
256  
257 Required.
258  
259 =head2 depth
260  
261 If this is provided, the graph will not go more than this number of levels
262 back from the target step.
263  
264 By default, this is zero and all levels are included.
265  
266 =head2 renderer
267  
268 The renderer instance (i.e. an instance of something that consumes the
269 L<Stepford::Grapher::Role::Renderer> role.)
270  
271 Required.
272  
273 =head1 METHOD
274  
275 =head2 run
276  
277 Use the renderer to render the dependencies graph.
278  
279 =head1 SUPPORT
280  
281 Bugs may be submitted through L<https://github.com/maxmind/Stepford-Grapher/issues>.
282  
283 =head1 AUTHOR
284  
285 Mark Fowler <mfowler@maxmind.com>
286  
287 =head1 CONTRIBUTOR
288  
289 =for stopwords Dave Rolsky
290  
291 Dave Rolsky <drolsky@maxmind.com>
292  
293 =head1 COPYRIGHT AND LICENSE
294  
295 This software is copyright (c) 2016 - 2017 by MaxMind, Inc..
296  
297 This is free software; you can redistribute it and/or modify it under
298 the same terms as the Perl 5 programming language system itself.
299  
300 =cut
301  
302 __END__
↑ Summary script/graph-stepford.pl »