| 1 | package Stepford::GraphBuilder; | ||||||||||
| 2 | |||||||||||
| 3 | 39 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 39 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 39 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 39 | our $VERSION = '0.006001'; | |||||||||
| 8 | |||||||||||
| 9 | 39 | ▶ | use List::AllUtils qw( sort_by uniq ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 39 | ▶ | use Stepford::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 39 | ▶ | use Stepford::FinalStep; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 39 | ▶ | use Stepford::Graph (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 39 | ▶ | use Stepford::Types qw( ArrayOfSteps HashRef Logger Step ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 39 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 39 | ▶ | use MooseX::StrictConstructor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 39 | has config => ( | |||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | isa => HashRef, | ||||||||||
| 21 | required => 1, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 39 | has _step_classes => ( | |||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | isa => ArrayOfSteps, | ||||||||||
| 27 | init_arg => 'step_classes', | ||||||||||
| 28 | required => 1, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 39 | has _final_steps => ( | |||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | isa => ArrayOfSteps, | ||||||||||
| 34 | init_arg => 'final_steps', | ||||||||||
| 35 | required => 1, | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 39 | has graph => ( | |||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | isa => 'Stepford::Graph', | ||||||||||
| 41 | lazy => 1, | ||||||||||
| 42 | builder => '_build_graph', | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 39 | has _production_map => ( | |||||||||
| 46 | is => 'ro', | ||||||||||
| 47 | isa => HashRef [Step], | ||||||||||
| 48 | init_arg => undef, | ||||||||||
| 49 | lazy => 1, | ||||||||||
| 50 | builder => '_build_production_map', | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 39 | has logger => ( | |||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | isa => Logger, | ||||||||||
| 56 | required => 1, | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | has _graph_cache => ( | ||||||||||
| 60 | traits => ['Hash'], | ||||||||||
| 61 | is => 'ro', | ||||||||||
| 62 | isa => HashRef, | ||||||||||
| 63 | 151 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 39 | handles => { | |||||||||
| 65 | _cache_graph => 'set', | ||||||||||
| 66 | _get_cached_graph => 'get', | ||||||||||
| 67 | }, | ||||||||||
| 68 | ); | ||||||||||
| 69 | |||||||||||
| 70 | 150 | ▶ | sub _build_graph { | ||||||||
|
Subroutine
_build_graph: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 150 | my $self = shift; | |||||||||
| 72 | |||||||||||
| 73 | my $graph = Stepford::Graph->new( | ||||||||||
| 74 | config => $self->config, | ||||||||||
| 75 | logger => $self->logger, | ||||||||||
| 76 | step_class => 'Stepford::FinalStep', | ||||||||||
| 77 | children_graphs => [ | ||||||||||
| 78 | 148 | ▶ | sort_by { $_->step_class } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 150 | map { $self->_create_graph( $_, {} ) } @{ $self->_final_steps } | |||||||||
| 80 | ], | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | $self->logger->debug( 'Graph for ' | ||||||||||
| 84 | 147 | . ( join q{ - }, @{ $self->_final_steps } ) . ":\n" | |||||||||
| 85 | . $graph->as_string ); | ||||||||||
| 86 | |||||||||||
| 87 | 147 | return $graph; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 150 | ▶ | sub _build_production_map { | ||||||||
|
Subroutine
_build_production_map: called CC=4 SCAR=13.9
| |||||||||||
| 91 | 150 | my $self = shift; | |||||||||
| 92 | |||||||||||
| 93 | 150 | my %map; | |||||||||
| 94 | 150 | for my $class ( @{ $self->_step_classes } ) { | |||||||||
| 95 | 623 | for my $attr ( map { $_->name } $class->productions ) { | |||||||||
| 96 | 699 | ▶ | next if exists $map{$attr}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | 698 | $map{$attr} = $class; | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 150 | return \%map; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 573 | ▶ | sub _create_graph { | ||||||||
|
Subroutine
_create_graph: called CC=3 SCAR=11.0
| |||||||||||
| 106 | 573 | my $self = shift; | |||||||||
| 107 | 573 | my $step_class = shift; | |||||||||
| 108 | 573 | my $parents = shift; | |||||||||
| 109 | |||||||||||
| 110 | Stepford::Error->throw( | ||||||||||
| 111 | "The set of dependencies for $step_class is cyclical") | ||||||||||
| 112 | 573 | ▶ | if exists $parents->{$step_class}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | my $childrens_parents = { | ||||||||||
| 115 | 572 | %{$parents}, | |||||||||
| 116 | $step_class => 1, | ||||||||||
| 117 | }; | ||||||||||
| 118 | |||||||||||
| 119 | 572 | ▶ | if ( my $graph = $self->_get_cached_graph($step_class) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 6 | return $graph; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 566 | my $graph = Stepford::Graph->new( | |||||||||
| 124 | config => $self->config, | ||||||||||
| 125 | logger => $self->logger, | ||||||||||
| 126 | step_class => $step_class, | ||||||||||
| 127 | children_graphs => | ||||||||||
| 128 | $self->_create_children_graphs( $step_class, $childrens_parents ), | ||||||||||
| 129 | ); | ||||||||||
| 130 | |||||||||||
| 131 | 562 | $self->_cache_graph( $step_class => $graph ); | |||||||||
| 132 | |||||||||||
| 133 | 562 | return $graph; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 566 | ▶ | sub _create_children_graphs { | ||||||||
|
Subroutine
_create_children_graphs: called CC=1 SCAR=0.0
| |||||||||||
| 137 | 566 | my $self = shift; | |||||||||
| 138 | 566 | my $step_class = shift; | |||||||||
| 139 | 566 | my $childrens_parents = shift; | |||||||||
| 140 | |||||||||||
| 141 | my @children_steps = uniq sort | ||||||||||
| 142 | 566 | map { $self->_step_for_dependency( $step_class, $_->name ) } | |||||||||
| 143 | $step_class->dependencies; | ||||||||||
| 144 | |||||||||||
| 145 | 564 | return [ map { $self->_create_graph( $_, $childrens_parents ) } | |||||||||
| 146 | @children_steps ]; | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 542 | ▶ | sub _step_for_dependency { | ||||||||
|
Subroutine
_step_for_dependency: called CC=4 SCAR=13.9
| |||||||||||
| 150 | 542 | my $self = shift; | |||||||||
| 151 | 542 | my $parent_step = shift; | |||||||||
| 152 | 542 | my $dep = shift; | |||||||||
| 153 | |||||||||||
| 154 | # if a dependency exists in the config, we don't need to build it. | ||||||||||
| 155 | 542 | ▶ | return if exists $self->config->{$dep}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | |||||||||||
| 157 | 542 | my $map = $self->_production_map; | |||||||||
| 158 | |||||||||||
| 159 | Stepford::Error->throw( "Cannot resolve a dependency for $parent_step." | ||||||||||
| 160 | . " There is no step that produces the $dep attribute." | ||||||||||
| 161 | . ' Do you have a cyclic dependency?' ) | ||||||||||
| 162 | 542 | ▶ | unless $map->{$dep}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | |||||||||||
| 164 | Stepford::Error->throw( | ||||||||||
| 165 | "A dependency ($dep) for $parent_step resolved to the same step.") | ||||||||||
| 166 | 541 | ▶ | if $map->{$dep} eq $parent_step; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | |||||||||||
| 168 | 540 | $self->logger->debug( | |||||||||
| 169 | "Dependency $dep for $parent_step is provided by $map->{$dep}"); | ||||||||||
| 170 | |||||||||||
| 171 | 540 | return $map->{$dep}; | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 39 | __PACKAGE__->meta->make_immutable; | |||||||||
| 175 | |||||||||||
| 176 | 39 | 1; | |||||||||
| 177 | |||||||||||
| 178 | # ABSTRACT: Represents a concrete plan for execution by a Stepford::Runner | ||||||||||
| 179 | |||||||||||
| 180 | __END__ | ||||||||||