]> iEval git - gruntmaster-page.git/blame - make_static.PL
fixed "Page 1 of 0" in job log
[gruntmaster-page.git] / make_static.PL
CommitLineData
8798626b
MG
1#!/usr/bin/perl
2use v5.14;
3use warnings;
4
5eae1f77 5use CSS::Minifier::XS qw/minify/;
7e8f9a5c 6use Digest::SHA qw/sha256_base64/;
3c9bcb47 7use IO::Compress::Gzip qw/gzip/;
7e8f9a5c 8use File::Slurp qw/read_file write_file edit_file_lines/;
fa8b7f99 9use File::Which;
5eae1f77 10use List::Util qw/first/;
8798626b
MG
11
12mkdir 'static';
13mkdir 'static/css';
f57a9178 14mkdir 'static/js';
8798626b 15
3c9bcb47
MG
16sub gzip_file {
17 my ($file) = @_;
ff4a482f 18 my $brotli = which 'brotli';
fa8b7f99 19 my $zopfli = which 'zopfli';
ff4a482f 20 system $brotli => '--input', $file, '--output', "$file.br" if $brotli;
fa8b7f99
MG
21 system $zopfli => $file if $zopfli;
22 gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli;
23}
24
25sub write_gzfile {
26 my ($file, @content) = @_;
27 write_file $file, @content;
28 gzip_file $file
3c9bcb47
MG
29}
30
5eae1f77
MG
31sub read_css_into_blocks {
32 my ($file) = @_;
33 my (@blocks, $block);
34 for (read_file $file) {
35 $block .= $_;
36 if (/^}/) {
37 push @blocks, $block;
38 $block = '';
39 }
40 }
41 \@blocks
42}
43
db73dc0c
MG
44my $default_theme = 'cyborg';
45
46sub theme_prefix {
47 my ($theme, $decl, $default) = @_;
48 return $decl if $theme eq $default_theme || !$decl;
49 return '' if $decl eq $default;
50
51 $default =~ s/[^{]*{\n//;
52 $default =~ s/\n}[^}]*//;
53 $decl =~ s/^$_$//m for split "\n", $default;
54 $decl =~ s/\n+/\n/g;
55
56 my $prefix = "html.$theme";
57 my ($first_line) = $decl =~ /([^{]*){/;
58 $first_line =~ s/(,\s+)/$1 $prefix /g;
59 $first_line = "$prefix $first_line";
60 $decl =~ s/([^{]*){/$first_line\{/;
61 $decl
62}
63
cf2b0139 64sub make_css {
db73dc0c 65 my $css = join '', map { read_file $_ } <css/*.css>;
5eae1f77 66
db73dc0c 67 my (%themes);
cf2b0139 68 for (<css/themes/*>) {
db73dc0c
MG
69 my ($theme) = m,themes/(.*)\.css,;
70 $themes{$theme} = read_css_into_blocks $_;
5eae1f77 71 }
db73dc0c 72 my @themes = sort grep { $_ ne $default_theme } keys %themes;
5eae1f77
MG
73
74 while (grep { scalar @$_ } values %themes) {
75 my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
db73dc0c
MG
76 $css .= $blocks{$default_theme};
77 $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes
5eae1f77
MG
78 }
79
db73dc0c 80 write_gzfile "static/css/all.css", minify $css
8798626b
MG
81}
82
cf2b0139 83sub make_js {
8345760a 84 system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js/js.map --js_output_file static/js/all.js --language_in ECMASCRIPT6_STRICT --language_out ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, <js/*>;
f57a9178 85 my $js = read_file 'static/js/all.js';
fa8b7f99 86 write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
f57a9178 87 system 'cp', '-rp', 'js', 'static/';
cf2b0139
MG
88}
89
db73dc0c 90my $css_mtime = -M 'static/css/all.css' // 0;
cf2b0139
MG
91for (<css/*>, <css/themes/*>) {
92 if (!$css_mtime || $css_mtime > -M) {
93 make_css;
94 last
95 }
96}
97
a90fc634 98my $js_mtime = -M 'static/js/all.js' // 0;
cf2b0139
MG
99for (<js/*>) {
100 if (!$js_mtime || $js_mtime > -M) {
101 make_js;
102 last
103 }
9cad7bdd 104}
7e8f9a5c
MG
105
106edit_file_lines {
107 my ($file) = m,(static.*\.(?:css|js)),;
108 return unless $file;
109 my $hash = sha256_base64 scalar read_file $file;
110 s/integrity=".*"/integrity="sha256-$hash="/;
111} 'tmpl/skel.en'
This page took 0.038797 seconds and 5 git commands to generate.