Created
November 13, 2010 22:05
-
-
Save outoftime/675690 to your computer and use it in GitHub Desktop.
less_compiler.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LessCompiler | |
class Middleware | |
def initialize(app) | |
@app = Rack::Static.new(app, :urls => %w(/stylesheets), :root => 'tmp') | |
@compiler = Compiler.instance | |
end | |
def call(env) | |
maybe_compile_less(env) | |
@app.call(env) | |
end | |
private | |
def maybe_compile_less(env) | |
path = env['PATH_INFO'] | |
return unless path =~ %r(^/stylesheets/(.*)\.css$) | |
@compiler.maybe_compile($1) | |
end | |
end | |
class Compiler | |
include Singleton | |
# | |
# path should be something like "global/nav" for "/global/nav.css" | |
# | |
def maybe_compile(path) | |
less_path = File.join(Rails.root, 'app', 'stylesheets', "#{path}.less") | |
css_path = File.join(Rails.root, 'tmp', 'stylesheets', "#{path}.css") | |
unless !File.exist?(less_path) || File.exist?(css_path) && File.mtime(less_path) <= File.mtime(css_path) | |
Rails.logger.debug "Recompiling #{less_path} => #{css_path}" | |
FileUtils.mkdir_p(File.dirname(css_path)) | |
Less::Command.new(:source => less_path, :destination => css_path).run! | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
middleware = ActionController::Dispatcher.middleware | |
middleware.use LessCompiler::Middleware |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment