]> iEval git - gruntmaster-page.git/blame - make_static.PL
Change js mtime test
[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/;
6eb2623e 6use CSS::SpriteMaker;
7e8f9a5c 7use Digest::SHA qw/sha256_base64/;
3c9bcb47 8use IO::Compress::Gzip qw/gzip/;
7e8f9a5c 9use File::Slurp qw/read_file write_file edit_file_lines/;
5eae1f77 10use List::Util qw/first/;
8798626b
MG
11
12mkdir 'static';
13mkdir 'static/css';
f57a9178 14mkdir 'static/js';
8798626b 15
69d77267
MG
16sub make_sprite {
17 my $maker = Local::CSS::SpriteMaker->new(
18 css_class_prefix => 'logo-',
19 rc_override_classname => sub {
20 my ($name) = @_;
21 $name =~ s/-light/.logo-light/r;
22 }
23 );
6eb2623e 24
69d77267
MG
25 $maker->make_sprite(
26 source_images => ['logos/'],
27 target_file => 'static/logos.png',
28 add_extra_padding => 10,
29 );
6eb2623e 30
69d77267
MG
31 $maker->print_css(
32 filename => 'css/logos.css',
0f623f3d 33 sprite_filename => '/static/logos.png',
69d77267 34 );
6eb2623e 35
0e8866c3
MG
36 system 'pngnq-s9', '-s1', 'static/logos.png';
37 system 'optipng', '-o7', '-zm1-9', 'static/logos-nq8.png';
38 rename 'static/logos-nq8.png', 'static/logos.png';
69d77267 39}
c9dee865 40
3c9bcb47
MG
41sub gzip_file {
42 my ($file) = @_;
43 gzip $file => "$file.gz", -Level => 9, Minimal => 1;
44}
45
5eae1f77
MG
46sub read_css_into_blocks {
47 my ($file) = @_;
48 my (@blocks, $block);
49 for (read_file $file) {
50 $block .= $_;
51 if (/^}/) {
52 push @blocks, $block;
53 $block = '';
54 }
55 }
56 \@blocks
57}
58
cf2b0139 59sub make_css {
5eae1f77
MG
60 my %css;
61 $css{common} .= read_file $_ for <css/*.css>;
62
63 my (%themes, $rndtheme);
cf2b0139 64 for (<css/themes/*>) {
5eae1f77
MG
65 ($rndtheme) = m,themes/(.*)\.css,;
66 $themes{$rndtheme} = read_css_into_blocks $_;
67 }
68
69 while (grep { scalar @$_ } values %themes) {
70 my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
71 if (grep { $_ ne $blocks{$rndtheme} } values %blocks) {
72 $css{$_} .= $blocks{$_} for keys %themes;
73 } else {
74 $css{common} .= $blocks{$rndtheme};
75 }
76 }
77
78 for my $name (keys %css) {
79 write_file "static/css/$name.css", minify $css{$name};
80 gzip_file "static/css/$name.css"
cf2b0139 81 }
8798626b
MG
82}
83
cf2b0139 84sub make_js {
8345760a 85 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
MG
86 my $js = read_file 'static/js/all.js';
87 write_file 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
88 system 'cp', '-rp', 'js', 'static/';
89 gzip_file 'static/js/all.js';
cf2b0139
MG
90}
91
7fd2e6a5
MG
92my $sprite_mtime = -M 'static/logos.png' // 0;
93for (<logos/*>) {
94 if (!$sprite_mtime || $sprite_mtime > -M) {
95 make_sprite;
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
MG
121} 'tmpl/skel.en';
122
6eb2623e
MG
123package
124 Local::CSS::SpriteMaker;
125
126use parent qw/CSS::SpriteMaker/;
127
128sub _get_stylesheet_string {
129 my $self = shift;
130 my @ret = split "\n", $self->SUPER::_get_stylesheet_string(@_);
131 shift @ret;
132 @ret = sort @ret;
133 unshift @ret, <<EOF;
134a.logo {
0f623f3d 135 background-image: url("/static/logos.png");
6eb2623e
MG
136 background-repeat: no-repeat;
137 display: inline-block;
138 vertical-align: middle;
139}
140EOF
141 join "\n", @ret;
142}
This page took 0.041546 seconds and 4 git commands to generate.