SFDC/Manifest.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SFDC/Constants.pm ↑ Summary
1 package WWW::SFDC::Manifest;
2 # ABSTRACT: Utility functions for Salesforce Metadata API interactions.
3  
4 1 ▶use 5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Scalar::Util qw(blessed);
10 use List::Util qw'first reduce pairmap pairgrep pairfirst';
11 use Data::Dumper;
12 use Log::Log4perl ':easy';
13 use WWW::SFDC::Constants qw(needsMetaFile hasFolders getEnding getDiskName getName);
14  
15 use Moo;
16  
17 has 'manifest', is => 'rw', default => sub { {} };
18 has 'isDeletion', is => 'ro';
19 has 'srcDir', is => 'rw', default => 'src';
20 has 'apiVersion', is => 'rw', default => 33;
21  
22  
23 # _splitLine($line)
24  
25 # Takes a string representing a file on disk, such as "email/foo/bar.email-meta.xml",
26 # and returns a hash containing the metadata type, folder name, file name, and
27 # file extension, excluding -meta.xml.
28  
29 sub _splitLine {
30 my ($self, $line) = @_;
31  
32 LOGDIE "Called _splitLine with no argument!" unless $line;
33  
34 # clean up the line
35 $line =~ s/.*src\///;
36 $line =~ s/[\n\r]//g;
37  
38 my %result = ("extension" => "");
39  
40 ($result{"type"}) = $line =~ /^(\w+)\//
41 or LOGDIE "Line $line doesn't have a type.";
42 $result{"folder"} = $1
43 if $line =~ /\/(\w+)\//;
44  
45 my $extension = getEnding($result{"type"});
46  
47 if ($line =~ /\/(\w+)-meta.xml/) {
48 $result{"name"} = $1
49 } elsif (!defined $extension) {
50 ($result{"name"}) = $line =~ /\/([^\/]*?)(-meta\.xml)?$/;
51 # This is because components get passed back from listDeletions with : replacing .
52 $result{"name"} =~ s/:/./;
53 } elsif ($line =~ /\/([^\/]*?)($extension)(-meta\.xml)?$/) {
54 $result{"name"} = $1;
55 $result{"extension"} = $2;
56 }
57  
58 LOGDIE "Line $line doesn't have a name." unless $result{"name"};
59  
60 return \%result;
61 }
62  
63 # _getFilesForLine($line)
64  
65 # Takes a string representing a file on disk, such as "email/foo/bar.email",
66 # and returns a list representing all the files needed in the zip file for
67 # that file to be successfully deployed, for example:
68  
69 # - email/foo-meta.xml
70 # - email/foo/bar.email
71 # - email/foo/bar.email-meta.xml
72  
73 sub _getFilesForLine {
74 my ($self, $line) = @_;
75  
76 return () unless $line;
77  
78 my %split = %{$self->_splitLine($line)};
79  
80 return map {"$split{type}/$_"} (
81 $split{"folder"}
82 ?(
83 "$split{folder}-meta.xml",
84 "$split{folder}/$split{name}$split{extension}",
85 (needsMetaFile($split{"type"}) ? "$split{folder}/$split{name}$split{extension}-meta.xml" : ())
86 )
87 :(
88 "$split{name}$split{extension}",
89 (needsMetaFile($split{"type"}) ? "$split{name}$split{extension}-meta.xml" : ())
90 )
91 )
92 }
93  
94  
95 # _dedupe($listref)
96  
97 # Returns a list reference to a _deduped version of the list
98 # reference passed in.
99  
100 sub _dedupe {
101 my ($self) = @_;
102 my %result;
103 for my $key (keys %{$self->manifest}) {
104 my %_deduped = map {$_ => 1} @{$self->manifest->{$key}};
105 $result{$key} = [sort keys %_deduped];
106 }
107 $self->manifest(\%result);
108 return $self;
109 }
110  
111  
112 sub getFileList {
113 my $self = shift;
114  
115 return map {
116 my $type = getDiskName($_);
117 my $ending = getEnding($type) || "";
118  
119 map {
120 if (hasFolders($type) and $_ !~ /\//) {
121 "$type/$_-meta.xml";
122 } else {
123 "$type/$_$ending", (needsMetaFile($type) ? "$type/$_$ending-meta.xml" : () );
124 }
125 } @{ $self->manifest->{$_} }
126 } keys %{$self->manifest};
127 }
128  
129  
130 sub add {
131 my ($self, $new) = @_;
132  
133 if (defined blessed $new and blessed $new eq blessed $self) {
134 push @{$self->manifest->{$_}}, @{$new->manifest->{$_}} for keys %{$new->manifest};
135 } else {
136 push @{$self->manifest->{$_}}, @{$new->{$_}} for keys %$new;
137 }
138  
139 return $self->_dedupe();
140 }
141  
142  
143 sub addList {
144 my $self = shift;
145  
146 return reduce {$a->add($b)} $self, map {
147 DEBUG "Adding $_ to manifest";
148 +{
149 getName($$_{type}) => [
150 defined $$_{folder}
151 ? (
152 ($self->isDeletion ? () : $$_{folder}),
153 "$$_{folder}/$$_{name}"
154 )
155 : ($$_{name})
156 ]
157 }
158 } map {$self->_splitLine($_)} @_;
159 }
160  
161  
162 sub readFromFile {
163 my ($self, $fileName) = @_;
164  
165 return reduce {$a->add($b)} $self, map {+{
166 do {
167 pairmap {$b->[2]} pairfirst {$a eq 'name'} @$_
168 } => [
169 pairmap {$b->[2]} pairgrep {$a eq 'members'} @$_
170 ]
171 }}
172 pairmap {[splice @{$b}, 1]} pairgrep {$a eq 'types'}
173 splice @{
174 XML::Parser->new(Style=>"Tree")->parsefile($fileName)->[1]
175 }, 1;
176 }
177  
178  
179 sub writeToFile {
180 my ($self, $fileName) = @_;
181 open my $fh, ">", $fileName or LOGDIE "Couldn't open $fileName to write manifest to disk";
182 print $fh $self->getXML();
183 return $self;
184 }
185  
186  
187 sub getXML {
188 my ($self) = @_;
189 return join "", (
190 "<?xml version='1.0' encoding='UTF-8'?>",
191 "<Package xmlns='http://soap.sforce.com/2006/04/metadata'>",
192 (
193 map {(
194 "<types>",
195 "<name>$_</name>",
196 ( map {"<members>$_</members>"} @{$self->manifest->{$_}} ),
197 "</types>",
198 )} sort keys %{$self->manifest}
199 ),
200 "<version>",$self->apiVersion,"</version></Package>"
201 );
202 }
203  
204  
205 1;
206  
207 __END__
208  
209 =pod
210  
211 =head1 NAME
212  
213 WWW::SFDC::Manifest - Utility functions for Salesforce Metadata API interactions.
214  
215 =head1 VERSION
216  
217 version 0.22
218  
219 =head1 SYNOPSIS
220  
221 This module is used to read SFDC manifests from disk, add files to them,
222 and get a structure suitable for passing into WWW::SFDC::Metadata functions.
223  
224 my $Manifest = WWW::SFDC::Manifest
225 ->new()
226 ->readFromFile("filename")
227 ->add(
228 WWW::SFDC::Manifest->new()->readFromFile("anotherFile")
229 )->add({Document => ["bar/foo.png"]});
230  
231 my $HashRef = $Manifest->manifest();
232 my $XMLString = $Manifest->getXML();
233  
234 =head1 METHODS
235  
236 =head2 getFileList(@list)
237  
238 Returns a list of files needed to deploy this manifest. Use this to construct
239 a .zip file.
240  
241 =head2 add($manifest)
242  
243 Adds an existing manifest object or hash to this one.
244  
245 =head2 addList($isDeletion, @list)
246  
247 Adds a list of components or file paths to the manifest file.
248  
249 =head2 readFromFile $location
250  
251 Reads a salesforce package manifest and adds it to the current object, then
252 returns it.
253  
254 =head2 writeToFile $location
255  
256 Writes the manifest's XML representation to the given file and returns
257 the manifest object.
258  
259 =head2 getXML($mapref)
260  
261 Returns the XML representation for this manifest.
262  
263 =head1 BUGS
264  
265 Please report any bugs or feature requests at L<https://github.com/alexander-brett/WWW-SFDC/issues>.
266  
267 =head1 SUPPORT
268  
269 You can find documentation for this module with the perldoc command.
270  
271 perldoc WWW::SFDC::Manifest
272  
273 You can also look for information at L<https://github.com/alexander-brett/WWW-SFDC>
274  
275 =head1 AUTHOR
276  
277 Alexander Brett <alexander.brett@sophos.com>
278  
279 =head1 COPYRIGHT AND LICENSE
280  
281 This software is Copyright (c) 2015 by Sophos Limited.
282  
283 This is free software, licensed under:
284  
285 The MIT (X11) License
286  
287 The full text of the license can be found in the
288 F<LICENSE> file included with this distribution.
289  
290 =cut
« SFDC/Constants.pm ↑ Summary