| 1 | package WWW::SFDC::Manifest; | ||
| 2 | # ABSTRACT: Utility functions for Salesforce Metadata API interactions. | ||
| 3 | |||
| 4 | use 5.12.0; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $VERSION = '0.36'; # TRIAL VERSION | ||
| 9 | |||
| 10 | use Data::Dumper; | ||
| 11 | use List::Util 1.29 qw'first reduce pairmap pairgrep pairfirst'; | ||
| 12 | use Log::Log4perl ':easy'; | ||
| 13 | use Method::Signatures; | ||
| 14 | use Scalar::Util qw(blessed); | ||
| 15 | use XML::Parser; | ||
| 16 | |||
| 17 | use Moo; | ||
| 18 | |||
| 19 | |||
| 20 | has 'apiVersion', is => 'rw', default => '34'; | ||
| 21 | |||
| 22 | |||
| 23 | has 'constants', is => 'ro', required => 1; | ||
| 24 | |||
| 25 | |||
| 26 | has 'isDeletion', is => 'ro'; | ||
| 27 | |||
| 28 | |||
| 29 | has 'manifest', is => 'rw', default => sub { {} }; | ||
| 30 | |||
| 31 | # _splitLine($line) | ||
| 32 | |||
| 33 | # Takes a string representing a file on disk, such as "email/foo/bar.email-meta.xml", | ||
| 34 | # and returns a hash containing the metadata type, folder name, file name, and | ||
| 35 | # file extension, excluding -meta.xml. | ||
| 36 | |||
| 37 | method _splitLine ($line) { | ||
| 38 | # we're using '' in lots of regexes to avoid escaping / all over the place | ||
| 39 | # This method could probably be cleaned up by using File::Spec or similar. | ||
| 40 | |||
| 41 | $line =~ s".*src/""; # Bin anything up to and including src/. | ||
| 42 | $line =~ s/[\n\r]//g; # Bin any newline characters. | ||
| 43 | $line =~ s"\\"/"g; # Turn \ into / for cross-platform paths. | ||
| 44 | |||
| 45 | my %result = (extension => ""); | ||
| 46 | |||
| 47 | ($result{type}) = $line =~ m"^(\w+)/" or LOGDIE "Line $line doesn't have a type."; | ||
| 48 | |||
| 49 | $result{folder} = $1 if $line =~ m"/(\w+)/"; | ||
| 50 | |||
| 51 | my $extension = (grep {$_ eq $result{type}} keys $self->constants->TYPES) | ||
| 52 | ? $self->constants->getEnding($result{type}) | ||
| 53 | : undef; | ||
| 54 | |||
| 55 | if ($line =~ m"/(\w+)-meta.xml") { | ||
| 56 | $result{name} = $1 | ||
| 57 | |||
| 58 | } elsif (!defined $extension) { | ||
| 59 | ($result{name}) = $line =~ m"/([^/]*?)(-meta\.xml)?$"; | ||
| 60 | # This is because components get passed back from listDeletions with : replacing . | ||
| 61 | $result{name} =~ s":"."; | ||
| 62 | |||
| 63 | } elsif ($line =~ m"/([^/]*?)($extension)(-meta\.xml)?$") { | ||
| 64 | $result{name} = $1; | ||
| 65 | $result{extension} = $2; | ||
| 66 | } | ||
| 67 | |||
| 68 | LOGDIE "Line $line doesn't have a name." unless $result{name}; | ||
| 69 | |||
| 70 | return \%result; | ||
| 71 | } | ||
| 72 | |||
| 73 | # _getFilesForLine($line) | ||
| 74 | |||
| 75 | # Takes a string representing a file on disk, such as "email/foo/bar.email", | ||
| 76 | # and returns a list representing all the files needed in the zip file for | ||
| 77 | # that file to be successfully deployed, for example: | ||
| 78 | |||
| 79 | # - email/foo-meta.xml | ||
| 80 | # - email/foo/bar.email | ||
| 81 | # - email/foo/bar.email-meta.xml | ||
| 82 | |||
| 83 | method _getFilesForLine ($line?) { | ||
| 84 | return () unless $line; | ||
| 85 | |||
| 86 | my %split = %{$self->_splitLine($line)}; | ||
| 87 | |||
| 88 | return map {"$split{type}/$_"} ( | ||
| 89 | $split{"folder"} | ||
| 90 | ? ( | ||
| 91 | "$split{folder}-meta.xml", | ||
| 92 | "$split{folder}/$split{name}$split{extension}", | ||
| 93 | ( | ||
| 94 | $self->constants->needsMetaFile($split{"type"}) | ||
| 95 | ? "$split{folder}/$split{name}$split{extension}-meta.xml" | ||
| 96 | : () | ||
| 97 | ) | ||
| 98 | ) | ||
| 99 | : ( | ||
| 100 | "$split{name}$split{extension}", | ||
| 101 | ( | ||
| 102 | $self->constants->needsMetaFile($split{"type"}) | ||
| 103 | ? "$split{name}$split{extension}-meta.xml" | ||
| 104 | : () | ||
| 105 | ) | ||
| 106 | ) | ||
| 107 | ) | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | # _dedupe($listref) | ||
| 112 | |||
| 113 | # Returns a list reference to a _deduped version of the list | ||
| 114 | # reference passed in. | ||
| 115 | |||
| 116 | method _dedupe { | ||
| 117 | my %result; | ||
| 118 | for my $key (keys %{$self->manifest}) { | ||
| 119 | my %_deduped = map {$_ => 1} @{$self->manifest->{$key}}; | ||
| 120 | $result{$key} = [sort keys %_deduped]; | ||
| 121 | } | ||
| 122 | $self->manifest(\%result); | ||
| 123 | return $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | method getFileList { | ||
| 128 | |||
| 129 | return map { | ||
| 130 | my $type = $self->constants->getDiskName($_); | ||
| 131 | my $ending = $self->constants->getEnding($type) || ""; | ||
| 132 | |||
| 133 | map { | ||
| 134 | if ($self->constants->hasFolders($type) and $_ !~ /\//) { | ||
| 135 | "$type/$_-meta.xml"; | ||
| 136 | } else { | ||
| 137 | "$type/$_$ending", ($self->constants->needsMetaFile($type) ? "$type/$_$ending-meta.xml" : () ); | ||
| 138 | } | ||
| 139 | } @{ $self->manifest->{$_} } | ||
| 140 | } keys %{$self->manifest}; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | method add ($new) { | ||
| 145 | |||
| 146 | if (defined blessed $new and blessed $new eq blessed $self) { | ||
| 147 | push @{$self->manifest->{$_}}, @{$new->manifest->{$_}} for keys %{$new->manifest}; | ||
| 148 | } else { | ||
| 149 | push @{$self->manifest->{$_}}, @{$new->{$_}} for keys %$new; | ||
| 150 | } | ||
| 151 | |||
| 152 | return $self->_dedupe(); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | method addList (@_) { | ||
| 157 | |||
| 158 | return reduce {$a->add($b)} $self, map { | ||
| 159 | TRACE "Adding $_ to manifest"; | ||
| 160 | +{ | ||
| 161 | $self->constants->getName($$_{type}) => [ | ||
| 162 | defined $$_{folder} | ||
| 163 | ? ( | ||
| 164 | ($self->isDeletion ? () : $$_{folder}), | ||
| 165 | "$$_{folder}/$$_{name}" | ||
| 166 | ) | ||
| 167 | : ($$_{name}) | ||
| 168 | ] | ||
| 169 | } | ||
| 170 | } map {$self->_splitLine($_)} @_; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | method readFromFile ($fileName) { | ||
| 175 | # XML::Parser returns a list which consists of | ||
| 176 | # | ||
| 177 | # ( | ||
| 178 | # $attributes, | ||
| 179 | # $childNodeName, $childNodeElements, | ||
| 180 | # $childNodeName, $childNodeElements, | ||
| 181 | # ... | ||
| 182 | # ) | ||
| 183 | # | ||
| 184 | # Where each $childNodeElements looks like this too (ie recursive). | ||
| 185 | # | ||
| 186 | # We use splice to remove the $attributes which we assume will be empty, | ||
| 187 | # then use pairmap and pairgrep to find the right nodes to operate on and | ||
| 188 | # transform the data. Finally, we use reduce (from List::Util) to add each | ||
| 189 | # resulting hashref to the current manifest. | ||
| 190 | |||
| 191 | return reduce {$a->add($b)} $self, map {+{ | ||
| 192 | do { | ||
| 193 | pairmap {$b->[2]} pairfirst {$a eq 'name'} @$_ | ||
| 194 | } => [ | ||
| 195 | pairmap {$b->[2]} pairgrep {$a eq 'members'} @$_ | ||
| 196 | ] | ||
| 197 | }} | ||
| 198 | pairmap {[splice @{$b}, 1]} pairgrep {$a eq 'types'} | ||
| 199 | splice @{ | ||
| 200 | XML::Parser->new(Style=>"Tree")->parsefile($fileName)->[1] | ||
| 201 | }, 1; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | method writeToFile ($fileName) { | ||
| 206 | open my $fh, ">", $fileName or LOGDIE "Couldn't open $fileName to write manifest to disk"; | ||
| 207 | print $fh $self->getXML(); | ||
| 208 | return $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | method getXML { | ||
| 213 | # Ultra-low-tech. | ||
| 214 | return join "", ( | ||
| 215 | "<?xml version='1.0' encoding='UTF-8'?>", | ||
| 216 | "<Package xmlns='http://soap.sforce.com/2006/04/metadata'>", | ||
| 217 | ( | ||
| 218 | map {( | ||
| 219 | "<types>", | ||
| 220 | "<name>$_</name>", | ||
| 221 | ( map {"<members>$_</members>"} @{$self->manifest->{$_}} ), | ||
| 222 | "</types>", | ||
| 223 | )} sort keys %{$self->manifest} | ||
| 224 | ), | ||
| 225 | "<version>",$self->apiVersion,"</version></Package>" | ||
| 226 | ); | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; | ||
| 230 | |||
| 231 | __END__ |