Skip to content

Commit

Permalink
Merge pull request #9 from sass/develop
Browse files Browse the repository at this point in the history
Implement watchdog option
  • Loading branch information
mgreter committed Mar 14, 2015
2 parents 6088074 + df90067 commit 41d2e24
Show file tree
Hide file tree
Showing 4 changed files with 460 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ my %config = (
create_license => 0,
configure_requires => {
'Module::Build' => 0,
'File::chdir' => 0,
'File::Slurp' => 0,
},
build_requires => {
'Test::More' => 0,
Expand All @@ -184,7 +186,9 @@ my %config = (
},
requires => {
'perl' => '5.008',
'File::chdir' => 0,
'File::Slurp' => 0,
'List::MoreUtils' => 0,
},
meta_merge => {
resources => {
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ Command line utility
--------------------

```
psass [options] [ source | - ]
psass [options] [ path_in | - ] [ path_out | - ]
```

```
-v, --version print version
-h, --help print this help
-w, --watch start watchdog mode
-p, --precision precision for float output
-o, --output-file=file output file to write result to
-t, --output-style=style output style [nested|compressed]
Expand All @@ -73,7 +74,7 @@ psass [options] [ source | - ]
Copyright And Licence
---------------------

Copyright © 2013 by David Caldwell
Copyright © 2013 by David Caldwell
Copyright © 2014 by Marcel Greter

This library is released under the MIT license.
114 changes: 73 additions & 41 deletions bin/psass.pl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/perl
####################################################################################################
# sass (scss) compiler
####################################################################################################
Expand All @@ -19,6 +20,7 @@
# load constants from libsass
use CSS::Sass qw(SASS_STYLE_NESTED);
use CSS::Sass qw(SASS_STYLE_COMPRESSED);
use CSS::Sass::Watchdog qw(start_watchdog);

####################################################################################################
# normalize command arguments to utf8
Expand All @@ -36,6 +38,7 @@
####################################################################################################

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

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


####################################################################################################
use CSS::Sass qw(sass_compile_file sass_compile);
# get sass standard option list
####################################################################################################

# variables
my ($css, $err, $stats);

# open filehandle if path is given
if (defined $ARGV[0] && $ARGV[0] ne '-')
{
($css, $err, $stats) = sass_compile_file(
$ARGV[0],
precision => $precision,
output_path => $output_file,
output_style => $output_style,
plugin_paths => \ @plugin_paths,
include_paths => \ @include_paths,
source_comments => $source_comments,
source_map_file => $source_map_file,
source_map_embed => $source_map_embed,
source_map_contents => $source_map_contents,
omit_source_map_url => $omit_source_map_url
);
}
# or use standard input
else
sub sass_options ()
{
($css, $err, $stats) = sass_compile(
join('', <STDIN>),
return (
dont_die => $watchdog,
precision => $precision,
output_path => $output_file,
output_style => $output_style,
Expand All @@ -124,27 +111,71 @@ sub version {
source_map_file => $source_map_file,
source_map_embed => $source_map_embed,
source_map_contents => $source_map_contents,
omit_source_map_url => $omit_source_map_url
omit_source_map_url => $omit_source_map_url,
);
}

# process return status values
if (defined $css)
####################################################################################################
use CSS::Sass qw(sass_compile_file sass_compile);
####################################################################################################

# first run we always want to die on error
# because we will not get any included files
our $error = sub { die @_ };

sub compile ()
{
# by default we just print to standard out
unless (defined $output_file) { print $css; }
# or if output_file is defined via options we write it there
else { write_file($output_file, { binmode => ':utf8' }, $css ); }
# variables
my ($css, $err, $stats);

# open filehandle if path is given
if (defined $ARGV[0] && $ARGV[0] ne '-')
{
($css, $err, $stats) = sass_compile_file(
$ARGV[0], sass_options()
);
}
# or use standard input
else
{
($css, $err, $stats) = sass_compile(
join('', <STDIN>), sass_options()
);
}

# process return status values
if (defined $css)
{
# by default we just print to standard out
unless (defined $output_file) { print $css; }
# or if output_file is defined via options we write it there
else { write_file($output_file, { binmode => ':utf8' }, $css ); }
}
elsif (defined $err) { $error->($err); }
else { $error->("fatal error - aborting"); }

# output source-map
if ($source_map_file)
{
my $smap = $stats->{'source_map_string'};
unless ($smap) { $error->("source-map not generated <$source_map_file>") }
else { write_file($source_map_file, { binmode => ':utf8' }, $smap ); }
}

# return according to expected return type
return wantarray ? ($css, $err, $stats) : $css;
}
elsif (defined $err) { die $err; }
else { die "fatal error - aborting"; }

# output source-map
if ($source_map_file)
####################################################################################################
# main program execution
####################################################################################################

my ($css, $err, $stats) = compile();

if ($watchdog)
{
my $smap = $stats->{'source_map_string'};
unless ($smap) { warn "source-map not generated <$source_map_file>" }
else { write_file($source_map_file, { binmode => ':utf8' }, $smap ); }
local $error = sub { warn @_ };
start_watchdog($stats, \&compile);
}

####################################################################################################
Expand All @@ -158,11 +189,12 @@ =head1 NAME
=head1 SYNOPSIS
psass [options] [ source | - ]
psass [options] [ path_in | - ] [ path_out | - ]
Options:
-v, --version print version
-h, --help print this help
-w, --watch start watchdog mode
-p, --precision precision for float output
-o, --output-file=file output file to write result to
-t, --output-style=style output style [nested|compressed]
Expand Down
Loading

0 comments on commit 41d2e24

Please # to comment.