Skip to content

Commit 41d2e24

Browse files
committed
Merge pull request #9 from sass/develop
Implement watchdog option
2 parents 6088074 + df90067 commit 41d2e24

File tree

4 files changed

+460
-43
lines changed

4 files changed

+460
-43
lines changed

Build.PL

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ my %config = (
173173
create_license => 0,
174174
configure_requires => {
175175
'Module::Build' => 0,
176+
'File::chdir' => 0,
177+
'File::Slurp' => 0,
176178
},
177179
build_requires => {
178180
'Test::More' => 0,
@@ -184,7 +186,9 @@ my %config = (
184186
},
185187
requires => {
186188
'perl' => '5.008',
189+
'File::chdir' => 0,
187190
'File::Slurp' => 0,
191+
'List::MoreUtils' => 0,
188192
},
189193
meta_merge => {
190194
resources => {

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ Command line utility
5252
--------------------
5353

5454
```
55-
psass [options] [ source | - ]
55+
psass [options] [ path_in | - ] [ path_out | - ]
5656
```
5757

5858
```
5959
-v, --version print version
6060
-h, --help print this help
61+
-w, --watch start watchdog mode
6162
-p, --precision precision for float output
6263
-o, --output-file=file output file to write result to
6364
-t, --output-style=style output style [nested|compressed]
@@ -73,7 +74,7 @@ psass [options] [ source | - ]
7374
Copyright And Licence
7475
---------------------
7576

76-
Copyright © 2013 by David Caldwell
77+
Copyright © 2013 by David Caldwell
7778
Copyright © 2014 by Marcel Greter
7879

7980
This library is released under the MIT license.

bin/psass.pl

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/perl
12
####################################################################################################
23
# sass (scss) compiler
34
####################################################################################################
@@ -19,6 +20,7 @@
1920
# load constants from libsass
2021
use CSS::Sass qw(SASS_STYLE_NESTED);
2122
use CSS::Sass qw(SASS_STYLE_COMPRESSED);
23+
use CSS::Sass::Watchdog qw(start_watchdog);
2224

2325
####################################################################################################
2426
# normalize command arguments to utf8
@@ -36,6 +38,7 @@
3638
####################################################################################################
3739

3840
# init options
41+
my $watchdog;
3942
my $precision;
4043
my $output_file;
4144
my $output_style;
@@ -48,7 +51,7 @@
4851
# define a sub to print out the version (mimic behaviour of node.js blessc)
4952
# this script has it's own version numbering as it's not dependent on any libs
5053
sub version {
51-
printf "psass %s (perl sass/scss compiler)\n", "0.3.0";
54+
printf "psass %s (perl sass/scss compiler)\n", "0.4.0";
5255
printf " libsass: %s\n", CSS::Sass::libsass_version();
5356
printf " sass2scss: %s\n", CSS::Sass::sass2scss_version();
5457
exit 0 };
@@ -60,6 +63,7 @@ sub version {
6063
# get options
6164
GetOptions (
6265
'help|h' => sub { pod2usage(1); },
66+
'watch|w' => \ $watchdog,
6367
'version|v' => \ &version,
6468
'precision|p=s' => \ $precision,
6569
'output-file|o=s' => \ $output_file,
@@ -85,36 +89,19 @@ sub version {
8589
# die with message if style is unknown
8690
else { die "unknown output style: $output_style" }
8791

92+
# do we have output path in second arg?
93+
if (defined $ARGV[1] && $ARGV[1] ne '-')
94+
{ $output_file = $ARGV[1]; }
95+
8896

8997
####################################################################################################
90-
use CSS::Sass qw(sass_compile_file sass_compile);
98+
# get sass standard option list
9199
####################################################################################################
92100

93-
# variables
94-
my ($css, $err, $stats);
95-
96-
# open filehandle if path is given
97-
if (defined $ARGV[0] && $ARGV[0] ne '-')
98-
{
99-
($css, $err, $stats) = sass_compile_file(
100-
$ARGV[0],
101-
precision => $precision,
102-
output_path => $output_file,
103-
output_style => $output_style,
104-
plugin_paths => \ @plugin_paths,
105-
include_paths => \ @include_paths,
106-
source_comments => $source_comments,
107-
source_map_file => $source_map_file,
108-
source_map_embed => $source_map_embed,
109-
source_map_contents => $source_map_contents,
110-
omit_source_map_url => $omit_source_map_url
111-
);
112-
}
113-
# or use standard input
114-
else
101+
sub sass_options ()
115102
{
116-
($css, $err, $stats) = sass_compile(
117-
join('', <STDIN>),
103+
return (
104+
dont_die => $watchdog,
118105
precision => $precision,
119106
output_path => $output_file,
120107
output_style => $output_style,
@@ -124,27 +111,71 @@ sub version {
124111
source_map_file => $source_map_file,
125112
source_map_embed => $source_map_embed,
126113
source_map_contents => $source_map_contents,
127-
omit_source_map_url => $omit_source_map_url
114+
omit_source_map_url => $omit_source_map_url,
128115
);
129116
}
130117

131-
# process return status values
132-
if (defined $css)
118+
####################################################################################################
119+
use CSS::Sass qw(sass_compile_file sass_compile);
120+
####################################################################################################
121+
122+
# first run we always want to die on error
123+
# because we will not get any included files
124+
our $error = sub { die @_ };
125+
126+
sub compile ()
133127
{
134-
# by default we just print to standard out
135-
unless (defined $output_file) { print $css; }
136-
# or if output_file is defined via options we write it there
137-
else { write_file($output_file, { binmode => ':utf8' }, $css ); }
128+
# variables
129+
my ($css, $err, $stats);
130+
131+
# open filehandle if path is given
132+
if (defined $ARGV[0] && $ARGV[0] ne '-')
133+
{
134+
($css, $err, $stats) = sass_compile_file(
135+
$ARGV[0], sass_options()
136+
);
137+
}
138+
# or use standard input
139+
else
140+
{
141+
($css, $err, $stats) = sass_compile(
142+
join('', <STDIN>), sass_options()
143+
);
144+
}
145+
146+
# process return status values
147+
if (defined $css)
148+
{
149+
# by default we just print to standard out
150+
unless (defined $output_file) { print $css; }
151+
# or if output_file is defined via options we write it there
152+
else { write_file($output_file, { binmode => ':utf8' }, $css ); }
153+
}
154+
elsif (defined $err) { $error->($err); }
155+
else { $error->("fatal error - aborting"); }
156+
157+
# output source-map
158+
if ($source_map_file)
159+
{
160+
my $smap = $stats->{'source_map_string'};
161+
unless ($smap) { $error->("source-map not generated <$source_map_file>") }
162+
else { write_file($source_map_file, { binmode => ':utf8' }, $smap ); }
163+
}
164+
165+
# return according to expected return type
166+
return wantarray ? ($css, $err, $stats) : $css;
138167
}
139-
elsif (defined $err) { die $err; }
140-
else { die "fatal error - aborting"; }
141168

142-
# output source-map
143-
if ($source_map_file)
169+
####################################################################################################
170+
# main program execution
171+
####################################################################################################
172+
173+
my ($css, $err, $stats) = compile();
174+
175+
if ($watchdog)
144176
{
145-
my $smap = $stats->{'source_map_string'};
146-
unless ($smap) { warn "source-map not generated <$source_map_file>" }
147-
else { write_file($source_map_file, { binmode => ':utf8' }, $smap ); }
177+
local $error = sub { warn @_ };
178+
start_watchdog($stats, \&compile);
148179
}
149180

150181
####################################################################################################
@@ -158,11 +189,12 @@ =head1 NAME
158189
159190
=head1 SYNOPSIS
160191
161-
psass [options] [ source | - ]
192+
psass [options] [ path_in | - ] [ path_out | - ]
162193
163194
Options:
164195
-v, --version print version
165196
-h, --help print this help
197+
-w, --watch start watchdog mode
166198
-p, --precision precision for float output
167199
-o, --output-file=file output file to write result to
168200
-t, --output-style=style output style [nested|compressed]

0 commit comments

Comments
 (0)