1#!/usr/bin/perl -w 2 3use strict; 4use Getopt::Long; 5use File::Basename; 6use File::Path; 7 8our $inputDirectory; 9our $outputDirectory; 10our $outputScriptName; 11our $outputStylesheetName; 12our $derivedSourcesDirectory; 13our $htmlDirectory; 14our $htmlFile; 15 16GetOptions('output-dir=s' => \$outputDirectory, 17 'output-script-name=s' => \$outputScriptName, 18 'output-style-name=s' => \$outputStylesheetName, 19 'derived-sources-dir=s' => \$derivedSourcesDirectory, 20 'input-dir=s' => \$inputDirectory, 21 'input-html-dir=s' => \$htmlDirectory, 22 'input-html=s' => \$htmlFile); 23 24unless (defined $htmlFile and defined $derivedSourcesDirectory and defined $outputDirectory and defined $outputScriptName and defined $outputStylesheetName) { 25 print "Usage: $0 --input-html <path> --derived-sources-dir <path> --output-dir <path> --output-script-name <name> --output-style-name <name>\n"; 26 exit; 27} 28 29$htmlDirectory = dirname($htmlFile) unless $htmlDirectory; 30 31our $htmlContents; 32 33{ 34 local $/; 35 open HTML, $htmlFile or die; 36 $htmlContents = <HTML>; 37 close HTML; 38} 39 40$htmlContents =~ m/<head>(.*)<\/head>/si; 41our $headContents = $1; 42 43mkpath $outputDirectory; 44 45sub concatinateFiles($$$) 46{ 47 my $filename = shift; 48 my $tagExpression = shift; 49 my $concatinatedTag = shift; 50 my $fileCount = 0; 51 52 open OUT, ">", "$outputDirectory/$filename" or die "Can't open $outputDirectory/$filename: $!"; 53 54 while ($headContents =~ m/$tagExpression/gi) { 55 local $/; 56 open IN, "$htmlDirectory/$1" or open IN, "$derivedSourcesDirectory/$1" or die "Can't open $htmlDirectory/$1: $!"; 57 print OUT "\n" if $fileCount++; 58 print OUT "/* $1 */\n\n"; 59 print OUT <IN>; 60 close IN; 61 } 62 63 close OUT; 64 65 # Don't use \s so we can control the newlines we consume. 66 my $replacementExpression = "([\t ]*)" . $tagExpression . "[\t ]*\n+"; 67 68 # Replace the first occurance with a token so we can inject the concatinated tag in the same place 69 # as the first file that got consolidated. This makes sure we preserve some order if there are other 70 # items in the head that we didn't consolidate. 71 $headContents =~ s/$replacementExpression/$1%CONCATINATED%\n/i; 72 $headContents =~ s/$replacementExpression//gi; 73 $headContents =~ s/%CONCATINATED%/$concatinatedTag/; 74} 75 76my $inputDirectoryPattern = "(?!External\/)[^\"]*"; 77$inputDirectoryPattern = $inputDirectory . "\/[^\"]*" if $inputDirectory; 78 79concatinateFiles($outputStylesheetName, "<link rel=\"stylesheet\" href=\"($inputDirectoryPattern)\">", "<link rel=\"stylesheet\" href=\"$outputStylesheetName\">"); 80concatinateFiles($outputScriptName, "<script src=\"($inputDirectoryPattern)\"><\/script>", "<script src=\"$outputScriptName\"></script>"); 81 82$htmlContents =~ s/<head>.*<\/head>/<head>$headContents<\/head>/si; 83 84open HTML, ">", "$outputDirectory/" . basename($htmlFile) or die "Can't open $outputDirectory/" . basename($htmlFile) . ": $!"; 85print HTML $htmlContents; 86close HTML; 87