File Coverage

blib/lib/ExtUtils/Builder/Serializer.pm
Criterion Covered Total %
statement 54 59 91.5
branch 3 6 50.0
condition n/a
subroutine 14 15 93.3
pod 8 8 100.0
total 79 88 89.7


line stmt bran cond sub pod time code
1             package ExtUtils::Builder::Serializer;
2             $ExtUtils::Builder::Serializer::VERSION = '0.020';
3 2     2   244087 use strict;
  2         6  
  2         91  
4 2     2   12 use warnings;
  2         3  
  2         170  
5              
6 2     2   12 use Carp 'croak';
  2         5  
  2         191  
7              
8 2     2   1034 use ExtUtils::Builder::Action::Command;
  2         9  
  2         88  
9 2     2   634 use ExtUtils::Builder::Action::Code;
  2         6  
  2         83  
10 2     2   547 use ExtUtils::Builder::Node;
  2         20  
  2         96  
11 2     2   653 use ExtUtils::Builder::Plan;
  2         5  
  2         1445  
12              
13             sub serialize_plan {
14 1     1 1 717 my ($self, $plan) = @_;
15              
16 1         3 my %nodes;
17 1         6 for my $node_name ($plan->node_names) {
18 4         15 $nodes{$node_name} = $self->serialize_node($plan->node($node_name));
19             }
20              
21             return {
22 1         7 nodes => \%nodes,
23             }
24             }
25              
26             sub serialize_node {
27 4     4 1 10 my ($self, $node, %opts) = @_;
28 4         13 my @actions = map { $self->serialize_action($_, %opts) } $node->flatten;
  3         11  
29             return {
30 4         14 dependencies => [ $node->dependencies ],
31             actions => \@actions,
32             type => $node->type,
33             }
34             }
35              
36             sub serialize_action {
37 3     3 1 7 my ($self, $action, %opts) = @_;
38 3         16 my $preference = $action->preference('code', 'command');
39 3 50       10 my $method = $preference eq 'code' ? 'serialize_code' : 'serialize_command';
40 3         13 return $self->$method($action, %opts);
41             }
42              
43             sub serialize_code {
44 3     3 1 8 my ($self, $action, %opts) = @_;
45 3         11 return [ 'code', $action->to_code_hash(%opts) ];
46             }
47              
48             sub serialize_command {
49 0     0 1 0 my ($self, $action, %opts) = @_;
50 0         0 return map { [ 'command', $_ ] } $action->to_command(%opts)
  0         0  
51             }
52              
53              
54             sub deserialize_plan {
55 1     1 1 453 my ($self, $serialized, %options) = @_;
56              
57 1         3 my %nodes;
58 1         3 for my $node_name (keys %{ $serialized->{nodes} }) {
  1         6  
59 4         16 $nodes{$node_name} = $self->deserialize_node($node_name, $serialized->{nodes}{$node_name}, %options);
60             }
61              
62 1         6 return ExtUtils::Builder::Plan->new(
63             nodes => \%nodes,
64             );
65             }
66              
67             sub deserialize_node {
68 4     4 1 11 my ($self, $name, $serialized, %options) = @_;
69              
70 4         8 my @actions = map { $self->deserialize_action($_, %options) } @{ $serialized->{actions} };
  3         11  
  4         11  
71              
72             return ExtUtils::Builder::Node->new(
73             target => $name,
74 4         33 dependencies => [ @{ $serialized->{dependencies} } ],
75             actions => \@actions,
76             type => $serialized->{type},
77 4         8 );
78             }
79              
80             sub deserialize_action {
81 3     3 1 7 my ($self, $serialized, %options) = @_;
82 3         5 my ($command, @args) = @{$serialized};
  3         7  
83              
84 3 50       12 if ($command eq 'command') {
    50          
85 0         0 return ExtUtils::Builder::Action::Command->new(command => $args[0]);
86             } elsif ($command eq 'code') {
87 3         8 return map { ExtUtils::Builder::Action::Code->new(%$_) } @args;
  3         14  
88             } else {
89 0           croak "Unknown serialized command $command";
90             }
91             }
92              
93             1;
94              
95             #ABSTRACT:
96              
97             __END__