-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild
executable file
·129 lines (103 loc) · 3.18 KB
/
build
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
#!/usr/bin/env perl
use strict;
use warnings;
my @args = @ARGV;
require Path::Class::Dir;
my $outdir = Path::Class::Dir->new( 'output' );
my %opts;
for ( 0..@args - 1 ) {
next unless $args[ $_ ] =~ m{^-};
$opts{ delete $args[ $_ ] }++;
}
if( !@args ) {
# clean out any existing build files
$outdir->rmtree;
$outdir->mkpath;
my $src = $outdir->parent->subdir( 'src' );
require Template;
my $tmpl = Template->new;
my %vars;
$vars{ css } = _gather_css( $src );
$vars{ js } = _gather_js( $src );
my $output;
$tmpl->process( $src->file( 'index.html' )->stringify, '', \$output );
if( !$opts{ '--no-minify' } && eval "require HTML::Packer;" ) {
my $packer = HTML::Packer->init();
$packer->minify( \$vars{ css }, {
remove_comments => 1,
remove_newlines => 1,
do_javascript => 'clean',
do_stylesheet => 'minify',
html5 => 1,
} );
$packer->minify( \$vars{ js }, {
remove_comments => 1,
remove_newlines => 1,
do_javascript => 'clean',
do_stylesheet => 'minify',
html5 => 1,
} );
}
my $dir = $outdir->subdir( 'js' );
$dir->mkpath;
my $fh = $dir->file( 'js-textmode-editor.js' )->open( 'w' );
print $fh $vars{ js };
$fh->close;
$dir = $outdir->subdir( 'css' );
$dir->mkpath;
$fh = $dir->file( 'js-textmode-editor.css' )->open( 'w' );
print $fh $vars{ css };
$fh->close;
$fh = $outdir->file( 'index.html' )->open( 'w' );
print $fh $output;
$fh->close;
# favicon
require File::Copy;
File::Copy::copy( $src->file( 'favicon.ico' )->stringify, $outdir );
# oauth receiver
File::Copy::copy( $src->file( 'oauth_receiver.html' )->stringify, $outdir );
# static folders, 1-to-1 copy
require File::Copy::Recursive;
for( qw( img js ) ) {
my $d = $outdir->subdir( $_ );
$d->mkpath;
File::Copy::Recursive::rcopy_glob( $src->subdir( $_ )->stringify . '/*.*', $d );
}
}
elsif( $args[ 0 ] eq 'server' ) {
require Plack::Loader;
require Plack::App::Directory;
my $app = Plack::App::Directory->new( { root => "$outdir" } )->to_app;
print STDERR 'browse to http://localhost:1337/index.html', "\n";
Plack::Loader->load( 'Standalone', port => 1337 )->run( $app );
}
elsif( $args[ 0 ] eq 'zip' ) {
require Cwd;
my $cur = Cwd::cwd();
chdir( 'output' );
$outdir->parent->file( '16c-draw.zip' )->remove;
`zip -9 -r ../16c-draw.zip .`;
chdir( $cur );
}
sub _gather_css {
my $src = shift;
$src = $src->subdir( 'css' );
my $css = '';
while ( my $file = $src->next ) {
next unless -f $file;
my $fh = $file->open('r') or die "Can't read $file: $!";
$css .= do { local $/; <$fh> } . "\n";
}
chop( $css );
return $css;
}
sub _gather_js {
my $src = shift;
$src = $src->subdir( 'coffee' );
require File::Temp;
my $temp;
( undef, $temp ) = File::Temp::tempfile();
my $js = `coffee -p -j $temp $src/*.coffee`;
$js .= `handlebars $src/../handlebars/*.handlebars`;
return $js;
}