]> iEval git - gruntmaster-page.git/blame_incremental - make_static.PL
Use brotli in make_static.PL if available
[gruntmaster-page.git] / make_static.PL
... / ...
CommitLineData
1#!/usr/bin/perl
2use v5.14;
3use warnings;
4
5use CSS::Minifier::XS qw/minify/;
6use Digest::SHA qw/sha256_base64/;
7use IO::Compress::Gzip qw/gzip/;
8use File::Slurp qw/read_file write_file edit_file_lines/;
9use SVG::SpriteMaker;
10use File::Which;
11use List::Util qw/first/;
12
13mkdir 'static';
14mkdir 'static/css';
15mkdir 'static/js';
16mkdir 'static/logos';
17
18sub gzip_file {
19 my ($file) = @_;
20 my $brotli = which 'brotli';
21 my $zopfli = which 'zopfli';
22 system $brotli => '--input', $file, '--output', "$file.br" if $brotli;
23 system $zopfli => $file if $zopfli;
24 gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli;
25}
26
27sub write_gzfile {
28 my ($file, @content) = @_;
29 write_file $file, @content;
30 gzip_file $file
31}
32
33sub sprite_name {
34 my ($name) = $_[0] =~ m,/(.*)\.svg,;
35 "logo-$name"
36}
37
38sub make_logos {
39 my @logos = <logos/*>;
40 my $logos = make_sprite \&sprite_name, @logos;
41 my @logos_light;
42 for (<logos/*>) {
43 my $light = s/logos/logos-light/r;
44 push @logos_light, -f $light ? $light : $_;
45 }
46 my $logos_light = make_sprite \&sprite_name, @logos_light;
47
48 write_gzfile 'static/logos/dark.svg', $logos->render;
49 write_gzfile 'static/logos/light.svg', $logos_light->render;
50}
51
52sub read_css_into_blocks {
53 my ($file) = @_;
54 my (@blocks, $block);
55 for (read_file $file) {
56 $block .= $_;
57 if (/^}/) {
58 push @blocks, $block;
59 $block = '';
60 }
61 }
62 \@blocks
63}
64
65my $default_theme = 'cyborg';
66
67sub theme_prefix {
68 my ($theme, $decl, $default) = @_;
69 return $decl if $theme eq $default_theme || !$decl;
70 return '' if $decl eq $default;
71
72 $default =~ s/[^{]*{\n//;
73 $default =~ s/\n}[^}]*//;
74 $decl =~ s/^$_$//m for split "\n", $default;
75 $decl =~ s/\n+/\n/g;
76
77 my $prefix = "html.$theme";
78 my ($first_line) = $decl =~ /([^{]*){/;
79 $first_line =~ s/(,\s+)/$1 $prefix /g;
80 $first_line = "$prefix $first_line";
81 $decl =~ s/([^{]*){/$first_line\{/;
82 $decl
83}
84
85sub make_css {
86 my $css = join '', map { read_file $_ } <css/*.css>;
87
88 my (%themes);
89 for (<css/themes/*>) {
90 my ($theme) = m,themes/(.*)\.css,;
91 $themes{$theme} = read_css_into_blocks $_;
92 }
93 my @themes = sort grep { $_ ne $default_theme } keys %themes;
94
95 while (grep { scalar @$_ } values %themes) {
96 my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
97 $css .= $blocks{$default_theme};
98 $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes
99 }
100
101 write_gzfile "static/css/all.css", minify $css
102}
103
104sub make_js {
105 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/*>;
106 my $js = read_file 'static/js/all.js';
107 write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
108 system 'cp', '-rp', 'js', 'static/';
109}
110
111my $sprite_mtime = -M 'static/logos/dark.svg' // 0;
112for (<logos/*>, <logos-light/*>) {
113 if (!$sprite_mtime || $sprite_mtime > -M) {
114 make_logos;
115 last
116 }
117}
118
119my $css_mtime = -M 'static/css/all.css' // 0;
120for (<css/*>, <css/themes/*>) {
121 if (!$css_mtime || $css_mtime > -M) {
122 make_css;
123 last
124 }
125}
126
127my $js_mtime = -M 'static/js/all.js' // 0;
128for (<js/*>) {
129 if (!$js_mtime || $js_mtime > -M) {
130 make_js;
131 last
132 }
133}
134
135edit_file_lines {
136 my ($file) = m,(static.*\.(?:css|js)),;
137 return unless $file;
138 my $hash = sha256_base64 scalar read_file $file;
139 s/integrity=".*"/integrity="sha256-$hash="/;
140} 'tmpl/skel.en';
This page took 0.025311 seconds and 4 git commands to generate.