Update/MSBuild.pm

filter: Statementstmt 99.2% 129 / 130 Branchbran 94.7% 72 / 76 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 97.7% 217 / 222 CC 41 SCAR 27.4
CC
41
Cov
98%
update_msbuild_filters
20.0
update_msbuild
18.0
update_msbuild_project
3.0
CRAP 15.5
« Update/VCProj.pm ↑ Summary Update/Bakefile0.pm »
1 package Makefile::Update::MSBuild;
2 # ABSTRACT: Update list of sources and headers in MSBuild projects.
3  
4 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our @EXPORT = qw(update_msbuild_project update_msbuild update_msbuild_filters);
6  
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.4'; # VERSION
11  
12  
13  
14 sub update_msbuild_project
15 3 ▶{
Subroutine update_msbuild_project: called CC=3 SCAR=11.0
Pod documented
16 3 my ($file_or_options, $sources, $headers) = @_;
17  
18 5 ▶ use Makefile::Update;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶ if (!Makefile::Update::upmake($file_or_options,
Branch: 1 branch
BranchTrueFalseTotal
unless (Makefile::Update::upmake($file_or_options, \&Makefile::Update::MSBuild::update_msbuild, $sources, $headers)) 1 2 3
21 \&update_msbuild, $sources, $headers
22 )) {
23 1 return 0;
24 }
25  
26 2 my $args;
27 2 ▶ if (ref $file_or_options eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_or_options eq 'HASH') { } 2 0 2
28 # Need to make a copy to avoid modifying the callers hash.
29 2 $args = { %$file_or_options };
30 2 $args->{file} .= ".filters"
31 } else {
32 0 $args = "$file_or_options.filters"
33 }
34  
35 2 return Makefile::Update::upmake($args,
36 \&update_msbuild_filters, $sources, $headers
37 );
38 }
39  
40  
41  
42 sub update_msbuild
43 7 ▶{
Subroutine update_msbuild: called CC=18 SCAR=28.9
Pod documented
44 7 my ($in, $out, $sources, $headers) = @_;
45  
46 # Hashes mapping the sources/headers names to 1 if they have been seen in
47 # the project or 0 otherwise.
48 7 my %sources = map { $_ => 0 } @$sources;
49 7 my %headers = map { $_ => 0 } @$headers;
50  
51 # Reference to the hash corresponding to the files currently being
52 # processed.
53 7 my $files;
54  
55 # Set to 1 when we are inside any <ItemGroup> tag.
56 7 my $in_group = 0;
57  
58 # Set to 1 when we are inside an item group containing sources or headers
59 # respectively.
60 7 my ($in_sources, $in_headers) = 0;
61  
62 # Set to 1 if we made any changes.
63 7 my $changed = 0;
64 7 ▶ while (my $line_with_eol = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line_with_eol = readline $in)) 128 7 135
65 128 (my $line = $line_with_eol) =~ s/\r?\n?$//;
66  
67 128 ▶ if ($line =~ /^\s*<ItemGroup>$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s*<ItemGroup>$/) { } 11 117 128
elsif ($line =~ m[^\s*</ItemGroup>$]) { } 18 99 117
elsif ($in_group) { } 19 80 99
68 11 $in_group = 1;
69 } elsif ($line =~ m{^\s*</ItemGroup>$}) {
70 18 ▶ if (defined $files) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files) 11 7 18
71 11 ▶ my $kind = $in_sources ? 'Compile' : 'Include';
Branch: 1 branch
BranchTrueFalseTotal
$in_sources ? : 7 4 11
72  
73 # Check if we have any new files.
74 #
75 # TODO Insert them in alphabetical order.
76 11 ▶ while (my ($file, $seen) = each(%$files)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$files) 14 11 25
77 14 ▶ if (!$seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 5 9 14
78 # Convert path separator to the one used by MSBuild.
79 5 $file =~ s@/@\\@g;
80  
81 5 print $out qq{ <Cl$kind Include="$file" />\r\n};
82  
83 5 $changed = 1;
84 }
85 }
86  
87 11 $in_sources = $in_headers = 0;
88 11 $files = undef;
89 }
90  
91 18 $in_group = 0;
92 } elsif ($in_group) {
93 19 ▶ if ($line =~ m{^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>$]) 19 0 19
94 19 my $kind = $+{kind};
95 19 ▶ if ($kind eq 'Compile') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'Compile') { } 11 8 19
96 11 ▶ warn "Mix of sources and headers at line $.\n" if $in_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $in_headers 1 10 11
97 11 $in_sources = 1;
98 11 $files = \%sources;
99 } else {
100 8 ▶ warn "Mix of headers and sources at line $.\n" if $in_sources;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sources 1 7 8
101 8 $in_headers = 1;
102 8 $files = \%headers;
103 }
104  
105 19 my $closed_tag = defined $+{slash};
106  
107 # Normalize the path separator, we always use Unix ones but the
108 # project files use Windows one.
109 19 my $file = $+{file};
110 19 $file =~ s@\\@/@g;
111  
112 19 ▶ if (not exists $files->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $files->{$file}) { } 8 11 19
113 # This file was removed.
114 8 $changed = 1;
115  
116 8 ▶ if (!$closed_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed_tag) 1 7 8
117 # We have just the opening <ClCompile> or <ClInclude>
118 # tag, ignore everything until the matching closing one.
119 1 my $tag = "Cl$kind";
120 1 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 2 0 2
121 2 ▶ last if m{^\s*</$tag>\r?\n$};
Branch: 1 branch
BranchTrueFalseTotal
if m[^\s*</$tag>\r?\n$] 1 1 2
122 }
123 }
124  
125 # In any case skip either this line containing the full
126 # <ClCompile/> tag or the line with the closing tag.
127 8 next;
128 } else {
129 11 ▶ if ($files->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files->{$file}) { } 1 10 11
130 1 warn qq{Duplicate file "$file" in the project at line $.\n};
131 } else {
132 10 $files->{$file} = 1;
133 }
134 }
135 }
136 }
137  
138 120 print $out $line_with_eol;
139 }
140  
141 $changed
142 7 }
143  
144  
145 sub update_msbuild_filters
146 7 ▶{
Subroutine update_msbuild_filters: called CC=20 SCAR=30.0
Pod documented
147 7 my ($in, $out, $sources, $headers, $filter_cb) = @_;
148  
149 # Use standard/default classifier for the files if none is explicitly
150 # specified.
151 7 ▶ if (!defined $filter_cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filter_cb) 6 1 7
152 4 ▶ $filter_cb = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
153 4 my ($file) = @_;
154  
155 4 ▶ return 'Source Files' if $file =~ q{\.c(c|pp|xx|\+\+)?$};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.c(c|pp|xx|\+\+)?$/ 2 2 4
156 2 ▶ return 'Header Files' if $file =~ q{\.h(h|pp|xx|\+\+)?$};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.h(h|pp|xx|\+\+)?$/ 1 1 2
157  
158 1 warn qq{No filter defined for the file "$file".\n};
159  
160 undef
161 1 }
162 6 }
163  
164 # Hashes mapping the sources/headers names to the text representing them in
165 # the input file if they have been seen in it or nothing otherwise.
166 7 my %sources = map { $_ => undef } @$sources;
167 7 my %headers = map { $_ => undef } @$headers;
168  
169 # Reference to the hash corresponding to the files currently being
170 # processed.
171 7 my $files;
172  
173 # Set to 1 when we are inside any <ItemGroup> tag.
174 7 my $in_group = 0;
175  
176 # Set to 1 when we are inside an item group containing sources or headers
177 # respectively.
178 7 my ($in_sources, $in_headers) = 0;
179  
180 # Set to 1 if we made any changes.
181 7 my $changed = 0;
182 7 ▶ while (my $line_with_eol = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line_with_eol = readline $in)) 119 7 126
183 119 (my $line = $line_with_eol) =~ s/\r?\n?$//;
184  
185 119 ▶ if ($line =~ /^\s*<ItemGroup>?$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s*<ItemGroup>?$/) { } 17 102 119
elsif ($line =~ m[^\s*</ItemGroup>?$]) { } 17 85 102
elsif ($in_group and $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]) { } 18 92 110
186 17 $in_group = 1;
187 } elsif ($line =~ m{^\s*</ItemGroup>?$}) {
188 17 ▶ if (defined $files) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files) 10 7 17
189 # Output the group contents now, all at once, inserting any new
190 # files: we must do it like this to ensure that they are
191 # inserted in alphabetical order.
192 10 ▶ my $kind = $in_sources ? 'Compile' : 'Include';
Branch: 1 branch
BranchTrueFalseTotal
$in_sources ? : 7 3 10
193  
194 10 foreach my $file (sort keys %$files) {
195 15 ▶ if (defined $files->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files->{$file}) { } 8 7 15
196 8 print $out $files->{$file};
197 } else {
198 7 my $filter = $filter_cb->($file);
199  
200 # Convert path separator to the one used by MSBuild.
201 7 $file =~ s@/@\\@g;
202  
203 7 my $indent = ' ' x 2;
204  
205 7 print $out qq{$indent$indent<Cl$kind Include="$file"};
206 7 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) { } 5 2 7
207 5 print $out ">\r\n$indent$indent$indent<Filter>$filter</Filter>\r\n$indent$indent</Cl$kind>\r\n";
208 } else {
209 2 print $out " />\r\n";
210 }
211  
212 7 $changed = 1;
213 }
214 }
215  
216 10 $in_sources = $in_headers = 0;
217 10 $files = undef;
218 }
219  
220 17 $in_group = 0;
221 ▶ } elsif ($in_group &&
Condition100%3/3
$in_group and $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
!ll&&!rl&&r
254218
MC/DC100%2/2
$in_group and $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
$in_group $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
Truth table100%3/3
A and B
A= $in_groupB= $line =~ m[^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$]
ABresult
0X0
100
111
222 $line =~ m{^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$}) {
223 18 my $kind = $+{kind};
224 18 ▶ if ($kind eq 'Compile') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'Compile') { } 11 7 18
225 11 ▶ warn "Mix of sources and headers at line $.\n" if $in_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $in_headers 1 10 11
226 11 $in_sources = 1;
227 11 $files = \%sources;
228 } else {
229 7 ▶ warn "Mix of headers and sources at line $.\n" if $in_sources;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sources 1 6 7
230 7 $in_headers = 1;
231 7 $files = \%headers;
232 }
233  
234 18 my $closed_tag = defined $+{slash};
235  
236 # Normalize the path separator, we always use Unix ones but the
237 # project files use Windows one.
238 18 my $file = $+{file};
239 18 $file =~ s@\\@/@g;
240  
241 18 my $text = $line_with_eol;
242 18 ▶ if (!$closed_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed_tag) 14 4 18
243 # We have just the opening <ClCompile> tag, get everything
244 # until the next </ClCompile>.
245 14 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 28 0 28
246 28 $text .= $_;
247 28 ▶ last if m{^\s*</Cl$kind>\r?\n?$};
Branch: 1 branch
BranchTrueFalseTotal
if m[^\s*</Cl$kind>\r?\n?$] 14 14 28
248 }
249 }
250  
251 18 ▶ if (not exists $files->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $files->{$file}) { } 8 10 18
252 # This file was removed.
253 8 $changed = 1;
254 } else {
255 10 ▶ if ($files->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files->{$file}) { } 1 9 10
256 1 warn qq{Duplicate file "$file" in the project at line $.\n};
257 } else {
258 9 $files->{$file} = $text;
259 }
260 }
261  
262 # Don't output this line yet, wait until the end of the group.
263 next
264 18 }
265  
266 101 print $out $line_with_eol;
267 }
268  
269 $changed
270 7 }
271  
272 __END__
« Update/VCProj.pm ↑ Summary Update/Bakefile0.pm »