OpenInteract2/Config/PackageChanges.pm

filter: Statementstmt 94.6% 124 / 131 Branchbran 72.7% 32 / 44 Conditioncond 75.0% 9 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 94.4% 17 / 18 Podpod 11.1% 1 / 9 totaltotal 83.1% 183 / 220 CC 29 SCAR 16.9
CC
29
Cov
88%
read_config
10.0
write_config
7.9
new
6.0
CRAP 5.4
« OpenInteract2/ContentGenerator.pm ↑ Summary OpenInteract2/Manage/Website/Create.pm »
1 package OpenInteract2::Config::PackageChanges;
2  
3 # $Id: PackageChanges.pm,v 1.12 2005/03/18 04:09:50 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Class::Accessor::Fast Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use File::Spec::Functions qw( catfile catdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use Text::Wrap qw( wrap );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 87 $OpenInteract2::Config::PackageChanges::VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/);
12  
13 87 ▶use constant CHANGES_FILE => 'Changes';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 87 @OpenInteract2::Config::PackageChanges::EXPORT_OK = qw( CHANGES_FILE );
15  
16 87 my @FIELDS = qw( source_package source_dir source_file source_content comments );
17 87 OpenInteract2::Config::PackageChanges->mk_accessors( @FIELDS );
18  
19 5 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
20 5 my ( $class, $params ) = @_;
21 5 my $self = bless( { _entries => [] }, $class );
22 5 my ( $to_read );
23 5 ▶ if ( $params->{package} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($params->{'package'}) { } 2 3 5
elsif ($params->{'dir'}) { } 1 2 3
elsif ($params->{'file'}) { } 1 1 2
elsif ($params->{'content'}) { } 1 0 1
24 2 $self->source_package( $params->{package} );
25 2 $to_read++;
26 }
27 elsif ( $params->{dir} ) {
28 1 $self->source_dir( $params->{dir} );
29 1 $to_read++;
30 }
31 elsif ( $params->{file} ) {
32 1 $self->source_file( $params->{file} );
33 1 $to_read++;
34 }
35 elsif ( $params->{content} ) {
36 1 $self->source_content( $params->{content} );
37 1 $to_read++;
38 }
39 5 ▶ $self->read_config if ( $to_read );
Branch: 1 branch
BranchTrueFalseTotal
if $to_read 5 0 5
40 5 return $self;
41 }
42  
43 5 ▶sub read_config {
Subroutine read_config: called CC=10 SCAR=23.0
Pod undocumented
44 5 my ( $self ) = @_;
45 5 ▶ unless ( $self->source_package
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->source_package or $self->source_dir or $self->source_file or $self->source_content) 0 5 5
Condition88%8/9
$self->source_package or $self->source_dir or $self->source_file or $self->source_content
l!l&&r!l&&!r
410
$self->source_package or $self->source_dir or $self->source_file
l!l&&r!l&&!r
311
$self->source_package or $self->source_dir
l!l&&r!l&&!r
212
MC/DC0%0/4
$self->source_package or $self->source_dir or $self->source_file or $self->source_content
$self->source_package $self->source_dir $self->source_file $self->source_content
Truth table80%4/5
A or B or C or D
A= $self->source_packageB= $self->source_dirC= $self->source_fileD= $self->source_content
ABCDresult
1XXX1
01XX1
001X1
00011
00000
46 or $self->source_dir
47 or $self->source_file
48 or $self->source_content ) {
49 0 oi_error "Cannot read configuration without first setting one of ",
50 "'source_package', 'source_dir' or 'source_file'";
51 }
52 5 my @content = $self->_read_content;
53 5 my ( %last_entry );
54 5 my @entries = ();
55 5 my @comments = ();
56  
57 LINE:
58 5 for ( @content ) {
59 245 chomp;
60 245 ▶ if ( /^(\d+.\d+)(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+.\d+)(.*)$/) 57 188 245
61 57 my $this_version = $1;
62 57 my $this_date = $2;
63 57 ▶ if ( keys %last_entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %last_entry) 52 5 57
64 52 chomp( $last_entry{message} );
65 52 push @entries, { %last_entry };
66 52 %last_entry = ();
67 }
68 57 $last_entry{version} = $this_version;
69 57 $this_date =~ s/^\s+//;
70 57 $this_date =~ s/\s+$//;
71 57 $last_entry{date} = $this_date;
72 57 next LINE;
73 }
74  
75 # This skips any initial comments, tossing them aside
76  
77 188 ▶ unless ( keys %last_entry ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %last_entry) 9 179 188
78 9 push @comments, $_;
79 9 next LINE;
80 }
81  
82 # Get rid of leading/trailing whitespace in the message...
83  
84 179 s/^\s+/ /;
85 179 s/\s+$/ /;
86 179 $last_entry{message} .= $_;
87 }
88  
89 5 ▶ chomp $last_entry{message} if ( $last_entry{message} );
Branch: 1 branch
BranchTrueFalseTotal
if $last_entry{"message"} 4 1 5
90 5 push @entries, { %last_entry };
91 5 $self->{_entries} = \@entries;
92 5 $self->comments( join( "\n", @comments ) );
93 5 return $self;
94 }
95  
96  
97 5 ▶sub _read_content {
Subroutine _read_content: called CC=3 SCAR=11.0
98 5 my ( $self ) = @_;
99 5 ▶ if ( $self->source_content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->source_content) 1 4 5
100 1 return map { "$_\n" } split( "\n", $self->source_content );
101 }
102 4 my $changes_file = $self->_get_full_config_file;
103 4 ▶ open( CHANGES, '<', $changes_file )
Branch: 1 branch
BranchTrueFalseTotal
open CHANGES, "<", $changes_file or oi_error("Failed to open '${changes_file}': $!") 4 0 4
104 || oi_error "Failed to open '$changes_file': $!";
105 4 my @lines = <CHANGES>;
106 4 close( CHANGES );
107 4 return @lines;
108 }
109  
110  
111 4 ▶sub _get_full_config_file {
Subroutine _get_full_config_file: called CC=5 SCAR=16.3
112 4 my ( $self ) = @_;
113 4 my ( $changes_file );
114 4 ▶ if ( $self->source_package ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->source_package) { } 2 2 4
elsif ($self->source_dir) { } 1 1 2
elsif ($self->source_file) { } 1 0 1
115 2 my $package_dir = $self->source_package->directory;
116 2 $changes_file = catfile( $package_dir, CHANGES_FILE );
117 }
118 elsif ( $self->source_dir ) {
119 1 $changes_file = catfile( $self->source_dir, CHANGES_FILE );
120 }
121 elsif ( $self->source_file ) {
122 1 $changes_file = $self->source_file;
123 }
124 4 ▶ unless ( -f $changes_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $changes_file) 0 4 4
125 0 oi_error "Specified file '$changes_file' does not exist";
126 }
127 4 return $changes_file;
128 }
129  
130 1 ▶sub write_config {
Subroutine write_config: called CC=7 SCAR=20.6
Pod undocumented
131 1 my ( $self, $outfile ) = @_;
132 1 local $Text::Wrap::columns = 70;
133 1 ▶ if ( -f $outfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $outfile) 0 1 1
134 0 ▶ rename( $outfile, "$outfile.bak" )
Branch: 1 branch
BranchTrueFalseTotal
rename $outfile, "$outfile.bak" or oi_error("Cannot rename '${outfile}' to backup: $!") 0 0 0
135 || oi_error "Cannot rename '$outfile' to backup: $!";
136 }
137 1 ▶ open( OUT, '>', $outfile )
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", $outfile or oi_error("Cannot write to '${outfile}': $!") 1 0 1
138 || oi_error "Cannot write to '$outfile': $!";
139 1 ▶ if ( my $comments = $self->comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comments = $self->comments) 1 0 1
140 1 print OUT $comments, "\n";
141 }
142 1 foreach my $entry ( $self->entries ) {
143 19 print OUT $entry->{version}, ' ', $entry->{date}, "\n\n";
144 19 ▶ if ( $entry->{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"message"}) 17 2 19
145 17 my $msg = $entry->{message};
146 17 $msg =~ s/^\s+//;
147 17 print OUT wrap( ' ', ' ', $msg ), "\n\n";
148 }
149 }
150 }
151  
152  
153 ########################################
154 # MANIPULATE ENTRIES
155  
156 0 ▶sub add_entry {
Subroutine add_entry: not called CC=1 SCAR=6.9
Pod undocumented
157 0 my ( $self, $version, $date, $message ) = @_;
158 0 unshift @{ $self->{_entries} }, { version => $version,
159 date => $date,
160 message => $message };
161 }
162  
163 6 ▶sub entries {
Subroutine entries: called CC=1 SCAR=0.0
Pod undocumented
164 6 return @{ $_[0]->{_entries} };
165 }
166  
167 6 ▶sub latest {
Subroutine latest: called CC=1 SCAR=0.0
Pod undocumented
168 6 my ( $self, $num ) = @_;
169 6 my $last = $num - 1;
170 6 return @{ $self->{_entries} }[0..$last];
171 }
172  
173 4 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod undocumented
174 4 my ( $self, $num ) = @_;
175 4 my $num_entries = scalar @{ $self->{_entries} };
176 4 my $head = $num_entries - $num;
177 4 my $last = $num_entries - 1;
178 4 return @{ $self->{_entries} }[$head..$last];
179 }
180  
181 3 ▶sub since {
Subroutine since: called CC=1 SCAR=0.0
Pod undocumented
182 3 my ( $self, $version ) = @_;
183 3 $self->_version_check( $version );
184 3 return grep { $_->{version} >= $version } @{ $self->{_entries} };
185 }
186  
187 3 ▶sub before {
Subroutine before: called CC=1 SCAR=0.0
Pod undocumented
188 3 my ( $self, $version ) = @_;
189 3 $self->_version_check( $version );
190 3 return grep { $_->{version} <= $version } @{ $self->{_entries} };
191 }
192  
193 6 ▶sub _version_check {
Subroutine _version_check: called CC=3 SCAR=13.3
194 6 my ( $self, $version ) = @_;
195 6 ▶ unless ( $version and $version =~ /^\d+\.\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version and $version =~ /^\d+\.\d+$/) 0 6 6
Condition33%1/3
$version and $version =~ /^\d+\.\d+$/
!ll&&!rl&&r
006
MC/DC0%0/2
$version and $version =~ /^\d+\.\d+$/
$version $version =~ /^\d+\.\d+$/
Truth table33%1/3
A and B
A= $versionB= $version =~ /^\d+\.\d+$/
ABresult
0X0
100
111
196 0 oi_error "Version argument '$version' is invalid, it ",
197 "should match '\\d+\\.\\d+'";
198 }
199 6 return $version;
200 }
201  
202 ########################################
203 # OTHER INFO
204  
205 87 1;
206  
207 __END__
« OpenInteract2/ContentGenerator.pm ↑ Summary OpenInteract2/Manage/Website/Create.pm »