CSS-Watcher

 view release on metacpan or  search on metacpan

lib/CSS/Watcher.pm  view on Meta::CPAN

package CSS::Watcher;

use strict; 
use warnings;

use Carp;
use Data::Dumper;

use Log::Log4perl qw(:easy);
use File::Slurp qw/read_file write_file/;
use Path::Tiny;
use Digest::MD5 qw/md5_hex/;
use List::MoreUtils qw(any);

use CSS::Watcher::Parser;
use CSS::Watcher::ParserLess;
use CSS::Watcher::Monitor;

our $VERSION = '0.5.0';

use constant DEFAULT_HTML_STUFF_DIR => '~/.emacs.d/ac-html-csswatcher/completion/';

sub new {
    my $class= shift;
    my $options = shift;

    return bless ({
        outputdir => $options->{'outputdir'} // DEFAULT_HTML_STUFF_DIR,
        parser_css => CSS::Watcher::Parser->new(),
        parser_less => CSS::Watcher::ParserLess->new(),
    }, $class);
}

sub update {
    my $self = shift;
    my $obj = shift;

    # check what is the monobj. file? dir?
    if (-f $obj || -d $obj) {
        my $proj_dir = $self->get_project_dir ($obj);
        return unless (defined $proj_dir);

        INFO "Update project: $proj_dir";

        my $prj = $self->_get_project ($proj_dir);
        $prj->{parsed_files} = [];    # clean old parsed file list

        my $changes = 0;

        my (@ignore, @allow, @skip_dirs);
        my $cfg = path($proj_dir)->child('.csswatcher');

        # clear project cache if .csswatcher changed
        if ($prj->{monitor}->is_changed($cfg)) {
            INFO ('.csswatcher changed, resetting');
            $prj->{monitor}->make_dirty();
            delete $prj->{parsed};
            $changes++;
        }

        if (-f $cfg) {
            if (open (CFG, '<:encoding(UTF-8)', $cfg)) {
                while (<CFG>) {
                    chomp;
                    (m/^\s*ignore:\s*(.*?)\s*$/i) ? push @ignore,    $1 :
                    (m/^\s*skip:\s*(.*?)\s*$/i)   ? push @skip_dirs, $1 :
                    (m/^\s*use:\s*(.*?)\s*$/i)    ? push @allow,     $1 : 1;
                }
                close CFG;
            }
        }

        # scan new or changed files, cache them
        $prj->{monitor}->scan (
            sub {
                my $file = shift;

                return if (any {$_ eq $file} @{$prj->{parsed_files}});

                my $allow = 0;
                foreach (@allow) {
                    if ($file =~ m/$_/) {
                        $allow = 1;
                        last;
                    }
                }
                unless ($allow) {
                    foreach (@ignore) {
                        if ($file =~ m/$_/) {
                            INFO " Ignored $file =~\"$_\"";
                            return;
                        }
                    }
                }



( run in 0.659 second using v1.01-cache-2.11-cpan-e1769b4cff6 )