Skip to content

Commit

Permalink
Item2041: Make BuildContrib gzip css and js when the .css.gz or .js.g…
Browse files Browse the repository at this point in the history
…z is in the MANIFEST

git-svn-id: http://svn.foswiki.org/trunk@5082 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelTempest authored and MichaelTempest committed Sep 22, 2009
1 parent 70c3f5e commit 36f2779
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions BuildContrib/lib/Foswiki/Contrib/Build.pm
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ my @stageFilters = (
my @compressFilters = (
{ RE => qr/\.js$/, filter => 'build_js' },
{ RE => qr/\.css$/, filter => 'build_css' },
{ RE => qr/\.gz$/, filter => 'build_gz' },
);

my @tidyFilters = ( { RE => qr/\.pl$/ }, { RE => qr/\.pm$/ }, );
Expand Down Expand Up @@ -879,18 +880,17 @@ won't fail if a source or target isn't missing.

sub target_compress {
my $this = shift;
FILE:
foreach my $file ( @{ $this->{files} } ) {
my %file_ok;
foreach my $filter (@compressFilters) {
FILE:
foreach my $file ( @{ $this->{files} } ) {
next FILE if $file_ok{$file};

# Find files that match the build filter and try to update
# them
foreach my $filter (@compressFilters) {
if ( $file->{name} =~ /$filter->{RE}/ ) {
my $fn = $filter->{filter};
my $ok = $this->$fn( $this->{basedir} . '/' . $file->{name} );
if ($ok) {
next FILE;
}
$file_ok{$file} = $this->$fn( $this->{basedir} . '/' . $file->{name} );
}
}
}
Expand Down Expand Up @@ -1150,6 +1150,50 @@ sub build_css {

=begin TML
---++++ build_gz
Uses Compress::Zlib to gzip files
* xxx.yyy -> xxx.yyy.gz
=cut

sub build_gz {
my ( $this, $to ) = @_;

unless ( eval { require Compress::Zlib } ) {
print STDERR "Cannot gzip $to: $@\n";
return 0;
}

my $from = $to;
$from =~ s/\.gz$// or return 0;
return 0 unless -e $from;

open( IF, '<', $from ) || die $!;
local $/ = undef;
my $text = <IF>;
close(IF);

$text = Compress::Zlib::memGzip( $text );

unless ( $this->{-n} ) {
if ( open( IF, '<', $to ) ) {
binmode IF;
my $ot = <IF>;
close($ot);
return 1 if $text eq $ot; # no changes?
}
open( OF, '>', $to ) || die "$to: $!";
binmode OF;
print OF $text;
close(OF);
print STDERR "Generated $to from $from\n";
}
return 1;
}

=begin TML
---++++ filter_pm($from, $to)
Filters expanding SVN rev number with correct version from repository
Note: unlike subversion, this puts in the version number of the whole
Expand Down

0 comments on commit 36f2779

Please sign in to comment.