1
+ # !/usr/bin/perl
1
2
# ###################################################################################################
2
3
# sass (scss) compiler
3
4
# ###################################################################################################
19
20
# load constants from libsass
20
21
use CSS::Sass qw( SASS_STYLE_NESTED) ;
21
22
use CSS::Sass qw( SASS_STYLE_COMPRESSED) ;
23
+ use CSS::Sass::Watchdog qw( start_watchdog) ;
22
24
23
25
# ###################################################################################################
24
26
# normalize command arguments to utf8
36
38
# ###################################################################################################
37
39
38
40
# init options
41
+ my $watchdog ;
39
42
my $precision ;
40
43
my $output_file ;
41
44
my $output_style ;
48
51
# define a sub to print out the version (mimic behaviour of node.js blessc)
49
52
# this script has it's own version numbering as it's not dependent on any libs
50
53
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" ;
52
55
printf " libsass: %s \n " , CSS::Sass::libsass_version();
53
56
printf " sass2scss: %s \n " , CSS::Sass::sass2scss_version();
54
57
exit 0 };
@@ -60,6 +63,7 @@ sub version {
60
63
# get options
61
64
GetOptions (
62
65
' help|h' => sub { pod2usage(1); },
66
+ ' watch|w' => \ $watchdog ,
63
67
' version|v' => \ &version,
64
68
' precision|p=s' => \ $precision ,
65
69
' output-file|o=s' => \ $output_file ,
@@ -85,36 +89,19 @@ sub version {
85
89
# die with message if style is unknown
86
90
else { die " unknown output style: $output_style " }
87
91
92
+ # do we have output path in second arg?
93
+ if (defined $ARGV [1] && $ARGV [1] ne ' -' )
94
+ { $output_file = $ARGV [1]; }
95
+
88
96
89
97
# ###################################################################################################
90
- use CSS::Sass qw( sass_compile_file sass_compile ) ;
98
+ # get sass standard option list
91
99
# ###################################################################################################
92
100
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 ()
115
102
{
116
- ( $css , $err , $stats ) = sass_compile (
117
- join ( ' ' , < STDIN >) ,
103
+ return (
104
+ dont_die => $watchdog ,
118
105
precision => $precision ,
119
106
output_path => $output_file ,
120
107
output_style => $output_style ,
@@ -124,27 +111,71 @@ sub version {
124
111
source_map_file => $source_map_file ,
125
112
source_map_embed => $source_map_embed ,
126
113
source_map_contents => $source_map_contents ,
127
- omit_source_map_url => $omit_source_map_url
114
+ omit_source_map_url => $omit_source_map_url ,
128
115
);
129
116
}
130
117
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 ()
133
127
{
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 ;
138
167
}
139
- elsif (defined $err ) { die $err ; }
140
- else { die " fatal error - aborting" ; }
141
168
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 )
144
176
{
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);
148
179
}
149
180
150
181
# ###################################################################################################
@@ -158,11 +189,12 @@ =head1 NAME
158
189
159
190
=head1 SYNOPSIS
160
191
161
- psass [options] [ source | - ]
192
+ psass [options] [ path_in | - ] [ path_out | - ]
162
193
163
194
Options:
164
195
-v, --version print version
165
196
-h, --help print this help
197
+ -w, --watch start watchdog mode
166
198
-p, --precision precision for float output
167
199
-o, --output-file=file output file to write result to
168
200
-t, --output-style=style output style [nested|compressed]
0 commit comments