| 1 | package CSS::Watcher::ParserLess; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 1 | ▶ | use File::Which; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use IPC::Run3; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 1 | ▶ | use CSS::Watcher::Parser; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||
| 14 | 5 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 5 | return bless ({}, $class); | |||||||||||||||||
| 17 | } | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | sub parse_less { | ||||||||||||||||||
| 20 | 0 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
parse_less: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||
| 21 | 0 | my $filename = shift; | |||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 0 | my $cmd = [ $self->executable, | |||||||||||||||||
| 24 | '--no-color', | ||||||||||||||||||
| 25 | $filename | ||||||||||||||||||
| 26 | ]; | ||||||||||||||||||
| 27 | 0 | my $err; | |||||||||||||||||
| 28 | my $out; | ||||||||||||||||||
| 29 | 0 | local ($!, $?) = (0, -1); | |||||||||||||||||
| 30 | 0 | IPC::Run3::run3($cmd, undef, \$out, \$err, {return_if_system_error => 1}); | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 0 | ▶ | if (!$?) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 33 | 0 | $err = ''; | |||||||||||||||||
| 34 | } elsif ($! == 2) { | ||||||||||||||||||
| 35 | 0 | ERROR sprintf "Cannot execute '%s'. See http://lesscss.org/#usage", $cmd->[0]; | |||||||||||||||||
| 36 | } else { | ||||||||||||||||||
| 37 | 0 | DEBUG sprintf "Failed to run \"%s\":\n %s\n%s", join(' ', @$cmd), $err, $out; | |||||||||||||||||
| 38 | 0 | $out = ''; | |||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 0 | my ($classes, $ids) = ({}, {}); | |||||||||||||||||
| 42 | 0 | ▶ | if ($out ne '') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 43 | 0 | INFO sprintf '%s: lessc done, parsing generated CSS', path($filename)->basename; | |||||||||||||||||
| 44 | 0 | ($classes, $ids) = CSS::Watcher::Parser->new->parse_css($out); | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | # Find dependencies for this less file. | ||||||||||||||||||
| 48 | 0 | my @requiries; | |||||||||||||||||
| 49 | 0 | foreach (path($filename)->lines_utf8()) { | |||||||||||||||||
| 50 | 0 | ▶ | (m/^\s*?\@import\s+"(.*?.less)"/) ? push @requiries, $1 : | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 51 | (m/^\s*?\@import\s+"(.*?.css)"/) ? push @requiries, $1 : | ||||||||||||||||||
| 52 | (m/^\s*?\@import\s+"(.*?)"/) ? push @requiries, $1 . '.less' : 1; | ||||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 0 | ▶ | INFO sprintf "%s: imports: %s", path($filename)->basename, join(', ', @requiries) if (@requiries); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 0 | return ($classes, $ids, \@requiries); | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | sub executable { | ||||||||||||||||||
| 61 | 0 | ▶ | File::Which::which('lessc') || 'lessc' ; | ||||||||||||||||
|
Subroutine
executable: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | 1 | 1; | |||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | __END__ | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | =head1 NAME | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | CSS::Watcher::ParserLess - Extract classes, ids from .less files | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | =head1 SYNOPSIS | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | use CSS::Watcher::ParserLess; | ||||||||||||||||||
| 75 | my $parser = CSS::Watcher::Parser->new() | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | # return requiries - ref array of dependencies | ||||||||||||||||||
| 78 | my ($hclasses, $hids, $requiries) = parser->parse_less (<<LESS) | ||||||||||||||||||
| 79 | @import "foo.less" | ||||||||||||||||||
| 80 | LESS | ||||||||||||||||||
| 81 | ; | ||||||||||||||||||
| 82 | # $requiries = ["foo.less"] | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | Use lessc for compile less and parse css result by CSS::Watcher::Parser. | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =head1 AUTHOR | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | Olexandr Sydorchuk (olexandr.syd@gmail.com) | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | Copyright (C) 2014 by Olexandr Sydorchuk | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||
| 97 | it under the same terms as Perl itself, either Perl version 5.8.2 or, | ||||||||||||||||||
| 98 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | =cut | ||||||||||||||||||