This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdev-make
More file actions
executable file
·349 lines (294 loc) · 10.3 KB
/
dev-make
File metadata and controls
executable file
·349 lines (294 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/perl
use local::lib;
use v5.10;
use strict;
use warnings;
use FindBin qw( $RealBin );
use lib "$RealBin/lib";
use File::Basename qw( basename );
use File::Copy qw( copy );
use File::Find qw( find );
use List::Util qw( any );
use LogBot::Util qw( file_time run slurp spurt touch );
use Mojo::Util qw( getopt );
$| = 1;
my %args;
getopt(\%args, 'rebuild|B|b', 'quiet|q', 'debug|d', 'help|h|?');
$args{debug} ||= $ENV{DEBUG};
$args{deps} = shift // '' eq 'deps';
$args{help} && die <<'EOF';
dev-make [options] ['deps']
build web assets for logbot.
options:
--rebuild|-B|-b : force rebuild
--quiet|-q : no output
--debug|-d : build debug assets (non-compressed)
'deps' : show build dependencies
EOF
#<<<
my $def = {
'web/public/static/logbot.min.js' => [
{ 'web/build/jquery.min.js' => { js => 'web/jquery/jquery-3.2.1.min.js' } },
{ 'web/build/pikaday.min.js' => { js => 'web/pikaday/pikaday.js' } },
{ 'web/build/chosen.min.js' => { js => 'web/chosen/chosen.jquery.js' } },
{ 'web/build/flot.min.js' => { js => 'web/flot/jquery.flot.js' } },
{ 'web/build/css-esc.min.js' => { js => 'web/css-escape/css.escape.js' } },
{ 'web/build/logbot.min.js' => { js => 'web/logbot.js' } },
],
'web/public/static/logbot.min.css' => [
{ 'web/build/pikaday.min.css' => { css => 'web/pikaday/pikaday.scss' } },
{ 'web/build/hind.min.css' => { css => 'web/hind/hind.sass' } },
{ 'web/build/chosen.min.css' => { css => 'web/chosen/chosen.css' } },
{ 'web/build/logbot.min.css' => { lb_css => [qw( web/logbot.sass web/_variables.sass )] } },
],
_svg => [
qw(
web/svg/*.svg
web/svg/font-awesome/*
web/templates/*.html.ep
web/templates/layouts/*.html.ep
web/templates/shared/*.html.ep
)
],
_cp => [
qw(
web/chosen/chosen-sprite.png
web/chosen/chosen-sprite@2x.png
web/hind/hind-bold.ttf
web/hind/hind-medium.ttf
web/hind/hind-regular.ttf
web/svg/logbot-favicon.svg
)
],
};
#>>>
mkdir("$RealBin/web/build");
# switching debug mode on/off forces a rebuild
my $last_mode_file = "$RealBin/web/build/last_mode";
my $last_mode = -e $last_mode_file ? slurp($last_mode_file) : '';
my $this_mode = $args{debug} ? 'debug' : 'production';
if ($this_mode ne $last_mode) {
$args{rebuild} = 1;
l(undef, "$this_mode assets");
}
my @deps;
foreach my $target (sort keys %{$def}) {
if ($target eq '_cp') {
# copy file
foreach my $source_file (@{ $def->{_cp} }) {
$source_file = "$RealBin/$source_file";
(push(@deps, $source_file), next) if $args{deps};
cp($source_file, "$RealBin/web/public/static/" . basename($source_file));
}
} elsif ($target eq '_svg') {
# if any svg or templates are changed, inline svg
my $target_file = "$RealBin/web/build/inline-svg.updated";
foreach my $source_file (expand($def->{_svg})) {
(push(@deps, $source_file), next) if $args{deps};
next unless update($source_file, $target_file);
inline_svg();
touch($target_file);
last;
}
} else {
# concatenated targets
my $target_file = "$RealBin/$target";
my @content;
my $dirty = 0;
foreach my $sub (@{ $def->{$target} }) {
my $sub_target = (keys %{$sub})[0];
my $sub_target_file = "$RealBin/$sub_target";
my $action = (keys %{ $sub->{$sub_target} })[0];
my $action_source = $sub->{$sub_target}->{$action};
if ($action eq 'css') {
my $sub_source_file = "$RealBin/$action_source";
(push(@deps, $sub_source_file), next) if $args{deps};
css($sub_source_file, $sub_target_file) && ($dirty = 1);
} elsif ($action eq 'lb_css') {
foreach my $sub_source_file (expand($action_source)) {
(push(@deps, $sub_source_file), next) if $args{deps};
next unless update($sub_source_file, $sub_target_file);
logbot_css("$RealBin/web/build/logbot.full.css");
css("$RealBin/web/build/logbot.full.css", $sub_target_file);
$dirty = 1;
last;
}
} elsif ($action eq 'js') {
my $sub_source_file = "$RealBin/$action_source";
(push(@deps, $sub_source_file), next) if $args{deps};
js($sub_source_file, $sub_target_file) && ($dirty = 1);
} else {
die $action;
}
push @content, slurp($sub_target_file) unless $args{deps};
}
if ($dirty && !$args{deps}) {
l(undef, $target_file);
spurt($target_file, join('', @content));
}
}
}
(say(join("\n", @deps)), exit) if $args{deps};
spurt($last_mode_file, $this_mode);
sub cp {
my ($source_file, $target_file) = @_;
return unless update($source_file, $target_file);
l($source_file, $target_file);
copy($source_file, $target_file) || die "$!\n";
return 1;
}
sub css {
my ($source_file, $target_file) = @_;
return unless update($source_file, $target_file);
l($source_file, $target_file);
if ($args{debug}) {
run('sass', '--style', 'expanded', $source_file, $target_file);
} else {
run('sass', '--style', 'compressed', $source_file, $target_file);
}
my $css = slurp($target_file);
$css =~ s{/\*.*?\*/}{}gs;
spurt($target_file, $css);
return 1;
}
sub js {
my ($source_file, $target_file) = @_;
return unless update($source_file, $target_file);
l($source_file, $target_file);
if ($args{debug}) {
run('uglifyjs', $source_file, '--beautify', '--output', $target_file);
} else {
run('uglifyjs', $source_file, '--compress', '--mangle', '--output', $target_file);
}
return 1;
}
sub inline_svg {
# inlines svg images into templates
# to use, create an svg element with a class name "svg-filename"
# the filename will be loaded from web/svg and replace the svg element
# only the svg-filename class will be carried over into the inline svg element,
# other classes and ids will be lost
# eg. <svg class="svg-sidebar-collapse"></svg>
my @files;
find(
sub {
my $file = $File::Find::name;
return unless -f $file && -s $file;
return unless $file =~ /\.html\.ep$/;
push @files, $file;
},
"$RealBin/web/templates"
);
foreach my $file (sort @files) {
my $orig = slurp($file);
(my $tmpl = $orig) =~ s{
<svg\sclass="svg-([^"]+)".+?</svg>
}{
load_svg($1)
}gxe;
next if $orig eq $tmpl;
spurt($file, $tmpl);
$file =~ s/^\Q$RealBin//;
l(undef, $file);
}
}
sub logbot_css {
my ($target_file) = @_;
# build css that contains light and dark modes
# the combined sass looks like:
# variables
# body
# logbot.sass unmodified
# body.dark
# logbot.sass with colour variables prefixed by `night-`
#
# to avoid duplicate styles in the body.dark section, non-colour styles are
# then commented out. the sass processor will remove these comments from
# the final minified css.
my @variables = split(/\n/, slurp("$RealBin/web/_variables.sass"));
my @sass = split(/\n/, slurp("$RealBin/web/logbot.sass"));
# remove @import from main sass as we'll be inlining here
@sass = grep { !/^\@import\s+variables/ } @sass;
# indent sass
my @top_level;
my $is_top_level = 0;
foreach my $line (@sass) {
# don't touch top-level :: selectors
$is_top_level = 1 if $line =~ /^::/;
if ($is_top_level) {
$is_top_level = 0 if $line eq '';
push @top_level, $line;
$line = '';
next;
}
# existing body definitions need special treatment
if ($line eq 'body') {
$line = '&.root';
}
$line = ' ' . $line;
}
# build duplicate styles, light and dark
my @combined;
push @combined, @variables;
push @combined, '';
push @combined, @top_level;
push @combined, 'body';
push @combined, @sass;
push @combined, 'body.dark';
foreach my $line (@sass) {
$line =~ s/\$(.+?-colour)/\$night-$1/g;
push @combined, $line;
}
# convert to css (easier to parse)
open(my $sass_cmd, '|-', 'sass', '--stdin', '--indented', 'web/build/logbot.combined.css') or die $!;
print $sass_cmd join("\n", @combined);
close($sass_cmd) or die $!;
# commend out body.dark selectors that don't involve colour
my @css = split(/\n/, slurp("$RealBin/web/build/logbot.combined.css"));
my $selector = '';
foreach my $line (@css) {
next if $line =~ /^\s*}/;
if ($line =~ /{$/) {
($selector = $line) =~ s/^\s+//;
} else {
next unless $selector =~ /^body\.dark\b/;
next
if $line =~ /#[a-z0-9]{3,6}/
|| $line =~ /\brgba?\(/
|| $line =~ /^\s*filter:/;
$line = "/* $line */";
}
}
spurt($target_file, join("\n", @css));
}
sub update {
my ($source_file, $target_file) = @_;
return 1 if $args{rebuild};
return 1 if !-e $target_file;
return file_time($source_file) > file_time($target_file);
}
sub expand {
my ($ra_spec) = @_;
my @files;
foreach my $spec (@{$ra_spec}) {
push @files, glob("'$RealBin/$spec'");
}
return @files;
}
sub l {
my ($source_file, $target_file) = @_;
return if $args{quiet};
$source_file =~ s{^\Q$RealBin/}{} if $source_file;
$target_file =~ s{^\Q$RealBin/}{};
say $source_file ? "$source_file → $target_file" : "→ $target_file";
}
sub load_svg {
my ($name) = @_;
my $file =
-e "$RealBin/web/svg/$name.svg" ? "$RealBin/web/svg/$name.svg" : "$RealBin/web/svg/font-awesome/$name.svg";
die "failed to find $name.svg\n" unless -e $file;
my $svg = slurp($file);
$svg =~ s/^<svg /<svg class="svg-$name" /;
$svg =~ s/\s+$//;
return $svg;
}