| 1 | package CSS::Watcher::ParserLess; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Path::Tiny; | ||
| 7 | use File::Which; | ||
| 8 | use IPC::Run3; | ||
| 9 | use Log::Log4perl qw(:easy); | ||
| 10 | |||
| 11 | use CSS::Watcher::Parser; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | |||
| 16 | return bless ({}, $class); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub parse_less { | ||
| 20 | my $self = shift; | ||
| 21 | my $filename = shift; | ||
| 22 | |||
| 23 | my $cmd = [ $self->executable, | ||
| 24 | '--no-color', | ||
| 25 | $filename | ||
| 26 | ]; | ||
| 27 | my $err; | ||
| 28 | my $out; | ||
| 29 | local ($!, $?) = (0, -1); | ||
| 30 | IPC::Run3::run3($cmd, undef, \$out, \$err, {return_if_system_error => 1}); | ||
| 31 | |||
| 32 | if (!$?) { | ||
| 33 | $err = ''; | ||
| 34 | } elsif ($! == 2) { | ||
| 35 | ERROR sprintf "Cannot execute '%s'. See http://lesscss.org/#usage", $cmd->[0]; | ||
| 36 | } else { | ||
| 37 | DEBUG sprintf "Failed to run \"%s\":\n %s\n%s", join(' ', @$cmd), $err, $out; | ||
| 38 | $out = ''; | ||
| 39 | } | ||
| 40 | |||
| 41 | my ($classes, $ids) = ({}, {}); | ||
| 42 | if ($out ne '') { | ||
| 43 | INFO sprintf '%s: lessc done, parsing generated CSS', path($filename)->basename; | ||
| 44 | ($classes, $ids) = CSS::Watcher::Parser->new->parse_css($out); | ||
| 45 | } | ||
| 46 | |||
| 47 | # Find dependencies for this less file. | ||
| 48 | my @requiries; | ||
| 49 | foreach (path($filename)->lines_utf8()) { | ||
| 50 | (m/^\s*?\@import\s+"(.*?.less)"/) ? push @requiries, $1 : | ||
| 51 | (m/^\s*?\@import\s+"(.*?.css)"/) ? push @requiries, $1 : | ||
| 52 | (m/^\s*?\@import\s+"(.*?)"/) ? push @requiries, $1 . '.less' : 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | INFO sprintf "%s: imports: %s", path($filename)->basename, join(', ', @requiries) if (@requiries); | ||
| 56 | |||
| 57 | return ($classes, $ids, \@requiries); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub executable { | ||
| 61 | File::Which::which('lessc') || 'lessc' ; | ||
| 62 | } | ||
| 63 | |||
| 64 | 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 |