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