CSS-Prepare

 view release on metacpan or  search on metacpan

bin/cssprepare  view on Meta::CPAN

            }
        }
        
        our $updates = 0;
        foreach my $stylesheet ( @files ) {
            my ( $processed, $saving, $error_count, $not_cached )
                = process_stylesheet( $stylesheet );
            
            $total_saving += $saving;
            $total_errors += $error_count;
            $output       .= $processed;
            $updates      += $not_cached;
        }
        
        print {$pipe} $output;
        close $pipe;
        
        status('') if $updates;
        
        # avoid dup signals
        select undef, undef, undef, 0.1;
        
        # touch the pipe to confound anything caching based upon the mtime
        utime undef, undef, $pipe;
    }
}

sub process_stylesheet {
    my $stylesheet = shift;
    
    $cache{ $stylesheet } = { timestamp => 0, }
        unless defined $cache{ $stylesheet };
    my $cache = $cache{ $stylesheet };
    
    my $stat        = stat $stylesheet;
    my $timestamp   = $stat->mtime;
    my $recalculate = $timestamp != $cache->{'timestamp'}
                      || $cache->{'error_count'};
    
    if ( $recalculate ) {
        my $start = [ gettimeofday() ];
        status( "Processing stylesheet '$stylesheet'" );
        
        my @structure = $preparer->parse_stylesheet( $stylesheet );
        
        $cache->{'saving'}      = 0;    # TODO
        $cache->{'error_count'} = 0;
        $cache->{'output'}      = '';
        $cache->{'timestamp'}   = $timestamp;
        
        foreach my $block ( @structure ) {
            foreach my $error ( @{$block->{'errors'}} ) {
                my $selector = defined $block->{'selectors'}
                             ? join ', ', @{$block->{'selectors'}}
                             : '';
                my( $level, $text ) = each %$error;
                
                status(
                        "  [${level}] '${selector}' - ${text}",
                        0,
                        'bold red'
                    );
                $cache->{'error_count'}++;
            }
        }
        
        if ( !defined $options{'warnings-only'} ) {
            @structure = $preparer->optimise( @structure )
                if defined $options{'optimise'};
            
            $cache->{'output'}
                = $preparer->output_as_string( @structure );
            
            my $interval = tv_interval( $start );
            status( "\r  Time taken ${interval} seconds" );
        }
    }
    
    return (
        $cache->{'output'},
        $cache->{'saving'},
        $cache->{'error_count'},
        $recalculate,
    );
}
sub output_stylesheet_to_directory {
    my $output = shift;
    
    my $sha1      = sha1_base64( $output );
       $sha1  =~ s{/}{_}g;
    
    if ( !defined $options{'use-all-shasum'} ) {
        # ten characters is not as unique as the full SHA1 digest, but is 
        # just about unique enough for our purposes
        $sha1 = substr( $sha1, 0, 5 );
    }
    
    my $filename = "$options{'output-dir'}/${sha1}.css";
    my $handle   = FileHandle->new( $filename, 'w' )
        or die "Cannot write $output: $!";
    
    print {$handle} $output;
    
    return $filename;
}
sub get_files_in_directory {
    my $directory = shift;
    
    opendir my $handle, $directory
        or return;
    
    my @files;
    my @directories;
    while ( my $entry = readdir $handle ) {
        next if $entry =~ m{^\.};
        
        my $target = "$directory/$entry";
        
        push( @files, $target )
            if -f $target && $target =~ m{\.css$};
        push( @directories, $target ) if -d $target;



( run in 1.092 second using v1.01-cache-2.11-cpan-39bf76dae61 )