]> iEval git - gruntmaster-page.git/blame - make_static.PL
Merge branch 'master' into newmc
[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/;
cf39f49f 9use SVG::SpriteMaker;
fa8b7f99 10use File::Which;
5eae1f77 11use List::Util qw/first/;
8798626b
MG
12
13mkdir 'static';
14mkdir 'static/css';
f57a9178 15mkdir 'static/js';
cf39f49f 16mkdir 'static/logos';
c9dee865 17
3c9bcb47
MG
18sub gzip_file {
19 my ($file) = @_;
fa8b7f99
MG
20 my $zopfli = which 'zopfli';
21 system $zopfli => $file if $zopfli;
22 gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli;
3c9bcb47
MG
23}
24
cf39f49f
MG
25sub write_gzfile {
26 my ($file, @content) = @_;
27 write_file $file, @content;
28 gzip_file $file
29}
30
31sub sprite_name {
32 my ($name) = $_[0] =~ m,/(.*)\.svg,;
33 "logo-$name"
34}
35
36sub make_logos {
37 my @logos = <logos/*>;
38 my $logos = make_sprite \&sprite_name, @logos;
39 my @logos_light;
40 for (<logos/*>) {
41 my $light = s/logos/logos-light/r;
42 push @logos_light, -f $light ? $light : $_;
43 }
44 my $logos_light = make_sprite \&sprite_name, @logos_light;
45
46 write_gzfile 'static/logos/dark.svg', $logos->render;
47 write_gzfile 'static/logos/light.svg', $logos_light->render;
48}
49
5eae1f77
MG
50sub read_css_into_blocks {
51 my ($file) = @_;
52 my (@blocks, $block);
53 for (read_file $file) {
54 $block .= $_;
55 if (/^}/) {
56 push @blocks, $block;
57 $block = '';
58 }
59 }
60 \@blocks
61}
62
cf2b0139 63sub make_css {
5eae1f77
MG
64 my %css;
65 $css{common} .= read_file $_ for <css/*.css>;
66
67 my (%themes, $rndtheme);
cf2b0139 68 for (<css/themes/*>) {
5eae1f77
MG
69 ($rndtheme) = m,themes/(.*)\.css,;
70 $themes{$rndtheme} = read_css_into_blocks $_;
71 }
72
73 while (grep { scalar @$_ } values %themes) {
74 my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
75 if (grep { $_ ne $blocks{$rndtheme} } values %blocks) {
76 $css{$_} .= $blocks{$_} for keys %themes;
77 } else {
78 $css{common} .= $blocks{$rndtheme};
79 }
80 }
81
cf39f49f 82 write_gzfile "static/css/$_.css", minify $css{$_} for keys %css
8798626b
MG
83}
84
cf2b0139 85sub make_js {
8345760a 86 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 87 my $js = read_file 'static/js/all.js';
cf39f49f 88 write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
f57a9178 89 system 'cp', '-rp', 'js', 'static/';
cf2b0139
MG
90}
91
cf39f49f
MG
92my $sprite_mtime = -M 'static/logos/dark.svg' // 0;
93for (<logos/*>, <logos-light/*>) {
7fd2e6a5 94 if (!$sprite_mtime || $sprite_mtime > -M) {
cf39f49f 95 make_logos;
7fd2e6a5
MG
96 last
97 }
98}
99
cf2b0139
MG
100my $css_mtime = -M 'static/css/slate.css' // 0;
101for (<css/*>, <css/themes/*>) {
102 if (!$css_mtime || $css_mtime > -M) {
103 make_css;
104 last
105 }
106}
107
b5d06694 108my $js_mtime = -M 'static/js/all.js' // 0;
cf2b0139
MG
109for (<js/*>) {
110 if (!$js_mtime || $js_mtime > -M) {
111 make_js;
112 last
113 }
9cad7bdd 114}
69d77267 115
7e8f9a5c
MG
116edit_file_lines {
117 my ($file) = m,(static.*\.(?:css|js)),;
118 return unless $file;
119 my $hash = sha256_base64 scalar read_file $file;
120 s/integrity=".*"/integrity="sha256-$hash="/;
65ef5465 121} 'tmpl/skel.en';
This page took 0.043739 seconds and 4 git commands to generate.