tsc-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +39 -0
- data/Rakefile +11 -0
- data/lib/tsc-ruby.rb +65 -0
- data/lib/tsc-ruby/compile_result.rb +26 -0
- data/lib/tsc-ruby/version.rb +5 -0
- data/test/data/bad.ts +1 -0
- data/test/data/es5.ts +19 -0
- data/test/data/hello.ts +1 -0
- data/test/test_tsc-ruby.rb +47 -0
- data/tsc-ruby.gemspec +23 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 853e7372f95291cbe7b82a24c46400e8605bc0b0
|
4
|
+
data.tar.gz: bedd491c4016e674bee54aa3b204b860b31a808e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b69c679650b387b0226d30494c5300e039b77fb41023d554537e00cf4dbb37886a31095027d576274cd17477dd290c4459f4c04aa2d49c1f5a169f667c40322c
|
7
|
+
data.tar.gz: e3d2ab2d1a7ec6f78f259a5d3d4937b2ceaaa6668199b5e5525d68478ce25cf5397cea638db63612430c3f773c06aaffcad1c0b3cccdc6cad2e9740b0cbe598d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2017 Valentin Vasilyev
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
TypeScript compiler as a Ruby gem.
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'tsc-ruby'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install tsc-ruby
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'tsc-ruby'
|
21
|
+
|
22
|
+
# compiles a TypeScript source file and returns TypeScript::Ruby::CompileResult
|
23
|
+
result = TypeScript::Ruby.compile_file(file, '--target', 'ES5')
|
24
|
+
result.success? # => true if succeeded
|
25
|
+
result.js # => output JavaScript source code
|
26
|
+
result.stdout # => what tsc(1) shows to stdout
|
27
|
+
result.stderr # => what tsc(1) shows to stderr
|
28
|
+
|
29
|
+
# compiles a TypeScript source code string and returns String
|
30
|
+
js_source = TypeScript::Ruby.compile_file(ts_source, '--target', 'ES5')
|
31
|
+
```
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/tsc-ruby.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'tsrc'
|
4
|
+
require 'tsc-ruby/version'
|
5
|
+
require 'tsc-ruby/compile_result'
|
6
|
+
require 'open3'
|
7
|
+
|
8
|
+
module TypeScript
|
9
|
+
module Ruby
|
10
|
+
class NodeNotFound < StandardError
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
def tsc_version
|
16
|
+
TypeScript::Src.version
|
17
|
+
end
|
18
|
+
|
19
|
+
def tsc(*args)
|
20
|
+
cmd = ['node', TypeScript::Src.tsc_path.to_s, *args]
|
21
|
+
Open3.capture3(*cmd)
|
22
|
+
rescue Errno::ENOENT => e
|
23
|
+
raise '`node` executable not found in PATH' if e.message == 'No such file or directory - node'
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# Compile TypeScript string to JavaScript string
|
28
|
+
# @param [String] script TypeScript to be compiled
|
29
|
+
# @return [String] compiled JavaScript
|
30
|
+
def compile(script, *tsc_options)
|
31
|
+
script = script.read if script.respond_to?(:read)
|
32
|
+
js_file = Tempfile.new(%w(tsc-ruby .ts))
|
33
|
+
begin
|
34
|
+
js_file.write(source)
|
35
|
+
js_file.close
|
36
|
+
result = compile_file(js_file.path, *tsc_options)
|
37
|
+
if result.success?
|
38
|
+
result.js
|
39
|
+
else
|
40
|
+
raise result.stderr + result.stdout
|
41
|
+
end
|
42
|
+
ensure
|
43
|
+
js_file.unlink
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# Compile TypeScript source file to JavaScript source file.
|
50
|
+
#
|
51
|
+
# Compilation is a one to one process, not implicit concatenation of referenced dependencies is performed.
|
52
|
+
#
|
53
|
+
# @param [String] source_file TypeScript source file
|
54
|
+
# @return [TypeScript::Ruby::CompileResult] compile result
|
55
|
+
def compile_file(source_file, *tsc_options)
|
56
|
+
Dir.mktmpdir do |output_dir|
|
57
|
+
stdout, stderr, exit_status = tsc(*tsc_options, '--outDir', output_dir, source_file)
|
58
|
+
output_file = "#{File.join(output_dir, File.basename(source_file, '.ts'))}.js"
|
59
|
+
output_js = File.exist?(output_file) ? File.read(output_file) : nil
|
60
|
+
CompileResult.new(output_js, exit_status, stdout, stderr)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module TypeScript
|
2
|
+
module Ruby
|
3
|
+
class CompileResult
|
4
|
+
|
5
|
+
# Constructor
|
6
|
+
#
|
7
|
+
# @param [String] js compiled JavaScript
|
8
|
+
# @param [Fixnum] exit_status return code of tsc command
|
9
|
+
# @param [String] stdout Standard out
|
10
|
+
# @return [String] stderr Standard err
|
11
|
+
def initialize(js, exit_status, stdout, stderr)
|
12
|
+
@js = js
|
13
|
+
@exit_status = exit_status
|
14
|
+
@stdout = stdout
|
15
|
+
@stderr = stderr
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :js, :source_map, :exit_status, :stdout, :stderr
|
19
|
+
|
20
|
+
def success?
|
21
|
+
@exit_status == 0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/test/data/bad.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
with syntax errors
|
data/test/data/es5.ts
ADDED
data/test/data/hello.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
console.log("Hello TypeScript")
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tsc-ruby'
|
3
|
+
|
4
|
+
|
5
|
+
class TestTypeScriptRuby < Test::Unit::TestCase
|
6
|
+
def test_compile_file_in_success
|
7
|
+
file = File.expand_path('data/hello.ts', File.dirname(__FILE__))
|
8
|
+
subject = TypeScript::Ruby.compile_file(file)
|
9
|
+
|
10
|
+
assert { subject.exit_status == 0 }
|
11
|
+
assert { subject.success? }
|
12
|
+
assert { subject.js == %Q{console.log("Hello TypeScript");\n} }
|
13
|
+
assert { subject.stdout == '' }
|
14
|
+
assert { subject.stderr == '' }
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_compile_file_in_failure
|
18
|
+
file = File.expand_path('data/bad.ts', File.dirname(__FILE__))
|
19
|
+
subject = TypeScript::Ruby.compile_file(file)
|
20
|
+
|
21
|
+
assert { subject.exit_status != 0 }
|
22
|
+
assert { !subject.success? }
|
23
|
+
assert { subject.stdout != '' || subject.stderr != '' }
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_compile_file_with_es5
|
27
|
+
file = File.expand_path('data/es5.ts', File.dirname(__FILE__))
|
28
|
+
subject = TypeScript::Ruby.compile_file(file, '--target', 'ES5')
|
29
|
+
|
30
|
+
assert { subject.success? }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_compile
|
34
|
+
subject = TypeScript::Ruby.compile('class T { say() { console.log("Hello, world!") } }')
|
35
|
+
|
36
|
+
assert { subject != '' }
|
37
|
+
assert { subject != nil }
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_compile_with_es5
|
41
|
+
subject = TypeScript::Ruby.compile('class T { get name() { return "foo" } }', '--target', 'ES5')
|
42
|
+
|
43
|
+
assert { subject != '' }
|
44
|
+
assert { subject != nil }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/tsc-ruby.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../lib/tsc-ruby/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'tsc-ruby'
|
5
|
+
|
6
|
+
gem.authors = ['Valentin Vasilyev']
|
7
|
+
gem.email = ['valentin.vasilyev@outlook.com']
|
8
|
+
gem.description = %q{TypeScript compiler interface for Ruby}
|
9
|
+
gem.summary = gem.description
|
10
|
+
gem.homepage = 'https://github.com/valve/tsc-ruby'
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = TypeScript::Ruby::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'tsrc'
|
19
|
+
gem.add_development_dependency 'rake'
|
20
|
+
gem.add_development_dependency 'pry-byebug'
|
21
|
+
|
22
|
+
gem.required_ruby_version = '>= 1.9.3'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tsc-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Valentin Vasilyev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tsrc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: TypeScript compiler interface for Ruby
|
56
|
+
email:
|
57
|
+
- valentin.vasilyev@outlook.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/tsc-ruby.rb
|
69
|
+
- lib/tsc-ruby/compile_result.rb
|
70
|
+
- lib/tsc-ruby/version.rb
|
71
|
+
- test/data/bad.ts
|
72
|
+
- test/data/es5.ts
|
73
|
+
- test/data/hello.ts
|
74
|
+
- test/test_tsc-ruby.rb
|
75
|
+
- tsc-ruby.gemspec
|
76
|
+
homepage: https://github.com/valve/tsc-ruby
|
77
|
+
licenses: []
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 1.9.3
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.5.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: TypeScript compiler interface for Ruby
|
99
|
+
test_files:
|
100
|
+
- test/data/bad.ts
|
101
|
+
- test/data/es5.ts
|
102
|
+
- test/data/hello.ts
|
103
|
+
- test/test_tsc-ruby.rb
|