-
Notifications
You must be signed in to change notification settings - Fork 1
/
SassTask.php
1040 lines (945 loc) · 24.2 KB
/
SassTask.php
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*
* @category Tasks
* @package phing.tasks.ext
* @author Paul Stuart <[email protected]>
* @author Ken Guest <[email protected]>
* @license LGPL (see http://www.gnu.org/licenses/lgpl.html)
*/
namespace Phing\Task\Ext\Sass;
use Exception;
use Phing\Exception\BuildException;
use Phing\Io\FileSystem;
use Phing\Project;
use Phing\Task;
use Phing\Type\FileSet;
use Phing\Util\StringHelper;
/**
* Executes Sass for a particular fileset.
*
* If the sass executable is not available, but scssphp is, then use that instead.
*
* @category Tasks
* @package phing.tasks.ext
* @author Paul Stuart <[email protected]>
* @author Ken Guest <[email protected]>
* @license LGPL (see http://www.gnu.org/licenses/lgpl.html)
* @link SassTask.php
*/
class SassTask extends Task
{
/**
* Style to generate to.
*
* @var string
*/
protected $style = 'nested';
/**
* Stack trace on error.
*
* @var bool
*/
protected $trace = false;
/**
* Unix-style newlines?
*
* @var bool
*/
protected $unixnewlines = true;
/**
* Encoding
*
* @var string
*/
protected $encoding = 'utf-8';
/**
* SASS import path.
*
* @var string
*/
protected $loadPath = '';
/**
* Whether to just check syntax
*
* @var bool
*/
protected $check = false;
/**
* Whether to use the sass command line tool.
*
* @var bool
*/
protected $useSass = true;
/**
* Whether to use the scssphp compiler, if available.
*
* @var bool
*/
protected $useScssphp = true;
/**
* Input filename if only processing one file is required.
*
* @var string|null
*/
protected $file = null;
/**
* Output filename
*
* @var string|null
*/
protected $output = null;
/**
* Contains the path info of our file to allow us to parse.
*
* @var array
*/
protected $pathInfo = null;
/**
* The Sass executable.
*
* @var string
*/
protected $executable = 'sass';
/**
* The ext type we are looking for when Verifyext is set to true.
*
* More than likely should be "scss" or "sass".
*
* @var string
*/
protected $extfilter = '';
/**
* This flag means 'note errors to the output, but keep going'
*
* @var bool
*/
protected $failonerror = true;
/**
* The fileset we will be running Sass on.
*
* @var array
*/
protected $filesets = [];
/**
* Additional flags to pass to sass.
*
* @var string
*/
protected $flags = '';
/**
* Indicates if we want to keep the directory structure of the files.
*
* @var bool
*/
protected $keepsubdirectories = true;
/**
* When true we will remove the current file ext.
*
* @var bool
*/
protected $removeoldext = true;
/**
* The new ext our files will have.
*
* @var string
*/
protected $newext = 'css';
/**
* The path to send our output files to.
*
* If not defined they will be created in the same directory the
* input is from.
*
* @var string
*/
protected $outputpath = '';
/**
* @var bool
*/
protected $force;
/**
* @var bool
*/
protected $lineNumbers = false;
/**
* @var bool
*/
protected $noCache;
/**
* Set input file (For example style.scss)
*
* Synonym for @see setFile
*
* @param string $file Filename
*
* @return void
*/
public function setInput($file)
{
$this->setFile($file);
}
/**
* Set name of output file.
*
* @param string $file Filename of [css] to output.
*
* @return void
*/
public function setOutput($file)
{
$this->output = $file;
}
/**
* Sets the failonerror flag. Default: true
*
* @param string $failonerror Jenkins style boolean value
*
* @access public
* @return void
*/
public function setFailonerror($failonerror)
{
$this->failonerror = StringHelper::booleanValue($failonerror);
}
/**
* Sets the executable to use for sass. Default: sass
*
* The default assumes sass is in your path. If not you can provide the full
* path to sass.
*
* @param string $executable Name of executable, optionally including full path
*
* @return void
*/
public function setExecutable(string $executable): void
{
$this->executable = $executable;
}
/**
* Return name/path of sass executable.
*/
public function getExecutable(): string
{
return $this->executable;
}
/**
* Sets the extfilter. Default: <none>
*
* This will filter the fileset to only process files that match
* this extension. This could also be done with the fileset.
*
* @param string $extfilter Extension to filter for.
*
* @access public
* @return void
*/
public function setExtfilter($extfilter)
{
$this->extfilter = trim($extfilter, ' .');
}
/**
* Return extfilter setting.
*
* @return string
*/
public function getExtfilter()
{
return $this->extfilter;
}
/**
* Additional flags to pass to sass.
*
* Command will be:
* sass {$flags} {$inputfile} {$outputfile}
*
* @param string $flags Flags to pass
*
* @return void
*/
public function setFlags(string $flags): void
{
$this->flags = trim($flags);
}
/**
* Return flags to be used when running the sass executable.
*/
public function getFlags(): string
{
return trim($this->flags);
}
/**
* Sets the removeoldext flag. Default: true
*
* This will cause us to strip the existing extension off the output
* file.
*
* @param string $removeoldext Jenkins style boolean value
*
* @access public
* @return void
*/
public function setRemoveoldext($removeoldext)
{
$this->removeoldext = StringHelper::booleanValue($removeoldext);
}
/**
* Return removeoldext value (true/false)
*
* @return bool
*/
public function getRemoveoldext()
{
return $this->removeoldext;
}
/**
* Set default encoding
*
* @param string $encoding Default encoding to use.
*
* @return void
*/
public function setEncoding($encoding)
{
$encoding = trim($encoding);
if ($encoding !== '') {
$this->flags .= " --default-encoding $encoding";
} else {
$this->flags = str_replace(
' --default-encoding ' . $this->encoding,
'',
$this->flags
);
}
$this->encoding = $encoding;
}
/**
* Return the output encoding.
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Sets the newext value. Default: css
*
* This is the extension we will add on to the output file regardless
* of if we remove the old one or not.
*
* @param string $newext New extension to use, e.g. css
*
* @access public
* @return void
*/
public function setNewext($newext)
{
$this->newext = trim($newext, ' .');
}
/**
* Return extension added to output files.
*
* @return string
*/
public function getNewext()
{
return $this->newext;
}
/**
* Sets the outputpath value. Default: <none>
*
* This will force the output path to be something other than
* the path of the fileset used.
*
* @param string $outputpath Path name
*
* @access public
* @return void
*/
public function setOutputpath($outputpath)
{
$this->outputpath = rtrim(trim($outputpath), DIRECTORY_SEPARATOR);
}
/**
* Return the outputpath value.
*
* @return string
*/
public function getOutputpath()
{
return $this->outputpath;
}
/**
* Sets the keepsubdirectories value. Default: true
*
* When set to true we will keep the directory structure. So any input
* files in subdirectories will have their output file in that same
* sub-directory. If false, all output files will be put in the path
* defined by outputpath or in the directory top directory of the fileset.
*
* @param bool $keepsubdirectories Jenkins style boolean
*
* @access public
* @return void
*/
public function setKeepsubdirectories($keepsubdirectories)
{
$this->keepsubdirectories = StringHelper::booleanValue($keepsubdirectories);
}
/**
* Return keepsubdirectories value.
*
* @return bool
*/
public function getKeepsubdirectories()
{
return $this->keepsubdirectories;
}
/**
* Nested creator, creates a FileSet for this task
*
* @return FileSet The created fileset object
*/
public function createFileSet()
{
$num = array_push($this->filesets, new FileSet());
return $this->filesets[$num - 1];
}
/**
* Whether to just check syntax.
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setCheck($value)
{
$check = StringHelper::booleanValue($value);
$this->check = $check;
if ($check) {
$this->flags .= ' --check ';
} else {
$this->flags = str_replace(' --check ', '', $this->flags);
}
}
/**
* Indicate if just a syntax check is required.
*
* @return boolean
*/
public function getCheck()
{
return $this->check;
}
/**
* Set style to compact
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setCompact($value)
{
$compress = StringHelper::booleanValue($value);
if ($compress) {
$this->flags = str_replace(' --style ' . $this->style, '', $this->flags);
$this->flags .= ' --style compact';
$this->style = 'compact';
}
}
/**
* Indicate whether style is set to 'coompact'.
*
* @return bool
* @see setCompact
*/
public function getCompact()
{
return $this->style === 'compact';
}
/**
* Set style to compressed
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setCompressed($value)
{
$compress = StringHelper::booleanValue($value);
if ($compress) {
$this->flags = str_replace(' --style ' . $this->style, '', $this->flags);
$this->flags .= ' --style compressed';
$this->style = 'compressed';
}
}
/**
* Indicate whether style is set to 'compressed'.
*
* @return bool
* @see setCompressed
*/
public function getCompressed()
{
return $this->style === 'compressed';
}
/**
* Set style to crunched. Supported by scssphp only.
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setCrunched($value)
{
$compress = StringHelper::booleanValue($value);
if ($compress) {
$this->style = 'crunched';
}
}
/**
* Indicate whether style is set to 'crunched'.
*
* @return bool
* @see setCrunched
*/
public function getCrunched()
{
return $this->style === 'crunched';
}
/**
* Set style to expanded
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setExpand($value)
{
$expand = StringHelper::booleanValue($value);
if ($expand) {
$this->flags = str_replace(' --style ' . $this->style, '', $this->flags);
$this->flags .= ' --style expanded';
$this->style = 'expanded';
}
}
/**
* Indicate whether style is set to 'expanded'.
*
* @return bool
* @see setExpand
*/
public function getExpand()
{
return $this->style === 'expanded';
}
/**
* Set style to nested
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setNested($value)
{
$nested = StringHelper::booleanValue($value);
if ($nested) {
$this->flags = str_replace(' --style ' . $this->style, '', $this->flags);
$this->flags .= ' --style nested';
$this->style = 'nested';
}
}
/**
* Indicate whether style is set to 'nested'.
*
* @return bool
* @see setNested
*/
public function getNested()
{
return $this->style === 'nested';
}
/**
* Whether to force recompiled when --update is used.
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setForce($value)
{
$force = StringHelper::booleanValue($value);
$this->force = $force;
if ($force) {
$this->flags .= ' --force ';
} else {
$this->flags = str_replace(' --force ', '', $this->flags);
}
}
/**
* Return force value.
*
* @return bool
*/
public function getForce()
{
return $this->force;
}
/**
* Whether to cache parsed sass files.
*
* @param string $value Jenkins style boolean value
*
* @return void
*/
public function setNoCache($value)
{
$noCache = StringHelper::booleanValue($value);
$this->noCache = $noCache;
if ($noCache) {
$this->flags .= ' --no-cache ';
} else {
$this->flags = str_replace(' --no-cache ', '', $this->flags);
}
}
/**
* Return noCache value.
*
* @return bool
*/
public function getNoCache()
{
return $this->noCache;
}
/**
* Specify SASS import path
*
* @param string $path Import path
*
* @return void
*/
public function setPath(string $path): void
{
$this->flags .= " --load-path $path ";
$this->loadPath = $path;
}
/**
* Return the SASS import path.
*/
public function getPath(): string
{
return $this->loadPath;
}
/**
* Set output style.
*
* @param string $style nested|compact|compressed|expanded|crunched
*
* @return void
*/
public function setStyle(string $style): void
{
$style = strtolower($style);
switch ($style) {
case 'nested':
case 'compact':
case 'compressed':
case 'expanded':
case 'crunched':
$this->flags = str_replace(" --style $this->style", '', $this->flags);
$this->style = $style;
$this->flags .= " --style $style ";
break;
default:
$this->log("Style $style ignored", Project::MSG_INFO);
}
}
/**
* Return style used for generating output.
*/
public function getStyle(): string
{
return $this->style;
}
/**
* Set trace option.
*
* IE: Whether to output a stack trace on error.
*
* @param string $trace Jenkins style boolean value
*
* @return void
*/
public function setTrace($trace)
{
$this->trace = StringHelper::booleanValue($trace);
if ($this->trace) {
$this->flags .= ' --trace ';
} else {
$this->flags = str_replace(' --trace ', '', $this->flags);
}
}
/**
* Return trace option.
*
* @return bool
*/
public function getTrace()
{
return $this->trace;
}
/**
* Whether to use unix-style newlines.
*
* @param string $newlines Jenkins style boolean value
*
* @return void
*/
public function setUnixnewlines($newlines)
{
$unixnewlines = StringHelper::booleanValue($newlines);
$this->unixnewlines = $unixnewlines;
if ($unixnewlines) {
$this->flags .= ' --unix-newlines ';
} else {
$this->flags = str_replace(' --unix-newlines ', '', $this->flags);
}
}
/**
* Return unix-newlines setting
*
* @return bool
*/
public function getUnixnewlines()
{
return $this->unixnewlines;
}
/**
* Whether to identify source-file and line number for generated CSS.
*
* @param string $lineNumbers Jenkins style boolean value
*/
public function setLineNumbers(string $lineNumbers): void
{
$lineNumbers = StringHelper::booleanValue($lineNumbers);
$this->lineNumbers = $lineNumbers;
if ($lineNumbers) {
$this->flags .= ' --line-numbers ';
} else {
$this->flags = str_replace(' --line-numbers ', '', $this->flags);
}
}
/**
* Return line-numbers setting.
*/
public function getLineNumbers(): bool
{
return $this->lineNumbers;
}
/**
* Whether to use the 'sass' command line tool.
*
* @param string $value Jenkins style boolean value.
*
* @return void
* @link http://sass-lang.com/install
*/
public function setUseSass($value)
{
$this->useSass = StringHelper::booleanValue($value);
}
/**
* Get useSass property value
*
* @return bool
*/
public function getUseSass(): bool
{
return $this->useSass;
}
/**
* Whether to use the scssphp compiler.
*
* @param string $value Jenkins style boolean value.
*
* @return void
* @link https://scssphp.github.io/scssphp/
*/
public function setUseScssphp($value)
{
$this->useScssphp = StringHelper::booleanValue($value);
}
/**
* Whether to use the Scss php library
*
* @return bool
*/
public function getUseScssPhp(): bool
{
return $this->useScssphp;
}
/**
* Set single filename to compile from scss to css.
*
* @param string $file Single filename to compile.
*
* @return void
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* Our main execution of the task.
*
* @throws BuildException
* @throws Exception
*
* @access public
* @return void
*/
public function main()
{
if ($this->useSass) {
if (strlen($this->executable) < 0) {
throw new BuildException("'executable' must be defined.");
}
}
if (empty($this->filesets) && $this->file === null) {
throw new BuildException(
"Missing either a nested fileset or attribute 'file'"
);
}
try {
$compiler = (new SassTaskCompilerFactory(FileSystem::getFileSystem()))->prepareCompiler($this);
} catch (BuildException $exception) {
if ($this->failonerror) {
throw $exception;
}
$this->log($exception->getMessage());
return;
}
if (count($this->filesets) > 0) {
$this->processFilesets($compiler);
} elseif ($this->file !== null) {
$this->processFile($compiler);
}
}
/**
* Compile a specified file.
*
* If output file is not specified, but outputpath is, place output in
* that directory. If neither is specified, place .css file in the
* directory that the input file is in.
*
* @param SassTaskCompiler $compiler Compiler to use for processing fileset
*
* @return void
*/
public function processFile(SassTaskCompiler $compiler)
{
$this->log("Process file", Project::MSG_INFO);
if (null === $this->output) {
$specifiedOutputPath = (strlen($this->outputpath) > 0);
$info = [];
if ($specifiedOutputPath === false) {
$info = pathinfo($this->file);
$path = $info['dirname'];
$this->outputpath = $path;
} else {
$path = $this->outputpath;
}
$output = $path . DIRECTORY_SEPARATOR . isset($info['filename']) ?? $info['filename'];
if (!$this->removeoldext) {
$output .= '.' . $this->pathInfo['extension'];
}
if (strlen($this->newext) > 0) {
$output .= '.' . $this->newext;
}
$this->output = $output;
} else {
$output = $this->output;
}
$compiler->compile($this->file, $output, $this->failonerror);
}
/**
* Process filesets - compiling/generating css files as required.
*
* @param SassTaskCompiler $compiler Compiler to use for processing fileset
*
* @return void
*/
public function processFilesets(SassTaskCompiler $compiler): void
{
foreach ($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($this->project);
$files = $ds->getIncludedFiles();
$dir = $fs->getDir($this->project)->getPath();
// If output path isn't defined then set it to the path of our fileset.
$specifiedOutputPath = (strlen($this->outputpath) > 0);
if ($specifiedOutputPath === false) {
$this->outputpath = $dir;
}
foreach ($files as $file) {
$fullFilePath = $dir . DIRECTORY_SEPARATOR . $file;
$this->pathInfo = pathinfo($file);
$run = true;
switch (strtolower($this->pathInfo['extension'])) {
case 'scss':
case 'sass':
break;
default:
$this->log('Ignoring ' . $file, Project::MSG_DEBUG);
$run = false;
}
if (
$run