]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | use v5.14; | |
3 | use warnings; | |
4 | ||
5 | use CSS::Minifier::XS qw//; | |
6 | use CSS::SpriteMaker; | |
7 | use JavaScript::Minifier::XS qw//; | |
8 | ||
9 | use IO::Compress::Gzip qw/gzip/; | |
10 | use File::Slurp qw/read_file write_file/; | |
11 | ||
12 | mkdir 'static'; | |
13 | mkdir 'static/css'; | |
14 | ||
15 | sub make_sprite { | |
16 | my $maker = Local::CSS::SpriteMaker->new( | |
17 | css_class_prefix => 'logo-', | |
18 | rc_override_classname => sub { | |
19 | my ($name) = @_; | |
20 | $name =~ s/-light/.logo-light/r; | |
21 | } | |
22 | ); | |
23 | ||
24 | $maker->make_sprite( | |
25 | source_images => ['logos/'], | |
26 | target_file => 'static/logos.png', | |
27 | add_extra_padding => 10, | |
28 | ); | |
29 | ||
30 | $maker->print_css( | |
31 | filename => 'css/logos.css', | |
32 | sprite_filename => '/static/logos.png', | |
33 | ); | |
34 | ||
35 | system 'pngnq-s9', '-s1', 'static/logos.png'; | |
36 | system 'optipng', '-o7', '-zm1-9', 'static/logos-nq8.png'; | |
37 | rename 'static/logos-nq8.png', 'static/logos.png'; | |
38 | } | |
39 | ||
40 | sub gzip_file { | |
41 | my ($file) = @_; | |
42 | gzip $file => "$file.gz", -Level => 9, Minimal => 1; | |
43 | } | |
44 | ||
45 | sub make_css { | |
46 | my $common_css; | |
47 | $common_css .= read_file $_ for <css/*.css>; | |
48 | for (<css/themes/*>) { | |
49 | my ($theme) = m,themes/(.*)\.css,; | |
50 | my $css = read_file $_; | |
51 | $css .= $common_css; | |
52 | write_file "static/css/$theme.css", CSS::Minifier::XS::minify $css; | |
53 | gzip_file "static/css/$theme.css"; | |
54 | } | |
55 | } | |
56 | ||
57 | sub make_js { | |
58 | if (-f 'compiler.jar') { | |
59 | system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js.map --js_output_file static/js.js --language_in ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, <js/*>; | |
60 | my $js = read_file 'static/js.js'; | |
61 | write_file 'static/js.js', '//# sourceMappingURL=/static/js.map', "\n", $js; | |
62 | system 'cp', '-rp', 'js', 'static/'; | |
63 | } else { | |
64 | my $js; | |
65 | $js .= read_file $_ for <js/*.js>; | |
66 | write_file 'static/js.js', JavaScript::Minifier::XS::minify $js; | |
67 | } | |
68 | gzip_file 'static/js.js'; | |
69 | } | |
70 | ||
71 | my $sprite_mtime = -M 'static/logos.png' // 0; | |
72 | for (<logos/*>) { | |
73 | if (!$sprite_mtime || $sprite_mtime > -M) { | |
74 | make_sprite; | |
75 | last | |
76 | } | |
77 | } | |
78 | ||
79 | my $css_mtime = -M 'static/css/slate.css' // 0; | |
80 | for (<css/*>, <css/themes/*>) { | |
81 | if (!$css_mtime || $css_mtime > -M) { | |
82 | make_css; | |
83 | last | |
84 | } | |
85 | } | |
86 | ||
87 | my $js_mtime = -M 'static/js.js' // 0; | |
88 | for (<js/*>) { | |
89 | if (!$js_mtime || $js_mtime > -M) { | |
90 | make_js; | |
91 | last | |
92 | } | |
93 | } | |
94 | ||
95 | package | |
96 | Local::CSS::SpriteMaker; | |
97 | ||
98 | use parent qw/CSS::SpriteMaker/; | |
99 | ||
100 | sub _get_stylesheet_string { | |
101 | my $self = shift; | |
102 | my @ret = split "\n", $self->SUPER::_get_stylesheet_string(@_); | |
103 | shift @ret; | |
104 | @ret = sort @ret; | |
105 | unshift @ret, <<EOF; | |
106 | a.logo { | |
107 | background-image: url("/static/logos.png"); | |
108 | background-repeat: no-repeat; | |
109 | display: inline-block; | |
110 | vertical-align: middle; | |
111 | } | |
112 | EOF | |
113 | join "\n", @ret; | |
114 | } |