OpenInteract2/Manage/Package/CreateCPAN.pm

filter: Statementstmt 30.9% 47 / 152 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 62.5% 15 / 24 Podpod 0.0% 0 / 6 totaltotal 28.1% 62 / 220 CC 21 SCAR 24.3
CC
21
Cov
25%
_create_dist_dir
20.0
_copy_package_modules
20.0
_read_files_for_brick
20.0
CRAP 11.4
« OpenInteract2/Response/Standalone.pm ↑ Summary OpenInteract2/Config/GlobalOverride.pm »
1 package OpenInteract2::Manage::Package::CreateCPAN;
2  
3 # $Id: CreateCPAN.pm,v 1.3 2005/03/09 20:26:23 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( OpenInteract2::Manage::Package );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use ExtUtils::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use File::Copy qw( cp );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use File::Path qw( mkpath rmtree );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 87 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 87 $OpenInteract2::Manage::Package::CreateCPAN::VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
20  
21 87 my @BINARIES = qw( gz zip gif png jpg ico pdf doc );
22 87 my $DIST_DIR = "tmp-build-cpan";
23  
24 87 ▶sub get_name {
Subroutine get_name: called CC=1 SCAR=0.0
Pod undocumented
25 87 return 'create_cpan';
26 }
27  
28 0 ▶sub get_brief_description {
Subroutine get_brief_description: not called CC=1 SCAR=6.9
Pod undocumented
29 0 return 'Create a CPAN distribution from your package.';
30 }
31  
32 1 ▶sub get_parameters {
Subroutine get_parameters: called CC=1 SCAR=0.0
Pod undocumented
33 1 my ( $self ) = @_;
34 return {
35 1 package_dir => {
36 description => q{Directory of package to export (default: pwd)},
37 default => cwd(),
38 is_required => 'yes',
39 },
40 make_bin => {
41 description => q{Binary for your "make" implementation (default: 'make')},
42 default => 'make',
43 is_required => 'yes',
44 },
45 keep_dir => {
46 description => q{If set to "yes" we keep the generated CPAN directory (default: 'no')},
47 default => 'no',
48 is_required => 'yes',
49 },
50 };
51 }
52  
53 0 ▶sub validate_param {
Subroutine validate_param: not called CC=2 SCAR=17.9
Pod undocumented
54 0 my ( $self, $name, $value ) = @_;
55 0 ▶ if ( $name eq 'package_dir' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "package_dir") 0 0 0
56 0 return $self->_check_package_dir( $value );
57 }
58 0 return $self->SUPER::validate_param( $name, $value );
59 }
60  
61 0 ▶sub tear_down_task {
Subroutine tear_down_task: not called CC=3 SCAR=24.8
Pod undocumented
62 0 my ( $self ) = @_;
63 0 my $keep_dir = lc $self->param( 'keep_dir' );
64 0 ▶ if ( -d $DIST_DIR && $keep_dir ne 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $DIST_DIR and $keep_dir ne "yes") 0 0 0
Condition0%0/3
-d $DIST_DIR and $keep_dir ne "yes"
!ll&&!rl&&r
000
MC/DC0%0/2
-d $DIST_DIR and $keep_dir ne "yes"
-d $DIST_DIR $keep_dir ne "yes"
Truth table0%0/3
A and B
A= -d $DIST_DIRB= $keep_dir ne "yes"
ABresult
0X0
100
111
65 0 rmtree( $DIST_DIR );
66 }
67 }
68  
69 0 ▶sub run_task {
Subroutine run_task: not called CC=3 SCAR=24.8
Pod undocumented
70 0 my ( $self ) = @_;
71 0 my $package_dir = $self->param( 'package_dir' );
72 0 my $package = OpenInteract2::Package->new({
73 directory => $package_dir
74 });
75  
76 0 my $subclass_name = $package->name_as_class;
77 0 my $brick_class_name = "OpenInteract2::Brick::$subclass_name";
78 0 my $app_class_name = "OpenInteract2::App::$subclass_name";
79  
80 0 my $config = $package->config;
81 0 ▶ my $modules = $config->module || [];
Condition0%0/2
$config->module || []
l!l
00
MC/DC0%0/1
$config->module || []
$config->module
Truth table0%0/2
A
A= $config->module
Aresult
11
00
82  
83 0 my @author_names = join( ', ', $config->author_names );
84 0 my @author_emails = join( ', ', $config->author_emails );
85 0 my ( @authors );
86 0 for ( 0..scalar @author_names ) {
87 0 push @authors, {
88 name => $author_names[$_],
89 email => $author_emails[$_]
90 };
91 }
92  
93 0 $self->_create_dist_dir();
94  
95 0 my $module_file_specs = $package->get_module_files;
96 0 my @module_files = map { join( '/', @{ $_ } ) } @{ $module_file_specs };
97 0 my @package_modules = $self->_copy_package_modules(
98 $package, \@module_files
99 );
100 0 my @brick_files = $self->_read_files_for_brick(
101 $package, \@module_files
102 );
103  
104 0 my %replacements = (
105 package_name => $package->name,
106 brick_name => $package->name,
107 full_app_class => $app_class_name,
108 full_brick_class => $brick_class_name,
109 subclass => $subclass_name,
110 authors => \@authors,
111 author_names => \@author_names,
112 abstract => $config->description,
113 required_modules => $modules,
114 package_modules => \@package_modules,
115 package_version => $package->version,
116 package_files => \@brick_files,
117 package_pod => '',
118 );
119  
120 0 my $brick = OpenInteract2::Brick->new( 'package_cpan' );
121 0 $brick->copy_all_resources_to( $DIST_DIR, \%replacements );
122 0 my $dist_file = $self->_create_distribution( $subclass_name );
123 0 $self->_ok(
124 'create CPAN distribution',
125 'Created distribution ok',
126 filename => $dist_file,
127 );
128 }
129  
130 0 ▶sub _create_dist_dir {
Subroutine _create_dist_dir: not called CC=4 SCAR=30.0
131 0 my ( $self ) = @_;
132 0 ▶ if ( -d $DIST_DIR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $DIST_DIR) 0 0 0
133 0 my $num_removed = rmtree( $DIST_DIR );
134 0 ▶ unless ( $num_removed > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num_removed > 0) 0 0 0
135 0 oi_error "Failed to remove directory '$DIST_DIR'; please ",
136 "remove manually and re-run task.";
137 }
138 }
139 0 ▶ mkdir( $DIST_DIR )
Branch: 1 branch
BranchTrueFalseTotal
mkdir $DIST_DIR or oi_error("Cannot create temporary directory '${DIST_DIR}': $!") 0 0 0
140 || oi_error "Cannot create temporary directory '$DIST_DIR': $!";
141 }
142  
143 # copy modules into cpan dist dir
144  
145 0 ▶sub _copy_package_modules {
Subroutine _copy_package_modules: not called CC=4 SCAR=30.0
146 0 my ( $self, $package, $module_files ) = @_;
147 0 my $package_dir = $package->directory;
148 0 my @package_modules = ();
149 0 foreach my $file ( @{ $module_files } ) {
150 0 my $full_src = catfile( $package_dir, $file );
151 0 my $full_dest = catfile( $DIST_DIR, 'lib', $file );
152 0 my $dest_dir = dirname( $full_dest );
153 0 ▶ unless ( -d $dest_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dest_dir) 0 0 0
154 0 mkpath( $dest_dir );
155 }
156 0 ▶ cp( $full_src, $full_dest )
Branch: 1 branch
BranchTrueFalseTotal
cp($full_src, $full_dest) or oi_error("Cannot copy '${full_src}' to '${full_dest}': $!") 0 0 0
157 || oi_error "Cannot copy '$full_src' to '$full_dest': $!";
158 0 my $module = $file;
159 0 $module =~ s/\.pm$//;
160 0 $module =~ s|/|::|g;
161 0 push @package_modules, $module;
162 }
163 0 return @package_modules;
164 }
165  
166 # copy non-modules files into brick as resources
167  
168 0 ▶sub _read_files_for_brick {
Subroutine _read_files_for_brick: not called CC=4 SCAR=30.0
169 0 my ( $self, $package, $module_files ) = @_;
170 0 my @brick_files = ();
171 0 my $package_dir = $package->directory;
172 0 my %module_check = map { $_ => 1 } @{ $module_files };
173 0 my %seen_names = ();
174 0 my $package_files = $package->get_files;
175 0 foreach my $file ( @{ $package_files } ) {
176 0 ▶ next if ( $module_check{ $file } ); # modules are separate
Branch: 1 branch
BranchTrueFalseTotal
if $module_check{$file} 0 0 0
177 0 my @file_pieces = split /\//, $file;
178 0 my $brick_name = $file_pieces[-1];
179 0 ▶ if ( $seen_names{ $brick_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_names{$brick_name}) 0 0 0
180 0 $brick_name = join ( '_', @file_pieces );
181 }
182 0 $seen_names{ $brick_name }++;
183 0 my $inline_name = uc( $brick_name );
184 0 $inline_name =~ s/\W/_/g;
185 0 push @brick_files, {
186 name => $brick_name,
187 inline_name => $inline_name,
188 destination => join( ' ', @file_pieces ),
189 evaluate => 'no',
190 contents => $self->_read_package_file_contents( $package_dir, $file ),
191 };
192 }
193 0 return @brick_files;
194 }
195  
196 0 ▶sub _read_package_file_contents {
Subroutine _read_package_file_contents: not called CC=4 SCAR=30.0
197 0 my ( $self, $package_dir, $file ) = @_;
198 0 my $full_path = catfile( $package_dir, $file );
199 0 my $binary_pat = join( '|', @BINARIES );
200 0 ▶ if ( $full_path =~ /$binary_pat$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_path =~ /$binary_pat$/) { } 0 0 0
201 0 ▶ open( IN, '<', $full_path )
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<", $full_path or oi_error("Cannot read '${full_path}': $!") 0 0 0
202 || oi_error "Cannot read '$full_path': $!";
203 0 my @content = ();
204 0 my ( $buf );
205 0 ▶ while ( read( IN, $buf, 60*57 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read IN, $buf, 3420) 0 0 0
206 0 push @content, encode_base64( $buf );
207 }
208 0 return join( ', ', @content );
209 }
210 else {
211 0 return OpenInteract2::Util->read_file( $full_path );
212 }
213 }
214  
215 0 ▶sub _create_distribution {
Subroutine _create_distribution: not called CC=4 SCAR=30.0
216 0 my ( $self, $subclass_name ) = @_;
217 0 chdir( $DIST_DIR );
218  
219 0 ExtUtils::Manifest::mkmanifest();
220  
221 0 my ( $dist_file );
222 0 eval {
223 0 do './Makefile.PL';
224 0 my $make_cmd = $self->param( 'make_bin' );
225 0 system( $make_cmd, 'dist' );
226 0 ▶ opendir( ARCHIVE, '.' )
Branch: 1 branch
BranchTrueFalseTotal
opendir ARCHIVE, "." or die "Cannot open current directory for reading: $!\n" 0 0 0
227 || die "Cannot open current directory for reading: $!\n";
228 0 ( $dist_file ) = grep /$subclass_name/, grep /\.tar\.gz$/, readdir( ARCHIVE );
229 0 closedir( ARCHIVE );
230 0 ▶ rename( $dist_file, catfile( '..', $dist_file ) )
Branch: 1 branch
BranchTrueFalseTotal
rename $dist_file, catfile("..", $dist_file) or die "Cannot move archive: $!\n" 0 0 0
231 || die "Cannot move archive: $!\n";
232 0 chdir( '..' );
233 };
234 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
235 0 chdir( '..' );
236 0 oi_error( $@ );
237 }
238 0 return $dist_file;
239 }
240  
241 87 OpenInteract2::Manage->register_factory_type( get_name() => __PACKAGE__ );
242  
243 87 1;
244  
245 __END__
« OpenInteract2/Response/Standalone.pm ↑ Summary OpenInteract2/Config/GlobalOverride.pm »