combine-resources.pl 3.0 KB

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