]>
Commit | Line | Data |
---|---|---|
8798626b MG |
1 | #!/usr/bin/perl |
2 | use v5.14; | |
3 | use warnings; | |
4 | ||
5 | use CSS::Minifier::XS qw//; | |
6eb2623e | 6 | use CSS::SpriteMaker; |
8798626b | 7 | |
7e8f9a5c | 8 | use Digest::SHA qw/sha256_base64/; |
3c9bcb47 | 9 | use IO::Compress::Gzip qw/gzip/; |
7e8f9a5c | 10 | use File::Slurp qw/read_file write_file edit_file_lines/; |
8798626b MG |
11 | |
12 | mkdir 'static'; | |
13 | mkdir 'static/css'; | |
f57a9178 | 14 | mkdir 'static/js'; |
8798626b | 15 | |
69d77267 MG |
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 | ); | |
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 |
41 | sub gzip_file { |
42 | my ($file) = @_; | |
43 | gzip $file => "$file.gz", -Level => 9, Minimal => 1; | |
44 | } | |
45 | ||
cf2b0139 MG |
46 | sub make_css { |
47 | my $common_css; | |
48 | $common_css .= read_file $_ for <css/*.css>; | |
49 | for (<css/themes/*>) { | |
50 | my ($theme) = m,themes/(.*)\.css,; | |
51 | my $css = read_file $_; | |
52 | $css .= $common_css; | |
53 | write_file "static/css/$theme.css", CSS::Minifier::XS::minify $css; | |
3c9bcb47 | 54 | gzip_file "static/css/$theme.css"; |
cf2b0139 | 55 | } |
8798626b MG |
56 | } |
57 | ||
cf2b0139 | 58 | sub make_js { |
8345760a | 59 | 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 |
60 | my $js = read_file 'static/js/all.js'; |
61 | write_file 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js; | |
62 | system 'cp', '-rp', 'js', 'static/'; | |
63 | gzip_file 'static/js/all.js'; | |
cf2b0139 MG |
64 | } |
65 | ||
7fd2e6a5 MG |
66 | my $sprite_mtime = -M 'static/logos.png' // 0; |
67 | for (<logos/*>) { | |
68 | if (!$sprite_mtime || $sprite_mtime > -M) { | |
69 | make_sprite; | |
70 | last | |
71 | } | |
72 | } | |
73 | ||
cf2b0139 MG |
74 | my $css_mtime = -M 'static/css/slate.css' // 0; |
75 | for (<css/*>, <css/themes/*>) { | |
76 | if (!$css_mtime || $css_mtime > -M) { | |
77 | make_css; | |
78 | last | |
79 | } | |
80 | } | |
81 | ||
82 | my $js_mtime = -M 'static/js.js' // 0; | |
83 | for (<js/*>) { | |
84 | if (!$js_mtime || $js_mtime > -M) { | |
85 | make_js; | |
86 | last | |
87 | } | |
9cad7bdd | 88 | } |
69d77267 | 89 | |
7e8f9a5c MG |
90 | edit_file_lines { |
91 | my ($file) = m,(static.*\.(?:css|js)),; | |
92 | return unless $file; | |
93 | my $hash = sha256_base64 scalar read_file $file; | |
94 | s/integrity=".*"/integrity="sha256-$hash="/; | |
65ef5465 MG |
95 | } 'tmpl/skel.en'; |
96 | ||
6eb2623e MG |
97 | package |
98 | Local::CSS::SpriteMaker; | |
99 | ||
100 | use parent qw/CSS::SpriteMaker/; | |
101 | ||
102 | sub _get_stylesheet_string { | |
103 | my $self = shift; | |
104 | my @ret = split "\n", $self->SUPER::_get_stylesheet_string(@_); | |
105 | shift @ret; | |
106 | @ret = sort @ret; | |
107 | unshift @ret, <<EOF; | |
108 | a.logo { | |
0f623f3d | 109 | background-image: url("/static/logos.png"); |
6eb2623e MG |
110 | background-repeat: no-repeat; |
111 | display: inline-block; | |
112 | vertical-align: middle; | |
113 | } | |
114 | EOF | |
115 | join "\n", @ret; | |
116 | } |