WiX3/XML/Directory.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 162 CC 11 SCAR 27.2
CC
11
Cov
0%
BUILDARGS
42.0
add_directory
20.0
get_directory_id
6.0
CRAP 15.1
1 package WiX3::XML::Directory;
2  
3 use 5.008003;
4  
5 # Must be done before Moose, or it won't get picked up.
6 use metaclass (
7 metaclass => 'Moose::Meta::Class',
8 error_class => 'WiX3::Util::Error',
9 );
10 use Moose 2;
11 use MooseX::Types::Moose qw( Int Str Maybe ArrayRef );
12 use WiX3::Types qw( ComponentGuidType );
13 use WiX3::XML::TagTypes qw( DirectoryChildTag );
14 use WiX3::Util::StrictConstructor;
15 use Params::Util qw( _IDENTIFIER _STRING );
16 use File::Spec::Functions qw( catdir );
17  
18 our $VERSION = '0.011';
19  
20 with qw(WiX3::XML::Role::TagAllowsChildTags
21 WiX3::XML::Role::GeneratesGUID
22 WiX3::Role::Traceable
23 );
24  
25 ## Allows Component, Directory, Merge, and SymbolPath as children.
26 ## SymbolPath will need added later.
27  
28 has '+child_tags' => ( isa => ArrayRef [DirectoryChildTag] );
29  
30  
31 #####################################################################
32 # Accessors:
33 # None.
34  
35 has id => (
36 is => 'ro',
37 isa => Str,
38 reader => 'get_id',
39 builder => 'id_build',
40 lazy => 1,
41 );
42  
43 # Path helps us in path searching.
44 has path => (
45 is => 'ro',
46 isa => Maybe [Str],
47 reader => 'get_path',
48 default => undef,
49 );
50  
51 has noprefix => (
52 is => 'ro',
53 isa => Maybe [Str],
54 reader => '_get_noprefix',
55 default => undef,
56 );
57  
58 has diskid => (
59 is => 'ro',
60 isa => Maybe [Int],
61 reader => '_get_diskid',
62 init_arg => 'diskid',
63 default => undef,
64 );
65  
66 has filesource => (
67 is => 'ro',
68 isa => Maybe [Str],
69 reader => '_get_filesource',
70 default => undef,
71 );
72  
73 has guid => (
74 is => 'ro',
75 isa => ComponentGuidType,
76 reader => 'get_guid',
77 lazy => 1,
78 init_arg => undef,
79 builder => 'guid_build',
80 );
81  
82 has name => (
83 is => 'ro',
84 isa => Maybe [Str], # LongFileNameType
85 reader => 'get_name',
86 default => undef,
87 );
88  
89 has sourcename => (
90 is => 'ro',
91 isa => Maybe [Str], # LongFileNameType
92 reader => '_get_sourcename',
93 default => undef,
94 );
95  
96 has _shortname => (
97 is => 'ro',
98 isa => Maybe [Str], # ShortFileNameType
99 reader => '_get_shortname',
100 init_arg => 'shortname',
101 default => undef,
102 );
103  
104 has _shortsourcename => (
105 is => 'ro',
106 isa => Maybe [Str], # ShortFileNameType
107 reader => '_get_shortsourcename',
108 init_arg => 'shortsourcename',
109 default => undef,
110 );
111  
112 # Since we generate GUID's when none is included,
113 # ComponentGuidGenerationSeed is not needed.
114  
115 #####################################################################
116 # Constructor for Directory
117 #
118  
119 sub BUILDARGS {
120 my $class = shift;
121 my %args;
122  
123 if ( @_ == 1 && 'HASH' eq ref $_[0] ) {
124 %args = %{ $_[0] };
125 } elsif ( 0 == @_ % 2 ) {
126 %args = @_;
127 } else {
128 WiX3::Exception::Parameter::Odd->throw("$class->new");
129 }
130  
131 if ( not exists $args{'path'}
132 and exists $args{'name'}
133 and exists $args{'parent'} )
134 {
135  
136 # Create our path off our parent's path.
137 my $parent_path = $args{'parent'}->get_path();
138 if ( defined $parent_path ) {
139 $args{'path'} = catdir( $parent_path, $args{'name'} );
140 }
141  
142 delete $args{'parent'};
143 } ## end if ( not exists $args{...})
144  
145 if ( defined $args{'id'} and not defined _IDENTIFIER( $args{'id'} ) ) {
146 WiX3::Exception::Parameter::Invalid->throw('id');
147 }
148  
149 return \%args;
150 } ## end sub BUILDARGS
151  
152 sub get_directory_id {
153 my $self = shift;
154 my $id = $self->get_id();
155  
156 if ( $self->_get_noprefix() ) {
157 return $id;
158 } else {
159 return "D_$id";
160 }
161 }
162  
163 sub add_directory {
164 my $self = shift;
165 my $class = ref $self;
166 my %args;
167  
168 if ( @_ == 1 && 'HASH' eq ref $_[0] ) {
169 %args = %{ $_[0] };
170 } elsif ( 0 == @_ % 2 ) {
171 %args = @_;
172 } else {
173 WiX3::Exception::Parameter::Odd->throw();
174 }
175  
176 my $name = $args{name};
177 ## no critic(ProhibitMagicNumbers)
178 $self->trace_line( 3, "Adding directory $name\n" ) if defined $name;
179  
180 # We make a new $class, rather than a new WiX3::XML::Directory,
181 # so subclasses can create more of themselves without
182 # having to override this routine.
183 my $new_dir = $class->new(
184  
185 # parent => $self,
186 %args
187 );
188 $self->add_child_tag($new_dir);
189  
190 return $new_dir;
191 } ## end sub add_directory
192  
193  
194  
195 #####################################################################
196 # Methods to implement the Tag role.
197  
198 sub as_string {
199 my $self = shift;
200  
201 my $children = $self->has_child_tags();
202 my $tags;
203 $tags = $self->print_attribute( 'Id', $self->get_directory_id() );
204 $tags .= $self->print_attribute( 'Name', $self->get_name() );
205 $tags .= $self->print_attribute( 'ShortName', $self->_get_shortname() );
206 $tags .=
207 $self->print_attribute( 'SourceName', $self->_get_sourcename() );
208 $tags .=
209 $self->print_attribute( 'ShortSourceName',
210 $self->_get_shortsourcename() );
211 $tags .= $self->print_attribute( 'DiskId', $self->_get_diskid() );
212 $tags .=
213 $self->print_attribute( 'FileSource', $self->_get_filesource() );
214  
215 if ($children) {
216 my $child_string = $self->as_string_children();
217 return qq{<Directory$tags>\n$child_string\n</Directory>\n};
218 } else {
219 return qq{<Directory$tags />\n};
220 }
221 } ## end sub as_string
222  
223 sub get_namespace {
224 return q{xmlns='http://schemas.microsoft.com/wix/2006/wi'};
225 }
226  
227 no Moose;
228 __PACKAGE__->meta->make_immutable;
229  
230 1;
231  
232 __END__