]> iEval git - plack-app-gruntmaster.git/blob - make_static.PL
Remove all logos, replace PNG with SVG
[plack-app-gruntmaster.git] / make_static.PL
1 #!/usr/bin/perl
2 use v5.14;
3 use warnings;
4
5 use CSS::Minifier::XS qw/minify/;
6 use Digest::SHA qw/sha256_base64/;
7 use IO::Compress::Gzip qw/gzip/;
8 use File::Slurp qw/read_file write_file edit_file_lines/;
9 use SVG::SpriteMaker;
10 use List::Util qw/first/;
11
12 mkdir 'static';
13 mkdir 'static/css';
14 mkdir 'static/js';
15 mkdir 'static/logos';
16
17 sub gzip_file {
18 my ($file) = @_;
19 gzip $file => "$file.gz", -Level => 9, Minimal => 1;
20 }
21
22 sub write_gzfile {
23 my ($file, @content) = @_;
24 write_file $file, @content;
25 gzip_file $file
26 }
27
28 sub sprite_name {
29 my ($name) = $_[0] =~ m,/(.*)\.svg,;
30 "logo-$name"
31 }
32
33 sub 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
47 sub 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
60 sub make_css {
61 my %css;
62 $css{common} .= read_file $_ for <css/*.css>;
63
64 my (%themes, $rndtheme);
65 for (<css/themes/*>) {
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
79 write_gzfile "static/css/$_.css", minify $css{$_} for keys %css
80 }
81
82 sub make_js {
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/*>;
84 my $js = read_file 'static/js/all.js';
85 write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
86 system 'cp', '-rp', 'js', 'static/';
87 }
88
89 my $sprite_mtime = -M 'static/logos/dark.svg' // 0;
90 for (<logos/*>, <logos-light/*>) {
91 if (!$sprite_mtime || $sprite_mtime > -M) {
92 make_logos;
93 last
94 }
95 }
96
97 my $css_mtime = -M 'static/css/slate.css' // 0;
98 for (<css/*>, <css/themes/*>) {
99 if (!$css_mtime || $css_mtime > -M) {
100 make_css;
101 last
102 }
103 }
104
105 my $js_mtime = -M 'static/js/all.js' // 0;
106 for (<js/*>) {
107 if (!$js_mtime || $js_mtime > -M) {
108 make_js;
109 last
110 }
111 }
112
113 edit_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="/;
118 } 'tmpl/skel.en';
This page took 0.063441 seconds and 5 git commands to generate.