| 1 | package Silki::Web::CSS; | ||
| 2 | { | ||
| 3 | $Silki::Web::CSS::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use autodie qw( :all ); | ||
| 11 | use CSS::Minifier qw( minify ); | ||
| 12 | use File::Slurp qw( read_file ); | ||
| 13 | use File::Temp; | ||
| 14 | use File::Which qw( which ); | ||
| 15 | use Path::Class; | ||
| 16 | use Silki::Config; | ||
| 17 | use Silki::Types qw( Str ); | ||
| 18 | |||
| 19 | use Moose; | ||
| 20 | |||
| 21 | with 'Silki::Role::Web::CombinedStaticFiles'; | ||
| 22 | |||
| 23 | has lessc_path => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => Str, | ||
| 26 | builder => '_build_lessc_path', | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub _build_files { | ||
| 30 | my $dir = dir( Silki::Config->instance()->share_dir(), 'css-source' ); | ||
| 31 | |||
| 32 | return [ | ||
| 33 | sort | ||
| 34 | grep { | ||
| 35 | !$_->is_dir() | ||
| 36 | && $_->basename() =~ /^\d+/ | ||
| 37 | && $_->basename() | ||
| 38 | =~ /\.(?:css|less)$/ | ||
| 39 | } $dir->children() | ||
| 40 | ]; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _build_target_file { | ||
| 44 | my $css_dir = dir( Silki::Config->instance()->var_lib_dir(), 'css' ); | ||
| 45 | |||
| 46 | $css_dir->mkpath( 0, 0755 ); | ||
| 47 | |||
| 48 | return file( $css_dir, 'silki-combined.css' ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _squish { | ||
| 52 | my $self = shift; | ||
| 53 | my $css = shift; | ||
| 54 | |||
| 55 | return minify( input => $css ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _process { | ||
| 59 | my $self = shift; | ||
| 60 | my $file = shift; | ||
| 61 | |||
| 62 | my $filename = $file->stringify(); | ||
| 63 | |||
| 64 | # We need to delay unlinking of the temp file until after it is read. | ||
| 65 | my $temp; | ||
| 66 | if ( $filename =~ /\.less$/ ) { | ||
| 67 | $temp = File::Temp->new(); | ||
| 68 | system( $self->lessc_path(), $filename, $temp->filename() ); | ||
| 69 | $filename = $temp->filename(); | ||
| 70 | } | ||
| 71 | |||
| 72 | return scalar read_file($filename); | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | sub _build_lessc_path { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | my $bin = which('lessc'); | ||
| 80 | return $bin if $bin; | ||
| 81 | |||
| 82 | my $default = '/var/lib/gems/1.8/bin/lessc'; | ||
| 83 | return $default if -f $default; | ||
| 84 | |||
| 85 | die "Cannot find lessc in your path or at $default"; | ||
| 86 | } | ||
| 87 | |||
| 88 | __PACKAGE__->meta()->make_immutable(); | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | # ABSTRACT: Combines and minifies CSS source files | ||
| 93 | |||
| 94 | __END__ |