Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Created April 12, 2013 16:29
Show Gist options
  • Save joshnesbitt/5373293 to your computer and use it in GitHub Desktop.
Save joshnesbitt/5373293 to your computer and use it in GitHub Desktop.
Register Dart with the asset pipeline.
module Dart
module Rails
class Railtie < ::Rails::Engine
initializer :setup_dart_rails do |app|
app.assets.register_engine '.dart', Dart::Rails::Template
end
end
end
end
require 'fileutils'
require 'open3'
module Dart
module Rails
class Template < Tilt::Template
include TiltCompat
def render(scope, locals, &block)
@data = dart(@file)
end
protected
def compile_target
@compile_target ||= File.open(File.join(cache_dir, 'output.js'), 'w')
end
def cache_dir
@cache_dir ||= ::Rails.root.join('tmp/cache/dart-rails').tap do |dir|
FileUtils.rm_r(dir) if Dir.exist?(dir)
Dir.mkdir(dir)
end
end
def dart(file, options = {})
bin = ENV['DART2JS_BIN'] || '/usr/bin/env dart2js'
command = "#{bin} --out=#{compile_target.path} #{file}"
stdin, stdout, stderr = Open3.popen3(command)
if error = stdout.gets
raise error
end
stdin.close
stdout.close
stderr.close
`cat #{compile_target.path}`
end
def logger
::Rails.logger
end
end
end
end
module Dart
module Rails
module TiltCompat
class << self
def engine_initialized?
true
end
end
def prepare
true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment