]> iEval git - gruntmaster-page.git/blob - make_static.PL
ea060bc001e7984c6a303969ec1d30ac97858e2a
[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 CSS::SpriteMaker;
7 use Digest::SHA qw/sha256_base64/;
8 use IO::Compress::Gzip qw/gzip/;
9 use File::Slurp qw/read_file write_file edit_file_lines/;
10 use List::Util qw/first/;
11
12 mkdir 'static';
13 mkdir 'static/css';
14 mkdir 'static/js';
15
16 sub 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 );
24
25 $maker->make_sprite(
26 source_images => ['logos/'],
27 target_file => 'static/logos.png',
28 add_extra_padding => 10,
29 );
30
31 $maker->print_css(
32 filename => 'css/logos.css',
33 sprite_filename => '/static/logos.png',
34 );
35
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';
39 }
40
41 sub gzip_file {
42 my ($file) = @_;
43 gzip $file => "$file.gz", -Level => 9, Minimal => 1;
44 }
45
46 sub 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
59 sub make_css {
60 my %css;
61 $css{common} .= read_file $_ for <css/*.css>;
62
63 my (%themes, $rndtheme);
64 for (<css/themes/*>) {
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"
81 }
82 }
83
84 sub make_js {
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/*>;
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';
90 }
91
92 my $sprite_mtime = -M 'static/logos.png' // 0;
93 for (<logos/*>) {
94 if (!$sprite_mtime || $sprite_mtime > -M) {
95 make_sprite;
96 last
97 }
98 }
99
100 my $css_mtime = -M 'static/css/slate.css' // 0;
101 for (<css/*>, <css/themes/*>) {
102 if (!$css_mtime || $css_mtime > -M) {
103 make_css;
104 last
105 }
106 }
107
108 my $js_mtime = -M 'static/js/all.js' // 0;
109 for (<js/*>) {
110 if (!$js_mtime || $js_mtime > -M) {
111 make_js;
112 last
113 }
114 }
115
116 edit_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="/;
121 } 'tmpl/skel.en';
122
123 package
124 Local::CSS::SpriteMaker;
125
126 use parent qw/CSS::SpriteMaker/;
127
128 sub _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;
134 a.logo {
135 background-image: url("/static/logos.png");
136 background-repeat: no-repeat;
137 display: inline-block;
138 vertical-align: middle;
139 }
140 EOF
141 join "\n", @ret;
142 }
This page took 0.058335 seconds and 3 git commands to generate.