Fix bugs in adding other (CSS, sprite) files to bundles.

This commit is contained in:
Colin Kuskie 2009-05-18 21:33:13 +00:00
parent 98a1e4c409
commit 57c427cb6d
4 changed files with 62 additions and 10 deletions

View file

@ -255,7 +255,7 @@ EOTABLE
my $dt = $session->datetime;
my $url = $session->url;
my $lastModifiedi18n = $i18n->get('last modified');
foreach my $fileType (qw/jsFiles cssFiles/) {
foreach my $fileType (qw/jsFiles cssFiles otherFiles/) {
my $type = $fileType eq 'jsFiles' ? 'JS'
: $fileType eq 'cssFiles' ? 'CSS'
: 'OTHER';

View file

@ -5,6 +5,7 @@ use WebGUI::International;
use WebGUI::Utility;
use URI;
use Path::Class;
use File::Basename;
use CSS::Minifier::XS;
use JavaScript::Minifier::XS;
use LWP;
@ -126,6 +127,36 @@ sub build {
return (0, $errorMessages);
}
##Copy files over
my $otherFiles = $self->get('otherFiles');
my $i18n = WebGUI::International->new($self->session, 'FilePump');
OTHERFILE: foreach my $file (@{ $otherFiles }) {
my $uri = $file->{uri};
my $results = $self->fetch($uri);
if (! $results->{content}) {
$error = $uri;
last OTHERFILE;
}
$file->{lastModified} = $results->{lastModified};
my $uriPath = URI->new($uri)->opaque;
$uriPath =~ tr{/}{/}s;
my $filename = basename($uriPath);
my $newFile = $newDir->file($filename);
if (-e $newFile->stringify) {
$error = join ' ', $uri, $i18n->get('duplicate file');
last OTHERFILE;
}
my $fh = $newFile->open('>');
$fh->binmode;
print $fh $results->{content};
close $fh;
}
if ($error) {
$newDir->rmtree;
return (0, $error);
}
##Minimize files, and write them out.
my $minimizedJS = JavaScript::Minifier::XS::minify($concatenatedJS);
@ -147,9 +178,10 @@ sub build {
##Delete the old build directory and update myself with the new data.
$self->deleteBuild();
$self->update({
jsFiles => $jsFiles,
cssFiles => $cssFiles,
lastBuild => $newBuild,
jsFiles => $jsFiles,
cssFiles => $cssFiles,
otherFiles => $otherFiles,
lastBuild => $newBuild,
});
return 1;
}

View file

@ -70,6 +70,12 @@ our $I18N = {
context => q|Edit bundle label.|
},
'otherFiles' => {
message => q|CSS Images|,
lastUpdated => 1242681632,
context => q|Edit bundle label.|
},
'build this bundle' => {
message => q|Build this bundle|,
lastUpdated => 1242495011,
@ -94,6 +100,12 @@ our $I18N = {
context => q|Meaning that something has not been done at this time. Before the first time.|
},
'duplicate file' => {
message => q|A file with the same name already exists in the build directory.|,
lastUpdated => 1242515308,
context => q|Error message when building a new bundle.|
},
};
1;

View file

@ -33,7 +33,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 46; # Increment this number for each test you create
my $tests = 50; # Increment this number for each test you create
plan tests => 1 + $tests; # 1 for the use_ok
#----------------------------------------------------------------------------
@ -334,18 +334,26 @@ $fileAsset->update({filename => 'pumpfile.css'});
$bundle->addFile('JS', 'asset://filePumpSnippet');
$bundle->addFile('CSS', 'asset://filePumpFileAsset');
$bundle->addFile('OTHER', 'file:'.WebGUI::Test->getTestCollateralPath('gooey.jpg'));
my ($buildFlag, $error) = $bundle->build();
ok($buildFlag, 'build returns true when there are no errors');
diag $error unless $buildFlag;
isnt($bundle->get('lastBuild'), $wgBday, '... lastBuild time updated');
my $buildDir = $bundle->getPathClassDir();
isnt($buildDir->stringify, $oldBuildDir->stringify, '... build directory did actually change');
ok(-e $buildDir->stringify && -d _, '... new build directory created');
ok(!-e $oldBuildDir->stringify && !-d _, '... old build directory deleted');
my $jsFile = $buildDir->file($bundle->bundleUrl . '.js');
my $cssFile = $buildDir->file($bundle->bundleUrl . '.css');
ok(-e $jsFile->stringify && -f _ && -s _, '... minified JS file built, not empty');
ok(-e $cssFile->stringify && -f _ && -s _, '... minified CSS file built, not empty');
my $jsFile = $buildDir->file($bundle->bundleUrl . '.js');
my $cssFile = $buildDir->file($bundle->bundleUrl . '.css');
my $otherFile = $buildDir->file('gooey.jpg');
ok(-e $jsFile->stringify && -f _ && -s _, '... minified JS file built, not empty');
ok(-e $cssFile->stringify && -f _ && -s _, '... minified CSS file built, not empty');
ok(-e $otherFile->stringify && -f _ && -s _, '... other file copied over, not empty');
ok($bundle->get('jsFiles')->[0]->{lastModified}, '... updated JS file lastModified');
ok($bundle->get('cssFiles')->[0]->{lastModified}, '... updated CSS file lastModified');
ok($bundle->get('otherFiles')->[0]->{lastModified}, '... updated OTHER file lastModified');
###################################################################
#
@ -354,7 +362,7 @@ ok(-e $cssFile->stringify && -f _ && -s _, '... minified CSS file built, not emp
###################################################################
$bundle->delete;
ok(!-e $buildDir->stringify && !-d _, 'delete deletes the current build directory deleted');
ok(!-e $buildDir->stringify, 'delete deletes the current build directory');
}