| 1 | package OpenInteract2::Brick; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # $Id: Brick.pm,v 1.4 2005/02/17 04:59:43 lachoy Exp $ | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 87 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 87 | ▶ | use base qw( Class::Factory ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 87 | ▶ | use File::Basename qw( basename dirname ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 87 | ▶ | use File::Path qw( mkpath ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 87 | ▶ | use File::Spec::Functions qw( catfile ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 87 | ▶ | use Log::Log4perl qw( get_logger ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 87 | ▶ | use OpenInteract2::Config::Readonly; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 87 | ▶ | use OpenInteract2::Constants qw( :log ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 87 | ▶ | use OpenInteract2::Exception qw( oi_error ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 87 | ▶ | use OpenInteract2::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 87 | ▶ | use Template; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 87 | $OpenInteract2::Brick::VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 87 | my $TEMPLATE = Template->new(); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 87 | my ( $log ); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | ▶ | sub list_bricks { | ||||||||||||||||||
|
Subroutine
list_bricks: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 24 | 0 | my ( $class ) = @_; | |||||||||||||||||||
| 25 | 0 | return $class->get_registered_types; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 8 | ▶ | sub list_resources { | ||||||||||||||||||
|
Subroutine
list_resources: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 29 | 8 | my ( $self ) = @_; | |||||||||||||||||||
| 30 | 8 | my %all_resources = $self->get_resources; | |||||||||||||||||||
| 31 | 8 | return sort keys %all_resources; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 82 | ▶ | sub load_resource { | ||||||||||||||||||
|
Subroutine
load_resource: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||
| 35 | 82 | my ( $self, $name ) = @_; | |||||||||||||||||||
| 36 | 82 | ▶ | unless ( $name ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | oi_error "You must specify a resource name to load."; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | 82 | my %all_resources = $self->get_resources; | |||||||||||||||||||
| 40 | 82 | my $info = $all_resources{ $name }; | |||||||||||||||||||
| 41 | 82 | ▶ | unless ( $info ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | oi_error "Resource '$name' is invalid. Valid resources are: ", | |||||||||||||||||||
| 43 | join( ', ', sort keys %all_resources ); | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 82 | ▶ | $info->[1] ||= 'yes'; | ||||||||||||||||||
|
Condition50%1/2
$info->[1] ||= "yes"
MC/DC0%0/1
$info->[1] ||= "yes"
$info->[1]
Truth table50%1/2
A
A= $info->[1]
| |||||||||||||||||||||
| 46 | return { | ||||||||||||||||||||
| 47 | 82 | content => $self->load( $name ), | |||||||||||||||||||
| 48 | destination => $info->[0], | ||||||||||||||||||||
| 49 | evaluate => $info->[1], | ||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 8 | ▶ | sub copy_all_resources_to { | ||||||||||||||||||
|
Subroutine
copy_all_resources_to: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 54 | 8 | my ( $self, $dest_dir, $template_vars ) = @_; | |||||||||||||||||||
| 55 | 8 | return $self->copy_resources_to( | |||||||||||||||||||
| 56 | $dest_dir, $template_vars, $self->list_resources | ||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 8 | ▶ | sub copy_resources_to { | ||||||||||||||||||
|
Subroutine
copy_resources_to: called CC=16 SCAR=31.4
Pod
undocumented
| |||||||||||||||||||||
| 61 | 8 | my ( $self, $dest_dir, $template_vars, @resource_names ) = @_; | |||||||||||||||||||
| 62 | 8 | ▶ | $log ||= get_logger( LOG_INIT ); | ||||||||||||||||||
|
Condition66%2/3
$log ||= get_logger("OI2.INITIALIZE")
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log
get_logger("OI2.INITIALIZE")
Truth table66%2/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 8 | ▶ | $template_vars ||= {}; | ||||||||||||||||||
|
Condition50%1/2
$template_vars ||= {}
MC/DC0%0/1
$template_vars ||= {}
$template_vars
Truth table50%1/2
A
A= $template_vars
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 8 | my @copied = (); | |||||||||||||||||||
| 67 | 8 | my @skipped = (); | |||||||||||||||||||
| 68 | 8 | my @same = (); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | NAME: | ||||||||||||||||||||
| 71 | 8 | foreach my $name ( @resource_names ) { | |||||||||||||||||||
| 72 | 82 | my $info = $self->load_resource( $name ); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # First process any template keys in the destination... | ||||||||||||||||||||
| 75 | 82 | my ( $final_dest_spec ); | |||||||||||||||||||
| 76 | 82 | ▶ | $TEMPLATE->process( \$info->{destination}, $template_vars, \$final_dest_spec ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | || oi_error "Cannot process destination '$info->{destination}': ", | ||||||||||||||||||||
| 78 | $TEMPLATE->error(); | ||||||||||||||||||||
| 79 | 82 | ▶ | $log->is_info && $log->info( "Translated '$info->{destination}' ", | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | "-> $final_dest_spec" ); | ||||||||||||||||||||
| 81 | 82 | my @dest_spec = split( /\s+/, $final_dest_spec ); | |||||||||||||||||||
| 82 | 82 | my $relative_dest = join( '/', @dest_spec ); | |||||||||||||||||||
| 83 | 82 | my $full_dest_file = catfile( $dest_dir, @dest_spec ); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 82 | ▶ | if ( $self->_is_readonly( $full_dest_file ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | ▶ | $log->is_info && | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | $log->info( "Skipping '$full_dest_file', marked ", | ||||||||||||||||||||
| 88 | "as readonly in the destination directory" ); | ||||||||||||||||||||
| 89 | 0 | push @skipped, $full_dest_file; | |||||||||||||||||||
| 90 | 0 | next NAME; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # ...next, evaluate the content if we're supposed to | ||||||||||||||||||||
| 94 | 82 | my $content = $info->{content}; | |||||||||||||||||||
| 95 | 82 | ▶ | unless ( 'no' eq lc $info->{evaluate} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 24 | my ( $new_content ); | |||||||||||||||||||
| 97 | 24 | ▶ | $TEMPLATE->process( \$content, $template_vars, \$new_content ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | || oi_error "Cannot copy and replace tokens from resource '$name': ", | ||||||||||||||||||||
| 99 | $TEMPLATE->error(); | ||||||||||||||||||||
| 100 | 24 | ▶ | $log->is_info && $log->info( "Processed template ok" ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 24 | $content = $new_content; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 82 | ▶ | if ( $self->_is_same( $full_dest_file, $content ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | ▶ | $log->is_info && | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | $log->info( "Skipping '$full_dest_file', content and ", | ||||||||||||||||||||
| 107 | "destination file are the same" ); | ||||||||||||||||||||
| 108 | 0 | push @same, $full_dest_file; | |||||||||||||||||||
| 109 | 0 | next NAME; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 82 | my $dest_dir = dirname( $full_dest_file ); | |||||||||||||||||||
| 113 | 82 | ▶ | unless ( -d $dest_dir ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 1 | mkpath( $dest_dir ); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 82 | ▶ | open( OUT, '>', $full_dest_file ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | || oi_error "Cannot write resource '$name' to '$full_dest_file': $!"; | ||||||||||||||||||||
| 118 | 82 | print OUT $content; | |||||||||||||||||||
| 119 | 82 | close( OUT ); | |||||||||||||||||||
| 120 | 82 | ▶ | $log->is_info && | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | $log->info( "Copied resource '$name' to '$full_dest_file' ok" ); | ||||||||||||||||||||
| 122 | 82 | push @copied, $full_dest_file; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | return { | ||||||||||||||||||||
| 125 | 8 | copied => \@copied, | |||||||||||||||||||
| 126 | skipped => \@skipped, | ||||||||||||||||||||
| 127 | same => \@same, | ||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 82 | ▶ | sub _is_readonly { | ||||||||||||||||||
|
Subroutine
_is_readonly: called CC=2 SCAR=10.9
| |||||||||||||||||||||
| 132 | 82 | my ( $self, $dest_file ) = @_; | |||||||||||||||||||
| 133 | 82 | ▶ | return 0 unless ( -f $dest_file ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | my $base_dest_file = basename( $dest_file ); | |||||||||||||||||||
| 135 | 0 | my $full_dest_dir = dirname( $dest_file ); | |||||||||||||||||||
| 136 | 0 | my $ro_check = OpenInteract2::Config::Readonly->new( $full_dest_dir ); | |||||||||||||||||||
| 137 | 0 | return ( ! $ro_check->is_writeable( $base_dest_file ) ); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 82 | ▶ | sub _is_same { | ||||||||||||||||||
|
Subroutine
_is_same: called CC=3 SCAR=19.2
| |||||||||||||||||||||
| 141 | 82 | my ( $self, $dest_file, $content ) = @_; | |||||||||||||||||||
| 142 | 82 | ▶ | return 0 unless ( -f $dest_file ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 0 | my $source_size = length $content; | |||||||||||||||||||
| 144 | 0 | my $dest_file_size = (stat $dest_file)[7]; | |||||||||||||||||||
| 145 | 0 | ▶ | return 0 unless ( $source_size == $dest_file_size ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | my $source_digest = | |||||||||||||||||||
| 147 | OpenInteract2::Util->digest_content( $content ); | ||||||||||||||||||||
| 148 | 0 | my $dest_digest = | |||||||||||||||||||
| 149 | OpenInteract2::Util->digest_file( $dest_file ); | ||||||||||||||||||||
| 150 | 0 | return ( $source_digest eq $dest_digest ); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | ######################################## | ||||||||||||||||||||
| 154 | # SUBCLASSES | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 0 | ▶ | sub get_name { _must_implement( 'get_name', @_ ) } | ||||||||||||||||||
|
Subroutine
get_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 157 | 0 | ▶ | sub get_resources { _must_implement( 'get_resources', @_ ) } | ||||||||||||||||||
|
Subroutine
get_resources: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 158 | 0 | ▶ | sub load { _must_implement( 'load', @_ ) } | ||||||||||||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 0 | ▶ | sub _must_implement { | ||||||||||||||||||
|
Subroutine
_must_implement: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 161 | 0 | my ( $method, $item ) = @_; | |||||||||||||||||||
| 162 | 0 | ▶ | my $class = ref( $item ) || $item; | ||||||||||||||||||
|
Condition0%0/3
ref $item || $item
MC/DC0%0/2
ref $item || $item
ref $item
$item
Truth table0%0/3
A || B
A= ref $itemB= $item
| |||||||||||||||||||||
| 163 | 0 | oi_error "Class '$class' must implement method '$method'"; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 87 | OpenInteract2::Util->find_factory_subclasses( | |||||||||||||||||||
| 167 | 'OpenInteract2::Brick', @INC | ||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | ######################################## | ||||||||||||||||||||
| 171 | # GENERATING NEW BRICKS | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | sub get_brick_class_template { | ||||||||||||||||||
|
Subroutine
get_brick_class_template: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 174 | 0 | return <<'TEMPLATE'; | |||||||||||||||||||
| 175 | package OpenInteract2::Brick::[% brick_name %]; | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | use strict; | ||||||||||||||||||||
| 178 | use base qw( OpenInteract2::Brick ); | ||||||||||||||||||||
| 179 | use OpenInteract2::Exception; | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | my %INLINED_SUBS = ( | ||||||||||||||||||||
| 182 | [% FOREACH file_info = all_files -%] | ||||||||||||||||||||
| 183 | '[% file_info.name %]' => '[% file_info.inline_name %]', | ||||||||||||||||||||
| 184 | [% END -%] | ||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | sub get_name { | ||||||||||||||||||||
| 188 | return '[% lc_brick_name %]'; | ||||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | sub get_resources { | ||||||||||||||||||||
| 192 | return ( | ||||||||||||||||||||
| 193 | [% FOREACH file_info = all_files -%] | ||||||||||||||||||||
| 194 | '[% file_info.name %]' => [ '[% file_info.destination %]', '[% file_info.evaluate %]' ], | ||||||||||||||||||||
| 195 | [% END -%] | ||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | sub load { | ||||||||||||||||||||
| 200 | my ( $self, $resource_name ) = @_; | ||||||||||||||||||||
| 201 | my $inline_sub_name = $INLINED_SUBS{ $resource_name }; | ||||||||||||||||||||
| 202 | unless ( $inline_sub_name ) { | ||||||||||||||||||||
| 203 | OpenInteract2::Exception->throw( | ||||||||||||||||||||
| 204 | "Resource name '$resource_name' not found ", | ||||||||||||||||||||
| 205 | "in ", ref( $self ), "; cannot load content." ); | ||||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | return $self->$inline_sub_name(); | ||||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | OpenInteract2::Brick->register_factory_type( get_name() => __PACKAGE__ ); | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =pod | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head1 NAME | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | OpenInteract2::Brick::[% brick_name %] - [% brick_summary %] | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =head1 SYNOPSIS | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | [% brick_example | indent(2) %] | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | =head1 DESCRIPTION | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | [% brick_description %] | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =head2 Resources | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | You can grab resources individually using the names below and | ||||||||||||||||||||
| 229 | C<load_resource()> and C<copy_resources_to()>, or you can copy all the | ||||||||||||||||||||
| 230 | resources at once using C<copy_all_resources_to()> -- see | ||||||||||||||||||||
| 231 | L<OpenInteract2::Brick> for details. | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =over 4 | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | [% FOREACH file_info = all_files %] | ||||||||||||||||||||
| 236 | =item B<[% file_info.name %]> | ||||||||||||||||||||
| 237 | [% END %] | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =head1 COPYRIGHT | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | Copyright (c) 2005 [% author_names.join( ', ' ) %]. All rights reserved. | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 246 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =head1 AUTHORS | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | [% FOREACH author_info = authors %] | ||||||||||||||||||||
| 251 | [% author_info.name %] E<lt>[% author_info.email %]E<gt> | ||||||||||||||||||||
| 252 | [% END %] | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | =cut | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | [% FOREACH file_info = all_files %] | ||||||||||||||||||||
| 257 | sub [% file_info.inline_name %] { | ||||||||||||||||||||
| 258 | return <<'SOMELONGSTRING'; | ||||||||||||||||||||
| 259 | [% file_info.contents %] | ||||||||||||||||||||
| 260 | SOMELONGSTRING | ||||||||||||||||||||
| 261 | } | ||||||||||||||||||||
| 262 | [% END %] | ||||||||||||||||||||
| 263 | TEMPLATE | ||||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | 87 | 1; | |||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | __END__ | ||||||||||||||||||||