Skip to content

Commit

Permalink
Start our own MCLess middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed Jan 14, 2014
1 parent 0cdef66 commit 92b1a97
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
115 changes: 115 additions & 0 deletions lib/Plack/Middleware/MCLess.pm
@@ -0,0 +1,115 @@
package Plack::Middleware::MCLess;

# ABSTRACT: LESS CSS support

# Code based off of Plack::Middleware::File::Less;;

use strict;
use warnings;
use 5.008_001;
our $VERSION = '0.02';

use parent qw(Plack::Middleware);
use Plack::Util;
use Plack::Util::Accessor qw(root);
use Capture::Tiny ':all';
use Try::Tiny;

use IPC::Open3 qw(open3);
use Carp;

sub prepare_app {
my $self = shift;

my $less = `lessc -v`;
unless($less) {
Carp::croak("Can't find lessc command");
}
}

sub _run_less {
my ($self, $file) = @_;

my ($stdout, $stderr, $exit) = capture {
system( 'lessc', $file );
};

croak $stderr if $stderr;
return $stdout;
}

sub less_response {
my ($self, $path_info) = @_;

my $docroot = $self->root || ".";
my $path = $docroot . $path_info;

return [404, [], []] unless -f $path;

try {
my $css = $self->_run_less($path);
return [
200,
[
'Content-Type' => 'text/css',
'Content-Length' => length $css,
],
["$css"]
];
} catch {
return [
500,
[
'Content-Type' => 'text/css',
'Content-Length' => length $_,
],
["$_"]
];

};

}


sub call {
my ($self, $env) = @_;

my $path_info = $env->{PATH_INFO};

if( $path_info =~ /\.less$/ ) {

return $self->less_response($path_info);

} else {
# carry on
return $self->app->($env);
}


# if ($env->{PATH_INFO} =~ s/\.css$/.less/i) {
# my $res = $self->app->($env);

# return $res unless ref $res eq 'ARRAY';

# if ($res->[0] == 200) {
# my $less;
# Plack::Util::foreach($res->[2], sub { $less .= $_[0] });


# my $h = Plack::Util::headers($res->[1]);
# $h->set('Content-Type' => 'text/css');
# $h->set('Content-Length' => length $css);

# $res->[2] = [$css];
# }
# elsif ($res->[0] == 404) {
# $env->{PATH_INFO} = $orig_path_info;
# $res = $self->app->($env);
# }

# return $res;
# }
# return $self->app->($env);
}

1;
1 change: 1 addition & 0 deletions t/plack/css/broken.less
@@ -0,0 +1 @@
@import a/dodgy/file
9 changes: 9 additions & 0 deletions t/plack/css/foo.less
@@ -0,0 +1,9 @@
@variable_c: #4D926F;

#header {
color: @variable_c;
}

h2 {
color: @variable_c;
}
1 change: 1 addition & 0 deletions t/plack/css/style.less
@@ -0,0 +1 @@
@import "foo.less";
35 changes: 35 additions & 0 deletions t/plack/mcless.t
@@ -0,0 +1,35 @@
use strict;
use Plack::App::File;
use Plack::Middleware::MCLess;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;

use Data::Dumper;

my $app = Plack::App::File->new(root => "t/plack/css");
$app = Plack::Middleware::MCLess->wrap($app, root => "t/plack/css");

test_psgi $app, sub {
my $cb = shift;

my $res = $cb->(GET "/foo.less");
is $res->code, 200;
is $res->content_type, 'text/css';
like $res->content, qr/color: #4D926F;/i, "Content match for foo.less";

# Something that uses includes
$res = $cb->(GET "/style.less");
is $res->code, 200;
is $res->content_type, 'text/css';
like $res->content, qr/\scolor: #4D926F;/i;

$res = $cb->(GET "/missing.less");
is $res->code, 404, '404 for a missing less file';

$res = $cb->(GET "/broken.less");
is $res->code, 500, '500 for a broken less file';

};

done_testing;

0 comments on commit 92b1a97

Please sign in to comment.