ruby-dart2js 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 748fcd906fe848f58b3b000f3abf965438bbec85
4
- data.tar.gz: 381841cc0d61b3f48b17beef9f5b37127a85b6e7
3
+ metadata.gz: 8975f5f12c70378910a52d911d2ed746c684af68
4
+ data.tar.gz: a943e00afd8467cee2885bded0b32b597d9d41fc
5
5
  SHA512:
6
- metadata.gz: b53ef16275b7a195cc36ca1398424b211a229ef1f63d21e926f2fcc94ebbd7dd20ffb905a1edc01bd006edf9b168ab9a6ed4fe77995a5fa8c6f9992ec296727d
7
- data.tar.gz: 41a5b4bc1c4ca6c205926f9bdc7fc87b709e6ca2d49018bcf014b8d7374e3476b68515198796ffe0d43733e130cbf9c17f526fdf3b3655fa3b841bbdf60b63e1
6
+ metadata.gz: 7cb646a3b2b75f61f9477c6d76703ea1100bd0cb576053b4434929e1759fd1d469568539b0aed9389a514533b54f07e03e1a9296bd57ab590bf0df697476e2be
7
+ data.tar.gz: ae9137727ed687af98990ac5c0e8f549022333e1c9842e13adfbe46e4ec6dfbcb45ae6acc137fd4aff05d629d4b20191fd4b8626f9117ebe0b1766f7d6f5a742
data/README.md CHANGED
@@ -10,7 +10,7 @@ Provide automated transcoding from [Dart](https://www.dartlang.org/ 'dartlang.or
10
10
 
11
11
  `Gemfile`
12
12
 
13
- gem 'ruby-dart2js', :git => https://github.com/m0gg/ruby-dart2js.git
13
+ gem 'ruby-dart2js'
14
14
 
15
15
  ###### Find SDK ######
16
16
 
@@ -2,6 +2,8 @@ require 'tempfile'
2
2
  require 'dart2js_exceptions'
3
3
 
4
4
  class Dart2Js
5
+ include Dart2JsExceptions
6
+
5
7
  class << self
6
8
  attr_writer :dart2js_binary
7
9
 
@@ -11,7 +13,7 @@ class Dart2Js
11
13
 
12
14
  private
13
15
  def find_dart2js_in_path
14
- system('dart2js -h') ? 'dart2js' : false
16
+ system('dart2js --version') ? 'dart2js' : false
15
17
  end
16
18
 
17
19
  def find_dart2js_in_sdk
@@ -22,49 +24,90 @@ class Dart2Js
22
24
  end
23
25
  end
24
26
 
25
- attr_reader :data, :input_file, :result
26
- attr_accessor :out_file, :out_dir, :dart2js_binary
27
+ attr_reader :data, :in_file, :result
28
+ attr_accessor :pwd, :out_file, :out_dir, :dart2js_binary
27
29
 
28
30
  def initialize(file_or_data, options = {})
29
31
  @dart2js_binary = options[:dart2js_binary] || self.class.dart2js_binary
30
- @out_dir = options[:out_dir] if options[:out_dir]
31
- @out_file = options[:out_file] || File.join((@out_dir || Dir::tmpdir), "dart2js_#{self.object_id}_#{Time.now.usec}.js")
32
- if file_or_data.respond_to?(:path)
33
- if File.exists?(file_or_data)
34
- @input_file = file_or_data
35
- else
36
- throw 'File not found!'
37
- end
32
+
33
+ if options[:out_file].is_a?(File)
34
+ @out_file = options[:out_file]
35
+ elsif options[:out_file]
36
+ @out_file = File.new(options[:out_file])
37
+ end
38
+
39
+ if options[:out_dir]
40
+ @out_dir = options[:out_dir]
41
+ @out_file ||= File.join(@out_dir, uniqe_tmpfile_name)
42
+ else
43
+ @out_file ||= Tempfile.open('dart2js_output')
44
+ end
45
+
46
+ if file_or_data.is_a?(File)
47
+ @in_file = file_or_data
48
+ elsif File.exist?(file_or_data)
49
+ @in_file = File.new(file_or_data)
38
50
  else
39
51
  @data = file_or_data
52
+ @pwd = options[:pwd]
40
53
  end
41
54
  end
42
55
 
43
- def compile minify=true
56
+ def compile(minify = true)
57
+ prepare_input
58
+ prepare_output
44
59
  cmd = [ @dart2js_binary,
45
- minify ? ' -m ' : '',
46
- %Q{-o"#{out_file}"},
47
- in_file = prepare_input.path ].join(' ')
60
+ minify ? ' -m ': nil,
61
+ %Q{-o"#{output_path}"},
62
+ input_path ].join(' ')
48
63
  process = IO.popen(cmd, 'r')
49
64
  @result = process.read
50
65
  process.close
51
66
  return_code = $?.to_i
52
- return_code == 0 ? true : Dart2JsExceptions::CompilationException.new(cmd, in_file, @result)
67
+ if return_code != 0
68
+ raise CompilationException.new(self, cmd)
69
+ else
70
+ get_js_content
71
+ end
53
72
  end
54
73
 
55
74
  def get_js_content
56
75
  File.read(@out_file)
57
76
  end
58
77
 
78
+ def close
79
+ [@in_file, @out_file].each do |f|
80
+ f.close! if f.respond_to?(:close!)
81
+ end
82
+ end
83
+
59
84
  private
85
+ def input_path
86
+ @in_file.path
87
+ end
88
+
89
+ def output_path
90
+ @out_file.path
91
+ end
92
+
60
93
  def prepare_input
61
94
  if data
62
- tmp_file = Tempfile.open(%w(dart2js_tmpread .dart))
63
- tmp_file.write data
64
- tmp_file.flush
65
- tmp_file
66
- else
67
- @input_file
95
+ @in_file = Tempfile.open('dart2js_input', @pwd)
96
+ @in_file.write data
97
+ @in_file.close
68
98
  end
99
+ unless File.exists?(input_path)
100
+ raise PrepareInputException.new(self, "prepare_input ran but 'in_file' does not exist!")
101
+ end
102
+ end
103
+
104
+ def prepare_output
105
+ if !@out_file.is_a?(Tempfile) && File.exists?(output_path)
106
+ raise PrepareOutputException.new(self, "Won't overwrite existing file that is not a 'Tempfile''")
107
+ end
108
+ end
109
+
110
+ def uniqe_tmpfile_name(ext = 'js')
111
+ "dart2js_#{self.object_id}_#{Time.now.usec}.#{ext}"
69
112
  end
70
113
  end
@@ -1,12 +1,40 @@
1
1
  module Dart2JsExceptions
2
- class CompilationException < RuntimeError
3
- attr_reader :cmd, :in_file, :result
2
+ class Dart2JsException < Exception
3
+ def initialize(compiler)
4
+ @instance = compiler
5
+ end
6
+ end
4
7
 
5
- def initialize cmd, in_file, result
6
- super("dart2js failed to compile '#{in_file}'")
7
- @result = result;
8
+ class CompilationException < Dart2JsException
9
+ def initialize(compiler, cmd)
10
+ super(compiler)
8
11
  @cmd = cmd;
9
- @in_file = in_file;
12
+ end
13
+
14
+ def message
15
+ "dart2js compilation '#{@cmd}' failed:\n #{@instance.result}"
16
+ end
17
+
18
+ end
19
+ class PrepareInputException < Dart2JsException
20
+ def initialize(compiler, reason)
21
+ super(compiler)
22
+ @reason = reason
23
+ end
24
+
25
+ def message
26
+ "dart2js prepare_input failed\n in_file: #{@instance.in_file}\n input_path: #{@instance.input_path}\n reason: #{@reason}"
27
+ end
28
+ end
29
+
30
+ class PrepareOutputException < Dart2JsException
31
+ def initialize(compiler, reason)
32
+ super(compiler)
33
+ @reason = reason
34
+ end
35
+
36
+ def message
37
+ "dart2js prepare_output failed\n out_file: #{@instance.out_file}\n output_path: #{@instance.output_path}\n reason: #{@reason}"
10
38
  end
11
39
  end
12
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dart2js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcel Sackermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-07 00:00:00.000000000 Z
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email:
@@ -42,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  version: '0'
43
43
  requirements: []
44
44
  rubyforge_project:
45
- rubygems_version: 2.2.2
45
+ rubygems_version: 2.4.8
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: Provides dart2js compiling for compatibility.