discourse-legacysite-perl/site/common/bin/minify.cgi
2024-06-17 21:49:12 +10:00

144 lines
4.5 KiB
Perl
Executable File

#!/usr/bin/perl
use strict;
use FindBin qw/$Bin/;
FindBin::again;
use lib "$Bin/../admin";
use GForum qw/$IN $CFG $DB %STASH/;
GForum::init("$Bin/../admin");
$STASH{admin_request} = 1;
$|++;
use constant HOME => "/home/slowtwitch/site/common";
our $YUI_COMP = HOME . '/bin/yuicompressor-2.4.8.jar';
our $GOOGLE_CLOSURE = HOME . '/bin/compiler.jar';
our %CSS = (
'core.css' => 'core.min.css',
'forum.css' => 'forum.min.css',
'print.css' => 'print.min.css'
);
our %CSS_APPEND = (
'forum.css' => [
'jquery-ui.min.css',
'lightbox.min.css',
'core.min.css',
]
);
# These must be in the static/js directory, and get placed into
# static/js/file.min.js
our %JS = (
'gforum.js' => 'gforum.min.js',
'utils.js' => 'utils.min.js',
'core.js' => 'base.min.js'
);
our %JS_APPEND = (
'core.js' => [
'jquery.min.js',
'jquery-ui.min.js',
'jquery.form.min.js',
'jquery.jcarousellite.min.js',
'jquery.autocomplete.min.js'
],
'gforum.js' => [
'utils.min.js'
]
);
print $IN->header, "<pre>";
main();
sub main {
# -------------------------------------------------------------------
print "Creating css files ... \n";
for my $css (keys %CSS) {
print " $css => $CSS{$css} ... \n";
my $full_path = HOME . "/static/css/$css";
my $output = HOME . "/static/css/$CSS{$css}";
if (! -e $full_path) {
die "Missing css file $full_path";
}
if ($CSS_APPEND{$css}) {
open my $fh, ">", "$full_path.tmp" or die "open $full_path.tmp ($!)";
for my $file (@{$CSS_APPEND{$css}}) {
print " Appending $file\n";
my $append_file = HOME . "/static/css/$file";
open my $append, "<", $append_file or die "open $append_file: $!";
while (<$append>) {
print $fh $_;
}
close $append;
}
close $fh;
}
system("java -jar $YUI_COMP $full_path >> $full_path.tmp") or die $!;
# Minifying breaks this WebKit css filter. Put a space back in between
# the 'and' and '('.
system("perl -p -i -e 's/\@media screen and\\(/\@media screen and (/' $full_path.tmp");
chmod(0644, "$full_path.tmp") or die "chmod $full_path.tmp ($!)";
unless (-s "$full_path.tmp") {
die "New css file is 0 bytes, bailing!";
}
rename("$full_path.tmp", $output) or die "rename ($full_path.tmp) ($output): $!";
}
print "\nCreate js files ... \n";
for my $js (keys %JS) {
print " $js => $JS{$js}\n";
my $full_path = HOME . "/static/js/$js";
my $output = HOME . "/static/js/$JS{$js}";
if (! -e $full_path) {
die "Missing js file $full_path";
}
if ($JS_APPEND{$js}) {
open my $fh, ">", "$full_path.tmp" or die "open $full_path.tmp ($!)";
for my $file (@{$JS_APPEND{$js}}) {
print " Appending $file\n";
my $append_file = HOME . "/static/js/$file";
open my $append, "<", $append_file or die "open $append_file: $!";
while (<$append>) {
print $fh $_;
}
close $append;
}
close $fh;
}
system("java -jar $GOOGLE_CLOSURE -js $full_path >> $full_path.tmp");
chmod(0644, "$full_path.tmp") or die "chmod $full_path.tmp ($!)";
unless (-s "$full_path.tmp") {
die "New js file is 0 bytes, bailing!";
}
rename("$full_path.tmp", $output) or die "rename ($full_path.tmp) ($output): ($!)";
}
my $rev;
my $globals = '';
open my $fh, "<", HOME . "/templates/include_global_head.html" or die "open include_global_head.html ($!)";
while (<$fh>) {
if (m{mini_version\s*=\s*'(\d+)'}) {
$rev = $1;
my $new = $rev + 1;
s{mini_version\s*=\s*'(\d+)'}{mini_version = '$new'};
$rev = $new;
}
$globals .= $_;
}
close $fh;
if (! $rev) { die "Couldn't find revision in include_global_head.html"; }
print "Updating include_global_head.html\n";
open $fh, ">", HOME . "/templates/include_global_head.html.new" or die "open: $!";
print $fh $globals;
close $fh;
chmod(0640, HOME . '/templates/include_global_head.html.new') or die "chmod ($!)";
rename(HOME . "/templates/include_global_head.html.new", HOME . "/templates/include_global_head.html") or die "rename ($!)";
print "Done\n";
}