class DartTrails::Tilt::Engine

Attributes

file[R]
options[R]

Public Class Methods

new(file, data, options = {}) click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 9
def initialize(file, data, options = {})
  @options = defaults.merge(options)
  @file    = fetch_file(file, data)
end

Public Instance Methods

compile() click to toggle source

This should return a string, the creation of a file is an unfortunate side effect (the dart2js compiler does not allow you to direct the compiled output to stdout). So I will need to read in the file, and unlink it when I am finished with it.

Also keep in mind that dart2js also creates .deps and .map files which will need to be cleaned up.

# File lib/dart_trails/tilt/engine.rb, line 26
def compile
  redirect = { [:out, :err] => '/dev/null' }
  success  = system( command, *cli_options,
                     output_file_option, input_file,
                     redirect )

  if success
    log(:info, "Successfully compiled #{file.path}.")
    read_and_unlink
  else
    log(:error, "Failed to compile #{file.path}.")
    ''
  end
end
config() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 14
def config
  DartTrails
end

Private Instance Methods

cli_option(option) click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 68
def cli_option(option)
  "--#{option}"
end
cli_options() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 62
def cli_options
  enabled_options.keys.map do |k|
    cli_option(k)
  end
end
command() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 47
def command
  'dart2js'
end
compile_command() click to toggle source

This isn’t used with the current format of the call to Kernel#system, which instead passes the options as separate arguments.

# File lib/dart_trails/tilt/engine.rb, line 54
def compile_command
  "dart2js #{cli_options.join(' ')} --out=#{output_file} #{input_file}"
end
defaults() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 43
def defaults
  config.cli_options || { minify: true }
end
enabled_options() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 58
def enabled_options
  options.select { |k, v| v }
end
fetch_file(file, data) click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 85
def fetch_file(file, data)
  file ? File.new(file) : string_to_file(data)
end
input_file() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 77
def input_file
  file.path
end
output_file() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 72
def output_file
  base = defined?(Rails) ? Rails.root.to_s : ''
  base + '/tmp/dart_trails.dart'
end
output_file_option() click to toggle source
# File lib/dart_trails/tilt/engine.rb, line 81
def output_file_option
  "--out=#{output_file}"
end
string_to_file(s) click to toggle source

The dart2js compiler does not accept input data on stdin and expects an input file, so unfortunately if we receive a string, we need to write it to a file first.

# File lib/dart_trails/tilt/engine.rb, line 93
def string_to_file(s)
  f = Tempfile.new('dart_trails_input.dart')
  f.write(s)
  f.close
  f
end
strip_source_mapping(s) click to toggle source

Strip the JavaScript Source Map declaration to prevent the browser making a request for it as currently it is unlinked along with the other dart2js output files and I believe support for source map files with Sprockets is still under development.

# File lib/dart_trails/tilt/engine.rb, line 113
def strip_source_mapping(s)
  s.gsub(/\/\/[@#] sourceMappingURL=.+\.map$/, '')
end