Class: Dart2Js

Inherits:
Object
  • Object
show all
Includes:
Dart2JsExceptions
Defined in:
lib/dart2js.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_or_data, options = {}) ⇒ Dart2Js

Returns a new instance of Dart2Js.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dart2js.rb', line 30

def initialize(file_or_data, options = {})
  @dart2js_binary = options[:dart2js_binary] || self.class.dart2js_binary

  if options[:out_file].is_a?(File)
    @out_file     = options[:out_file]
  elsif options[:out_file]
    @out_file     = File.new(options[:out_file])
  end

  if options[:out_dir]
    @out_dir      = options[:out_dir]
    @out_file   ||= File.join(@out_dir, uniqe_tmpfile_name)
  else
    @out_file   ||= Tempfile.open('dart2js_output')
  end

  if file_or_data.is_a?(File)
    @in_file = file_or_data
  elsif File.exist?(file_or_data)
    @in_file = File.new(file_or_data)
  else
    @data = file_or_data
    @pwd  = options[:pwd]
  end
end

Class Attribute Details

.dart2js_binaryObject



10
11
12
# File 'lib/dart2js.rb', line 10

def dart2js_binary
  @dart2js_binary ||= (ENV['DART2JS_SOURCE_PATH'] || find_dart2js_in_path || find_dart2js_in_sdk)
end

Instance Attribute Details

#dart2js_binaryObject

Returns the value of attribute dart2js_binary.



28
29
30
# File 'lib/dart2js.rb', line 28

def dart2js_binary
  @dart2js_binary
end

#dataObject (readonly)

Returns the value of attribute data.



27
28
29
# File 'lib/dart2js.rb', line 27

def data
  @data
end

#in_fileObject (readonly)

Returns the value of attribute in_file.



27
28
29
# File 'lib/dart2js.rb', line 27

def in_file
  @in_file
end

#out_dirObject

Returns the value of attribute out_dir.



28
29
30
# File 'lib/dart2js.rb', line 28

def out_dir
  @out_dir
end

#out_fileObject

Returns the value of attribute out_file.



28
29
30
# File 'lib/dart2js.rb', line 28

def out_file
  @out_file
end

#pwdObject

Returns the value of attribute pwd.



28
29
30
# File 'lib/dart2js.rb', line 28

def pwd
  @pwd
end

#resultObject (readonly)

Returns the value of attribute result.



27
28
29
# File 'lib/dart2js.rb', line 27

def result
  @result
end

Instance Method Details

#closeObject



78
79
80
81
82
# File 'lib/dart2js.rb', line 78

def close
  [@in_file, @out_file].each do |f|
    f.close! if f.respond_to?(:close!)
  end
end

#compile(minify = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dart2js.rb', line 56

def compile(minify = true)
  prepare_input
  prepare_output
  cmd = [ @dart2js_binary,
          minify ? ' -m ': nil,
          %Q{-o"#{output_path}"},
          input_path ].join(' ')
  process = IO.popen(cmd, 'r')
  @result = process.read
  process.close
  return_code = $?.to_i
  if return_code != 0
    raise CompilationException.new(self, cmd)
  else
    get_js_content
  end
end

#get_js_contentObject



74
75
76
# File 'lib/dart2js.rb', line 74

def get_js_content
  File.read(@out_file)
end