Add a very basic test
[gruntmaster-page.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 File::Which;
10 use List::Util qw/first/;
11
12 mkdir 'static';
13 mkdir 'static/css';
14 mkdir 'static/js';
15
16 sub gzip_file {
17 my ($file) = @_;
18 my $zopfli = which 'zopfli';
19 system $zopfli => $file if $zopfli;
20 gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli;
21 }
22
23 sub write_gzfile {
24 my ($file, @content) = @_;
25 write_file $file, @content;
26 gzip_file $file
27 }
28
29 sub read_css_into_blocks {
30 my ($file) = @_;
31 my (@blocks, $block);
32 for (read_file $file) {
33 $block .= $_;
34 if (/^}/) {
35 push @blocks, $block;
36 $block = '';
37 }
38 }
39 \@blocks
40 }
41
42 my $default_theme = 'cyborg';
43
44 sub theme_prefix {
45 my ($theme, $decl, $default) = @_;
46 return $decl if $theme eq $default_theme || !$decl;
47 return '' if $decl eq $default;
48
49 $default =~ s/[^{]*{\n//;
50 $default =~ s/\n}[^}]*//;
51 $decl =~ s/^$_$//m for split "\n", $default;
52 $decl =~ s/\n+/\n/g;
53
54 my $prefix = "html.$theme";
55 my ($first_line) = $decl =~ /([^{]*){/;
56 $first_line =~ s/(,\s+)/$1 $prefix /g;
57 $first_line = "$prefix $first_line";
58 $decl =~ s/([^{]*){/$first_line\{/;
59 $decl
60 }
61
62 sub make_css {
63 my $css = join '', map { read_file $_ } <css/*.css>;
64
65 my (%themes);
66 for (<css/themes/*>) {
67 my ($theme) = m,themes/(.*)\.css,;
68 $themes{$theme} = read_css_into_blocks $_;
69 }
70 my @themes = sort grep { $_ ne $default_theme } keys %themes;
71
72 while (grep { scalar @$_ } values %themes) {
73 my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
74 $css .= $blocks{$default_theme};
75 $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes
76 }
77
78 write_gzfile "static/css/all.css", minify $css
79 }
80
81 sub make_js {
82 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/*>;
83 my $js = read_file 'static/js/all.js';
84 write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
85 system 'cp', '-rp', 'js', 'static/';
86 }
87
88 my $css_mtime = -M 'static/css/all.css' // 0;
89 for (<css/*>, <css/themes/*>) {
90 if (!$css_mtime || $css_mtime > -M) {
91 make_css;
92 last
93 }
94 }
95
96 my $js_mtime = -M 'static/js/all.js' // 0;
97 for (<js/*>) {
98 if (!$js_mtime || $js_mtime > -M) {
99 make_js;
100 last
101 }
102 }
103
104 edit_file_lines {
105 my ($file) = m,(static.*\.(?:css|js)),;
106 return unless $file;
107 my $hash = sha256_base64 scalar read_file $file;
108 s/integrity=".*"/integrity="sha256-$hash="/;
109 } 'tmpl/skel.en'
This page took 0.027973 seconds and 4 git commands to generate.